Skip to content

Commit 21c7cbb

Browse files
authored
REF: make FY523Mixin a cdef class (#34322)
1 parent 55ad334 commit 21c7cbb

File tree

2 files changed

+71
-47
lines changed

2 files changed

+71
-47
lines changed

pandas/_libs/tslibs/offsets.pyx

+50
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,56 @@ cdef class MonthOffset(SingleConstructorOffset):
13291329
return type(dtindex)._simple_new(shifted, dtype=dtindex.dtype)
13301330

13311331

1332+
# ---------------------------------------------------------------------
1333+
# Special Offset Classes
1334+
1335+
cdef class FY5253Mixin(SingleConstructorOffset):
1336+
cdef readonly:
1337+
int startingMonth
1338+
int weekday
1339+
str variation
1340+
1341+
def __init__(
1342+
self, n=1, normalize=False, weekday=0, startingMonth=1, variation="nearest"
1343+
):
1344+
BaseOffset.__init__(self, n, normalize)
1345+
self.startingMonth = startingMonth
1346+
self.weekday = weekday
1347+
self.variation = variation
1348+
1349+
if self.n == 0:
1350+
raise ValueError("N cannot be 0")
1351+
1352+
if self.variation not in ["nearest", "last"]:
1353+
raise ValueError(f"{self.variation} is not a valid variation")
1354+
1355+
def is_anchored(self) -> bool:
1356+
return (
1357+
self.n == 1 and self.startingMonth is not None and self.weekday is not None
1358+
)
1359+
1360+
# --------------------------------------------------------------------
1361+
# Name-related methods
1362+
1363+
@property
1364+
def rule_code(self) -> str:
1365+
prefix = self._prefix
1366+
suffix = self.get_rule_code_suffix()
1367+
return f"{prefix}-{suffix}"
1368+
1369+
def _get_suffix_prefix(self) -> str:
1370+
if self.variation == "nearest":
1371+
return "N"
1372+
else:
1373+
return "L"
1374+
1375+
def get_rule_code_suffix(self) -> str:
1376+
prefix = self._get_suffix_prefix()
1377+
month = MONTH_ALIASES[self.startingMonth]
1378+
weekday = int_to_weekday[self.weekday]
1379+
return f"{prefix}-{month}-{weekday}"
1380+
1381+
13321382
# ----------------------------------------------------------------------
13331383
# RelativeDelta Arithmetic
13341384

pandas/tseries/offsets.py

+21-47
Original file line numberDiff line numberDiff line change
@@ -1511,50 +1511,7 @@ class YearBegin(liboffsets.YearOffset):
15111511
# Special Offset Classes
15121512

15131513

1514-
class FY5253Mixin(SingleConstructorOffset):
1515-
def __init__(
1516-
self, n=1, normalize=False, weekday=0, startingMonth=1, variation="nearest"
1517-
):
1518-
BaseOffset.__init__(self, n, normalize)
1519-
object.__setattr__(self, "startingMonth", startingMonth)
1520-
object.__setattr__(self, "weekday", weekday)
1521-
1522-
object.__setattr__(self, "variation", variation)
1523-
1524-
if self.n == 0:
1525-
raise ValueError("N cannot be 0")
1526-
1527-
if self.variation not in ["nearest", "last"]:
1528-
raise ValueError(f"{self.variation} is not a valid variation")
1529-
1530-
def is_anchored(self) -> bool:
1531-
return (
1532-
self.n == 1 and self.startingMonth is not None and self.weekday is not None
1533-
)
1534-
1535-
# --------------------------------------------------------------------
1536-
# Name-related methods
1537-
1538-
@property
1539-
def rule_code(self) -> str:
1540-
prefix = self._prefix
1541-
suffix = self.get_rule_code_suffix()
1542-
return f"{prefix}-{suffix}"
1543-
1544-
def _get_suffix_prefix(self) -> str:
1545-
if self.variation == "nearest":
1546-
return "N"
1547-
else:
1548-
return "L"
1549-
1550-
def get_rule_code_suffix(self) -> str:
1551-
prefix = self._get_suffix_prefix()
1552-
month = ccalendar.MONTH_ALIASES[self.startingMonth]
1553-
weekday = ccalendar.int_to_weekday[self.weekday]
1554-
return f"{prefix}-{month}-{weekday}"
1555-
1556-
1557-
class FY5253(FY5253Mixin):
1514+
class FY5253(liboffsets.FY5253Mixin):
15581515
"""
15591516
Describes 52-53 week fiscal year. This is also known as a 4-4-5 calendar.
15601517
@@ -1605,6 +1562,10 @@ class FY5253(FY5253Mixin):
16051562
_prefix = "RE"
16061563
_attributes = frozenset(["weekday", "startingMonth", "variation"])
16071564

1565+
def __reduce__(self):
1566+
tup = (self.n, self.normalize, self.weekday, self.startingMonth, self.variation)
1567+
return type(self), tup
1568+
16081569
def is_on_offset(self, dt: datetime) -> bool:
16091570
if self.normalize and not is_normalized(dt):
16101571
return False
@@ -1722,7 +1683,7 @@ def _from_name(cls, *args):
17221683
return cls(**cls._parse_suffix(*args))
17231684

17241685

1725-
class FY5253Quarter(FY5253Mixin):
1686+
class FY5253Quarter(liboffsets.FY5253Mixin):
17261687
"""
17271688
DateOffset increments between business quarter dates
17281689
for 52-53 week fiscal year (also known as a 4-4-5 calendar).
@@ -1792,9 +1753,22 @@ def __init__(
17921753
qtr_with_extra_week=1,
17931754
variation="nearest",
17941755
):
1795-
FY5253Mixin.__init__(self, n, normalize, weekday, startingMonth, variation)
1756+
liboffsets.FY5253Mixin.__init__(
1757+
self, n, normalize, weekday, startingMonth, variation
1758+
)
17961759
object.__setattr__(self, "qtr_with_extra_week", qtr_with_extra_week)
17971760

1761+
def __reduce__(self):
1762+
tup = (
1763+
self.n,
1764+
self.normalize,
1765+
self.weekday,
1766+
self.startingMonth,
1767+
self.qtr_with_extra_week,
1768+
self.variation,
1769+
)
1770+
return type(self), tup
1771+
17981772
@cache_readonly
17991773
def _offset(self):
18001774
return FY5253(
@@ -1918,7 +1892,7 @@ def is_on_offset(self, dt: datetime) -> bool:
19181892

19191893
@property
19201894
def rule_code(self) -> str:
1921-
suffix = FY5253Mixin.rule_code.fget(self) # type: ignore
1895+
suffix = liboffsets.FY5253Mixin.rule_code.__get__(self)
19221896
qtr = self.qtr_with_extra_week
19231897
return f"{suffix}-{qtr}"
19241898

0 commit comments

Comments
 (0)