nrcjea001
nrcjea001

Reputation: 1057

Pandas: converting to datetime with timezone name

I am trying to convert to datetime in pandas dataframe, where name of timezone is included. I have tried the following but it yields an error:

d = {'col1': ['Sun Dec 31 00:00:00 EAT 1899', 'Mon Mar 02 00:00:00 EAT 2020']}
x = pd.DataFrame(data=d)
pd.to_datetime(x['col1'], format='%a %b %d %H:%M:%S %Z %Y', errors='coerce')

0   NaT
1   NaT
Name: col1, dtype: datetime64[ns]

Is there any way to convert to datetime this way? If not, how to ignore the timezone name?

Upvotes: 1

Views: 82

Answers (1)

Ben.T
Ben.T

Reputation: 29635

you can use dateutil:

import dateutil
print (x['col1'].apply(dateutil.parser.parse))
0   1899-12-31
1   2020-03-02
Name: col1, dtype: datetime64[ns]

or to ignore the part with the timezoen, something like:

pd.to_datetime(x['col1'].str[:20] + x['col1'].str[24:])

but not sure if it is interesting

Upvotes: 1

Related Questions