gpt4 book ai didi

python - 如何获取 datetime64[ns] 的时间偏移信息

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:30 25 4
gpt4 key购买 nike

我正在将 datetime[ns] 列转换为不同的时区,结果列如下所示:

0   2011-05-25 20:30:00+02:00
1 2011-05-24 20:30:00+02:00
2 2011-05-20 20:30:00+02:00
3 2011-05-19 20:30:00+02:00
4 2011-05-15 13:30:00+02:00

我正在尝试提取偏移信息,这里是+02:00

offset = converted.apply(lambda x: x.strftime('%z'))

但它给了我一个空白栏。

当列的 dtype == object/str 时,offest 信息消失,因此也无法使用正则表达式将其输出。

有什么想法吗?

最佳答案

我认为你需要strftime :

print (df)
date
0 2011-05-25 20:30:00+02:00
1 2011-05-24 20:30:00+02:00
2 2011-05-20 20:30:00+02:00
3 2011-05-19 20:30:00+02:00
4 2011-05-15 13:30:00+02:00

print (df.date.dt.tz)
Europe/Bratislava

df['timezone'] = df.date.dt.strftime('%z')
print (df)
date timezone
0 2011-05-25 20:30:00+02:00 +0200
1 2011-05-24 20:30:00+02:00 +0200
2 2011-05-20 20:30:00+02:00 +0200
3 2011-05-19 20:30:00+02:00 +0200
4 2011-05-15 13:30:00+02:00 +0200

如果date列的dtypeobject/string使用indexing with str :

df['date'] = df.date.astype(str)
df['timezone'] = df.date.str[-6:]
print (df)
date timezone
0 2011-05-25 20:30:00+02:00 +02:00
1 2011-05-24 20:30:00+02:00 +02:00
2 2011-05-20 20:30:00+02:00 +02:00
3 2011-05-19 20:30:00+02:00 +02:00
4 2011-05-15 13:30:00+02:00 +02:00

关于python - 如何获取 datetime64[ns] 的时间偏移信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37878167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com