Vectorised Timezone Identifier

Question:

I’m trying to create a vectorised implementation of the following function, as currently using pandas apply with my dataframe takes too long, but coming unstuck. Can anyone help?

    """
    Returns GMT if date is between clock changes in winter, and BST if date is between clock changes in summer
    """
    import pytz
    timezone_str = 'Europe/London'
    timezone = pytz.timezone(timezone_str)
    t = timezone.utcoffset(d)
    timezone = 'GMT' if t.seconds==0 else 'BST'
    return timezone
Asked By: Iain MacCormick

||

Answers:

an illustration of my comment; are you looking for strftime’s "%Z"?

import pandas as pd

# a dummy example
df = pd.DataFrame({"dt": ["2022-01-05", "2022-08-05"]})
# assuming we have aware datetime
df["dt"] = pd.to_datetime(df["dt"]).dt.tz_localize("Europe/London")

df["zone"] = df["dt"].dt.strftime("%Z")

print(df)
                         dt zone
0 2022-01-05 00:00:00+00:00  GMT
1 2022-08-05 00:00:00+01:00  BST
Answered By: FObersteiner
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.