Skip to content

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

Closed
@spencerkclark

Description

@spencerkclark
  • 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexRelated to the Index class or subclassesUsage Question

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions