Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pandas Convert Date Column From UTC to EST dates
#!/usr/bin/env python
import pandas as pd
df = pd.DataFrame([
{
'date': '2019-01-01 14:30:00',
'epochms': 1546353000000
},
{
'date': '2019-01-01 14:31:00',
'epochms': 1546353060000
}
])
for c in df:
if c == 'date': # as a string formatted '%Y-%m-%d %H:%M:%S'
df[c] = pd.DatetimeIndex(pd.to_datetime(
df[c],
format='%Y-%m-%d %H:%M:%S')).tz_localize(
'UTC').tz_convert(
'US/Eastern')
else: # otherwise convert epoch milliseconds to EST
df[c] = pd.DatetimeIndex(pd.to_datetime(
df[c],
unit='ms')).tz_localize(
'UTC').tz_convert(
'US/Eastern')
# for all columns in the df
print(df)
print(df['date'])
print(df['epochms'])
@jay-johnson

This comment has been minimized.

Copy link
Owner Author

commented Jan 3, 2019

                       date                   epochms
0 2019-01-01 09:30:00-05:00 2019-01-01 09:30:00-05:00
1 2019-01-01 09:31:00-05:00 2019-01-01 09:31:00-05:00
0   2019-01-01 09:30:00-05:00
1   2019-01-01 09:31:00-05:00
Name: date, dtype: datetime64[ns, US/Eastern]
0   2019-01-01 09:30:00-05:00
1   2019-01-01 09:31:00-05:00
Name: epochms, dtype: datetime64[ns, US/Eastern]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.