TypeError:无法使用此索引类型执行_uTrueDiv__;:DatetimeArray

2024-04-24 13:26:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由收盘价组成的数据框架:

    Date         World        EN      UT      IND         MAT         CS       CD      IT      FN   TC  HC
0   1997-06-13  939.3672    96.0238 75.2840 105.2635    124.7077    80.4562 82.8793 59.5524 88.0499 60.2713 69.0940
1   1997-06-20  944.3056    95.3351 75.8622 106.0365    124.8201    80.0543 82.5784 60.3146 89.0581 60.5441 70.1105
2   1997-06-27  945.2955    95.2789 76.2589 105.5658    124.0537    79.9143 82.2506 60.4379 89.4949 60.4505 71.1205
3   1997-07-04  966.1033    99.9787 78.0173 107.2277    125.4118    81.8571 83.0711 62.0707 91.3587 61.7000 73.4790
4   1997-07-11  966.9804    98.1188 77.1991 107.5794    125.7659    81.9250 83.4724 64.3737 91.0970 60.8316 72.9100



 df_koersen.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1197 entries, 0 to 1196
Data columns (total 12 columns):
 #   Column  Non-Null Count  Dtype         
---  ------  --------------  -----         
 0   Date    1197 non-null   datetime64[ns]
 1   World   1197 non-null   float64       
 2   EN      1197 non-null   float64       
 3   UT      1197 non-null   float64       
 4   IND     1197 non-null   float64       
 5   MAT     1197 non-null   float64       
 6   CS      1197 non-null   float64       
 7   CD      1197 non-null   float64       
 8   IT      1197 non-null   float64       
 9   FN      1197 non-null   float64       
 10  TC      1197 non-null   float64       
 11  HC      1197 non-null   float64       
dtypes: datetime64[ns](1), float64(11)
memory usage: 112.3 KB

我正在尝试使用以下函数创建varcov矩阵:

varcov = df_koersen.pct_change().apply(lambda x: np.log(x+1)).cov

但我一直在犯这样的错误:

TypeError: cannot perform __truediv__ with this index type: DatetimeArray

为了使此功能正常工作,我必须执行哪些调整? 谢谢


Tags: hcworlddatecditcsnullen
1条回答
网友
1楼 · 发布于 2024-04-24 13:26:58

试一试

df_koersen.set_index('Date')

varcov = df_koersen.pct_change().apply(lambda x: np.log(x+1)).cov

相关问题 更多 >