I have a dataframe with epoch time. I convert the epoch time to a timestamp with my local timezone. I would like to remove the timezone information but keep my local timezone in the timestamp (subtract the timezone offset from the timestamp and then remove the timezone). This is the code I have:
epochs = np.arange(1644516000, 1644516000 + 1800*10, 1800)
df = pd.DataFrame({'time': epochs})
df['time'] = pd.to_datetime(df['time'], unit='s').dt.tz_localize("US/Pacific")
I cannot use:
dt.tz_localize(None)
Since it converts it back to UTC.
My desired output is a timestamp with no timezone information but in my local timezone:
pd.date_range('2022-02-10 10:00', freq='30min', periods=10)
How do I do that?
Essentially you're trying to get whatever time it was locally after x seconds since the unix epoch in a tz-naive timestamp. Achieving this is a bit weird because:
So what I would expect from e.g. pd.to_datetime(1644516000, unit="s") would be one of:
Timestamp('2022-02-10 18:00:00+0000', tz='UTC')Timestamp('2022-02-10 10:00:00-0800', tz='US/Pacific')Timestamp('2022-02-10 10:00:00') (which is what you're searching for)But instead, pd.to_datetime gives you the UTC local time since the UTC epoch, but as a tz-naive timestamp:
>>> pd.to_datetime(1644516000, unit="s")
Timestamp('2022-02-10 18:00:00')
One solution would be to manually do the three steps above, i.e. by first specifying that the received timestamps are UTC, then converting to your local time, then removing the tz-info:
df['time'] = pd.to_datetime(
df['time'],
unit='s',
utc=True
).dt.tz_convert("US/Pacific").dt.tz_localize(None)
but it feels like I'm missing something easier here...
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