-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Better ordering of coords in repr #11091
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42c6ce9
Updating coords ordering to keep matching dims together based on @ian…
jsignell 9898f1c
Update tests to match new order
jsignell 9ef813e
Update docstring examples with new coord order
jsignell 76bf15b
Try to make the comments clearer
jsignell 24039b7
Merge branch 'main' into ordered-coords
jsignell 6b245e3
Update what's new
jsignell 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
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
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
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 |
|---|---|---|
|
|
@@ -443,12 +443,37 @@ def _mapping_repr( | |
| ) | ||
|
|
||
|
|
||
| def _coord_sort_key(coord, dims): | ||
| """Sort key for coordinate ordering. | ||
|
|
||
| Orders by: | ||
| 1. Primary: index of the first matching dimension in dataset dims | ||
| 2. Secondary: dimension coordinates (name == dim) come before non-dimension coordinates | ||
|
|
||
| This groups non-dimension coordinates right after their associated dimension | ||
| coordinate. | ||
| """ | ||
| name, var = coord | ||
|
|
||
| # Dimension coordinates sorted by their position in dims come first (0) | ||
| if name in dims: | ||
| return (dims.index(name), 0) | ||
|
|
||
| # Non-dimension coordinates sorted by their last dim come second (1) | ||
| for d in var.dims[::-1]: | ||
|
Member
Author
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. I switched this to look at dims in reverse order because this feels like the right order: >>> import numpy as np
>>> import xarray as xr
>>> temperature = np.arange(25).reshape(5, 5)
>>> pressure = np.arange(50, 75).reshape(5, 5)
>>> da = xr.DataArray(
... data=temperature,
... dims=["x", "y"],
... coords=dict(
... lon=("x", np.arange(10, 15)),
... lat=("y", np.arange(20, 25)),
... Pressure=(["x", "y"], pressure),
... ),
... name="Temperature",
... )
>>> da
<xarray.DataArray 'Temperature' (x: 5, y: 5)> Size: 200B
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
Coordinates:
lon (x) int64 40B 10 11 12 13 14
lat (y) int64 40B 20 21 22 23 24
Pressure (x, y) int64 200B 50 51 52 53 54 55 56 57 ... 68 69 70 71 72 73 74
Dimensions without coordinates: x, y |
||
| if d in dims: | ||
| return (dims.index(d), 1) | ||
|
|
||
| # Scalar coords or coords with dims not in dataset dims go at end | ||
| return (len(dims), 1) | ||
|
|
||
|
|
||
| def coords_repr(coords: AbstractCoordinates, col_width=None, max_rows=None): | ||
| if col_width is None: | ||
| col_width = _calculate_col_width(coords) | ||
| dims = tuple(coords._data.dims) | ||
| dim_ordered_coords = sorted( | ||
| coords.items(), key=lambda x: dims.index(x[0]) if x[0] in dims else len(dims) | ||
| coords.items(), key=functools.partial(_coord_sort_key, dims=dims) | ||
| ) | ||
| return _mapping_repr( | ||
| dict(dim_ordered_coords), | ||
|
|
||
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.