Skip to content

Commit 9f1faf6

Browse files
DOC: fix GL08 for pandas.tseries.offsets.YearEnd.rule_code
1 parent 642d244 commit 9f1faf6

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

ci/code_checks.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
726726
-i "pandas.tseries.offsets.YearEnd.n GL08" \
727727
-i "pandas.tseries.offsets.YearEnd.nanos GL08" \
728728
-i "pandas.tseries.offsets.YearEnd.normalize GL08" \
729-
-i "pandas.tseries.offsets.YearEnd.rule_code GL08" \
730729
-i "pandas.util.hash_pandas_object PR07,SA01" # There should be no backslash in the final line, please keep this comment in the last ignored function
731730

732731
RET=$(($RET + $?)) ; echo $MSG "DONE"

pandas/_libs/tslibs/offsets.pyx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,68 @@ cdef class YearOffset(SingleConstructorOffset):
24662466

24672467
@property
24682468
def rule_code(self) -> str:
2469+
"""
2470+
Return the rule code for the YearEnd offset.
2471+
2472+
The `rule_code` is a shorthand identifier used within pandas to denote
2473+
the end-of-year frequency. This code is useful when working with time
2474+
series data, particularly when you need to perform operations that require
2475+
a specific frequency. For example, it can be used in functions like
2476+
`pd.date_range`, `resample`, or `asfreq` to indicate that operations
2477+
should be aligned to the end of the year. The YearEnd offset represents the
2478+
end of a year, typically on December 31st. The `rule_code` for this offset
2479+
helps to abstract away the complexity of manually calculating end-of-year
2480+
dates, especially when handling irregular date ranges or resampling data.
2481+
2482+
Returns
2483+
-------
2484+
str
2485+
The rule code associated with the YearEnd offset.
2486+
2487+
See Also
2488+
--------
2489+
date_range : Generate a range of dates with a specific frequency.
2490+
Series.resample : Resample time-series data.
2491+
tseries.offsets.Week : Represents a weekly offset.
2492+
DateOffset : Base class for all other offset classes.
2493+
tseries.offsets.Day : Represents a single day offset.
2494+
tseries.offsets.MonthEnd : Represents a monthly offset that
2495+
snaps to the end of the month.
2496+
2497+
Examples
2498+
--------
2499+
Create a date range with random dates
2500+
>>> date_range = pd.to_datetime([
2501+
... '2020-01-01', '2020-07-15', '2021-02-28', '2021-08-20', '2022-03-10'
2502+
... ])
2503+
Create a DataFrame with the date_range as index and some random data
2504+
>>> data = np.random.rand(len(date_range))
2505+
>>> df = pd.DataFrame(data, index=date_range, columns=['Value'])
2506+
Print the original DataFrame
2507+
>>> df
2508+
Value
2509+
2020-01-01 0.460402
2510+
2020-07-15 0.339131
2511+
2021-02-28 0.697027
2512+
2021-08-20 0.839201
2513+
2022-03-10 0.079523
2514+
2515+
Use the YearEnd.offsets with its rule_code
2516+
>>> year_end = pd.tseries.offsets.YearEnd()
2517+
>>> rule_code = year_end.rule_code
2518+
>>> rule_code
2519+
YE-DEC
2520+
2521+
Resample the data using the YearEnd rule code
2522+
>>> df_resampled = df.resample(rule_code).sum()
2523+
2524+
Print the resampled DataFrame
2525+
>>df_resampled
2526+
Value
2527+
2020-12-31 0.799533
2528+
2021-12-31 1.536228
2529+
2022-12-31 0.079523
2530+
"""
24692531
month = MONTH_ALIASES[self.month]
24702532
return f"{self._prefix}-{month}"
24712533

0 commit comments

Comments
 (0)