将 pandas datetime64 列的时区设置为另一列

huangapple go评论64阅读模式
英文:

Setting the timezone of a pandas datetime64 column to another column

问题

在给定一个带有datetime64列和时区列的pandas DataFrame的情况下,你想知道如何将event_time列的时区用event_tz列中指定的时区覆盖。datetime列的tz_localizetz_convert函数似乎只接受单个值,而不是一个Series

谢谢你的问题。

英文:

Given a pandas DataFrame with a datetime64 column and a column representing the timezone:

dates_df = pandas.read_json("""[{"event_time": "2023-04-03T15:03:52", "event_tz": "EDT"}, {"event_time": "2023-04-03T15:03:52", "event_tz": "EDT"}]""")

How can the timezone of the event_time column be overwritten with the timezone specified in the event_tz column?

The tz_localize and tz_convert functions of the datetime column seem to only accept single values, as opposed to a Series.

Thank you in advance for your consideration and response.

答案1

得分: 0

pd.to_datetime(dates_df['event_time'].astype(str) + " " + dates_df['event_tz'])
英文:

Based on the suggestion in the comments:

pd.to_datetime(dates_df['event_time'].astype(str) + " " + dates_df['event_tz'])

huangapple
  • 本文由 发表于 2023年4月4日 07:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定