pythonpandas

Change week definition in pandas resample


Currently, when I use the pandas resample function for days to weeks it uses a Sunday to Saturday week, but I'd like it to use a Monday to Sunday week. Is this possible? I tried using loffset from the docs, but it doesn't change the data at all.

pivot_news = pivot_news.resample('w', 'sum')


Solution

  • You want freq='W-MON'. For example:

    >>> dates = pd.Series(np.arange(30),
    ...                   index=pd.date_range('2015-10-1', periods=30, freq='D'))
    
    >>> dates.resample('W', 'sum')
    2015-10-04      6
    2015-10-11     49
    2015-10-18     98
    2015-10-25    147
    2015-11-01    135
    Freq: W-SUN, dtype: int64
    
    >>> dates.resample('W-MON', 'sum')
    2015-10-05     10
    2015-10-12     56
    2015-10-19    105
    2015-10-26    154
    2015-11-02    110
    Freq: W-MON, dtype: int64
    

    You can find more information in the Pandas docs