Python Pandas字符串和系列之间的时间差
Python Pandas time difference between string and series
我收到以下错误:
--------------------------------------------------------------------------- TypeError Traceback (most recent call
last)
~/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 except TypeError:
~/anaconda3/lib/python3.8/site-packages/pandas/core/computation/expressions.py
in evaluate(op, a, b, use_numexpr)
232 if use_numexpr:
--> 233 return _evaluate(op, op_str, a, b) # type: ignore
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 if result is None:
--> 119 result = _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 return op(a, b)
69
TypeError: unsupported operand type(s) for -: 'datetime.time' and
'builtin_function_or_method'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call
last) in
----> 1 NVAX['snap_ts'].dt.time - datetime.strptime('14:30:00', '%H:%M:%S').time
~/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 return 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=True)
--> 343 result = arithmetic_op(lvalues, rvalues, op)
344
345 return left._construct_result(result, name=res_name)
~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py
in arithmetic_op(left, right, op)
188 else:
189 with np.errstate(all="ignore"):
--> 190 res_values = na_arithmetic_op(lvalues, rvalues, op)
191
192 return res_values
~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py
in na_arithmetic_op(left, right, op, is_cmp)
148 # will handle complex numbers incorrectly, see GH#32047
149 raise
--> 150 result = 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"Cannot broadcast np.ndarray with operand of type { type(y) }"
98 )
TypeError: Cannot broadcast np.ndarray with operand of type <class
'builtin_function_or_method'>
执行此操作时:
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
我收到以下错误:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~/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 except TypeError:
~/anaconda3/lib/python3.8/site-packages/pandas/core/computation/expressions.py in evaluate(op, a, b, use_numexpr) 232 if use_numexpr: --> 233 return _evaluate(op, op_str, a, b) # type: ignore 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 if result is None: --> 119 result = _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 return op(a, b) 69
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'builtin_function_or_method'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last) in ----> 1 NVAX['snap_ts'].dt.time - datetime.strptime('14:30:00', '%H:%M:%S').time
~/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 return 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=True) --> 343 result = arithmetic_op(lvalues, rvalues, op) 344 345 return left._construct_result(result, name=res_name)
~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py in arithmetic_op(left, right, op) 188 else: 189 with np.errstate(all="ignore"): --> 190 res_values = na_arithmetic_op(lvalues, rvalues, op) 191 192 return res_values
~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py in na_arithmetic_op(left, right, op, is_cmp) 148 # will handle complex numbers incorrectly, see GH#32047 149 raise --> 150 result = 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"Cannot broadcast np.ndarray with operand of type { type(y) }" 98 )
TypeError: Cannot broadcast np.ndarray with operand of type <class 'builtin_function_or_method'>
执行此操作时:
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