Change Utcoffset Of A Timestamp
I know that sometimes when you're converting between timezones Python gets confused about what the result should be, because timezones are hard. from pandas import Timestamp strin
Solution 1:
Before 1901-12-13 20:45:52, the utcoffset was 4 hours and 56 minutes.
You can confirm this using pytz which uses the Olson database. This is the same module that Pandas uses to perform timezone calculations:
importpytzeastern= pytz.timezone('US/Eastern')
for utcdate, info in zip(
eastern._utc_transition_times, eastern._transition_info):
utcoffset, dstoffset, tzabbrev = info
print('{} | {} '.format(utcdate, utcoffset.total_seconds()))
This prints all the utc transition boundaries and utcoffets (in seconds) for the US/Eastern timezone. The first few lines look like this
0001-01-01 00:00:00|-17760.01901-12-13 20:45:52|-18000.01918-03-31 07:00:00|-14400.01918-10-27 06:00:00|-18000.01919-03-30 07:00:00|-14400.0...
So before 1901-12-13 20:45:52, the utcoffset was -17760 seconds (or, equivalently, 4 hours and 56 minutes).
The standard way to make a timezone-aware date from a localtime using pytz is to call the localize
method:
import datetime as DT
import pytz
eastern = pytz.timezone('US/Eastern')
date = DT.datetime(1900,1,1)
local_date = eastern.localize(date)
print(local_date)
prints
1900-01-01 00:00:00-04:56
This confirms that the Timestamp returned by Pandas is correct:
import pandas as pd
string = "1900-01-01 00:00:00"
ts = pd.Timestamp(string, tz='US/Eastern')
print(ts)
# 1900-01-01 00:00:00-04:56
Post a Comment for "Change Utcoffset Of A Timestamp"