Reputation: 515
When I use the pd.to_datetime function on the data as shown below I get I time zone aware series that is in UTC. I am aware of the tz_convert function but I do not believe it suits my purpose.
The times that I have are in the US central time zone. If I don't specify this then my 21:00 CDT is assumed to be 21:00 UTC and tz_convert would incorrectly give me 16:00-5:00. Maybe I am just confused by this representation of time but as I understand it this would incorrectly represent 21:00 CDT as 16:00 CDT.
If I could specify the time zone to use when converting the time column I do not feel this would be an issue. Or if there was simply a way to set the timezone without doing a conversion. Below is an example:
df = pd.DataFrame([])
theseAreCentralTime = ['2015-04-24T23:48:28Z','2015-04-24T23:40:59Z','2015-04-24T23:48:28Z']
df['time'] = theseAreCentralTime
df['time'] = pd.to_datetime(df['time'])
print(df['time'].dt.tz)
print(df['time'])
df['time'] = df['time'].dt.tz_convert('US/Central')
print(df['time'].dt.tz)
print(df['time'])
the output of this is:
UTC
0 2015-04-24 23:48:28+00:00
1 2015-04-24 23:40:59+00:00
2 2015-04-24 23:48:28+00:00
Name: time, dtype: datetime64[ns, UTC]
US/Central
0 2015-04-24 18:48:28-05:00
1 2015-04-24 18:40:59-05:00
2 2015-04-24 18:48:28-05:00
Name: time, dtype: datetime64[ns, US/Central]
I am hoping to see the times as:
23:48:28-05:00
23:40:59-05:00
23:48:28-05:00
etc
Thanks in advance for any help
Upvotes: 4
Views: 9967
Reputation: 662
By changing your dates (removing the trailing 'Z'), you can do it like this:
df = pd.DataFrame([])
theseAreCentralTime = ['2015-04-24T23:48:28','2015-04-24T23:40:59','2015-04-24T23:48:28']
df['time'] = theseAreCentralTime
df['time'] = pd.to_datetime(df['time'])
print(df['time'].dt.tz_localize(tz='US/Central'))
Which would return:
0 2015-04-24 23:48:28-05:00
1 2015-04-24 23:40:59-05:00
2 2015-04-24 23:48:28-05:00
Name: time, dtype: datetime64[ns, US/Central]
Upvotes: 3