TypeError Traceback (most recent call last) <ipython-input-94-942b147f7ea7> in <module> 7 8 # 计算参会时间 ----> 9 duration_col = (end_time_col - start_time_col).astype('timedelta64[m]') 10 11 # 将计算结果添加到表格中 D:\anaconda3\lib\site-packages\pandas\core\ops\common.py in new_method(self, other) 63 other = item_from_zerodim(other) 64 ---> 65 return method(self, other) 66 67 return new_method D:\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in wrapper(left, right) 341 lvalues = extract_array(left, extract_numpy=True) 342 rvalues = extract_array(right, extract_numpy=True) --> 343 result = arithmetic_op(lvalues, rvalues, op) 344 345 return left._construct_result(result, name=res_name) D:\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in arithmetic_op(left, right, op) 184 if should_extension_dispatch(lvalues, rvalues) or isinstance(rvalues, Timedelta): 185 # Timedelta is included because numexpr will fail on it, see GH#31457 --> 186 res_values = op(lvalues, rvalues) 187 188 else: D:\anaconda3\lib\site-packages\pandas\core\arrays\datetimelike.py in __rsub__(self, other) 1520 # GH#19959 datetime - datetime is well-defined as timedelta, 1521 # but any other type - datetime is not well-defined. -> 1522 raise TypeError( 1523 f"cannot subtract {type(self).__name__} from {type(other).__name__}" 1524 ) TypeError: cannot subtract DatetimeArray from ndarray这行代码那里错误
时间: 2024-02-14 07:08:24 浏览: 246
dict-recursive-update:一个执行递归更新的Python模块处理2个字典
5星 · 资源好评率100%
这个错误是因为在计算 `duration_col` 时,`end_time_col` 和 `start_time_col` 的数据类型不一致,可能一个是 `DatetimeArray` 类型,一个是 `ndarray` 类型。需要将两者的数据类型一致化,可以通过将 `start_time_col` 转换成 `DatetimeArray` 类型来解决这个问题,例如:`start_time_col = pd.to_datetime(start_time_col)`。
阅读全文