Python Forum
pandas convert date/time to week
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas convert date/time to week
#1
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
Reply
#2
anyone please?
Reply
#3
Please explain data format details clearly.
Your code snippet doesn't really help.

Just noticed it's pandas. (sometimes I skip headings)
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas - compute means per category and time rama27 7 197 Nov-13-2020, 08:55 AM
Last Post: PsyPy
  How can I convert time-series data in rows into column srvmig 0 385 Apr-11-2020, 05:40 AM
Last Post: srvmig
  replace nan values by mean group by date.year, date.month wissam1974 5 2,725 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  Obtaining Correct Date In Pandas DataFrame eddywinch82 14 1,910 Feb-17-2020, 11:45 AM
Last Post: eddywinch82
  Trying to Pass date to pandas search from input prompt curranjohn46 1 529 Oct-10-2019, 10:01 AM
Last Post: curranjohn46
  Need help passing date to pandas query curranjohn46 1 1,284 Oct-10-2019, 09:59 AM
Last Post: curranjohn46
  create 10 yearly blocks from time series using pandas Staph 1 570 Jul-23-2019, 12:01 PM
Last Post: Malt
  Pandas converting date to epoch randor 2 1,278 Jul-16-2019, 02:41 AM
Last Post: scidam
  [pandas] convert Int to str Scott 8 4,636 Jun-24-2019, 11:14 AM
Last Post: scidam
  [pandas] Convert categorical data to numbers pradeep_as400 1 1,055 Jun-15-2019, 08:27 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020