@@ -2466,6 +2466,68 @@ cdef class YearOffset(SingleConstructorOffset):
2466
2466
2467
2467
@property
2468
2468
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
+ """
2469
2531
month = MONTH_ALIASES[self .month]
2470
2532
return f"{self._prefix}-{month}"
2471
2533
0 commit comments