[Python] How do I define a week start frequency in Pandas?

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to come up with a frequency in Pandas that represents the start of a calendar week (configurable by week start). For example, for all dates from 2025-01-06 (Monday) to 2025-01-13 (Sunday), I want them to be categorized under Monday's date of 2025-01-06.

    However, because of how Pandas DateOffset rolls dates forward to the next period, not all the dates in that week are categorized under 2025-01-06.

    You can see what I'm describing in this sample example:

    >>> import pandas as pd
    >>>
    >>> range_dex = pd.date_range(start="2025-01-06", end="2025-01-30")
    >>> sample_series = pd.Series(1, index=range_dex)
    >>> sample_series
    2025-01-06 1
    2025-01-07 1
    2025-01-08 1
    ...
    2025-01-28 1
    2025-01-29 1
    2025-01-30 1
    Freq: D, dtype: int64
    >>>
    >>> week_start = 0 # 0 for Monday, ..., 6 for Sunday
    >>> freq_pd = pd.offsets.Week(n=1, weekday=week_start)
    >>>
    >>> sample_series.groupby(pd.Grouper(freq=freq_pd)).size()
    2025-01-06 1
    2025-01-13 7
    2025-01-20 7
    2025-01-27 7
    2025-02-03 3
    Freq: W-MON, dtype: int64


    The result I am hoping to achieve instead is:

    >>> sample_series.groupby(pd.Grouper(freq=freq_pd)).size()
    2025-01-06 7
    2025-01-13 7
    2025-01-20 7
    2025-01-27 4
    Freq: W-MON, dtype: int64


    I've also tried pd.offsets.Week(n=0, weekday=week_start) and pd.offsets.Week(n=-1, weekday=week_start), which both error, and I can't do the typical math I could do on a Series with something like series + pd.offsets.Week(n=1, weekday=week_start) - pd.offsets.Week(n=1, weekday=week_start). I also have a use case for .asfreq so really want this specified in terms of a frequency, if possible. Thanks!

    For reference, this is the calendar for January 2025:

    Sun​
    Mon​
    Tue​
    Wed​
    Thu​
    Fri​
    Sat
    29​
    30​
    31​
    1​
    2​
    3​
    4
    5​
    6​
    7​
    8​
    9​
    10​
    11
    12​
    13​
    14​
    15​
    16​
    17​
    18
    19​
    20​
    21​
    22​
    23​
    24​
    25
    26​
    27​
    28​
    29​
    30​
    31​
    1​

    Continue reading...

Compartilhe esta Página