Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

tz_localize based on another column

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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"])

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading