使用dataframe.loc时无法将dtyped [datetime64 [ns]]数组与标量类型为[bool]的错误

时间:2018-08-13 01:32:52

标签: python python-3.x pandas dataframe

有一个数据框,其中包含以下几列以及其他列

Identification                     time                       
368                              2006-1-18
440                              2007-1-30
452                              2006-12-20
464                              2007-1-18

首先,我使用timedatetime列转换为相关的df['time'] = pd.to_datetime(df['time'])类型

我想选择某些行,然后使用

 df.loc[df.time<='2017-1-29' & df.date>='2016-12-28' ]

但是我收到以下错误消息,我做错了什么?

TypeError                                 Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
   1273         try:
-> 1274             result = op(x, y)
   1275         except TypeError:

~\Anaconda3\lib\site-packages\pandas\core\ops.py in rand_(left, right)
    145 def rand_(left, right):
--> 146     return operator.and_(right, left)
    147 

TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
   1290                 try:
-> 1291                     result = libops.scalar_binop(x, y, op)
   1292                 except:

pandas\_libs\ops.pyx in pandas._libs.ops.scalar_binop()

ValueError: cannot include dtype 'M' in a buffer

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-42-315be32eacf7> in <module>()
----> 1 df.loc[df.time<='2017-1-29' & df.time>='2016-12-28' ]

~\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other)
   1328                       is_integer_dtype(np.asarray(other)) else fill_bool)
   1329 
-> 1330             res_values = na_op(self.values, other)
   1331             unfilled = self._constructor(res_values, index=self.index)
   1332             return filler(unfilled).__finalize__(self)

~\Anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
   1294                                     "with a scalar of type [{typ}]"
   1295                                     .format(dtype=x.dtype,
-> 1296                                             typ=type(y).__name__))
   1297 
   1298         return result

TypeError: cannot compare a dtyped [datetime64[ns]] array with a scalar of type [bool]

1 个答案:

答案 0 :(得分:2)

这是运算符的优先级。在熊猫中,按位运算符会重载,但在被AST解析时,它们会保留其优先级。您必须将条件包装在括号中。

df.loc[(df.time <= '2017-1-29') & (df.time >= '2016-12-28')]

也请看一下pd.Series.between,它适用于包含双方或不包含双方的范围检查:

df.time.between('2016-12-28', '2017-1-29')