Pandas中的resample
,重新采样,是对原样本重新处理的一个方法,是一个对常规时间序列数据重新采样和频率转换的便捷的方法。
其中函数resample
的方法格式:
DataFrame.resample(rule, how=None, axis=0, fill_method=None, closed=None, label=None, convention='start',kind=None, loffset=None, limit=None, base=0)
将代码:
m_returns = ret_index.resample('BM',how='last').pct_change()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-125-10be9ad4be8f> in <module>()
----> 1 m_returns = ret_index.resample('BM',how='last').pct_change()
TypeError: resample() got an unexpected keyword argument 'how'
m_returns = ret_index.resample('BM').last().pct_change()
m_returns['2020']
报错的原因是函数resample
的‘how’
参数已经不适用了,可以将resample(how='last')
改成resample.last()
的形式,改成代码:
m_returns = ret_index.resample('BM').last().pct_change()
m_returns['2020']
Out:
Date
2020-01-31 0.054010
2020-02-28 -0.114701
2020-03-31 -0.069761
2020-04-30 0.112785
Freq: BM, Name: Adj Close, dtype: float64