3

我正在设置一个带有两个日期时间列的 DataFrame,如下所示:

range1 = Series(date_range('1/1/2011', periods=50, freq='D'))
range2 = Series(date_range('2/5/2011', periods=50, freq='D'))
df1    = DataFrame({'a': rng1, 'b': rng2}, dtype='datetime64[D]')

奇怪的是,询问 df1 的 dtypes 给了我:

In [71]: df1.dtypes
Out[71]:
a    datetime64[ns]
b    datetime64[ns]

更糟糕的是,当我尝试像这样过滤数据框时:

In [62]:

d = datetime(2011,1,14)
df1[df1 > d]

我收到一个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-62-50b4b9735157> in <module>()
      1 d = datetime(2011,1,14)
----> 2 df1[df1 > d]

/usr/local/lib/python2.7/site-packages/pandas/core/frame.pyc in f(self, other)
    313             return self._combine_series_infer(other, func)
    314         else:
--> 315             return self._combine_const(other, func)
    316 
    317     f.__name__ = name

/usr/local/lib/python2.7/site-packages/pandas/core/frame.pyc in _combine_const(self, other, func)
   3677             return self
   3678 
-> 3679         result_values = func(self.values, other)
   3680 
   3681         if not isinstance(result_values, np.ndarray):

TypeError: can't compare datetime.datetime to long

谁能告诉我为什么会这样?我正在使用 numpy 1.7 和 pandas 0.10.1。

4

1 回答 1

2

datetime64[ns]支持 dtype,尝试不使用 dtype

In [9]: df1    = DataFrame({'a': range1, 'b' : range2})

In [10]: df1
In [15]: df1.head()
Out[15]: 
                    a                   b
0 2011-01-01 00:00:00 2011-02-05 00:00:00
1 2011-01-02 00:00:00 2011-02-06 00:00:00
2 2011-01-03 00:00:00 2011-02-07 00:00:00
3 2011-01-04 00:00:00 2011-02-08 00:00:00
4 2011-01-05 00:00:00 2011-02-09 00:00:00

In [16]: df1[df1.a>datetime.datetime(2011,1,14)].head()
Out[16]: 
                     a                   b
14 2011-01-15 00:00:00 2011-02-19 00:00:00
15 2011-01-16 00:00:00 2011-02-20 00:00:00
16 2011-01-17 00:00:00 2011-02-21 00:00:00
17 2011-01-18 00:00:00 2011-02-22 00:00:00
18 2011-01-19 00:00:00 2011-02-23 00:00:00

仅供参考,在此之后:https://github.com/pydata/pandas/issues/3311被合并,然后OP指示的操作,awhere返回:

In [15]: df1[df1>datetime.datetime(2011,1,14)].head(20)
Out[15]: 
                     a                   b
0                  NaT 2011-02-05 00:00:00
1                  NaT 2011-02-06 00:00:00
2                  NaT 2011-02-07 00:00:00
3                  NaT 2011-02-08 00:00:00
4                  NaT 2011-02-09 00:00:00
5                  NaT 2011-02-10 00:00:00
6                  NaT 2011-02-11 00:00:00
7                  NaT 2011-02-12 00:00:00
8                  NaT 2011-02-13 00:00:00
9                  NaT 2011-02-14 00:00:00
10                 NaT 2011-02-15 00:00:00
11                 NaT 2011-02-16 00:00:00
12                 NaT 2011-02-17 00:00:00
13                 NaT 2011-02-18 00:00:00
14 2011-01-15 00:00:00 2011-02-19 00:00:00
15 2011-01-16 00:00:00 2011-02-20 00:00:00
16 2011-01-17 00:00:00 2011-02-21 00:00:00
17 2011-01-18 00:00:00 2011-02-22 00:00:00
18 2011-01-19 00:00:00 2011-02-23 00:00:00
19 2011-01-20 00:00:00 2011-02-24 00:00:00
于 2013-04-10T13:51:43.480 回答