Reputation: 4210
I am trying to convert dataframe date indext into datetime.
DTSdata_combined.index
Index(['10/28/2021 18:01', '10/28/2021 18:16', '10/28/2021 18:31',
'10/28/2021 18:46', '10/28/2021 19:01', '10/28/2021 19:16',
'10/28/2021 19:31', '10/28/2021 19:46', '10/28/2021 20:01',
'10/28/2021 20:16',
...
'5/27/2021 17:37', '5/27/2021 17:52', '5/27/2021 18:07',
'5/27/2021 18:22', '5/27/2021 18:37', '5/27/2021 18:52',
'5/27/2021 19:07', '5/27/2021 19:22', '5/27/2021 19:37',
'5/27/2021 19:52'],
dtype='object', length=1951)
then
pd.to_datetime(DTSdata_combined.index)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-128-3ad446f69a60> in <module>
----> 1 pd.to_datetime(DTSdata_combined.index)
C:\Anaconda\envs\tf_cpu\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
808 result = _assemble_from_unit_mappings(arg, errors, tz)
809 elif isinstance(arg, Index):
--> 810 cache_array = _maybe_cache(arg, format, cache, convert_listlike)
811 if not cache_array.empty:
812 result = _convert_and_box_cache(arg, cache_array, name=arg.name)
C:\Anaconda\envs\tf_cpu\lib\site-packages\pandas\core\tools\datetimes.py in _maybe_cache(arg, format, cache, convert_listlike)
167 from pandas import Series
168
--> 169 cache_array = Series(dtype=object)
170
171 if cache:
TypeError: 'list' object is not callable
so I check it, access each one element is fine
pd.to_datetime(DTSdata_combined.index[1])
Timestamp('2021-10-28 18:16:00')
now if I tried to access multiple elements, I got same error
pd.to_datetime(DTSdata_combined.index[1:2])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-130-9ca57df559ee> in <module>
----> 1 pd.to_datetime(DTSdata_combined.index[1:2])
C:\Anaconda\envs\tf_cpu\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
808 result = _assemble_from_unit_mappings(arg, errors, tz)
809 elif isinstance(arg, Index):
--> 810 cache_array = _maybe_cache(arg, format, cache, convert_listlike)
811 if not cache_array.empty:
812 result = _convert_and_box_cache(arg, cache_array, name=arg.name)
C:\Anaconda\envs\tf_cpu\lib\site-packages\pandas\core\tools\datetimes.py in _maybe_cache(arg, format, cache, convert_listlike)
167 from pandas import Series
168
--> 169 cache_array = Series(dtype=object)
170
171 if cache:
TypeError: 'list' object is not callable
Upvotes: 0
Views: 222
Reputation: 2359
Looks like a pandas Index isn't a requisite data type of pd.to_datetime().
What if you try: pd.to_datetime(DTSdata_combined.index[1:2].to_list())
Upvotes: 1
Reputation: 291
You convert Index of DataFrame to datatime format, so, first, need to convert DataFrame.Index to list. In pandas.to_datetime, first argument would be int, float, str, datetime, list, tuple, 1-d array, Series. That's why, please use the method pandas.Index.tolist
Upvotes: 1