Issue
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.
Solution
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
Answered By - FObersteiner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.