如何使用pandas将带有时区的时间戳转换为本地时间格式?

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

How to convert timestamp with time zone to local time format with pandas?

问题

让我们假设我有一个名为“timestamp”的列,其中包含以下格式的值 -
'2023-02-04 08:08:42+03:00'(当dtype为“Timestamp”时)

转换的预期结果应该是'2023-02-04 05:08:42'

我该如何转换它?

英文:

Let's say I have a column called timestamp with values from the following format -
2023-02-04 08:08:42+03:00 (when the dtype is Timestamp)

The expected result of the conversion should be 2023-02-04 05:08:42

How can I convert it?

答案1

得分: 1

尝试,

import pandas as pd
s=pd.Series([pd.Timestamp('now', 
tz='America/Chicago')])
t=s.dt.tz_convert('utc').dt.tz_localize(None)
print(s,t)
英文:

Try,

import pandas as pd
s=pd.Series([pd.Timestamp('now', 
tz='America/Chicago')])
t=s.dt.tz_convert('utc').dt.tz_localize(None)
print(s,t)

huangapple
  • 本文由 发表于 2023年4月16日 23:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028735.html
匿名

发表评论

匿名网友

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

确定