1

Is there a vectorized way to extract 6 from air and 7 from wind?

ds = xr.merge([
    xr.DataArray([3, 5, 6], coords={'init': [0, 1, 2]}, dims='init', name='air'),
    xr.DataArray([8, 7, np.nan], coords={'init': [0, 1, 2]}, dims='init', name='wind')
])

The loop method is:

xr.merge([
    ds[var]
    .dropna('init', how='all')
    .isel(init=-1)
    .drop('init')
    for var in ds.data_vars
])

which returns:

<xarray.Dataset>
Dimensions:  ()
Data variables:
    air      int64 6
    wind     float64 7.0

But I don't want it to loop through each variable and use dropna (dropna takes long for big arrays) so I was wondering if there's an alternative.

Andrew
  • 507
  • 5
  • 16

1 Answers1

0

I think the most straightforward solution is to forward fill: ds.ffill('init').isel(init=-1)

Andrew
  • 507
  • 5
  • 16