How to get DataFrame.pct_change to calculate monthly change on daily price data?

You can resample the data to business month. If you don't want the mean price (which is the default in resample) you can use a custom resample method using the keyword argument how:

In [31]: from pandas.io import data as web

# read some example data, note that this is not exactly your data!
In [32]: s = web.get_data_yahoo('AAPL', start='2009-01-02',
...                             end='2009-12-31')['Adj Close']

# resample to business month and return the last value in the period
In [34]: monthly = s.resample('BM', how=lambda x: x[-1])

In [35]: monthly
Out[35]: 
Date
2009-01-30     89.34
2009-02-27     88.52
2009-03-31    104.19
...
2009-10-30    186.84
2009-11-30    198.15
2009-12-31    208.88
Freq: BM

In [36]: monthly.pct_change()
Out[36]: 
Date
2009-01-30         NaN
2009-02-27   -0.009178
2009-03-31    0.177022
...
2009-10-30    0.016982
2009-11-30    0.060533
2009-12-31    0.054151
Freq: BM

I stumbled on this error as well while using the pct_change function, and would like to offer my two cents on this question.

The freq argument for the pct_change function seems to only accept fixed-period time offset, such as "2D" and "3D". However, "M" is an indefinite time period, and could range between 28 day to 31 day. So that's where the errors come from.

Pct_change operates similarly to the rolling() function, and using "M" time offset with rolling() would get the same error.

Here is a working example using the freq argument in the pct_change argument:

import pandas_datareader.data as web

return.pct_change(periods = 1, freq = '2D')

Date
2008-03-26         NaN
2008-03-27         NaN
2008-03-28   -0.010342
2008-03-31         NaN
2008-04-01         NaN
            ...   

Tags:

Python

Pandas