【问题标题】:Python Pandas time difference between string and series字符串和系列之间的Python Pandas时间差
【发布时间】:2021-10-02 01:53:58
【问题描述】:

我收到以下错误:

----------------------------------- ---------------------------- TypeError Traceback(最近一次调用 最后的) ~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py 在 na_arithmetic_op(left, right, op, is_cmp) 142 尝试: --> 143 结果 = 表达式.evaluate(op, left, right) 144 除了类型错误:

~/anaconda3/lib/python3.8/site-packages/pandas/core/computation/expressions.py 在评估(op,a,b,use_numexpr) 232 如果使用_numexpr: --> 233 return _evaluate(op, op_str, a, b) # 类型:忽略 第234章

~/anaconda3/lib/python3.8/site-packages/pandas/core/computation/expressions.py 在 _evaluate_numexpr(op, op_str, a, b) 118 如果结果为无: --> 119 结果 = _evaluate_standard(op, op_str, a, b) 120

~/anaconda3/lib/python3.8/site-packages/pandas/core/computation/expressions.py 在 _evaluate_standard(op, op_str, a, b) 67 与 np.errstate(all="ignore"): ---> 68 返回操作(a,b) 69

TypeError: 不支持的操作数类型 -: 'datetime.time' 和 'builtin_function_or_method'

在处理上述异常的过程中,又发生了一个异常:

TypeError Traceback(最近调用 最后)在 ----> 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 在 新方法(自我,其他) 63 其他 = item_from_zerodim(其他) 64 ---> 65 返回方法(自我,其他) 66 67 返回新方法

~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/init.py 包装(左,右) 341 左值 = extract_array(左,extract_numpy=True) 342 rvalues = extract_array(对,extract_numpy=True) --> 343 结果 = 算术运算(左值,右值,运算) 344 345 return left._construct_result(result, name=res_name)

~/anaconda3/lib/python3.8/site-packages/pandas/core/ops/array_ops.py 在算术运算(左,右,运算) 188 其他: 189 与 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 在 na_arithmetic_op(left, right, op, is_cmp) 148 # 将错误地处理复数,参见 GH#32047 149 加薪 --> 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 在 masked_arith_op(x, y, op) 94 其他: 95 如果不是 is_scalar(y): ---> 96 引发类型错误( 97 f“无法使用类型为 { type(y) } 的操作数广播 np.ndarray” 98)

TypeError: 无法使用

执行此操作时:

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

我做错了什么?

【问题讨论】:

  • 看起来像一个对象(来自 dtype)而不是日期时间序列
  • sammywemmy 那么呢?

标签: python pandas time difference


【解决方案1】:

你在找那个吗?

df['snap_ts'].sub(pd.Timedelta('14:30:00')).dt.time

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 2015-04-16
    • 2018-10-08
    • 2022-10-24
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多