【问题标题】:error of cannot compare a dtyped [datetime64[ns]] array with a scalar of type [bool] when using dataframe.loc使用 dataframe.loc 时无法将 dtyped [datetime64[ns]] 数组与 [bool] 类型的标量进行比较的错误
【发布时间】:2018-08-13 01:32:51
【问题描述】:

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

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

首先,我将time 列转换为相关的datetime 类型,使用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]

【问题讨论】:

  • df.loc[(df.time&lt;='2017-1-29') &amp; (df.time&gt;='2016-12-28') ] 会很有用。

标签: python python-3.x pandas dataframe


【解决方案1】:

这是运算符优先级。位运算符在 pandas 中被重载,但在被 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')

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2016-11-10
    • 2013-12-18
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2021-05-24
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多