Reputation: 25
I have a column in a dataframe that I want to convert to a date. The values of the column are either DDMONYYY
or DD Month YYYY 00:00:00.000 GMT
. For example, one row in the dataframe could have the value 31DEC2002
and the next row could have 31 December 2015 00:00:00.000 GMT
. I think this is why I get an error when trying to convert the column to a date using pd.to_datetime or datetime.strptime to convert.
Anyone got any ideas? I'd be very grateful for any help/pointers.
Upvotes: 1
Views: 120
Reputation: 863651
For me working to_datetime
with utc=True
for converting all values to UTC
and errors='coerce'
for convert not parseable values to NaT
(missing datetime):
df = pd.DataFrame({'date':['31DEC2002','31 December 2015 00:00:00.000 GMT','.']})
df['date'] = pd.to_datetime(df['date'], utc=True, errors='coerce')
print (df)
date
0 2002-12-31 00:00:00+00:00
1 2015-12-31 00:00:00+00:00
2 NaT
Upvotes: 3