I have this Pandas dataframe column:
time_UTC
0 2015-01-05 16:44:34+00:00
1 2015-08-11 16:44:38+00:00
2 2015-08-02 16:53:25+00:00
3 2015-08-17 16:53:25+00:00
4 2015-09-28 16:53:26+00:00
Name: time_UTC, dtype: datetime64[ns, UTC]
and I converted it from UTC to US-Eastern timezone using:
list_temp = []
for row in df['time_UTC']:
list_temp.append(Timestamp(row, tz = 'UTC').tz_convert('US/Eastern'))
df['time_EST'] = list_temp
to get this:
0 2015-01-05 11:44:34-05:00
1 2015-08-11 11:44:38-05:00
2 2015-08-02 11:53:25-05:00
3 2015-08-17 11:53:25-05:00
4 2015-09-28 11:53:26-05:00
Name: time_EST, dtype: datetime64[ns, US/Eastern]
Now, I need to drop the date part of the entries so that I only get the time. Here is what I need:
0 11:44:34-05:00
1 11:44:38-05:00
2 11:53:25-05:00
3 11:53:25-05:00
4 11:53:26-05:00
Name: time_EST, dtype: datetime64[ns, US/Eastern]
Attempt:
I tried this:
print df['time_EST'].apply(lambda x: dt.time(x.hour,x.minute,x.second))
The conversion is made so that date is dropped and I only get time. But it is reverting back to the UTC timezone. Here is the output of the above command:
0 16:44:34
1 16:44:38
2 16:53:25
3 16:53:25
4 16:53:26
Name: time_EST, dtype: object
Question:
Is there a way to drop the date and keep time as US-Eastern, without automatically reverting back to UTC?
EDIT:
To recreate the problem, just copy the first DataFrame above and use this code:
import pandas as pd
from pandas.lib import Timestamp
import datetime as dt
df = pd.read_clipboard()
Then copy the remaining lines of code from the question. Any assistance with this problem would be greatly appreciated.
You want to use strftime
to format your string, also note the vectorized date manipulations:
df = pd.read_clipboard()
df.time_UTC = pd.to_datetime(df.time_UTC)
df['EST'] = (df.time_UTC.dt.tz_localize('UTC')
.tz_convert('US/Eastern')
.strftime("%H:%M:%S"))
In [41]: df
Out[41]:
time_UTC EST
time_UTC
2016-02-15 16:44:34 2016-02-15 16:44:34 11:44:34
2016-02-15 16:44:38 2016-02-15 16:44:38 11:44:38
2016-02-15 16:53:25 2016-02-15 16:53:25 11:53:25
2016-02-15 16:53:25 2016-02-15 16:53:25 11:53:25
2016-02-15 16:53:26 2016-02-15 16:53:26 11:53:26
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With