Python Forum

Full Version: pandas convert date/time to week
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

Below error when trying to convert date/time to week


data.columns
data.index
data['DATECREATED'].dt.week
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in _make_dt_accessor(self) 2819 try: -> 2820 return maybe_to_datetimelike(self) 2821 except Exception: ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\accessors.py in maybe_to_datetimelike(data, copy) 83 raise TypeError("cannot convert an object of type {0} to a " ---> 84 "datetimelike index".format(type(data))) 85 TypeError: cannot convert an object of type <class 'pandas.core.series.Series'> to a datetimelike index During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) <ipython-input-35-dedf61725963> in <module>() 14 data.columns 15 data.index ---> 16 data['DATECREATED'].dt.week 17 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name) 3075 if (name in self._internal_names_set or name in self._metadata or 3076 name in self._accessors): -> 3077 return object.__getattribute__(self, name) 3078 else: 3079 if name in self._info_axis: ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\base.py in __get__(self, instance, owner) 241 # this ensures that Series.str.<method> is well defined 242 return self.accessor_cls --> 243 return self.construct_accessor(instance) 244 245 def __set__(self, instance, value): ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in _make_dt_accessor(self) 2820 return maybe_to_datetimelike(self) 2821 except Exception: -> 2822 raise AttributeError("Can only use .dt accessor with datetimelike " 2823 "values") 2824 AttributeError: Can only use .dt accessor with datetimelike values
data['DATECREATED']
Output:
0 26/01/2018 4 23/01/2018 13 07/02/2018 17:35 14 27/01/2018 15 29/01/2018 20 24/01/2018 27 07/02/2018 09:57 31 05/02/2018 11:26
anyone please?
Please explain data format details clearly.
Your code snippet doesn't really help.

Just noticed it's pandas. (sometimes I skip headings)
Can you give us the result of the following?

for i in data['DATECREATED']]:
    print(type(i))
My assumption is that you have a datetime.date in some of your rows, and you can only use the .dt.week with purely datetime.datetime values.

If that is the case, you will need to convert them, or change the way your dataframe is filled.