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