-
-
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
Unify Index._dir_* with Series implementation #17117
Changes from 6 commits
e9d1f05
b980958
f43d247
dbfc115
2a6afcb
7aa973e
d71dbf8
a213927
bf66c5b
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,30 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
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. can you add a comment about what this module contains |
||
|
||
class DirNamesMixin(object): | ||
_accessors = frozenset([]) | ||
|
||
def _dir_deletions(self): | ||
""" delete unwanted __dir__ for this object """ | ||
return self._accessors | ||
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. can you put a comment why accessors are deleted? (as only the relevant ones are added back afterwards?) |
||
|
||
def _dir_additions(self): | ||
""" add addtional __dir__ for this object """ | ||
rv = set() | ||
for accessor in self._accessors: | ||
try: | ||
getattr(self, accessor) | ||
rv.add(accessor) | ||
except AttributeError: | ||
pass | ||
return rv | ||
|
||
def __dir__(self): | ||
""" | ||
Provide method name lookup and completion | ||
Only provide 'public' methods | ||
""" | ||
rv = set(dir(type(self))) | ||
rv = (rv - self._dir_deletions()) | self._dir_additions() | ||
return sorted(rv) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,8 +192,9 @@ def __unicode__(self): | |
|
||
def _dir_additions(self): | ||
""" add the string-like attributes from the info_axis """ | ||
return set([c for c in self._info_axis | ||
if isinstance(c, string_types) and isidentifier(c)]) | ||
additions = set([c for c in self._info_axis | ||
if isinstance(c, string_types) and isidentifier(c)]) | ||
return PandasObject._dir_additions(self).union(additions) | ||
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. Is there a reason not to use 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. Not really. I developed the habit working in 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. super is the idiom. Calling specifically named methods usually indicates something odd in the inheritance chain. |
||
|
||
@property | ||
def _constructor_sliced(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ | |
import pandas.core.sorting as sorting | ||
from pandas.io.formats.printing import pprint_thing | ||
from pandas.core.ops import _comp_method_OBJECT_ARRAY | ||
from pandas.core.strings import StringAccessorMixin | ||
from pandas.core import strings | ||
from pandas.core.config import get_option | ||
|
||
|
||
|
@@ -98,7 +98,7 @@ def _new_Index(cls, d): | |
return cls.__new__(cls, **d) | ||
|
||
|
||
class Index(IndexOpsMixin, StringAccessorMixin, PandasObject): | ||
class Index(IndexOpsMixin, PandasObject): | ||
""" | ||
Immutable ndarray implementing an ordered, sliceable set. The basic object | ||
storing axis labels for all pandas objects | ||
|
@@ -151,6 +151,11 @@ class Index(IndexOpsMixin, StringAccessorMixin, PandasObject): | |
|
||
_engine_type = libindex.ObjectEngine | ||
|
||
_accessors = frozenset(['dt', 'str', 'cat']) | ||
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. maybe leave out 'dt' and 'cat' until they are actually added as accessor? (in a next PR I suppose) 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. ah, but they are not yet really added as accessor (so no user visible change), only 'removed' from I suppose we already have tests to check that 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'm not sure off the top, but I wrote some tests for #17204 that can be adapted pretty readily. 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. pls remove dt/cat, leave for another PR. str is covered I believe. |
||
|
||
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. there are no tests for the non string accessors in Index |
||
# String Methods | ||
str = base.AccessorProperty(strings.StringMethods) | ||
|
||
def __new__(cls, data=None, dtype=None, copy=False, name=None, | ||
fastpath=False, tupleize_cols=True, **kwargs): | ||
|
||
|
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.
remove the first line, we don't normally do this
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.
Just pushed a commit that does this and adds a comment in the docstring as requested below. The docstring isn't super-useful at this point because there isn't much here. After this is merged I'll rebase #17042 and accessor.py will have a coherent theme.