简体   繁体   English

如何使用 xarray 沿时间维度扩展数据变量?

[英]How do I expand a data variable along a time dimension using xarray?

I have a DataSet ds_object that looks like this:我有一个看起来像这样的 DataSet ds_object

ds_object
<xarray.Dataset>
Dimensions:    (time: 14392)
Coordinates:
* time       (time) datetime64[ns] 2021-08-28T00:00:02.14...
Data variables:
variable  .......

Where "variable" is:其中“变量”是:

<xarray.DataArray 'variable' ()>
array(0., dtype=float32)
Attributes:
   units:      count

I am trying to expand the zero-dimensional variable by time.我正在尝试按时间扩展零维variable So essentially, change 'variable' from a 0D to a 1D array, in the shape (1, 14392) .因此,本质上,将“变量”从 0D 更改为 1D 数组,形状为(1, 14392) variable would then have the same length as 'time', but with the singular value of variable repeated 14392 times. variable将具有与“时间”相同的长度,但variable的奇异值重复了14392次。

I was able to do this:我能够做到这一点:

variable = np.full((1,len(time)),variable)

Which gives variable the shape I need, but this goes back into ds_object as a coordinate variable for some reason:这为variable提供了我需要的形状,但是由于某种原因,这又回到了 ds_object 作为坐标变量:

ds_object = ds_object.assign(variable_new=(variable[0]))
print(ds_object)

<xarray.Dataset>
Dimensions: (time: 13164, variable_new: 13164)
Coordinates:
  * time                  (time) datetime64[ns] 2021-08-28T00:00:02.14...
  * variable_new          (variable_new) float32 0.0 0.0 ... 0.0
Data variables: (12/28)

Why does this happen?为什么会这样? How do I get variable_new added to ds_object as a new data variable?如何将 variable_new 作为新数据变量添加到ds_object

Creating a dummy dataset similar to yours:创建一个类似于您的虚拟数据集:

In [2]: ds = xr.Dataset(
   ...:     {'variable': ((), 0)},
   ...:     coords={'time': pd.date_range('2021-08-28', periods=1000, freq='D')},
   ...: )

In [3]: ds
Out[3]:
<xarray.Dataset>
Dimensions:   (time: 1000)
Coordinates:
  * time      (time) datetime64[ns] 2021-08-28 2021-08-29 ... 2024-05-23
Data variables:
    variable  int64 0

In [4]: ds['variable']
Out[4]:
<xarray.DataArray 'variable' ()>
array(0)

We can use xr.DataArray.expand_dims to broadcast the array into a new dimension:我们可以使用xr.DataArray.expand_dims将数组广播到一个新维度:

In [11]: ds['variable'] = ds['variable'].expand_dims(time=ds.time)

In [12]: ds
Out[12]:
<xarray.Dataset>
Dimensions:   (time: 1000)
Coordinates:
  * time      (time) datetime64[ns] 2021-08-28 2021-08-29 ... 2024-05-23
Data variables:
    variable  (time) int64 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0 0

Alternatively, you should also be able to do this assignment with xr.Dataset.assign :或者,您也应该能够使用xr.Dataset.assign进行此分配:

In [16]: ds = ds.assign(variable=ds['variable'].expand_dims(time=ds.time))

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

相关问题 如何细分/优化 xarray 数据集中的维度? - How do I subdivide/refine a dimension in an xarray DataSet? 沿维度 xarray 相乘/乘积 - Multiply/product along a dimension xarray xarray - 将 function 应用于 DataArray 的时间维度并将结果作为变量添加 - xarray - apply a function to the time dimension of a DataArray and add the result as a variable 如何添加时间维度并从一堆栅格创建 xarray 数据集/数据数组? - How to add time dimension and create an xarray dataset/data array from a stack of rasters? 如何使用 PyTorch 沿特定维度进行热编码? - How do I one hot encode along a specific dimension using PyTorch? xarray 沿切片维度删除元素 - xarray dropping elements along a sliced dimension 沿 xarray 中的单个维度对多个坐标进行分组 - groupby multiple coords along a single dimension in xarray 如何从xarray DataArray中删除不需要的空尺寸(挤压不起作用) - how do I remove unwanted empty dimension from xarray DataArray (squeeze doesn't work) 如何将一组 xarray 数据集变量转换为具有附加维度的单个变量 - How to transform a set of xarray dataset variables into a single variable with additional dimension xarray:重塑数据,拆分维 - xarray: reshape data, split dimension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM