Check if datetime object in pandas has a timezone?

Viewed 4752

I'm importing data into pandas and want to remove any timezones – if they're present in the data. If the data has a time zone, the following code works successfully:

col = "my_date_column"
df[col] = pd.to_datetime(df[col]).dt.tz_localize(None) # We don't want timezones...

If the data does not contain a timezone, I'd like to use the following code:

df[col] = pd.to_datetime(df[col])

My issue is that I'm not sure how to test for timezone in the datetime object / series.

2 Answers

Assuming you have a column of type datetime, you can check the tzinfo of each timestamp in the column. It's basically described here (although this is not specific to pytz). Ex:

import pandas as pd

# example series:
s = pd.Series([
        pd.Timestamp("2020-06-06").tz_localize("Europe/Berlin"), # tzinfo defined
        pd.Timestamp("2020-06-07") # tzinfo is None
        ])

# s
# 0    2020-06-06 00:00:00+02:00
# 1          2020-06-07 00:00:00
# dtype: object
  
# now find a mask which is True where the timestamp has a timezone:
has_tz = s.apply(lambda t: t.tzinfo is not None)

# has_tz
# 0     True
# 1    False
# dtype: bool

This builds upon the prior answer by MrFuppes.

If the column is of type datetime64[ns], use Series.dt.tz:

col.dt.tz is None

If the column is of type object of pd.Timestamp, it doesn't support .dt, so use Timestamp.tz instead:

col.apply(lambda t: t.tz is None).all()
Related