Reputation: 2330
I have a pandas dataframe that unfortunately switches datetime formats from:
to:
I need to parse the df['DT'] into a Datetime and then a DatetimeIndex. It seems to work, but then keeps the two types of datetimes:
df['Datetime'] = pd.to_datetime(df['DT'])
df = df.set_index('Datetime')
del df['DT']
But the pandas timeseries functionality does not work, i.e.:
from datetime import time
df.between_time(time(0),time(8,59,59))
Throws the error: TypeError: Index must be DatetimeIndex.
I would therefor like to know how to parse these two different Datetime formats into a single DateTimeIndex. Thanks for your help!
Upvotes: 2
Views: 1904
Reputation: 317
first of all, try the following:
In[54]: b = '2010-10-02T24:00:00+0000'
In[55]: pd.to_datetime(b,errors='raise')
(... ...)
ValueError: hour must be in 0..23
This tells u that the datetime format of variable {b} is wrong. so two choices here. the first one is to correct the str format (modify "24" to "00"), then apply the {pd.to_datetime} func:
In[56]: df
Out[56]:
0
0 11/23/2014 01:37:00 AM +0000
1 2010-10-02T00:00:00+0000
In[57]: pd.to_datetime(df[0])
Out[57]:
0 2014-11-23 01:37:00
1 2010-10-02 00:00:00
Name: 0, dtype: datetime64[ns]
the second one would be to specify the format within {pd.to_datetime} func so that it can recognize ur special str format (which is not very straightforward if u want to keep ur time info in this case)
Upvotes: 2