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
  [Numpy] Load date/time from .txt to 'datetime64' type. water 4 416 Mar-01-2024, 11:16 PM
Last Post: Gribouillis
  Parsing and summing time deltas (duration) onto bar + pie charts using pandas - - DRY Drone4four 2 515 Feb-10-2024, 06:04 PM
Last Post: Drone4four
  Pandas read csv file in 'date/time' chunks MorganSamage 4 1,649 Feb-13-2023, 11:24 AM
Last Post: MorganSamage
  How to convert time to sec in boucle for cencen 1 1,873 Apr-17-2021, 05:42 PM
Last Post: cencen
  Does a pandas have a date without a time? AlekseyPython 6 4,875 Feb-10-2021, 09:24 AM
Last Post: Naheed
  Pandas - compute means per category and time rama27 7 3,419 Nov-13-2020, 08:55 AM
Last Post: PsyPy
  How can I convert time-series data in rows into column srvmig 0 2,036 Apr-11-2020, 05:40 AM
Last Post: srvmig
  replace nan values by mean group by date.year, date.month wissam1974 5 8,332 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  Obtaining Correct Date In Pandas DataFrame eddywinch82 14 5,852 Feb-17-2020, 11:45 AM
Last Post: eddywinch82
  Trying to Pass date to pandas search from input prompt curranjohn46 1 2,073 Oct-10-2019, 10:01 AM
Last Post: curranjohn46

Forum Jump:

User Panel Messages

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