TypeError: cannot perform __truediv__ with this index type: DatetimeArray

Viewed 6746

I have a dataframe that consists of closing prices:

    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

I am trying to create a varcov matrix, using the following function:

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

But I keep getting this error:

TypeError: cannot perform __truediv__ with this index type: DatetimeArray

Which adjustment do I have to implement in order to get this function working? Thanks!

1 Answers

Try

df_koersen.set_index('Date')

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