简体   繁体   中英

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

I have a DataSet ds_object that looks like this:

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. So essentially, change 'variable' from a 0D to a 1D array, in the shape (1, 14392) . variable would then have the same length as 'time', but with the singular value of variable repeated 14392 times.

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:

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?

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:

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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