You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That's really convenient, what a nice API.
But when I want to switch to another coordinate, I found that I can not recovery my arr to the version before I using :
>>>arr=arr.reset_index('x'); arr# why the croodinate to used as index now lose its name "x_str"? <xarray.DataArray (t: 4, x: 4)>array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
Coordinates:
x_num (x) int641234x_ (x) object'a''b''c''d'Dimensionswithoutcoordinates: t, x>>>arr=arr.set_index(x="x_num");arr# anyway, continue going to the code use coordinate "x_num" as index>>>#... some code with "arr[{'x':[... some int index ...]}]" ...>>>arr=arr.reset_index('x');arr# now I need "x_str" coordinate as index, here we go<xarray.DataArray (t: 4, x: 4)>array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
Coordinates:
x_ (x) int641234Dimensionswithoutcoordinates: t, x>>>#NOOP!!! the "x_int" coordinate COVER the "x_str" coordinate, I can't access the later any more :(
To solve this problem, I get following ways:
Add new function to do this.
may be most directly way.
add new function to implement and new API to design.
stop the DataArray.set_index to change coordinate name
The benefit is that without new function, its the most convenient one.
But it may change the calling-promise of DataArray.set_index, so it may be useless.
Here are some example to show how it can be used:
>>>arr=arr.set_index({'x':'x_str'})
>>>#... some code with "arr[{'x':[... some str index ...]}]" ...>>>arr=arr.set_index({'x':'x_int'})
>>>#... some code with "arr[{'x':[... some int index ...]}]" ...>>>arr=arr.set_index({'x':'x_str'}) ...
store the coordinate name in DataArray when calling "DataArray.set_index" and recovery them when calling "DataArray.reset_index"
Just a bit more complex than previews one.
But it need to add redundant data inner DataArray, useless too.
Example:
>>>arr=arr.set_index({'x':'x_str'})
>>>#... some code with "arr[{'x':[... some str index ...]}]" ...>>>arr=arr.reset_index('x').set_index({'x':'x_int'})
>>>#... some code with "arr[{'x':[... some int index ...]}]" ...>>>arr=arr.reset_index('x').set_index({'x':'x_str'}) ...
let DataArray.reset_index support Mapping as names parameters, while use the keys as dims to reset indices and the value as the names of coordinates converted from those indices.
More complex.
Maybe the one cause least change, so I prefer it.
Example:
>>>arr=arr.set_index({'x':'x_str'})
>>>#... some code with "arr[{'x':[... some str index ...]}]" ...>>>arr=arr.reset_index({'x':'x_str'}).set_index({'x':'x_int'})
>>>#... some code with "arr[{'x':[... some int index ...]}]" ...>>>arr=arr.reset_index({'x':'x_int'}).set_index({'x':'x_str'}) ...
The text was updated successfully, but these errors were encountered:
weipeng1999
changed the title
Need a way to speciefy the names of coordinates, for the indices droped by DataArray.reset_index.
Need a way to speciefy the names of coordinates from the indices which droped by DataArray.reset_index.
Oct 18, 2021
When I try to use some different coordinates as the index of a dim, I notice the new API on v0.9 provided by DataArray.set_index:
That's really convenient, what a nice API.
But when I want to switch to another coordinate, I found that I can not recovery my arr to the version before I using :
To solve this problem, I get following ways:
The text was updated successfully, but these errors were encountered: