我是编程新手,我想使用熊猫来计算某些数据的指数衰减。但是,我在时间增量值上运行乘法或指数运算有麻烦。我认为这些更复杂的操作可能不支持timedelta值,但是肯定有一种简单的方法吗?
输入代码:
import pandas as pd
import numpy as np
df = pd.read_excel('exponential example.xlsx', sheet_name = 'Sheet1',
usecols = ("A:D"), parse_dates = True)
df['time difference']= df['date 1']-df['date 2']
print(df.head(3))
df['output'] = df['value 1']*np.exp(df['time difference']*.01)
输出:
sample date 1 date 2 value 1 time difference
0 1 2018-01-01 2019-01-01 2 -365 days
1 2 2018-01-01 2019-01-01 4 -365 days
Traceback (most recent call last):
File "/Users/l225445/Desktop/python test/exponential example.py", line 15, in <module>
df['output'] = df['value 1']*np.exp(df['time difference']*.01)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/series.py", line 679, in __array_ufunc__
result = getattr(ufunc, method)(*inputs, **kwargs)
TypeError: ufunc 'exp' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
答案 0 :(得分:0)
首先,从我们对您的数据的了解来看,进行定义似乎更自然
df['time difference'] = df['date 2'] - df['date 1']
,以便您获得正值。
然后为了能够使用这些值进行任意计算,您应该选择一个单位并将其转换为纯数字类型。例如。您可以这样做以数字列的形式获取以天为单位的时差:
df['time difference days'] = df['time difference'].dt.days
请参见熊猫Timedelta documentation中有关属性的部分。
现在,如果您只使用数字系列而不是Timedelta系列,则计算应该可以进行。