-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add _repr_inline_ documentation to CustomIndex docs
#11046
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
Open
sshekhar563
wants to merge
2
commits into
pydata:main
Choose a base branch
from
sshekhar563:doc-add-repr-inline-to-custom-index
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+44
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,6 +128,46 @@ e.g., a kd-tree object may not be easily indexed. If ``Index.isel()`` is not | |
| implemented, the index in just dropped in the DataArray or Dataset resulting | ||
| from the selection. | ||
|
|
||
| Custom representation | ||
| --------------------- | ||
|
|
||
| When a :py:class:`Dataset` or :py:class:`DataArray` is displayed, Xarray shows | ||
| a summary of its indexes. By default, the base :py:class:`Index` class returns | ||
| just the class name. You can customize this by implementing the | ||
| :py:meth:`Index._repr_inline_` method, which should return a short, single-line | ||
| string representation of the index. | ||
|
|
||
| The ``_repr_inline_`` method receives a ``max_width`` argument (number of | ||
| characters) that indicates the available space for the representation. If the | ||
| representation exceeds this width, it should be truncated: | ||
|
|
||
| .. jupyter-execute:: | ||
|
|
||
| from xarray import Index | ||
|
|
||
| class MyIndex(Index): | ||
| def __init__(self, data): | ||
| self._data = data | ||
|
|
||
| def _repr_inline_(self, max_width: int) -> str: | ||
| # Return a concise representation | ||
| return f"{self.__class__.__name__} (size={len(self._data)})" | ||
|
|
||
| Here are some examples from Xarray's built-in indexes: | ||
|
|
||
| - ``RangeIndex`` returns: ``RangeIndex (start=0, stop=1, step=0.1)`` | ||
| - ``NDPointIndex`` returns: ``NDPointIndex (KDTree)`` | ||
|
|
||
| This representation appears in the indexes section when displaying a Dataset or | ||
| DataArray: | ||
|
|
||
| .. code-block:: none | ||
|
|
||
| <xarray.DataArray (x: 10)> | ||
| ... | ||
| Indexes: | ||
| x MyIndex (size=10) | ||
|
Comment on lines
+164
to
+169
Contributor
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 this be a jupyter-execute block? to make sure this stays up to date |
||
|
|
||
| Alignment | ||
| --------- | ||
|
|
||
|
|
@@ -198,6 +238,10 @@ regularly spaced 2-dimensional data) that internally relies on two | |
|
|
||
| return merge_sel_results(results) | ||
|
|
||
| def _repr_inline_(self, max_width: int) -> str: | ||
| dims = [idx.dim for idx in self._xy_indexes.values()] | ||
| return f"{self.__class__.__name__} (dims={dims})" | ||
|
|
||
|
|
||
| This basic index only supports label-based selection. Providing a full-featured | ||
| index by implementing the other ``Index`` methods should be pretty | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.