1

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

1 Answer 1

0

The series can be grouped by the weekofyear attribute

grp = sample_series.groupby([x.weekofyear for x in sample_series.index])
# also possible
# grp = sample_series.groupby(by={i:i.weekofyear for i in range_dex})

for g in grp:
    print(g[1].size, g[1].index[0])
7 2025-01-06 00:00:00
7 2025-01-13 00:00:00
7 2025-01-20 00:00:00
4 2025-01-27 00:00:00

Series.groupby docs

If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups

Similar use case using a frequency defined by a string:

range_dex = pd.date_range(start="2025-01-06", freq='D', periods=26)
sample_series = pd.Series(range_dex, index=range_dex)

grp = sample_series.groupby(pd.Grouper(freq='1W-MON', label='left', origin="start"))

print(grp.size())
2024-12-30    1
2025-01-06    7
2025-01-13    7
2025-01-20    7
2025-01-27    4
Freq: W-MON, dtype: int64
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.