时间戳减法必须具有相同的时区或没有时区,但它们都是 UTC [英] Timestamp subtraction must have the same timezones or no timezones but they are both UTC

查看:33
本文介绍了时间戳减法必须具有相同的时区或没有时区,但它们都是 UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些问题解决了相同的错误TypeError:时间戳减法必须具有相同的时区或没有时区,但没有一个问题与此相同.

There are questions that addresses the same error TypeError: Timestamp subtraction must have the same timezones or no timezones but none faces the same issue as this one.

我有 2 个 UTC 时间戳,在减去时会抛出该错误.

I have 2 UTC Timestamps that throw that error when substracted.

print(date, type(date), date.tzinfo)
>>> 2020-07-17 00:00:00+00:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'> UTC
print(date2, type(date2), date2.tzinfo)
>>> 2020-04-06 00:00:00.000000001+00:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'> UTC
date - date2
>>> TypeError: Timestamp subtraction must have the same timezones or no timezones

我正在使用 Python 3.6.9Pandas 1.0.1

推荐答案

检查时区类型后: type(date.tzinfo) 给出 type(date2.tzinfo) 给出了 所以根据 pandas 源代码,它们不被认为是相等的,即使它们都是 UTC.

After checking the timezone types: type(date.tzinfo) gives <class 'datetime.timezone'> and type(date2.tzinfo) gives <class 'pytz.UTC'> so acording of pandas source code they are not considered equal even even if they are both UTC.

所以解决方案是让它们具有相同的 tzinfo 类型(pytzdatitme.timezone)

So the solution was to make them have the same tzinfo type (either pytz or datitme.timezone)

这是 Github 中的一个开放问题:https://github.com/pandas-dev/pandas/issues/32619

This is an open issue in Github: https://github.com/pandas-dev/pandas/issues/32619

这篇关于时间戳减法必须具有相同的时区或没有时区,但它们都是 UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆