rolling_mean()とrolling().mean()

「IPythonデータサイエンスクックブック」って本を読んでたら出てきたpandasのrolling_mean()という関数について。


IPythonと見てわかるように少々古い本なので(今はJupyter)、書いてあるコードを実行しているとたまに警告文が出てくる。今回はその内の一つ、p.18 In[12]で出てきた移動平均の関数rolling_mean()について。本に載ってるコードは、(※一部引用してるだけなので、下だけ実行しても動かない。)


from ipywidgets import interact
@interact
def plot(n=(1, 30)):
        pd.rolling_mean(df['Berri1'], n).dropna().plot()
        plt.ylim(0, 8000)
        plt.show()


警告文は、

FutureWarning: pd.rolling_mean is deprecated for Series and will be removed in a future version, replace with Series.rolling(center=False,window=15).mean()


ということで、将来的にpandasの関数pd.rolling_meanはSeriesのメソッド、Series.rolling.mean()に取って代わられるらしい。2016年3月に公開されているpandas v0.18.0のAPIにも書かれている。




現状本の通りでまだ動くけど、警告文に倣うなら修正して、


from ipywidgets import interact
@interact
def plot(n=(1, 30)):
        df['Berri1'].rolling(window=n).mean().dropna().plot()
        plt.ylim(0, 8000)
        plt.show()


となる。これで警告文は出ない。


コメント

人気の投稿