【发布时间】:2016-08-06 06:14:16
【问题描述】:
我正在尝试在 Pandas DataFrame 和 Datetime 对象之间进行比较。
import pandas as pd
from datetime import datetime
df = pd.DataFrame({'date': [ datetime(2000, 1, 1)]})
# Works fine
test1 = df['date'] >= datetime(2000, 1, 2)
# Returns error
test2 = datetime(2000, 1, 2) <= df['date']
导致此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-950-a1d9be25e98f> in <module>()
7 test1 = df['date'] >= datetime(2000, 1, 2)
8 # Returns error
----> 9 test2 = datetime(2000, 1, 2) <= df['date']
TypeError: can't compare datetime.datetime to Series
为什么第二个比较不起作用?是有错误还是执行比较的方法不正确?
运行: python 2.7、pandas 0.18、Windows 2010 服务器、anaconda
【问题讨论】:
-
你的 numpy 版本是什么,因为这对我有用:
test2 Out[222]: 0 False Name: date, dtype: bool -
@EdChum 我在 py27 中安装了 numpy 1.10.4 但这对我不起作用。你的 numpy 版本是什么?
-
np 是
1.10.4pandas 是0.18.0但 python 3.4 64 位
标签: python python-2.7 datetime pandas