Skip to content
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

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

Closed
wants to merge 6 commits into from
Closed
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
46 changes: 46 additions & 0 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2477,11 +2477,57 @@ cdef class MonthEnd(MonthOffset):
"""
DateOffset of one month end.

Parameters
----------
n : int, default 1
n = 0 -> offset to the end of month.
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.

Comment on lines +2482 to +2487
Copy link
Member

Choose a reason for hiding this comment

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

does this look right? can you try building this page of the docs to check please? https://pandas.pydata.org/docs/dev/development/contributing_documentation.html

Copy link
Author

Choose a reason for hiding this comment

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

Ok, but I will need more time for this. I still have trouble with setting up virtual environment (spend one hour with it today and will continue tomorrow)

Copy link
Author

@jtimko16 jtimko16 Oct 18, 2022

Choose a reason for hiding this comment

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

What is the version of Python you are using? I created a virtual environment on top of Python 3.10, and trying to install packagse from requirements-dev.txt, however it always stuck at installation of boto3:
`

Using cached boto3-1.24.38-py3-none-any.whl (132 kB)
Using cached boto3-1.24.37-py3-none-any.whl (132 kB)
Using cached boto3-1.24.36-py3-none-any.whl (132 kB)
Using cached boto3-1.24.35-py3-none-any.whl (132 kB)
Using cached boto3-1.24.34-py3-none-any.whl (132 kB)
Using cached boto3-1.24.33-py3-none-any.whl (132 kB)
Using cached boto3-1.24.32-py3-none-any.whl (132 kB)`

Is it better to use older version of Python - e.g. 3.9? Thanks

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

please do ask (here or on Slack) if it's unclear / anything trips you up

Copy link
Author

Choose a reason for hiding this comment

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

I tried to run it again, and also installed C++ Build Tools, but still receiving similar error. Please see below the full traceback:
Traceback_error.txt

Copy link
Member

Choose a reason for hiding this comment

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

I don't know, sorry - you could ask on Slack , or try using Ubuntu via the WSL on Windows

Copy link
Author

Choose a reason for hiding this comment

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

Hi, thank you. I don't want to sound like giving up. However unfortunately, this is going deeper and deeper technical, and I feel I am not able to effectively contribute. Is it OK, if I share the updated text of documentation in thread under the issue, and leave it for someone more technically capable to proceed with implementation?

Copy link
Member

Choose a reason for hiding this comment

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

sure, that's fine, someone else can take this forwards, and if you feel like trying again, feel free to reach out and ask

Copy link
Author

Choose a reason for hiding this comment

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

Ok, thanks. Maybe I will give it another go this weekend. Will let you know how I decide to proceed then. Thanks

Examples
--------
Offset to the end of the month:

>>> ts = pd.Timestamp(2022, 1, 1)
>>> ts + pd.offsets.MonthEnd()
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

However be careful, if given date is the last day of a month, method offsets to the end of the following month:

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

With the ``n`` parameter, we can control logic of offset.

See below, when ``n`` is set to 0, offset is always made to the end of the current month:

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

When ``n`` is set to 1 (default) it will be moved to the following month:

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

When ``n`` is set to 2, method offsets one additional month. And again depends whether given date is on an anchor point:

>>> ts = pd.Timestamp(2022, 1, 1)
>>> ts + pd.offsets.MonthEnd(n=2)
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')

Also we can use negative ``n`` to find end of previous months:

>>> 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