Skip to content

Conversation

@sshekhar563
Copy link

Closes #11036

As noted in #11035, the custom index docs page was missing information about _repr_inline_.

This PR adds:

  • A new "Custom representation" section explaining the _repr_inline_ method
  • Documentation of the method signature with the max_width parameter
  • A code example showing how to implement it
  • Real-world examples from RangeIndex and NDPointIndex
  • Updated the RasterIndex example to include a _repr_inline_ implementation

Closes pydata#11036

- Add new 'Custom representation' section explaining _repr_inline_
- Document method signature with max_width parameter
- Include code example and real-world examples from RangeIndex/NDPointIndex
- Update RasterIndex example to include _repr_inline_ implementation
@welcome
Copy link

welcome bot commented Dec 22, 2025

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

Copy link
Member

@jsignell jsignell left a comment

Choose a reason for hiding this comment

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

Thanks @sshekhar563! I just had one little suggestion.

def _repr_inline_(self, max_width: int) -> str:
dims = [idx.dim for idx in self._xy_indexes.values()]
return f"RasterIndex (dims={dims})"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return f"RasterIndex (dims={dims})"
return f"{self.__class__.__name__} (dims={dims})"

def _repr_inline_(self, max_width: int) -> str:
# Return a concise representation
return f"MyIndex (size={len(self._data)})"
Copy link
Member

Choose a reason for hiding this comment

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

Just a little nitpick. Prefer to use class name rather than hardcoding:

Suggested change
return f"MyIndex (size={len(self._data)})"
return f"{self.__class__.__name__} (size={len(self._data)})"

Copy link
Contributor

@ianhi ianhi left a comment

Choose a reason for hiding this comment

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

Looks great thanks @sshekhar563

I left a few suggestions but nothing crucial. I agree re the suggestion to use a classname instead of hardcoding.

Comment on lines +161 to +166
.. code-block:: none
<xarray.DataArray (x: 10)>
...
Indexes:
x MyIndex (size=10)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be a jupyter-execute block? to make sure this stays up to date

Comment on lines +155 to +156
- ``RangeIndex`` returns: ``RangeIndex (start=0, stop=1, step=0.1)``
- ``NDPointIndex`` returns: ``NDPointIndex (KDTree)``
Copy link
Contributor

Choose a reason for hiding this comment

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

If possible to link to the source code for these (ideally linked to the release) that would be amazing. but no worries if that's too difficult ot get working with auto updating permalinks.

Comment on lines 140 to 141
The ``_repr_inline_`` method receives a ``max_width`` argument (number of
characters) that can be used to truncate the output if needed:
Copy link
Contributor

Choose a reason for hiding this comment

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

The phrasing here could be a bit more precise. I think this is either xarray telling us that we are going to be truncated, so we better fit it all in there, or telling us that we must truncate. I'm actually not sure what the behavior is and this would be a great place to discuss it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

📚 Add repr information to CustomIndex docs

3 participants