[英]Python Pandas time difference between string and series
我收到以下錯誤:
-------------------------------------------------- ------------------------- TypeError Traceback(最近一次調用最后一次)~/anaconda3/lib/python3.8/site-packages/pandas/core /ops/array_ops.py in na_arithmetic_op(left, right, op, is_cmp) 142 try: --> 143 result = expressions.evaluate(op, left, right) 144 除了類型錯誤:
~/anaconda3/lib/python3.8/site-packages/pandas/core/computation/expressions.py inevaluate(op, a, b, use_numexpr) 232 if use_numexpr: --> 233 return _evaluate(op, op_str, a , b) # 類型:忽略 234 return _evaluate_standard(op, op_str, a, b)
~/anaconda3/lib/python3.8/site-packages/pandas/core/computation/expressions.py in _evaluate_numexpr(op, op_str, a, b) 118 如果結果是 None: --> 119 結果 = _evaluate_standard(op, op_str, a, b) 120
~/anaconda3/lib/python3.8/site-packages/pandas/core/computation/expressions.py in _evaluate_standard(op, op_str, a, b) 67 with np.errstate(all="ignore"): --- > 68 返回 op(a, b) 69
類型錯誤:不支持的操作數類型 -:'datetime.time' 和 'builtin_function_or_method'
在處理上述異常的過程中,又發生了一個異常:
----> 1 NVAX['snap_ts'].dt.time - datetime.strptime('14:30:00', '%H:%M:%S') 中的 TypeError Traceback(最近一次調用)。時間
~/anaconda3/lib/python3.8/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 返回 new_method
~/anaconda3/lib/python3.8/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=真)--> 343 結果 = 算術運算(左值,右值,運算) 344 345 返回左。_construct_result(結果,名稱=res_name)
~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py in algorithm_op(left, right, op) 188 else: 189 with np.errstate(all="ignore"): -- > 190 res_values = na_arithmetic_op(lvalues, rvalues, op) 191 192 返回 res_values
~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py in na_arithmetic_op(left, right, op, is_cmp) 148 # 將錯誤地處理復數,參見 GH#32047 149 raise -- > 150 結果 = masked_arith_op(left, right, op) 151 152 if is_cmp and (is_scalar(result) or result is NotImplemented):
~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py in masked_arith_op(x, y, op) 94 else: 95 if not is_scalar(y): ---> 96 raise TypeError ( 97 f"不能廣播 np.ndarray 類型為 { type(y) }" 98 的操作數)
類型錯誤:無法使用 <class 'builtin_function_or_method'> 類型的操作數廣播 np.ndarray
執行此操作時:
df['snap_ts'].dt.time - datetime.strptime('14:30:00', '%H:%M:%S')
df['snap_ts'].dt.time 相當於:
0 14:30:10
1 14:30:20
2 14:30:30
3 14:30:40
4 14:30:50
...
157763 19:59:20
157764 19:59:30
157765 19:59:40
157766 19:59:50
157767 20:00:00
Name: snap_ts, Length: 157768, dtype: object
這是一個 pandas.core.series.Series
我究竟做錯了什么?
你在找那個?
df['snap_ts'].sub(pd.Timedelta('14:30:00')).dt.time
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.