Skip to content

BUG: #29049 make holiday support offsets of offsets #57938

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

Conversation

dontgoto
Copy link
Contributor

@dontgoto dontgoto commented Mar 20, 2024

The summing of offsets is generally not possible, so the bug is fixed by simply flattening the asymmetric list of lists of offsets that one might easily end up with when defining holidays in relation to each other.

In theory it is possible to sum consecutive DataOffset(n, weekday=None) in the offset list, but I did not want to introduce even more complexity for such little gain.

Allowing asymmetrical lists of lists and their element dtypes as parameters is not very pretty, but the only solution to this problem that does not involve a major refactoring of the offset classes to allow representation of lists of offsets in a single composite offset e.g. ["TUE(1)", 3]. Summing of composite offsets is not possible because additions between weekday and regular day shift offsets are not associative, not distributive, and not commutative.

Another option would be to just enhance the error message given to the end user when trying to chain offsets in the manner shown in the issue and let them do the aggregation to a simple list of offsets themselves.

@dontgoto dontgoto changed the title #29049 make holiday support offsets of offsets BUG: #29049 make holiday support offsets of offsets Mar 21, 2024
@@ -159,24 +166,34 @@ def __init__(
year=None,
month=None,
day=None,
offset=None,
observance=None,
offset: BaseOffset | list[BaseOffset | list[BaseOffset]] | None = None,
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 think list[list[BaseOffset]] should be supported. It's not worth the complexity when the provided list[BaseOffset] can be spat into a new argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the logic for handling this case and instead only throw a more informative error now. The type hint alone might already be sufficient for when someone chains Holiday.offset without considering that they can be lists, but I think the more detailed exception could also be nice to have.

@@ -149,6 +156,7 @@ class Holiday:
for observance.
"""

offset: BaseOffset | list[BaseOffset] | None
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't mypy be able to infer this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does. I removed this part.

Comment on lines 237 to 239
if offset is not None and observance is not None:
raise NotImplementedError("Cannot use both offset and observance.")
if isinstance(offset, list) and any(isinstance(off, list) for off in offset):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if offset is not None and observance is not None:
raise NotImplementedError("Cannot use both offset and observance.")
if isinstance(offset, list) and any(isinstance(off, list) for off in offset):
if offset is not None:
if observance is not None:
raise NotImplementedError("Cannot use both offset and observance.")
if not (isinstance(offset, DateOffset) or (isinstance(list, DateOffset) and all(isinstance(off, DateOffset) for off in offsets))):

Comment on lines 241 to 242
"Nested lists are not supported for offset. "
"Flatten composite offsets of `Holiday.offset`s first."
Copy link
Member

Choose a reason for hiding this comment

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

This should probably just say what is allowed for offset instead of a specific failure case

@mroeschke mroeschke added the Error Reporting Incorrect or improved errors from pandas label Mar 27, 2024
@mroeschke mroeschke added this to the 3.0 milestone Mar 28, 2024
@mroeschke mroeschke merged commit 7f93018 into pandas-dev:main Mar 28, 2024
46 checks passed
@mroeschke
Copy link
Member

Thanks @dontgoto

@dontgoto
Copy link
Contributor Author

Thank you for the input @mroeschke

pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
…-dev#57938)

* support offsets of offsets in holiday creation

* update whatsnew

* add type annotations

* update type annotation

* make types compatible

* update docstring

* don't accept, only raise with more informative exception

* add attribute type hint

* change array to list in docstring

Co-authored-by: Matthew Roeschke <[email protected]>

* broaden if check; specify error message

* update test raises message

---------

Co-authored-by: Matthew Roeschke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multiple dateoffsets in Holiday raises a TypeError
2 participants