-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
TYP: some more static definitions of methods for DatetimeIndex #36742
TYP: some more static definitions of methods for DatetimeIndex #36742
Conversation
pandas/core/indexes/datetimes.py
Outdated
from pandas import DataFrame, PeriodIndex, TimedeltaIndex | ||
|
||
|
||
def dispatch_to_array_and_wrap(delegate) -> Callable[[F], F]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not the final location for this. may do PeriodIndex next and move to share.
duplication with inherit_from_data. different in that inherit_from_data creates a method, whereas this wraps a method.
it maybe that we eventually replace all uses inherit_from_data and therefore can delete rather than deduplicate.
web and doc fails
|
pandas/core/indexes/datetimes.py
Outdated
|
||
def dispatch_to_array_and_wrap(delegate) -> Callable[[F], F]: | ||
def decorator(func: F) -> F: | ||
name = func.__name__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't functools.wraps already copy over the __name__
and __doc__
? I think you might be able to reduce the layers of wrapping by one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this to copy doc from delegate not func.
wraps is to copy name (the code here does not set the name attribute) and the signature for the help/docs
can always add the doc decorator to each method individually and no need for the decorator factory. I did this originally but thought it looked messy with two decorators and repetition.
pandas/core/indexes/datetimes.py
Outdated
return type(self)._simple_new(arr, name=self.name) | ||
@dispatch_to_array_and_wrap(DatetimeArray) | ||
def isocalendar(self) -> "DataFrame": | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i find the 4 line version clearer than the 3 line version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am happy with the existing static code, if others are happy with the repetition/duplications
Thanks @WillAyd and @jbrockmendel for the review. I'll do two commits, one to remove the decorator factory and another to remove the actual decorator. I think the static approach that I did originally is clearer, especially considering the handling of the different return types. I raised this PR in connection with #31160 (comment) to be more like #31160 (comment) |
and more error prone. Looks like made an error originally for I think adding the doc decorator to each method could also be error prone, but at the same now allows substitution so could have separate html docs for Index and array methods. |
LGTM |
sure |
xref #32100, #31160 (comment)