Replies: 2 comments 2 replies
-
You seem to have answered (1) yourself. It does work - what happens is that non-coord dimensions have implicit coords which just enumerate from 0 to len(dim). Try: da["lat"] In (2) it does not sort the values but sorts by the "coordinates" - as the coords are 0..9 it reverses them. Two more examples:
da2 = xr.DataArray([1, 2, 3], coords=dict(lon=[2, 1, 3]))
da2.sortby('lon')
# the second sortby reverses again
da.sortby('lon', ascending=False).sortby('lon', ascending=False)
da1 = xr.DataArray([1, 2, 3], coords=dict(lon=[1, 2, 3]))
# the second sortby has no effect
da1.sortby('lon', ascending=False).sortby('lon', ascending=False) |
Beta Was this translation helpful? Give feedback.
1 reply
-
Yes your understanding is correct. They are called Non-dimension-coordinates but I am not sure if there is much more info in the docs. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I read a lot about questions and solutions related to xarray in this discussion forum. They are super useful and saved me time to dig out the issue and find fixes by myself.
This is my first question posted here and I hope someone could help me understand how the sortby() method should be used.
Recently I found something weird about DataArray.sortby() method. Here is the code:
Here is the print out:
My questions are:
Does sortby() accept dimensions without coordinates as its parameter of "variables"?
Based on the documentation of xarray, it seems NO. "variables" should be "1D DataArray objects or name(s) of 1D variable(s) in coords whose values are used to sort this array."
I tested "lat" and "lon" dimensions that are without coordinate, but sortby() does not raise an Error instead it still does something.
"da.sortby('lon', ascending=True)" returns the same values as da.values, which is reasonable. However, "da.sortby('lon', ascending=False)" returns the array with column values in a reversed order. Note that it does not sort the column values but just reversed the order ([19, 18, 17, 16, 15, 14, 13, 12, 0, 10]. Such behavior is held for the row values "lat".
I am using xarray with the version of
xarray 0.19.0 pyhd8ed1ab_1 conda-forge
Is this how sortby() supposed to work or did I get something messed up?
Thanks for your time and look forward to a discussion.
Qinjian Jin
Beta Was this translation helpful? Give feedback.
All reactions