2

I have a column of date time strings in UTC which I need to convert to a viable datetime format in EST using pandas. I successfully converted the column, however I feel that my solution is long-winded and can be simplified, but I'm not sure how. Here is the way I'm currently converting the dates:

df['datetime'] = pd.to_datetime(df['datetime'])
df['datetime'] = df['datetime'].dt.strftime('%m/%d/%Y %H:%M:%S')
df['datetime'] = pd.to_datetime(df['datetime'])
df['datetime'] = df['datetime'] - pd.Timedelta(hours=5)

Here are a few sample of the original string format:

2014-02-07T00:25:40Z
2014-02-07T00:25:40Z
2014-02-07T00:25:41Z
2014-02-07T00:25:42Z
2014-02-07T00:25:42Z
2014-02-07T00:25:43Z
2014-02-07T00:25:43Z
2014-02-07T00:25:44Z
2014-02-07T00:25:44Z    
2014-02-07T00:25:44Z

Can I convert this column with one or two lines? I tried performing the Timedelta() in the same step as the datetime formatting but received an error. Alternately I tried using tz_convert() and tz_localize() but received error with tz_convert() saying that my datetime wasn't a viable datetime format (hence why I reset the column to datetime after formatting). With tz_localize() it just added -5:00 to the end of my datetime instead of actually subtracting the 5 hours from the UTC time.

This is how the output should look:

2014-02-06 19:25:40
2014-02-06 19:25:40
2014-02-06 19:25:41
2014-02-06 19:25:42
2014-02-06 19:25:42
2014-02-06 19:25:43
2014-02-06 19:25:43
2014-02-06 19:25:44
2014-02-06 19:25:44
2014-02-06 19:25:44

1 Answer 1

2

I will do tz_localize and tz_localize

pd.to_datetime(df.date).dt.tz_localize('UTC').\
     dt.tz_convert('EST').\
      dt.strftime('%Y-%m-%d %H:%M:%S')
0    2014-02-06 19:25:40
1    2014-02-06 19:25:41
2    2014-02-06 19:25:42
3    2014-02-06 19:25:42
4    2014-02-06 19:25:43
5    2014-02-06 19:25:43
6    2014-02-06 19:25:44
7    2014-02-06 19:25:44
8    2014-02-06 19:25:44
Name: date, dtype: object
Sign up to request clarification or add additional context in comments.

4 Comments

I received a TypeError: Already tz-aware, use tz_convert to convert.
@andrrvt15 I am just using your input data and this is working fine on my side
@andrrvt15 github.com/influxdata/influxdb-python/issues/671, try to update your pandas
I removed dt.tz_localize() and it looks to have worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.