2

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

Sami Wood
  • 305
  • 3
  • 10
  • Refer this https://stackoverflow.com/questions/22800079/converting-time-zone-pandas-dataframe – VH Praneeth Aug 21 '20 at 16:11
  • 3
    the problem with your date formatting is the last 'Z' means the date are UTC. – Julien Roullé Aug 21 '20 at 16:17
  • as a bit of a hack I added ```df['time'] = df['time'] + datetime.timedelta(hours = 5)``` this shift the time back but there must be a better way that would work for any given tz – Sami Wood Aug 21 '20 at 16:19
  • In support of @JulienRoullé: the format of your input is RFC3339 - the Z indicates UTC. If this is actually US/Central, your input is wrong (wrongly formatted). – FObersteiner Aug 21 '20 at 16:27

1 Answers1

3

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]
Julien Roullé
  • 662
  • 4
  • 15
  • 1
    Btw. you don't have to remove the Z upfront, you can parse with to_datetime with a suitable format string that contains a literal "Z" at the end. – FObersteiner Aug 21 '20 at 16:49