pandas'rolling_apply

时间:2017-04-18 10:56:52

标签: python pandas

我正在查看引入以下参数的rolling.apply函数示例

(a DataFrame['M30'], rolling window of 10 periods,  a function).

我不明白M30代表什么,事实上在我尝试这个例子时给了我一个关键错误:'M30'。我会发布整个代码。

import quandl
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
# For the rolling apply
from statistics import mean
style.use('fivethirtyeight')

api_key=open(XXXX).read()


# define this function to show the rolling_apply
def moving_average(values):
return mean (values)

housing_data=pd.read_pickle('HPI.pickle')
housing_data=housing_data.pct_change()

housing_data.replace([np.inf, -np.inf], np.nan, inplace=True)
housing_data['US_HPI_future']=housing_data['Benchmark'].shift(-1)
housing_data.dropna(inplace=True)


# Here we shoe the rolling apply. We will include the function (given is a 
# rolling_apply)
housing_data['ma_apply_example'] = 
pd.rolling_apply(housing_data['M30'],10,moving_average)
# We apply tail because it is a MVA
print(housing_data.tail())

0 个答案:

没有答案