Skip to content
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

QST: How to handle Index._id in an Index subclass? #37213

Closed
2 tasks
spencerkclark opened this issue Oct 18, 2020 · 3 comments · Fixed by #37321
Closed
2 tasks

QST: How to handle Index._id in an Index subclass? #37213

spencerkclark opened this issue Oct 18, 2020 · 3 comments · Fixed by #37321
Labels
Index Related to the Index class or subclasses Usage Question
Milestone

Comments

@spencerkclark
Copy link
Contributor

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.


Question about pandas

In xarray we make use of an Index subclass for datetime-like indexing with non-standard calendars, which we call CFTimeIndex. A recent change, #37087, led to some test failures on our end. We currently do not define an _id attribute on our subclass, so we are vulnerable to the kinds of attribute errors illustrated below (note this example requires the xarray and cftime libraries be installed, with the development version of pandas):

In [1]: import xarray as xr

In [2]: xr.cftime_range("2000", periods=2).view()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-5226cd5e3bc1> in <module>
----> 1 xr.cftime_range("2000", periods=2).view()

~/Software/pandas/pandas/core/indexes/base.py in view(self, cls)
    630             result = self._shallow_copy()
    631         if isinstance(result, Index):
--> 632             result._id = self._id
    633         return result
    634

AttributeError: 'CFTimeIndex' object has no attribute '_id'

Where possible, we try to avoid using private methods/attributes on our subclass. In this case would you recommend we go against that by setting the _id attribute, e.g. by using _reset_identity in our constructor, or would it be possible to continue to have _id be somewhat of an optional attribute of Index objects in pandas?

@jbrockmendel
Copy link
Member

cc @topper-123

@topper-123
Copy link
Contributor

I think you've got two choices:

  1. Add a _id = None class attribute. This corresponds to how pandas did it before CLN: clean Index._id #37087.
  2. Call _reset_cache in your _simple_new. This helps performance if comparing views of the same index.

Maybe we should add _id = None a back to pandas?

@topper-123 topper-123 added Index Related to the Index class or subclasses and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 18, 2020
@spencerkclark
Copy link
Contributor Author

Thanks @topper-123!

Maybe we should add _id = None a back to pandas?

I'm obviously biased -- the fewer changes we have to make in xarray the better -- but this would be my preferred approach.

@jreback jreback added this to the 1.2 milestone Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Index Related to the Index class or subclasses Usage Question
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants