ufunc'subtract'不能使用类型为dtype('<M8 [ns]')和dtype('float64')的操作数

时间:2020-07-20 13:04:15

标签: python pandas numpy timestamp pipe

我遇到一个我不知道的问题。这是在安装了Python 3.6.3的GCP VM上发生的。我有以下Python代码:

test = dframes.groupby(level=1).pipe(lambda g: dframes.sub(g.mean(), level=1).div(g.std(), level=1))    

我收到以下错误:

Traceback (most recent call last):
  File "final_calculation4_(3).py", line 1419, in <module>
    test = dframes.groupby(level=1).pipe(lambda g: dframes.sub(g.mean(), level=1).div(g.std(), level=1))    
  File "/usr/local/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 607, in pipe
    return com.pipe(self, func, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/common.py", line 461, in pipe
    return func(obj, *args, **kwargs)
  File "final_calculation4_(3).py", line 1419, in <lambda>
    test = dframes.groupby(level=1).pipe(lambda g: dframes.sub(g.mean(), level=1).div(g.std(), level=1))    
  File "/usr/local/lib/python3.6/site-packages/pandas/core/ops/__init__.py", line 767, in f
    new_data = left._combine_frame(right, pass_op, fill_value)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 5300, in _combine_frame
    new_data = ops.dispatch_to_series(self, other, _arith_op)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/ops/__init__.py", line 419, in dispatch_to_series
    new_data = expressions.evaluate(column_op, str_rep, left, right)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/computation/expressions.py", line 208, in evaluate
    return _evaluate(op, op_str, a, b)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/computation/expressions.py", line 70, in _evaluate_standard
    return op(a, b)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/ops/__init__.py", line 388, in column_op
    return {i: func(a.iloc[:, i], b.iloc[:, i]) for i in range(len(a.columns))}
  File "/usr/local/lib/python3.6/site-packages/pandas/core/ops/__init__.py", line 388, in <dictcomp>
    return {i: func(a.iloc[:, i], b.iloc[:, i]) for i in range(len(a.columns))}
  File "/usr/local/lib/python3.6/site-packages/pandas/core/ops/common.py", line 64, in new_method
    return method(self, other)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/ops/__init__.py", line 503, in wrapper
    result = arithmetic_op(lvalues, rvalues, op, str_rep)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/ops/array_ops.py", line 193, in arithmetic_op
    res_values = dispatch_to_extension_op(op, lvalues, rvalues)
  File "/usr/local/lib/python3.6/site-packages/pandas/core/ops/dispatch.py", line 125, in dispatch_to_extension_op
    res_values = op(left, right)
numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' cannot use operands with types dtype('<M8[ns]') and dtype('float64')

我不知道该如何解决。我的数据框看起来像一个包含索引和时间戳的多级数据框:

(0, 2008-12-20 00:00:00)
(1, 2008-12-20 00:00:00)
(2, 2008-12-20 00:00:00)
etc...

所有值都将是float类型。

0 个答案:

没有答案