在 xarray 中使用非索引坐标加入
Join using a non-index coord in xarray
我正在尝试在 xarray 中执行 'join',但在其中一项上使用了非索引坐标。我认为这应该不会太难,但我一直在旋转。
我在 'a'
上索引了一个数据集,在 'b'
上索引了一个数据集,在 'b'
上索引了一个 DataArray:
In [17]: ds=xr.Dataset(dict(a=(('x'),np.random.rand(10))), coords=dict(b=(('x'),list(range(10)))))
In [18]: ds
Out[18]:
<xarray.Dataset>
Dimensions: (x: 10)
Coordinates:
b (x) int64 0 1 2 3 4 5 6 7 8 9
Dimensions without coordinates: x
Data variables:
a (x) float64 0.3634 0.2132 0.6945 0.5359 0.1053 0.07045 0.5945 ...
In [19]: da=xr.DataArray(np.random.rand(10), dims=('b',), coords=dict(b=(('b'),list(range(10)))))
In [20]: da
Out[20]:
<xarray.DataArray (b: 10)>
array([0.796987, 0.275992, 0.747882, 0.240374, 0.435143, 0.285271, 0.753582,
0.556038, 0.365889, 0.434844])
Coordinates:
* b (b) int64 0 1 2 3 4 5 6 7 8 9
我可以通过加入 ds.b
等于 da.b
来将 da
添加到我的数据集吗?
这个和有点关系,但是我不想做任何运算,最后的结果应该是'a'
维度,而不是'b'
[=24] =]
谢谢!
编辑:根据@DSM 的要求,输出为:
<xarray.Dataset>
Dimensions: (x: 10)
Coordinates:
b (x) int64 0 1 2 3 4 5 6 7 8 9
Dimensions without coordinates: x
Data variables:
a (x) float64 0.3634 0.2132 0.6945 0.5359 0.1053 0.07045 0.5945 ...
da (x) float64 0.796987, 0.275992, 0.747882, 0.240374, 0.435143 ...
Edit2:这是一个带有字符串键的示例答案,其中 ds
只有键的子集。 (如果 da
有一个子集,我不认为这种方法有效,但额外的对齐可能会有所帮助)
In [23]: da=xr.DataArray(np.random.rand(5), dims=('b',), coords=dict(b=(('b'),list('edcba'))))
In [27]: da
Out[27]:
<xarray.DataArray (b: 5)>
array([0.174843, 0.953963, 0.092538, 0.749569, 0.780823])
Coordinates:
* b (b) <U1 'e' 'd' 'c' 'b' 'a'
In [25]: ds=xr.Dataset(dict(a=(('x'),np.random.rand(4))), coords=dict(b=(('x'),list('abcd'))))
In [24]: ds
Out[24]:
<xarray.Dataset>
Dimensions: (x: 5)
Coordinates:
b (x) <U1 'a' 'b' 'c' 'd' 'e'
Dimensions without coordinates: x
Data variables:
a (x) float64 0.7586 0.3529 0.5936 0.741 0.6344
In [26]: da.sel(b=ds.b)
Out[26]:
<xarray.DataArray (x: 4)>
array([0.780823, 0.749569, 0.092538, 0.953963])
Coordinates:
b (x) <U1 'a' 'b' 'c' 'd'
Dimensions without coordinates: x
如果您提供的不是 list/vector 个索引器,而是一个 DataArray,则该数组将重新整形为索引器的坐标:
In [5]: da.sel(b=ds.b)
Out[5]:
<xarray.DataArray (x: 10)>
array([0.327058, 0.904678, 0.455166, 0.67915 , 0.426856, 0.295434, 0.272206,
0.630101, 0.447915, 0.157343])
Coordinates:
b (x) int64 0 1 2 3 4 5 6 7 8 9
Dimensions without coordinates: x
您可以在作业中使用它在 ds.b
上加入 da
:
In [6]: ds['da'] = da.sel(b=ds.b)
In [7]: ds
Out[7]:
<xarray.Dataset>
Dimensions: (x: 10)
Coordinates:
b (x) int64 0 1 2 3 4 5 6 7 8 9
Dimensions without coordinates: x
Data variables:
a (x) float64 0.9338 0.9901 0.4498 0.49 0.3023 0.2622 0.03598 0.2 ...
da (x) float64 0.3271 0.9047 0.4552 0.6791 0.4269 0.2954 0.2722 ...
More advanced indexing 上的 xarray 文档提供了此功能的一些很好的示例。
我正在尝试在 xarray 中执行 'join',但在其中一项上使用了非索引坐标。我认为这应该不会太难,但我一直在旋转。
我在 'a'
上索引了一个数据集,在 'b'
上索引了一个数据集,在 'b'
上索引了一个 DataArray:
In [17]: ds=xr.Dataset(dict(a=(('x'),np.random.rand(10))), coords=dict(b=(('x'),list(range(10)))))
In [18]: ds
Out[18]:
<xarray.Dataset>
Dimensions: (x: 10)
Coordinates:
b (x) int64 0 1 2 3 4 5 6 7 8 9
Dimensions without coordinates: x
Data variables:
a (x) float64 0.3634 0.2132 0.6945 0.5359 0.1053 0.07045 0.5945 ...
In [19]: da=xr.DataArray(np.random.rand(10), dims=('b',), coords=dict(b=(('b'),list(range(10)))))
In [20]: da
Out[20]:
<xarray.DataArray (b: 10)>
array([0.796987, 0.275992, 0.747882, 0.240374, 0.435143, 0.285271, 0.753582,
0.556038, 0.365889, 0.434844])
Coordinates:
* b (b) int64 0 1 2 3 4 5 6 7 8 9
我可以通过加入 ds.b
等于 da.b
来将 da
添加到我的数据集吗?
这个和 谢谢! 编辑:根据@DSM 的要求,输出为: Edit2:这是一个带有字符串键的示例答案,其中 'a'
维度,而不是'b'
[=24] =]
<xarray.Dataset>
Dimensions: (x: 10)
Coordinates:
b (x) int64 0 1 2 3 4 5 6 7 8 9
Dimensions without coordinates: x
Data variables:
a (x) float64 0.3634 0.2132 0.6945 0.5359 0.1053 0.07045 0.5945 ...
da (x) float64 0.796987, 0.275992, 0.747882, 0.240374, 0.435143 ...
ds
只有键的子集。 (如果 da
有一个子集,我不认为这种方法有效,但额外的对齐可能会有所帮助)In [23]: da=xr.DataArray(np.random.rand(5), dims=('b',), coords=dict(b=(('b'),list('edcba'))))
In [27]: da
Out[27]:
<xarray.DataArray (b: 5)>
array([0.174843, 0.953963, 0.092538, 0.749569, 0.780823])
Coordinates:
* b (b) <U1 'e' 'd' 'c' 'b' 'a'
In [25]: ds=xr.Dataset(dict(a=(('x'),np.random.rand(4))), coords=dict(b=(('x'),list('abcd'))))
In [24]: ds
Out[24]:
<xarray.Dataset>
Dimensions: (x: 5)
Coordinates:
b (x) <U1 'a' 'b' 'c' 'd' 'e'
Dimensions without coordinates: x
Data variables:
a (x) float64 0.7586 0.3529 0.5936 0.741 0.6344
In [26]: da.sel(b=ds.b)
Out[26]:
<xarray.DataArray (x: 4)>
array([0.780823, 0.749569, 0.092538, 0.953963])
Coordinates:
b (x) <U1 'a' 'b' 'c' 'd'
Dimensions without coordinates: x
如果您提供的不是 list/vector 个索引器,而是一个 DataArray,则该数组将重新整形为索引器的坐标:
In [5]: da.sel(b=ds.b)
Out[5]:
<xarray.DataArray (x: 10)>
array([0.327058, 0.904678, 0.455166, 0.67915 , 0.426856, 0.295434, 0.272206,
0.630101, 0.447915, 0.157343])
Coordinates:
b (x) int64 0 1 2 3 4 5 6 7 8 9
Dimensions without coordinates: x
您可以在作业中使用它在 ds.b
上加入 da
:
In [6]: ds['da'] = da.sel(b=ds.b)
In [7]: ds
Out[7]:
<xarray.Dataset>
Dimensions: (x: 10)
Coordinates:
b (x) int64 0 1 2 3 4 5 6 7 8 9
Dimensions without coordinates: x
Data variables:
a (x) float64 0.9338 0.9901 0.4498 0.49 0.3023 0.2622 0.03598 0.2 ...
da (x) float64 0.3271 0.9047 0.4552 0.6791 0.4269 0.2954 0.2722 ...
More advanced indexing 上的 xarray 文档提供了此功能的一些很好的示例。