Skip to content

Commit bbc497c

Browse files
jbrockmendelPuneethaPai
authored andcommitted
REF: FY5253Mixin to share methods (pandas-dev#34262)
1 parent ad232db commit bbc497c

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

doc/source/reference/offset_frequency.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ Methods
10151015

10161016
FY5253Quarter.apply
10171017
FY5253Quarter.copy
1018+
FY5253Quarter.get_rule_code_suffix
10181019
FY5253Quarter.get_weeks
10191020
FY5253Quarter.isAnchored
10201021
FY5253Quarter.onOffset

pandas/tseries/offsets.py

+48-53
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,50 @@ class YearBegin(YearOffset):
16651665
# Special Offset Classes
16661666

16671667

1668-
class FY5253(SingleConstructorOffset):
1668+
class FY5253Mixin(BaseOffset):
1669+
def __init__(
1670+
self, n=1, normalize=False, weekday=0, startingMonth=1, variation="nearest"
1671+
):
1672+
BaseOffset.__init__(self, n, normalize)
1673+
object.__setattr__(self, "startingMonth", startingMonth)
1674+
object.__setattr__(self, "weekday", weekday)
1675+
1676+
object.__setattr__(self, "variation", variation)
1677+
1678+
if self.n == 0:
1679+
raise ValueError("N cannot be 0")
1680+
1681+
if self.variation not in ["nearest", "last"]:
1682+
raise ValueError(f"{self.variation} is not a valid variation")
1683+
1684+
def is_anchored(self) -> bool:
1685+
return (
1686+
self.n == 1 and self.startingMonth is not None and self.weekday is not None
1687+
)
1688+
1689+
# --------------------------------------------------------------------
1690+
# Name-related methods
1691+
1692+
@property
1693+
def rule_code(self) -> str:
1694+
prefix = self._prefix
1695+
suffix = self.get_rule_code_suffix()
1696+
return f"{prefix}-{suffix}"
1697+
1698+
def _get_suffix_prefix(self) -> str:
1699+
if self.variation == "nearest":
1700+
return "N"
1701+
else:
1702+
return "L"
1703+
1704+
def get_rule_code_suffix(self) -> str:
1705+
prefix = self._get_suffix_prefix()
1706+
month = ccalendar.MONTH_ALIASES[self.startingMonth]
1707+
weekday = ccalendar.int_to_weekday[self.weekday]
1708+
return f"{prefix}-{month}-{weekday}"
1709+
1710+
1711+
class FY5253(SingleConstructorMixin, FY5253Mixin):
16691712
"""
16701713
Describes 52-53 week fiscal year. This is also known as a 4-4-5 calendar.
16711714
@@ -1716,26 +1759,6 @@ class FY5253(SingleConstructorOffset):
17161759
_prefix = "RE"
17171760
_attributes = frozenset(["weekday", "startingMonth", "variation"])
17181761

1719-
def __init__(
1720-
self, n=1, normalize=False, weekday=0, startingMonth=1, variation="nearest"
1721-
):
1722-
BaseOffset.__init__(self, n, normalize)
1723-
object.__setattr__(self, "startingMonth", startingMonth)
1724-
object.__setattr__(self, "weekday", weekday)
1725-
1726-
object.__setattr__(self, "variation", variation)
1727-
1728-
if self.n == 0:
1729-
raise ValueError("N cannot be 0")
1730-
1731-
if self.variation not in ["nearest", "last"]:
1732-
raise ValueError(f"{self.variation} is not a valid variation")
1733-
1734-
def is_anchored(self) -> bool:
1735-
return (
1736-
self.n == 1 and self.startingMonth is not None and self.weekday is not None
1737-
)
1738-
17391762
def is_on_offset(self, dt: datetime) -> bool:
17401763
if self.normalize and not is_normalized(dt):
17411764
return False
@@ -1830,24 +1853,6 @@ def get_year_end(self, dt):
18301853
# The previous self.weekday is closer than the upcoming one
18311854
return target_date + timedelta(days_forward - 7)
18321855

1833-
@property
1834-
def rule_code(self) -> str:
1835-
prefix = self._prefix
1836-
suffix = self.get_rule_code_suffix()
1837-
return f"{prefix}-{suffix}"
1838-
1839-
def _get_suffix_prefix(self) -> str:
1840-
if self.variation == "nearest":
1841-
return "N"
1842-
else:
1843-
return "L"
1844-
1845-
def get_rule_code_suffix(self) -> str:
1846-
prefix = self._get_suffix_prefix()
1847-
month = ccalendar.MONTH_ALIASES[self.startingMonth]
1848-
weekday = ccalendar.int_to_weekday[self.weekday]
1849-
return f"{prefix}-{month}-{weekday}"
1850-
18511856
@classmethod
18521857
def _parse_suffix(cls, varion_code, startingMonth_code, weekday_code):
18531858
if varion_code == "N":
@@ -1871,7 +1876,7 @@ def _from_name(cls, *args):
18711876
return cls(**cls._parse_suffix(*args))
18721877

18731878

1874-
class FY5253Quarter(SingleConstructorOffset):
1879+
class FY5253Quarter(SingleConstructorMixin, FY5253Mixin):
18751880
"""
18761881
DateOffset increments between business quarter dates
18771882
for 52-53 week fiscal year (also known as a 4-4-5 calendar).
@@ -1941,15 +1946,8 @@ def __init__(
19411946
qtr_with_extra_week=1,
19421947
variation="nearest",
19431948
):
1944-
BaseOffset.__init__(self, n, normalize)
1945-
1946-
object.__setattr__(self, "startingMonth", startingMonth)
1947-
object.__setattr__(self, "weekday", weekday)
1949+
FY5253Mixin.__init__(self, n, normalize, weekday, startingMonth, variation)
19481950
object.__setattr__(self, "qtr_with_extra_week", qtr_with_extra_week)
1949-
object.__setattr__(self, "variation", variation)
1950-
1951-
if self.n == 0:
1952-
raise ValueError("N cannot be 0")
19531951

19541952
@cache_readonly
19551953
def _offset(self):
@@ -1959,9 +1957,6 @@ def _offset(self):
19591957
variation=self.variation,
19601958
)
19611959

1962-
def is_anchored(self) -> bool:
1963-
return self.n == 1 and self._offset.is_anchored()
1964-
19651960
def _rollback_to_year(self, other):
19661961
"""
19671962
Roll `other` back to the most recent date that was on a fiscal year
@@ -2077,9 +2072,9 @@ def is_on_offset(self, dt: datetime) -> bool:
20772072

20782073
@property
20792074
def rule_code(self) -> str:
2080-
suffix = self._offset.get_rule_code_suffix()
2075+
suffix = FY5253Mixin.rule_code.fget(self) # type: ignore
20812076
qtr = self.qtr_with_extra_week
2082-
return f"{self._prefix}-{suffix}-{qtr}"
2077+
return f"{suffix}-{qtr}"
20832078

20842079
@classmethod
20852080
def _from_name(cls, *args):

0 commit comments

Comments
 (0)