我有一个名为“ df”的时间序列熊猫数据框。它具有一列并且具有以下形状:(2000,1)。数据框的标题如下所示:
Weight
Date
2004-06-01 1.9219
2004-06-02 1.8438
2004-06-03 1.8672
2004-06-04 1.7422
2004-06-07 1.8203
目标
我正在尝试使用“ for循环”来计算“重量”变量在各种时间范围或时滞内的百分比变化之间的相关性。这样做是为了评估各种长度的时间段内饲养牲畜的影响。
循环可以在下面找到:
from scipy.stats.stats import pearsonr
# Loop for producing combinations of different timelags and holddays
# and calculating the pearsonr correlation and p-value of each combination
for timelags in [1, 5, 10, 25, 60, 120, 250]:
for holddays in [1, 5, 10, 25, 60, 120, 250]:
weight_change_lagged = df.pct_change(periods=timelags)
weight_change_future = df.shift(-holddays).pct_change(periods=holddays)
if (timelags >= holddays):
indepSet=range(0, weight_change_lagged.shape[0], holddays)
else:
indepSet=range(0, weight_change_lagged.shape[0], timelags)
weight_change_lagged = weight_change_lagged.iloc[indepSet]
weight_change_future = weight_change_future.iloc[indepSet]
not_na = (weight_change_lagged.notna() & weight_change_future.notna()).values
(correlation, p-value)=pearsonr(weight_change_lagged[not_na], weight_change_future[not_na])
print('%4i %4i %7.4f %7.4f' % (timelags, holddays, correlation, p-value))
循环执行得很好,但是,在计算皮尔逊相关性和p值时,即在本节中,它会失败:
(correlation, p-value)=pearsonr(weight_change_lagged[not_na], weight_change_future[not_na])
它产生此错误:
TypeError:没有与指定签名匹配的循环,并且强制转换为为ufunc找到添加
有人对如何解决我的问题有任何线索吗?我looked through the forums,没有找到符合我确切要求的答案。
通过随机修补,我设法解决了如下问题:
scipy的pearsonr包仅接受数组或类似数组的输入。这意味着:
但是,即使变量包含一列,它们的完整Pandas数据框也不起作用。
因此,我按如下方式编辑了有问题的代码段:
# Define an object containing observations that are not NA
not_na = (weight_change_lagged.notna() & weight_change_future.notna()).values
# Remove na values before inputting the data into the peasonr function (not within the function as I had done):
weight_change_lagged = weight_change_lagged[not_na]
weight_change_future = weight_change_future[not_na]
# Input Pandas Series of the Future and Lagged Variables into the function
(correlation, p-value)=pearsonr(weight_change_lagged['Weight'], weight_change_future['Weight'])
只需稍加修改,代码即可顺利执行。
注意:
如果使用双方括号,如下所示,您输入的不是一个系列的熊猫数据框,则peersonr函数将引发错误:
weight_change_future[['Weight']]
感谢所有尝试提供帮助的人,您的问题使我得到了答案。