0

I need to calculate the monthly covariance from two columns which contains daily values.

Day x y
2010-01-01 0,02 0,05
2010-01-02 0,04 -0,06
2010-01-03 0,90 0,02
..... .. ..
2010-02-01 0,04 0,05
2010-02-02 0,88 0,09
..... .. ..
2010-03-01 0,03 0,25
2010-03-02 0,28 0,19

The result should be one line each month with the estimated covariance.

I tried to resample it but it doesn´t work:

df.resample('M').cov()

Error:

AttributeError: 'DatetimeIndexResampler' object has no attribute 'cov'

I looked up already for solutions but I only found some codes without monthly evaluation. How I could calculate this??

Hopper
  • 1
  • 1
  • https://stackoverflow.com/questions/39492004/how-to-convert-datetimeindexresampler-to-dataframe I hope it can help you – Mehmaam Aug 11 '22 at 13:10

1 Answers1

1

The reason you get that error is because when you utilize resample it returns a resample object that is uncapable of receiving the cov method for that you want to apply it to the given groups series.

The way of approaching this issue is to resample your data as you did it and then applying you cov on the given groupby objects.

df.resample('M').apply(lambda x : x.ffill()).cov(df.iloc[:,1)
INGl0R1AM0R1
  • 1,532
  • 5
  • 16
  • thank you - if I try your approach, I receive the error: "TypeError: cov() missing 1 required positional argument: 'other'" I guess any argument is needed for y? – Hopper Aug 12 '22 at 11:22
  • Yes since you are passing a series, you need to define the other series to calculate the cov, basically the other column you want to calculate with – INGl0R1AM0R1 Aug 12 '22 at 12:57
  • Check the edited question and see if that helps u. – INGl0R1AM0R1 Aug 12 '22 at 13:51
  • Thank you very much, it needed a slicy adaption from to: **df.resample('M').apply(lambda x : x.ffill().cov(df.iloc[:,1]))** (the last x to my dataframename and a square bracket ... – Hopper Aug 14 '22 at 08:16