Last active
January 3, 2019 19:02
-
-
Save jay-johnson/8fdeedca3b077f336112a0a4c0512a49 to your computer and use it in GitHub Desktop.
Pandas Convert Date Column From UTC to EST dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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']) |
Author
jay-johnson
commented
Jan 3, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment