gpt4 book ai didi

python - 使用 dataframe.loc 时无法将 dtyped [datetime64[ns]] 数组与 [bool] 类型的标量进行比较的错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:27 26 4
gpt4 key购买 nike

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

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

首先,我使用 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]

最佳答案

这是运算符优先级。按位运算符在 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')

关于python - 使用 dataframe.loc 时无法将 dtyped [datetime64[ns]] 数组与 [bool] 类型的标量进行比较的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51814173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com