Home > Back-end >  Vectorised Timezone Identifier
Vectorised Timezone Identifier

Time:08-16

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

CodePudding user response:

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
  • Related