O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Resampling data from daily to monthly returns

To calculate the monthly rate of return, we can use a little pandas magic and resample the original daily returns. During this process, we will also need to throw out the days that are not an end of month as well as forward fill any missing values. This can be done using the .ffill() on the result of the resampling:

Note the date of the entries and that they are now all month-end dates. Values have not  changed, as the resample only selects the dates at the end of the month, or fills with the value prior to that date if it did not exist in the source.

Now we can use this to calculate the monthly ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required