Interpolate an array with an nd indexer and `NaN` values.
()
| 403 | @requires_scipy |
| 404 | @pytest.mark.filterwarnings("ignore:All-NaN slice") |
| 405 | def test_interpolate_nd_with_nan() -> None: |
| 406 | """Interpolate an array with an nd indexer and `NaN` values.""" |
| 407 | |
| 408 | # Create indexer into `a` with dimensions (y, x) |
| 409 | x = [0, 1, 2] |
| 410 | y = [10, 20] |
| 411 | c = {"x": x, "y": y} |
| 412 | a = np.arange(6, dtype=float).reshape(2, 3) |
| 413 | a[0, 1] = np.nan |
| 414 | ia = xr.DataArray(a, dims=("y", "x"), coords=c) |
| 415 | |
| 416 | da = xr.DataArray([1, 2, 2], dims=("a"), coords={"a": [0, 2, 4]}) |
| 417 | out = da.interp(a=ia) |
| 418 | expected = xr.DataArray( |
| 419 | [[1.0, np.nan, 2.0], [2.0, 2.0, np.nan]], dims=("y", "x"), coords=c |
| 420 | ) |
| 421 | xr.testing.assert_allclose(out.drop_vars("a"), expected) |
| 422 | |
| 423 | db = 2 * da |
| 424 | ds = xr.Dataset({"da": da, "db": db}) |
| 425 | out2 = ds.interp(a=ia) |
| 426 | expected_ds = xr.Dataset({"da": expected, "db": 2 * expected}) |
| 427 | xr.testing.assert_allclose(out2.drop_vars("a"), expected_ds) |
| 428 | |
| 429 | |
| 430 | @requires_scipy |