【发布时间】: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<='2017-1-29') & (df.time>='2016-12-28') ]会很有用。
标签: python python-3.x pandas dataframe