简体   繁体   English

沿新维度组合 xarray 数据集变量而不扩展每个变量

[英]Combine xarray Dataset variables along a new dimension without expanding each one

I have an xarray with dimensions lat and lon.我有一个维度为纬度和经度的 xarray。 It includes 17 variables, each one corresponding with a different time step.它包括 17 个变量,每个变量对应不同的时间步长。 I would like to expand the dimensions to include time and reduce the number of variables to only one, with 17 time steps.我想扩展维度以包括时间并将变量的数量减少到只有一个,有 17 个时间步长。 I've tried implementing this with the following code, but I end up with n variables, with each variable including all the time steps and lat, long dimensions.我已经尝试使用以下代码实现它,但我最终得到了 n 个变量,每个变量都包括所有时间步长和纬度、经度维度。

The original dataset, ds:原始数据集,ds: DS

all_dates = ['2019-03-29','2019-05-10','2019-06-21','2019-07-19','2019-08-30','2019-10-11','2019-11-22','2020-01-03','2020-02-14','2020-03-27','2020-05-08','2020-06-19','2020-07-17','2020-08-28','2020-10-09','2021-10-08','2021-11-19']

date_list = pd.to_datetime(all_dates)
ds2 = ds.expand_dims(time=date_list)

The new dataset includes an array of 17 timesteps for each data variable, instead of one data variable with 17 time steps:新数据集包含每个数据变量的 17 个时间步的数组,而不是一个具有 17 个时间步的数据变量: DS2

Is there a way to add a time dimension, and reduce the dataset's n variables to include a single variable corresponding with the n time steps in the time dimension?有没有办法添加时间维度,并减少数据集的 n 个变量以包含与时间维度中的 n 个时间步长对应的单个变量?

You're looking for xr.Dataset.to_array .您正在寻找xr.Dataset.to_array expand_dims does exactly what it's doing in your post - it copies each array in your data so that they each include the full time dimnension. expand_dims的作用与它在您的帖子中所做的完全相同——它复制数据中的每个数组,以便它们每个都包含完整的时间维度。 But you're essentially looking to concatenate all arrays into a single one along the new dimension.但是您实际上是在寻找沿着新维度将所有 arrays 连接成一个。

# concatenate the arrays, assign the name "all_images", return to a Dataset,
# and assign the time dimension
ds.to_array("time", name="all_images").to_dataset().assign_coords(
    time=date_list
)

You could also do this with xr.concat by pulling out each array and concatenating them.您也可以使用xr.concat执行此操作,方法是拉出每个数组并将它们连接起来。 Using concat may be a more careful method to ensure the correct ordering since you're relabeling your arrays.使用 concat 可能是确保正确排序的更谨慎的方法,因为您要重新标记 arrays。

# concatenate each image with an explicit order while assigning
# them the coordinate from date_list
xr.concat(
    [ds[f"image{i}" for i in range(1, len(date_list) + 1)],
    dim=pd.Index(date_list, name="time"),
).to_dataset(name="all_images")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM