Home > other >  tz_localize based on another column
tz_localize based on another column

Time:09-06

I have a dataframe with the following structure:

import numpy as np
import pandas as pd

df = pd.DataFrame(
    dict(
        time=pd.DatetimeIndex([
            '2018-10-28 01:30:00',
            '2018-10-28 02:00:00',
            '2018-10-28 02:30:00',
            '2018-10-28 02:00:00',
            '2018-10-28 02:30:00',
            '2018-10-28 03:00:00',
            '2018-10-28 03:30:00']), 
        zone=np.random.choice(['Europe/Madrid', 'Europe/London', 'Europe/Paris'], 7)
    )
)

I want to apply tz_localize row-wise, something like:

df.assign(
    time_localized=df.time.dt.tz_localize(df["zone"])
)

it returns

TypeError: <class 'pandas.core.series.Series'>

how can I apply row-wise tz_localize?

I am also interested in doing the reverse operation, moving from UTC to each of the time zones in the rows.

Thanks!

CodePudding user response:

Not sure there's a vectorised way to do this, might have to use apply:

df.assign(
    time_localized=lambda df: df.apply(
        lambda row: row["time"]
        .tz_localize(row["zone"], ambiguous=True),
        axis=1,
    )
)

which gives

                 time           zone             time_localized
0 2018-10-28 01:30:00  Europe/London  2018-10-28 01:30:00 00:00
1 2018-10-28 02:00:00  Europe/Madrid  2018-10-28 03:00:00 01:00
2 2018-10-28 02:30:00  Europe/London  2018-10-28 02:30:00 00:00
3 2018-10-28 02:00:00   Europe/Paris  2018-10-28 03:00:00 01:00
4 2018-10-28 02:30:00   Europe/Paris  2018-10-28 03:30:00 01:00
5 2018-10-28 03:00:00  Europe/London  2018-10-28 03:00:00 00:00
6 2018-10-28 03:30:00   Europe/Paris  2018-10-28 04:30:00 01:00

For the reverse operation, change .tz_localize(row["zone"], ambiguous=True) to .tz_localize("UTC").tz_convert(row["zone"])

  • Related