読者です 読者をやめる 読者になる 読者になる

<Python, pandas, sentdex> resample

目的とするDataFrameから、値を抜き取りする、リサンプリング resample をしてみた。

In [20]: import datetime as dt

In [21]: import pandas as pd

In [22]: import pandas_datareader.data as web

In [23]: s = dt.datetime(2000,1,1)

In [24]: e = dt.datetime(2016,12,31)


In [30]: df = web.DataReader('TXN', 'yahoo', s, e)

In [31]: df.resample('M').mean().head()
Out[31]: 
                  Open        High         Low       Close    Volume  \
Date                                                                   
2000-01-31  105.481250  107.803125  102.500000  105.242965   9585750   
2000-02-29  138.125000  143.434375  135.156250  140.340625  10091150   
2000-03-31  172.709239  177.774457  165.434783  171.730978  11085226   
2000-04-30  150.115132  156.256579  144.059211  150.125000  12271800   
2000-05-31  126.215909  128.931818  121.397727  124.840909   8540727   

            Adj Close  
Date                   
2000-01-31  41.146986  
2000-02-29  54.886878  
2000-03-31  67.163569  
2000-04-30  58.715439  
2000-05-31  56.198717  

In [32]: df['Adj Close'].resample('M').mean().head()
Out[32]: 
Date
2000-01-31    41.146986
2000-02-29    54.886878
2000-03-31    67.163569
2000-04-30    58.715439
2000-05-31    56.198717
Freq: M, Name: Adj Close, dtype: float64

In [33]: df['Adj Close'].resample('M').ohlc().head()
Out[33]: 
                 open       high        low      close
Date                                                  
2000-01-31  40.218784  44.519213  36.553646  42.140764
2000-02-29  44.242913  64.971085  44.242913  64.971085
2000-03-31  64.286664  73.379683  57.491344  62.575612
2000-04-30  58.957959  65.117746  54.362563  63.718405
2000-05-31  62.593675  62.593675  50.074940  56.529912

いつものお世話になったところ。

sinhrks.hatenablog.com

マニュアル。

pandas.DataFrame.resample — pandas 0.19.2 documentation

アンカー Anchor の表。
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#anchored-offsets