Skip to content

Accessor registry #20780

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
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/core/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def decorator(accessor):
UserWarning,
stacklevel=2)
setattr(cls, name, CachedAccessor(name, accessor))
cls._accessors.add(name)
return accessor
return decorator

Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def _constructor(self):
_constructor_sliced = Series
_deprecations = NDFrame._deprecations | frozenset(
['sortlevel', 'get_value', 'set_value', 'from_csv', 'from_items'])
_accessors = set()

@property
def _constructor_expanddim(self):
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ class Index(IndexOpsMixin, PandasObject):

_engine_type = libindex.ObjectEngine

_accessors = frozenset(['str'])
_accessors = set(['str'])

str = CachedAccessor("str", StringMethods)

def __new__(cls, data=None, dtype=None, copy=False, name=None,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
Copy input data
"""
_metadata = ['name']
_accessors = frozenset(['dt', 'cat', 'str'])
_accessors = set(['dt', 'cat', 'str'])
_deprecations = generic.NDFrame._deprecations | frozenset(
['asobject', 'sortlevel', 'reshape', 'get_value', 'set_value',
'from_csv', 'valid'])
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/test_register_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def ensure_removed(obj, attr):
delattr(obj, attr)
except AttributeError:
pass
obj._accessors.discard(attr)


class MyAccessor(object):
Expand All @@ -38,13 +39,14 @@ def method(self):
(pd.DataFrame, pd.api.extensions.register_dataframe_accessor),
(pd.Index, pd.api.extensions.register_index_accessor)
])
def test_series_register(obj, registrar):
def test_register(obj, registrar):
with ensure_removed(obj, 'mine'):
before = set(dir(obj))
registrar('mine')(MyAccessor)
assert obj([]).mine.prop == 'item'
after = set(dir(obj))
assert (before ^ after) == {'mine'}
assert 'mine' in obj._accessors


def test_accessor_works():
Expand Down
3 changes: 2 additions & 1 deletion scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def doc_parameters(self):
@property
def signature_parameters(self):
if (inspect.isclass(self.method_obj)
and self.method_name.split('.')[-1] in {'dt', 'str', 'cat'}):
and self.method_name.split('.')[-1] in
self.method_obj._accessors):
# accessor classes have a signature, but don't want to show this
return tuple()
try:
Expand Down