Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

python - Passing a paremeter taken from a dataframe column to a pandas method

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'
question from:https://stackoverflow.com/questions/65866411/passing-a-paremeter-taken-from-a-dataframe-column-to-a-pandas-method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...