我有两个可识别时区的panda日期时间戳。当我减去它们时,我得到了错误:
TypeError: Timestamp subtraction must have the same timezones or no timezones
第一个是从转换为熊猫日期时间:
start_time = pd.to_datetime(dt.datetime(2019, 6, 28, 0, 52, 57, tzinfo=dt.timezone.utc))
第二个来自DataFrame索引,它看起来像这样:
>> phase_df.index[0]
Timestamp('2019-06-28 00:52:30.000130+0000', tz='UTC')
确认它们都是同一类型:
>> type(phase_df.index[0]), type(start_time)
(pandas._libs.tslibs.timestamps.Timestamp,
pandas._libs.tslibs.timestamps.Timestamp)
我能找到的唯一解决方案似乎是在减法之前删除所有时区信息。
start_time = pd.to_datetime(dt.datetime(2019, 6, 28, 0, 52, 57, tzinfo=dt.timezone.utc)).tz_localize(None)
other_time = phase_df.index.tz_localize(None)
start_time - other_time
没有错误。但它确实应该能够与时区一起工作。一定是个bug。