[英]Python Change Dimension and Coordinates Xarray Dataset
I have a xarray Dataset that looks like this below.我有一个如下所示的 xarray 数据集。 I need to be able to plot by latitude and longitude any of the three variables in "Data variables: si10, si10_u, avg".
我需要能够通过纬度和经度“数据变量:si10,si10_u,avg”中的三个变量中的任何一个来计算 plot。 However, I cannot figure out how to change the dimensions to latitude and longitude from index_id.
但是,我无法弄清楚如何将维度从 index_id 更改为纬度和经度。 Or, to delete "index_id" in Coordinates.
或者,删除坐标中的“index_id”。 I've tried that and then 'latitude' and 'longitude' disappear from "Coordinates".
我试过了,然后“纬度”和“经度”从“坐标”中消失了。 Thank you for suggestions.
谢谢你的建议。
Here is my xarray Dataset:这是我的 xarray 数据集:
<xarray.Dataset>
Dimensions: (index: 2448, index_id: 2448)
Coordinates:
* index_id (index_id) MultiIndex
- latitude (index_id) float64 58.0 58.0 58.0 58.0 ... 23.0 23.0 23.0 23.0
- longitude (index_id) float64 -130.0 -129.0 -128.0 ... -65.0 -64.0 -63.0
Dimensions without coordinates: index
Data variables:
si10 (index) float32 1.7636629 1.899161 ... 5.9699616 5.9121003
si10_u (index) float32 1.6784391 1.7533684 ... 6.13361 6.139127
avg (index) float32 1.721051 1.8262646 ... 6.0517855 6.025614
You have two issues.你有两个问题。 First, you need to replace
'index'
with 'index_id'
so your data is indexed consistently.首先,您需要将
'index'
替换为'index_id'
,以便您的数据得到一致的索引。 Second, to unstack 'index_id'
, you're looking for xr.Dataset.unstack
:其次,要取消堆叠
'index_id'
,您正在寻找xr.Dataset.unstack
:
ds = ds.unstack('index_id')
As an example... here's a dataset like yours举个例子......这是一个像你这样的数据集
In [16]: y = np.arange(58, 23, -1)
...: x = np.arange(-130, -63, 1)
In [17]: ds = xr.Dataset(
...: data_vars={
...: v: (("index",), np.random.random(len(x) * len(y)))
...: for v in ["si10", "si10_u", "avg"]
...: },
...: coords={
...: "index_id": pd.MultiIndex.from_product(
...: [y, x], names=["latitude", "longitude"],
...: ),
...: },
...: )
In [18]: ds
Out[18]:
<xarray.Dataset>
Dimensions: (index: 2345, index_id: 2345)
Coordinates:
* index_id (index_id) MultiIndex
- latitude (index_id) int64 58 58 58 58 58 58 58 58 ... 24 24 24 24 24 24 24
- longitude (index_id) int64 -130 -129 -128 -127 -126 ... -68 -67 -66 -65 -64
Dimensions without coordinates: index
Data variables:
si10 (index) float64 0.9412 0.7395 0.6843 ... 0.03979 0.4259 0.09203
si10_u (index) float64 0.7359 0.1984 0.5919 ... 0.5535 0.2867 0.4093
avg (index) float64 0.04257 0.1442 0.008705 ... 0.1911 0.2669 0.1498
First, reorganize your data to have consistent dims:首先,重新组织您的数据以具有一致的暗淡:
In [19]: index_id = ds['index_id']
In [20]: ds = (
...: ds.drop("index_id")
...: .rename({"index": "index_id"})
...: .assign_coords(index_id=index_id)
...: )
Then, ds.unstack
reorganizes the data to be the combinatorial product of all dimensions in the MultiIndex:然后,
ds.unstack
将数据重新组织为 MultiIndex 中所有维度的组合乘积:
In [21]: ds.unstack("index_id")
Out[21]:
<xarray.Dataset>
Dimensions: (latitude: 35, longitude: 67)
Coordinates:
* latitude (latitude) int64 24 25 26 27 28 29 30 31 ... 52 53 54 55 56 57 58
* longitude (longitude) int64 -130 -129 -128 -127 -126 ... -67 -66 -65 -64
Data variables:
si10 (latitude, longitude) float64 0.9855 0.1467 ... 0.6569 0.9479
si10_u (latitude, longitude) float64 0.4672 0.2664 ... 0.4894 0.128
avg (latitude, longitude) float64 0.3738 0.01793 ... 0.1264 0.21
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.