TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('M8[ns]')

简介: TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('M8[ns]')

TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('M8[ns]')


Pandas 处理两列时间,想求时间差,在做差时报错如下:

TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('<M8[ns]')

bug 解释:

1.

'b'       boolean
'i'       (signed) integer
'u'       unsigned integer
'f'       floating-point
'c'       complex-floating point
'O'       (Python) objects
'S', 'a'  (byte-)string
'U'       Unicode
'V'       raw data (void)

2. dtype('<M8[ns]') 是表示时间的数据类型。


解决办法:

将两列都变为 pandas 中日期的标准类型:

df['after'] = pd.to_datetime(df['after'],format = '%Y-%m-%d %H:%M:%S')

即把 after 字段变成 标准日期格式。


问题解决。

相关文章
|
TensorFlow 算法框架/工具 Python
成功解决AttributeError: module 'numpy' has no attribute 'equal'
成功解决AttributeError: module 'numpy' has no attribute 'equal'
成功解决AttributeError: module 'numpy' has no attribute 'equal'
成功解决TypeError: unhashable type: 'numpy.ndarray'
成功解决TypeError: unhashable type: 'numpy.ndarray'
成功解决ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
成功解决ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
|
并行计算 Python
TypeError: can‘t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory
运行程序,出现报错信息 TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.。
276 0
|
PyTorch 算法框架/工具 开发者
RuntimeError: Can‘t call numpy() on Variable that requires grad. Use var.detach().numpy()
出现这个现象的原因是:待转换类型的PyTorch Tensor变量带有梯度,直接将其转换为numpy数据将破坏计算图,因此numpy拒绝进行数据转换,实际上这是对开发者的一种提醒。如果自己在转换数据时不需要保留梯度信息,可以在变量转换之前添加detach()调用。
147 0
|
PyTorch 算法框架/工具 索引
如何将[array([5, 0, 0, 0, 0, 0], dtype=uint32), array([0, 1, 0, 4, 0, 0], dtype=uint32), array([0, 0, 0, 3, 3, 3], dtype=uint32)] 转换成一个torch张量
在这个代码中,我先使用 torch.randperm() 函数生成一个长度为原始张量大小的随机索引序列,并用这个索引序列对原始张量进行重排,得到了打乱后的张量 shuffled_tensor。然后,我使用 torch.split() 函数将 shuffled_tensor 拆分成 3 份子张量,并打印出来以验证是否拆分成功。
267 0
|
机器学习/深度学习 PyTorch 算法框架/工具
解决Pytorch中RuntimeError: expected scalar type Double but found Float
解决Pytorch中RuntimeError: expected scalar type Double but found Float
2514 0
|
TensorFlow 算法框架/工具
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
243 0
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
Only Tensors of floating point and complex dtype can require gradients问题解决方案
Only Tensors of floating point and complex dtype can require gradients问题解决方案
303 0
Only Tensors of floating point and complex dtype can require gradients问题解决方案
|
Python
完美解决 TypeError: Invalid shape (3, 224, 224) for image data
完美解决 TypeError: Invalid shape (3, 224, 224) for image data
718 0