emax
emax

Reputation: 7255

Why do I get an error when I try to interpolate an xarray between dates?

I am trying to interpolate the values of an xarray called pop

pop

enter image description here

I am using the function xarray.interp

dates = pd.date_range('1990-01-01', '2020-01-01', freq='1Y')
popI = pop.interp(time=dates, kwargs={"fill_value": "extrapolate"})

but I get the following error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-75-1393bc257da7> in <module>
----> 1 popI = pop.interp(time=dates, kwargs={"fill_value": "extrapolate"})

/usr/lib/python3/dist-packages/xarray/core/dataset.py in interp(self, coords, method, assume_sorted, kwargs, method_non_numeric, **coords_kwargs)
   3163         if method in ["linear", "nearest"]:
   3164             for k, v in validated_indexers.items():
-> 3165                 obj, newidx = missing._localize(obj, {k: v})
   3166                 validated_indexers[k] = newidx[k]
   3167 

/usr/lib/python3/dist-packages/xarray/core/missing.py in _localize(var, indexes_coords)
    561     indexes = {}
    562     for dim, [x, new_x] in indexes_coords.items():
--> 563         minval = np.nanmin(new_x.values)
    564         maxval = np.nanmax(new_x.values)
    565         index = x.to_index()

<__array_function__ internals> in nanmin(*args, **kwargs)

/usr/lib/python3/dist-packages/numpy/lib/nanfunctions.py in nanmin(a, axis, out, keepdims)
    319         # which do not implement isnan (gh-9009), or fmin correctly (gh-8975)
    320         res = np.fmin.reduce(a, axis=axis, out=out, **kwargs)
--> 321         if np.isnan(res).any():
    322             warnings.warn("All-NaN slice encountered", RuntimeWarning,
    323                           stacklevel=3)

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Upvotes: 0

Views: 197

Answers (1)

Michael Delgado
Michael Delgado

Reputation: 15452

You're calling interp on a Dataset, which will always apply functions to all data variables. One of your data variable is a string array mollewide. This can't be interpolated. So you can either set this as a coordinate:

popI = pop.set_coords('mollewide').interp(time=dates, kwargs={"fill_value": "extrapolate"})

or you can only operate on the popDensity data variable:

popI = pop["popDensity"].interp(time=dates, kwargs={"fill_value": "extrapolate"})

Upvotes: 1

Related Questions