Skip to content

BUG: CustomBusinessMonthBegin(End) sometimes ignores extra offset (GH41356) #41488

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
Jul 28, 2021

Conversation

jackzyliu
Copy link
Contributor

@jackzyliu jackzyliu commented May 15, 2021


Problem description: _CustomBusinessMonth does not apply extra offset when initially rolled month begin new is already a business day. This happens because we currently use CustomBusinessDay.rollforward (rollback) to both 1) roll to a business day and 2) apply the extra offset.
Fix: Separating the logic to 1) roll to a business day and 2) apply the extra offset.

@@ -971,6 +971,7 @@ Other
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
- Bug in :meth:`DataFrame.convert_dtypes` incorrectly raised ValueError when called on an empty DataFrame (:issue:`40393`)
- Bug in :meth:`DataFrame.clip` not interpreting missing values as no threshold (:issue:`40420`)
- Bug in :meth:`_CustomBusinessMonth.apply` not applying extra offset when initially rolled date `new` is already a business day (:issue:`41356`)
Copy link
Member

Choose a reason for hiding this comment

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

_CustomBusinessMonth is not user-facing. can this be phrased in terms of something users will directly see?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I just re-phased the entry. Is it more along the line of what you were thinking?

datetime(2021, 3, 2): datetime(2021, 3, 30) + timedelta(days=1),
},
),
]
Copy link
Contributor

Choose a reason for hiding this comment

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

can you just put these directly in the paremeterize?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! Will do.

As a side note, the existing tests for .apply also put the case definition outside of parametrize . Happy to quickly change these as well if you would like.

),
]

@pytest.mark.parametrize("case", apply_with_extra_offset_cases)
Copy link
Contributor

Choose a reason for hiding this comment

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

same

@@ -3350,7 +3350,13 @@ cdef class _CustomBusinessMonth(BusinessMixin):
"""
Define default roll function to be called in apply method.
"""
cbday = CustomBusinessDay(n=self.n, normalize=False, **self.kwds)
if self.offset:
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't offset always defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the purpose of the check here is to make sure we only pass in a zero self.offset into CustomBusinessDay (and that we do not do unnecessary copying of the kwd dict).

@jackzyliu jackzyliu force-pushed the test_custom_b_month_begin branch from 32ce31b to bc6d718 Compare May 18, 2021 19:12
@@ -974,6 +974,7 @@ Other
- Bug in :meth:`DataFrame.convert_dtypes` incorrectly raised ValueError when called on an empty DataFrame (:issue:`40393`)
- Bug in :meth:`DataFrame.clip` not interpreting missing values as no threshold (:issue:`40420`)
- Bug in :class:`Series` backed by :class:`DatetimeArray` or :class:`TimedeltaArray` sometimes failing to set the array's ``freq`` to ``None`` (:issue:`41425`)
- Bug in :meth:`CustomBusinessMonthBegin` (:meth:`CustomBusinessMonthEnd`) not applying the extra ``offset`` parameter when beginning (end) of the target month is already a business day (:issue:`41356`)
Copy link
Member

Choose a reason for hiding this comment

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

you've given the class but not the method. Is it e.g. CustomBusinessMonthBegin.__add__?

Copy link
Contributor Author

@jackzyliu jackzyliu May 18, 2021

Choose a reason for hiding this comment

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

Oops, my apologies. It should be corrected now. I mentioned the function .apply to be more specific, but maybe I should say .__add__ to be more user-centric?

@jackzyliu jackzyliu force-pushed the test_custom_b_month_begin branch from bc6d718 to 52bfd15 Compare May 18, 2021 19:57
@simonjayhawkins
Copy link
Member

@jackzyliu can you merge master to resolve conflicts

@simonjayhawkins simonjayhawkins added this to the 1.3 milestone May 25, 2021
@jackzyliu jackzyliu force-pushed the test_custom_b_month_begin branch from 52bfd15 to 02f6ceb Compare May 25, 2021 18:38
@jackzyliu
Copy link
Contributor Author

Hi @simonjayhawkins, I just rebased after master, but some checks started failing and I can't replicate the failed tests offline. Examples:

  • pandas/tests/tseries/offsets/test_offsets_properties.py::test_on_offset_implementations with actions-38.yaml
  • pandas/tests/arithmetic/test_datetime64.py::TestDatetimeIndexArithmetic::test_sub_dti_dti with actions-39-numpydev.yaml

These checks were successful before rebasing without any code change. What could be the issue here?

@simonjayhawkins
Copy link
Member

The test_on_offset_implementations failure is a hypothesis failure so that maybe the reason it didn't fail before. may need to investigate that one further. The numpydev failures are a known issue.

@simonjayhawkins simonjayhawkins removed this from the 1.3 milestone May 26, 2021
@jackzyliu jackzyliu force-pushed the test_custom_b_month_begin branch 2 times, most recently from 7068952 to 8cb2f65 Compare June 9, 2021 02:12
@jackzyliu
Copy link
Contributor Author

jackzyliu commented Jun 9, 2021

Re-rebased after master. @simonjayhawkins

The test_on_offset_implementations failure is a hypothesis failure so that maybe the reason it didn't fail before. may need to investigate that one further. The numpydev failures are a known issue.

Previously, pandas/tests/tseries/offsets/test_offsets_properties.py::test_on_offset_implementations unrelatedly failed with the following pair of randomly generated test inputs,:

dt = datetime.datetime(1900, 1, 1, 0, 0, tzinfo=pytz.timezone('Africa/Kinshasa'))
offset = pd.offsets.MonthBegin(66)

I created a separate issue and pull request for this.

@jackzyliu
Copy link
Contributor Author

jackzyliu commented Jul 6, 2021

Bumping up this PR for further review. Since we just released v1.3.0, I have moved the whatsnew entry to 1.3.1 for now. Happy to move it to 1.4.0 instead.

The failed test seem to come from master?

Please let me know of further comments. Thanks.

@jackzyliu jackzyliu force-pushed the test_custom_b_month_begin branch 2 times, most recently from 4a8fcec to ba151ef Compare July 14, 2021 04:58
@jackzyliu
Copy link
Contributor Author

@jreback @jbrockmendel @simonjayhawkins

  • Moved whatsnew entry to 1.4.0
  • Rebased after master
  • All checks green

Please let me know of further comments. Thanks!

@jackzyliu jackzyliu force-pushed the test_custom_b_month_begin branch from ba151ef to 47bf257 Compare July 23, 2021 19:16
…41356)

CustomBusinessMonthBegin(End) does not apply extra offset when the initially rolled month begin (end) is already a business day.
@jackzyliu jackzyliu force-pushed the test_custom_b_month_begin branch 2 times, most recently from de9c975 to b85ba8d Compare July 24, 2021 02:54
@jackzyliu jackzyliu force-pushed the test_custom_b_month_begin branch from b85ba8d to 05c8ee8 Compare July 24, 2021 20:40
@jackzyliu
Copy link
Contributor Author

Resolved conflict with master. Green.

@jreback jreback added this to the 1.4 milestone Jul 28, 2021
@jreback jreback merged commit 8e79e33 into pandas-dev:master Jul 28, 2021
@jreback
Copy link
Contributor

jreback commented Jul 28, 2021

thanks @jackzyliu, sorry about the wait, lots of PRs :->

keep em coming!

CGe0516 pushed a commit to CGe0516/pandas that referenced this pull request Jul 29, 2021
feefladder pushed a commit to feefladder/pandas that referenced this pull request Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frequency DateOffsets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: CustomBusinessMonthBegin(End) sometimes ignores extra offset
4 participants