Search code examples
pythonpandasdataframeyfinance

How to aggregate 2-day bars historical stock data using yfinance in Python?


I am using the code in the answer provided in the link below to aggregate the daily prices to 2-day period (freq='2B'); however, the datestamp shows the starting date of the aggregation period. Is it possible to show the result stamped with the last date of the period?

example: if aggregating over Jan 16th and 17th, the datestamp would show 2023-01-17

https://stackoverflow.com/a/74775358/13415270


Solution

  • pd.Grouper() supports a convention parameter. You could set this param to "e" or "end". convention requires to have a period index. Thus, you should set the index first. Putting it all together:

    import yfinance as yf
    import pandas as pd
    
    df = yf.download("AAPL", period="2y", interval="1h")
    
    # set period index
    df.index = df.index.to_period(freq="2B")
    # use convention
    df_agg = df.groupby(pd.Grouper(freq="2B", convention="end")).agg(
        {"Open": "first", "High": "max", "Low": "min", "Close": "last", "Adj Close": "last"}
    )
    df_agg.head()
    

    Output:

        Open    High    Low Close   Adj Close
    Datetime                    
    2021-01-22  133.863998  139.850006  133.789993  138.969894  138.969894
    2021-01-26  143.222702  145.080002  136.539993  143.184998  143.184998
    2021-01-28  143.429993  144.300003  136.699997  137.089996  137.089996
    2021-02-01  136.179993  136.729996  130.210007  134.110001  134.110001
    2021-02-03  135.729996  136.309998  133.610001  133.904999  133.904999