Skip to content

REF: make MonthOffset a cdef class #34324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions doc/source/reference/offset_frequency.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,40 +173,6 @@ Methods
CustomBusinessHour.is_on_offset
CustomBusinessHour.__call__

MonthOffset
-----------
.. autosummary::
:toctree: api/

MonthOffset

Properties
~~~~~~~~~~
.. autosummary::
:toctree: api/

MonthOffset.freqstr
MonthOffset.kwds
MonthOffset.name
MonthOffset.nanos
MonthOffset.normalize
MonthOffset.rule_code
MonthOffset.n

Methods
~~~~~~~
.. autosummary::
:toctree: api/

MonthOffset.apply
MonthOffset.apply_index
MonthOffset.copy
MonthOffset.isAnchored
MonthOffset.onOffset
MonthOffset.is_anchored
MonthOffset.is_on_offset
MonthOffset.__call__

MonthEnd
--------
.. autosummary::
Expand Down
25 changes: 25 additions & 0 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,31 @@ cdef class QuarterOffset(BaseOffset):
return type(dtindex)._simple_new(shifted, dtype=dtindex.dtype)


cdef class MonthOffset(BaseOffset):
def is_on_offset(self, dt) -> bool:
if self.normalize and not is_normalized(dt):
return False
return dt.day == self._get_offset_day(dt)

@apply_wraps
def apply(self, other):
compare_day = self._get_offset_day(other)
n = roll_convention(other.day, self.n, compare_day)
return shift_month(other, n, self._day_opt)

@apply_index_wraps
def apply_index(self, dtindex):
shifted = shift_months(dtindex.asi8, self.n, self._day_opt)
return type(dtindex)._simple_new(shifted, dtype=dtindex.dtype)

@classmethod
def _from_name(cls, suffix=None):
# default _from_name calls cls with no args
if suffix:
raise ValueError(f"Bad freq suffix {suffix}")
return cls()


# ----------------------------------------------------------------------
# RelativeDelta Arithmetic

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/tseries/offsets/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

from pandas._libs.tslibs.offsets import MonthOffset

import pandas.tseries.offsets as offsets


Expand All @@ -15,7 +17,7 @@ def offset_types(request):
params=[
getattr(offsets, o)
for o in offsets.__all__
if issubclass(getattr(offsets, o), offsets.MonthOffset) and o != "MonthOffset"
if issubclass(getattr(offsets, o), MonthOffset) and o != "MonthOffset"
]
)
def month_classes(request):
Expand Down
30 changes: 7 additions & 23 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,25 +820,7 @@ def __init__(
# Month-Based Offset Classes


class MonthOffset(SingleConstructorOffset):
def is_on_offset(self, dt: datetime) -> bool:
if self.normalize and not is_normalized(dt):
return False
return dt.day == self._get_offset_day(dt)

@apply_wraps
def apply(self, other):
compare_day = self._get_offset_day(other)
n = liboffsets.roll_convention(other.day, self.n, compare_day)
return shift_month(other, n, self._day_opt)

@apply_index_wraps
def apply_index(self, i):
shifted = liboffsets.shift_months(i.asi8, self.n, self._day_opt)
return type(i)._simple_new(shifted, dtype=i.dtype)


class MonthEnd(MonthOffset):
class MonthEnd(SingleConstructorMixin, liboffsets.MonthOffset):
"""
DateOffset of one month end.
"""
Expand All @@ -847,7 +829,7 @@ class MonthEnd(MonthOffset):
_day_opt = "end"


class MonthBegin(MonthOffset):
class MonthBegin(SingleConstructorMixin, liboffsets.MonthOffset):
"""
DateOffset of one month at beginning.
"""
Expand All @@ -856,7 +838,7 @@ class MonthBegin(MonthOffset):
_day_opt = "start"


class BusinessMonthEnd(MonthOffset):
class BusinessMonthEnd(SingleConstructorMixin, liboffsets.MonthOffset):
"""
DateOffset increments between business EOM dates.
"""
Expand All @@ -865,7 +847,7 @@ class BusinessMonthEnd(MonthOffset):
_day_opt = "business_end"


class BusinessMonthBegin(MonthOffset):
class BusinessMonthBegin(SingleConstructorMixin, liboffsets.MonthOffset):
"""
DateOffset of one business month at beginning.
"""
Expand All @@ -875,7 +857,9 @@ class BusinessMonthBegin(MonthOffset):


@doc(bound="bound")
class _CustomBusinessMonth(CustomMixin, BusinessMixin, MonthOffset):
class _CustomBusinessMonth(
CustomMixin, BusinessMixin, SingleConstructorMixin, liboffsets.MonthOffset
):
"""
DateOffset subclass representing custom business month(s).

Expand Down