Pandas date time formatting with timezone shifting

1.8k Views Asked by At

I have a sqlite database where each row has a unix timestamp in EST. Changing this is not possible. I put this data into a pandas data frame.

I am running a script on a server that is on UTC and when I convert the EST timestamps to date time objects I am unable to capture the full day of EST time because it is shifted 4 hours forward like so:

df['time'] = pd.to_datetime(df['time'], unit='s').dt.date

Is there way to do timezone shifting in pandas so that date time objects are created based on EST unix time?

1

There are 1 best solutions below

5
On BEST ANSWER

The middle line is how I've converted timezones in pandas:

df['time'] = pd.to_datetime(df['time'], unit='s')
df = df.set_index('time').tz_localize('US/Eastern').tz_convert('UTC').tz_convert(None).reset_index()
df['time'] = df['time'].dt.date