在 pandas 中乘以时间增量和指数计算

Multiplying timedeltas and exponential calculations in pandas

我是编程新手,想使用 pandas 计算某些数据的指数衰减。但是,我在对时间增量值进行 运行 乘法或指数运算时遇到了麻烦。我认为这些更复杂的操作可能不支持 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''

首先,从我们可以看到的您的数据来看,定义

似乎更自然
df['time difference'] = df['date 2'] - df['date 1']

这样你就可以获得正值。

然后为了能够对这些值进行任意计算,您应该选择一个单位并将它们转换为纯数值类型。例如。你可以这样做来获得以天为单位的时差作为数字列:

df['time difference days'] = df['time difference'].dt.days

请参阅 pandas Timedelta documentation 中有关属性的部分。

现在,如果您只使用数字系列而不是 Timedelta 系列,您的计算应该可行。