Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove timezone from timestamp but keep the local time

Tags:

python

pandas

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?

like image 472
Eyal S. Avatar asked Dec 06 '25 18:12

Eyal S.


1 Answers

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:

  • My experience with tz-naive timestamps in pandas says that they are usually "local" to the user's current timezone.
  • Converting to timestamp from an epoch timestamp feels to me a tz-aware operation, given that you count from UTC 1970-01-01, not your local 1970-01-01.

So what I would expect from e.g. pd.to_datetime(1644516000, unit="s") would be one of:

  • A UTC-localized timestamp: Timestamp('2022-02-10 18:00:00+0000', tz='UTC')
  • The above converted to local: Timestamp('2022-02-10 10:00:00-0800', tz='US/Pacific')
  • Or a tz-naive timestamp representing the local time since UTC epoch: 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...

like image 126
Chrysophylaxs Avatar answered Dec 08 '25 08:12

Chrysophylaxs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!