如何重新标记 xarray DataArray?
How can I relabel an xarry DataArray?
我有一个如下所示的 DataArray:
<xarray.DataArray (rows: 3, cols: 3)>
array([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
Coordinates:
* rows (rows) <U5 'row1' 'row2' 'row3'
* cols (cols) <U4 'col1' 'col2' 'col3'
如何更改标签以获得:
<xarray.DataArray (rows: 3, cols: 3)>
array([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
Coordinates:
* rows (rows) <U5 'rowA' 'rowB' 'rowC'
* cols (cols) <U4 'col1' 'col2' 'col3'
我尝试使用 test.rename({"row1":"rowA", "row2":"rowB", "row3":"rowC"})
但它给了我一个 ValueError:
ValueError: cannot rename 'row1' because it is not a variable or dimension in this dataset
我认为重命名只是维度名称的意思,但如果是这种情况,那么文档根本没有说清楚。
这可能是目前最简单的方法。目前它不如 pandas' 选项好,我们有兴趣让它更容易。特别是,我们对 PR 非常开放,根据您的反馈使文档更清晰。
In [8]: da = xr.DataArray([1,2,3], coords=dict(a=list('xyz')), dims=['a'])
In [10]: da
Out[10]:
<xarray.DataArray (a: 3)>
array([1, 2, 3])
Coordinates:
* a (a) <U1 'x' 'y' 'z'
In [9]: da.to_dataset('a').rename(x='x2').to_array('a')
Out[9]:
<xarray.DataArray (a: 3)>
array([1, 2, 3])
Coordinates:
* a (a) <U2 'x2' 'y' 'z'
我有一个如下所示的 DataArray:
<xarray.DataArray (rows: 3, cols: 3)>
array([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
Coordinates:
* rows (rows) <U5 'row1' 'row2' 'row3'
* cols (cols) <U4 'col1' 'col2' 'col3'
如何更改标签以获得:
<xarray.DataArray (rows: 3, cols: 3)>
array([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
Coordinates:
* rows (rows) <U5 'rowA' 'rowB' 'rowC'
* cols (cols) <U4 'col1' 'col2' 'col3'
我尝试使用 test.rename({"row1":"rowA", "row2":"rowB", "row3":"rowC"})
但它给了我一个 ValueError:
ValueError: cannot rename 'row1' because it is not a variable or dimension in this dataset
我认为重命名只是维度名称的意思,但如果是这种情况,那么文档根本没有说清楚。
这可能是目前最简单的方法。目前它不如 pandas' 选项好,我们有兴趣让它更容易。特别是,我们对 PR 非常开放,根据您的反馈使文档更清晰。
In [8]: da = xr.DataArray([1,2,3], coords=dict(a=list('xyz')), dims=['a'])
In [10]: da
Out[10]:
<xarray.DataArray (a: 3)>
array([1, 2, 3])
Coordinates:
* a (a) <U1 'x' 'y' 'z'
In [9]: da.to_dataset('a').rename(x='x2').to_array('a')
Out[9]:
<xarray.DataArray (a: 3)>
array([1, 2, 3])
Coordinates:
* a (a) <U2 'x2' 'y' 'z'