Problem
我在 pandas DataFrame 中有一个列,其中包含带有时区的时间戳。此列中有两个不同的时区,我需要确保只有一个。这是该列末尾的输出:
260003 2019-05-21 12:00:00-06:00
260004 2019-05-21 12:15:00-06:00
Name: timestamp, Length: 260005, dtype: object
就其价值而言,时间戳之间有所不同-06:00
and -07:00
,并有以下输出:
datetime.datetime(2007, 10, 1, 1, 0, tzinfo=tzoffset(None, -21600))
for -06:00
datetime.datetime(2007, 11, 17, 5, 15, tzinfo=tzoffset(None, -25200))
for -07:00
我做了什么
我一直在尝试使用 tz.localize 和 tz.convert,它们在过去运行良好,但我认为数据只有一个时区。例如,如果我这样做:
df['timestamp'].dt.tz_localize('MST', ambiguous='infer').dt.tz_convert('MST')
I get:
ValueError: Array must be all same time zone
During handling of the above exception, another exception occurred:
ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True
Question
有没有办法将它们转换为 MST?或者任何时区,真的吗?我想我可以按时区分解 DataFrame(不是 100% 确定如何,但我想这是可能的)并对其进行操作,但我想我会问是否有更智能的解决方案。谢谢你!