pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.8k stars 17.98k forks source link

ENH: `FY5253Quarter` to support alternative week allocations #58272

Open GTLangseth opened 7 months ago

GTLangseth commented 7 months ago

Feature Type

Problem Description

The current implementation of FY5253Quarter assumes that quarters have an even distribution of weeks, for a total of 13 weeks per quarter except in the event of a 53 week year, in which case a designated quarter will consist of a total of 14 weeks.

It is sometimes the case the companies elect to use quarters with an alternative allocation of weeks, the alternative often being 16-12-12-12.

I propose the addition of an optional argument called quarter_lengths that accepts an input of Tuple[int, int, int, int] where sum(quarter_lengths) == 52.

Feature Description

In addition to modifying the interface to include the extra parameter, I think this can be accomplished with a very simple change to the existing get_weeks function:

https://github.com/pandas-dev/pandas/blob/main/pandas/_libs/tslibs/offsets.pyx#L4052C1-L4060C19

def get_weeks(self, dt: datetime):
    ret = self.quarter_lengths or [13] * 4

    year_has_extra_week = self.year_has_extra_week(dt)

    if year_has_extra_week:
        ret[self.qtr_with_extra_week - 1]  += 1

    return ret

Alternative Solutions

n/a

Additional Context

Example 10-k describing this pattern

GTLangseth commented 7 months ago

I'm also happy to submit a PR if it feels like this is worth adding!