[Python] Localize time zone based on column in pandas

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to assign a timezone to a datetime column, based on another column containing the time zone.

    Example data:

    DATETIME VALUE TIME_ZONE
    0 2021-05-01 00:00:00 1.00 Europe/Athens
    1 2021-05-01 00:00:00 2.13 Europe/London
    2 2021-05-01 00:00:00 5.13 Europe/London
    3 2021-05-01 01:00:00 4.25 Europe/Dublin
    4 2021-05-01 01:00:00 4.25 Europe/Paris


    I am trying to assign a time zone to the DATETIME column, but using the tz_localize method, I cannot avoid using an apply call, which will be very slow on my large dataset. Is there some way to do this without using apply?

    What I have now (which is slow):

    df['DATETIME_WITH_TZ'] = df.apply(lambda row: row['DATETIME'].tz_localize(row['TIME_ZONE']), axis=1)

    Continue reading...

Compartilhe esta Página