Passing a paremeter taken from a dataframe column to a pandas method
Pablo's answer was spot on, this is what I ended up using:
df['local_date'] = df.apply(lambda x: x['naive_date'].tz_localize(tz=x['timezone']), axis=1)
Let's say I have a dataframe with two columns, a naive date, and it's corresponding timezone. I'd like to convert the naive date to a timezone aware date using the second column.




naive_date
timezone




24-01-2021 05:00:00
'Europe/London'


24-01-2021 06:00:00
'Europe/Amsterdam'


24-01-2021 00:00:00
'US/Eastern'




If I just wanted to convert to a known timezone, I would do:
df['naive_date'].dt.tz_localize(tz='Europe/London')
But what if the value passed to tz should be taken from the timezone column for that particular row? Is that possible? I tried, which unsurprisingly didn't work:
df['naive_date'].dt.tz_localize(tz=df['timezone'])
For context I'll be comparing the date against another timezone aware date column, and the comparison returns false when comparing naive to timezone aware datetime.