-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: Allow dt accessor when using ArrowDtype with datetime types #50954
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
Changes from all commits
029a175
5174cbd
2329065
b7f791e
c5cd79b
d01c5f8
774dcd2
5eb6568
0455080
48cb9ba
18f1743
4cecc96
83206ad
f5d1bc7
8d26a24
e4ca91e
7566710
7f4a80e
c94a91b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ | |
""" | ||
from __future__ import annotations | ||
|
||
from typing import final | ||
from typing import ( | ||
Callable, | ||
final, | ||
) | ||
import warnings | ||
|
||
from pandas.util._decorators import doc | ||
|
@@ -59,7 +62,13 @@ def _delegate_method(self, name, *args, **kwargs): | |
|
||
@classmethod | ||
def _add_delegate_accessors( | ||
cls, delegate, accessors, typ: str, overwrite: bool = False | ||
cls, | ||
delegate, | ||
accessors: list[str], | ||
typ: str, | ||
overwrite: bool = False, | ||
accessor_mapping: Callable[[str], str] = lambda x: x, | ||
raise_on_missing: bool = True, | ||
) -> None: | ||
""" | ||
Add accessors to cls from the delegate class. | ||
|
@@ -75,6 +84,11 @@ def _add_delegate_accessors( | |
typ : {'property', 'method'} | ||
overwrite : bool, default False | ||
Overwrite the method/property in the target class if it exists. | ||
accessor_mapping: Callable, default lambda x: x | ||
Callable to map the delegate's function to the cls' function. | ||
raise_on_missing: bool, default True | ||
Raise if an accessor does not exist on delegate. | ||
False skips the missing accessor. | ||
""" | ||
|
||
def _create_delegator_property(name): | ||
|
@@ -88,20 +102,28 @@ def _setter(self, new_values): | |
_setter.__name__ = name | ||
|
||
return property( | ||
fget=_getter, fset=_setter, doc=getattr(delegate, name).__doc__ | ||
fget=_getter, | ||
fset=_setter, | ||
doc=getattr(delegate, accessor_mapping(name)).__doc__, | ||
) | ||
|
||
def _create_delegator_method(name): | ||
def f(self, *args, **kwargs): | ||
return self._delegate_method(name, *args, **kwargs) | ||
|
||
f.__name__ = name | ||
f.__doc__ = getattr(delegate, name).__doc__ | ||
f.__doc__ = getattr(delegate, accessor_mapping(name)).__doc__ | ||
|
||
return f | ||
|
||
for name in accessors: | ||
|
||
if ( | ||
not raise_on_missing | ||
and getattr(delegate, accessor_mapping(name), None) is None | ||
): | ||
continue | ||
|
||
if typ == "property": | ||
f = _create_delegator_property(name) | ||
else: | ||
|
@@ -112,7 +134,14 @@ def f(self, *args, **kwargs): | |
setattr(cls, name, f) | ||
|
||
|
||
def delegate_names(delegate, accessors, typ: str, overwrite: bool = False): | ||
def delegate_names( | ||
delegate, | ||
accessors: list[str], | ||
typ: str, | ||
overwrite: bool = False, | ||
accessor_mapping: Callable[[str], str] = lambda x: x, | ||
raise_on_missing: bool = True, | ||
): | ||
""" | ||
Add delegated names to a class using a class decorator. This provides | ||
an alternative usage to directly calling `_add_delegate_accessors` | ||
|
@@ -127,6 +156,11 @@ def delegate_names(delegate, accessors, typ: str, overwrite: bool = False): | |
typ : {'property', 'method'} | ||
overwrite : bool, default False | ||
Overwrite the method/property in the target class if it exists. | ||
accessor_mapping: Callable, default lambda x: x | ||
Callable to map the delegate's function to the cls' function. | ||
raise_on_missing: bool, default True | ||
Raise if an accessor does not exist on delegate. | ||
False skips the missing accessor. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you expect to reuse this machinery again? if not, could the pyarrow-dt be accessor be implemented standalone to avoid making this more complicaed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am hoping to use this for the pyarrow duration types as well. I think a standalone implementation would require a further refactoring of this |
||
|
||
Returns | ||
------- | ||
|
@@ -141,7 +175,14 @@ class CategoricalAccessor(PandasDelegate): | |
""" | ||
|
||
def add_delegate_accessors(cls): | ||
cls._add_delegate_accessors(delegate, accessors, typ, overwrite=overwrite) | ||
cls._add_delegate_accessors( | ||
delegate, | ||
accessors, | ||
typ, | ||
overwrite=overwrite, | ||
accessor_mapping=accessor_mapping, | ||
raise_on_missing=raise_on_missing, | ||
) | ||
return cls | ||
|
||
return add_delegate_accessors | ||
|
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.
can we do this once near the top?
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.
ill put this into my next Assorted Cleanups branch