-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: implement indexes.extension to share delegation #30629
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
d546e76
74cf95b
f736da2
96628dd
0cda142
806be9a
f231762
aa923f5
0ccb13e
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Shared methods for Index subclasses backed by ExtensionArray. | ||
""" | ||
from typing import List | ||
|
||
from pandas.util._decorators import cache_readonly | ||
|
||
|
||
def inherit_from_data(name: str, delegate, cache: bool = False): | ||
""" | ||
Make an alias for a method of the underlying ExtensionArray. | ||
|
||
Parameters | ||
---------- | ||
name : str | ||
Name of an attribute the class should inherit from its EA parent. | ||
delegate : class | ||
cache : bool, default False | ||
Whether to convert wrapped properties into cache_readonly | ||
|
||
Returns | ||
------- | ||
attribute, method, property, or cache_readonly | ||
""" | ||
|
||
attr = getattr(delegate, name) | ||
|
||
if isinstance(attr, property): | ||
if cache: | ||
method = cache_readonly(attr.fget) | ||
|
||
else: | ||
|
||
def fget(self): | ||
return getattr(self._data, name) | ||
|
||
def fset(self, value): | ||
setattr(self._data, name, value) | ||
|
||
fget.__name__ = name | ||
fget.__doc__ = attr.__doc__ | ||
|
||
method = property(fget, fset) | ||
|
||
elif not callable(attr): | ||
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. Hmm somewhat confused by this function. Documented as working on 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. Will update docstring. This clause is for regular class attributes, in particular a few List[str] that we share |
||
# just a normal attribute, no wrapping | ||
method = attr | ||
|
||
else: | ||
|
||
def method(self, *args, **kwargs): | ||
result = attr(self._data, *args, **kwargs) | ||
return result | ||
|
||
method.__name__ = name | ||
method.__doc__ = attr.__doc__ | ||
return method | ||
|
||
|
||
def inherit_names(names: List[str], delegate, cache: bool = False): | ||
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. you could type this -> Callable[[Type[T]], Type[T]] i think |
||
""" | ||
Class decorator to pin attributes from an ExtensionArray to a Index subclass. | ||
|
||
Parameters | ||
---------- | ||
names : List[str] | ||
delegate : class | ||
cache : bool, default False | ||
""" | ||
|
||
def wrapper(cls): | ||
for name in names: | ||
meth = inherit_from_data(name, delegate, cache=cache) | ||
setattr(cls, name, meth) | ||
|
||
return cls | ||
|
||
return wrapper |
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.
you have the cache=True ones first in datetimelike, can you conform