[英]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.