gpt4 book ai didi

python - Pandas:将 CSV 文件中本地时间不明确的数据框设置为区域设置

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

我读取的 CSV 文件的本地时间不明确——由于夏令时而不明确。

当我应用tz_localize()时,我运行时出现以下错误:

AmbiguousTimeError: There are %i dst switches when there should only be 1.

这是我重现错误的代码。请注意,由于夏令时,从凌晨 2 点到凌晨 3 点,每个日期时间都是重复的。

index = pd.DatetimeIndex(['2018-10-28 01:00:00', '2018-10-28 01:15:00',
'2018-10-28 01:30:00', '2018-10-28 01:45:00',
'2018-10-28 02:00:00', '2018-10-28 02:00:00',
'2018-10-28 02:15:00', '2018-10-28 02:15:00',
'2018-10-28 02:30:00', '2018-10-28 02:30:00',
'2018-10-28 02:45:00', '2018-10-28 02:45:00',
'2018-10-28 03:00:00', '2018-10-28 03:15:00',
'2018-10-28 03:30:00', '2018-10-28 03:45:00',
'2018-10-28 04:00:00'], freq='infer')

data = list(range(len(index)))

df = pd.DataFrame(data=data,index=index)
df.index = df.index.tz_localize('Europe/Berlin', ambiguous='infer')

我不太明白为什么即使使用参数ambigously='infer'也会发生错误。
我的数据框必须以任何方式排序吗?
如何使用 pd.read_csv() 创建排序数据框
我希望对 CSV 文件的顺序应用 ambigously='infer' 。确切地说,我的意思是:模糊的时间格式顺序有助于区分夏季时间和冬季时间。

我很高兴能提供解决方案的帮助和更广泛的解释。我找不到太多关于顺序如何在数据框中发挥作用的信息——有时会这样吗?

最佳答案

index.tz_localize('Europe/Berlin', ambiguous=np.array([True]*17))

我认为需要推断你的一些 dst 时间,但是你有重复的时间索引导致推断失败,使用可选的 np.array 可以工作

关于python - Pandas:将 CSV 文件中本地时间不明确的数据框设置为区域设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58415410/

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