Skip to content

REF: delegate attrs for CategoricalIndex, IntervalIndex #30605

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 1 commit into from
Jan 2, 2020

Conversation

jbrockmendel
Copy link
Member

Working towards creating an ExtensionIndexMixin to be shared by all our backed-be-EA indexes, which would ideally grow into ExtensionIndex.

Removes CategoricalIndex._codes_for_groupby, which would raise AttributeError if it were ever called.

@jreback jreback added Categorical Categorical Data Type Interval Interval data type Refactor Internal refactoring of code labels Jan 2, 2020
@jreback jreback added this to the 1.0 milestone Jan 2, 2020
def _delegate_method(self, name, *args, **kwargs):
""" method delegation to the ._values """
method = getattr(self._values, name)
if "inplace" in kwargs:
raise ValueError("cannot use inplace with CategoricalIndex")
res = method(*args, **kwargs)
if is_scalar(res):
if is_scalar(res) or name in self._raw_inherit:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment here on what is happening

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_scalar is the heavier check, can you put ‘name in self._raw_inherit’ as the first check.

@@ -192,6 +214,8 @@ class IntervalIndex(IntervalMixin, Index):
# Immutable, so we are able to cache computations like isna in '_mask'
_mask = None

_raw_inherit = {"_ndarray_values", "__array__", "overlaps", "contains"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, should we also add this attribute as an empty dict in Index?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id rather not have a dummy attr on Index, but im building up to implementing ExtensionIndex which will have it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comment below

""" method delegation to the ._data """
method = getattr(self._data, name)
res = method(*args, **kwargs)
if is_scalar(res) or name in self._raw_inherit:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment.

can we not put these methods on Index?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean remove them from IntervalIndex? No idea. @jschendel?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no the delegation methods that you added could be on Index itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see. yes, the point of this exercise is to build up to moving as much of it to to either ExtensionIndex or even Index as possible. Doing this on II/CI before refactoring is just to limit the scope.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its fine, but then will have to clean later

Copy link
Member

@jschendel jschendel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, wanted to do this after I saw this pattern in DatetimeIndex but haven't gotten around to it.

klass="IntervalIndex",
examples=textwrap.dedent(
"""\
>>> intervals = pd.IntervalIndex.from_tuples([(0, 1), (1, 3), (2, 4)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this means that we'll lose any IntervalIndex specific portions of docstrings? And the docstring will just be whatever the IntervalArray docstring is? I'm fine with this since the usage is basically identical between the two so I don't think it will cause any confusion. Will allow for some cleanup of the docstrings on the IntervalArray side but that can be done later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this means that we'll lose any IntervalIndex specific portions of docstrings?

Yes

@jreback jreback merged commit 44d6b28 into pandas-dev:master Jan 2, 2020
@jreback
Copy link
Contributor

jreback commented Jan 2, 2020

thanks @jbrockmendel comments above, but i think you have already noted them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Interval Interval data type Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants