熊猫无效类型比较错误

ora*_*nge 5 python datetime pandas

由于某种原因,我无法在0.17.1的Pandas更新日志中找到,将日期时间序列与一个int值(Unix纪元)进行比较不再有效。谁能解释一下或在变更日志中指向我正确的部分吗?

在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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Sto*_*ica 1

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

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
Run Code Online (Sandbox Code Playgroud)

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

u_time_ns = d.view('int64')
Run Code Online (Sandbox Code Playgroud)

抱歉,不知道为什么会发生这种变化。