pandas 熊猫无效类型比较错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35265462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:39:17  来源:igfitidea点击:

Pandas invalid type comparison error

pythondatetimepandas

提问by orange

For some reason, which I can't find in the Pandas changelog for 0.17.1, comparing a datetime series with an int value (Unix epoch) does not work anymore. Could anyone please explain this or point me to the right section in the changelog?

出于某种原因,我在 0.17.1 的 Pandas 更新日志中找不到,将日期时间系列与 int 值(Unix 纪元)进行比较不再有效。任何人都可以解释这一点或将我指向更改日志中的正确部分吗?

Working in 0.16.2

在 0.16.2 中工作

>>> import pandas as pd
>>> import datetime
>>> d = pd.Series([datetime.datetime(2016, 1, 1), datetime.datetime(2016, 1, 1)])
>>> d
0   2016-01-01
1   2016-01-01
dtype: datetime64[ns]
>>> d.dtype
dtype('<M8[ns]')
>>> d > 10
0    True
1    True
dtype: bool

Error in 0.17.1

0.17.1 中的错误

>>> import pandas as pd
>>> import datetime
>>> d = pd.Series([datetime.datetime(2016, 1, 1), datetime.datetime(2016, 1, 1)])
>>> d > 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sven/tmp/pandastest/pandas-0.17.1/lib/python2.7/site-packages/pandas/core/ops.py", line 726, in wrapper
    res = na_op(values, other)
  File "/Users/sven/tmp/pandastest/pandas-0.17.1/lib/python2.7/site-packages/pandas/core/ops.py", line 657, in na_op
    raise TypeError("invalid type comparison")
TypeError: invalid type comparison

回答by Stop harming Monica

You can still use an explicit conversion:

您仍然可以使用显式转换:

u_time_ns = d.apply(lambda x: x.to_datetime64().view('int64'))
u_time_ns

0    1451606400000000000
1    1451606400000000000
dtype: int64

u_time_ns > 10

0    True
1    True
dtype: bool

Or, if you want to rely on pandas timestamps being stored as datetime64[ns]:

或者,如果您想依赖存储为的Pandas时间戳datetime64[ns]

u_time_ns = d.view('int64')

Sorry, no idea why this changed.

抱歉,不知道为什么这会改变。