Skip to content

BUG: pd.offsets.MonthEnd() delevers wrong month #49133

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

Closed
wants to merge 6 commits into from
Closed
Changes from 2 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
30 changes: 28 additions & 2 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2476,12 +2476,38 @@ cdef class MonthOffset(SingleConstructorOffset):
cdef class MonthEnd(MonthOffset):
"""
DateOffset of one month end.


When parameter n=0, always offset to the end of month.

When parameter n=1, depend on the given date:
a) Given date is on an anchor point (last day of the month) -> offset to the end of the following month.
b) Given date is not on an anchor point -> offset to the end of the same month.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can these go in the description of the n parameter?


Parameters
----------
n : int, default 1

Examples
--------
>>> ts = pd.Timestamp(2022, 1, 1)
>>> ts + pd.offsets.MonthEnd()
>>> ts + pd.offsets.MonthEnd(n=1)
Timestamp('2022-01-31 00:00:00')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a short sentence before the examples you've added to clarify what the reader is meant to take away from them? see https://pandas.pydata.org/docs/development/contributing_docstring.html#section-7-examples

>>> ts = pd.Timestamp(2022, 1, 31)
>>> ts + pd.offsets.MonthEnd(n=0)
Timestamp('2022-01-31 00:00:00')

>>> ts = pd.Timestamp(2022, 1, 31)
>>> ts + pd.offsets.MonthEnd(n=1)
Timestamp('2022-02-28 00:00:00')

>>> ts = pd.Timestamp(2022, 1, 31)
>>> ts + pd.offsets.MonthEnd(n=2)
Timestamp('2022-03-31 00:00:00')

>>> ts = pd.Timestamp(2022, 1, 31)
>>> ts + pd.offsets.MonthEnd(n=-1)
Timestamp('2021-12-31 00:00:00')
"""
_period_dtype_code = PeriodDtypeCode.M
_prefix = "M"
Expand Down