Problems with UTC datetime - how to fit a colon - time data '2021-03-11 09:30:01-05:00' does not match format '%Y%m%d %H:%M:%S %z'

Viewed 91

I'm trying to convert the string '2021-03-11 09:30:01-05:00' to a datetime using the following code.

It seems that my string as a colon (:) between 05 and 00 and that this colon is not in the default %z date format.

How can I convert my string to an EST datetime?

df = pd.read_csv(mypath + "ticks.csv")
df['time'] = pd.to_datetime(df['time'], format="'%Y-%m-%d %H:%M:%S%z'")
df['time'] = df['time'].dt.tz_localize('US/Eastern')
1 Answers

The following slight variation from what you were trying is working fine for me.

df = pd.read_csv('testfile.csv')

df['time'] = pd.to_datetime(df['time'], utc=True)
df['time'] = df['time'].dt.tz_convert('US/Eastern')

Result:

In [115]: df['time']
Out[115]: 
0       2021-03-11 09:30:01-05:00
1       2021-03-11 09:30:02-05:00
2       2021-03-11 09:30:14-05:00
3       2021-03-11 09:30:14-05:00
4       2021-03-11 09:30:14-05:00
                   ...           
35518   2021-03-15 15:59:53-04:00
35519   2021-03-15 15:59:54-04:00
35520   2021-03-15 15:59:56-04:00
35521   2021-03-15 15:59:56-04:00
35522   2021-03-15 15:59:57-04:00
Name: time, Length: 35523, dtype: datetime64[ns, US/Eastern]
Related