Skip to content

Commit 70c8534

Browse files
OckenfussPaul Ockenfussdcherian
authored
Fix missing 'dim' argument in _get_nan_block_lengths (#7598)
* Fix missing 'dim' argument in _get_nan_block_lengths * Add missing dim argument (GH7597) * Append a nan gap at the end of existing tests cases * Explicitly call 'dim' by keyword * Update Whats-new.rst * Preserve one row with valid values at the beginning * Update xarray/core/missing.py --------- Co-authored-by: Paul Ockenfuss <[email protected]> Co-authored-by: Deepak Cherian <[email protected]>
1 parent ccbb84d commit 70c8534

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ Bug fixes
5757
- Fix matplotlib raising a UserWarning when plotting a scatter plot
5858
with an unfilled marker (:issue:`7313`, :pull:`7318`).
5959
By `Jimmy Westling <https://github.com/illviljan>`_.
60+
- Fix issue with ``max_gap`` in ``interpolate_na``, when applied to
61+
multidimensional arrays. (:issue:`7597`, :pull:`7598`).
62+
By `Paul Ockenfuß <https://github.com/Ockenfuss>`_.
6063
- Improved performance in ``open_dataset`` for datasets with large object arrays (:issue:`7484`, :pull:`7494`).
6164
By `Alex Goodman <https://github.com/agoodm>`_ and `Deepak Cherian <https://github.com/dcherian>`_.
6265
- Fix :py:meth:`DataArray.plot.pcolormesh` which now works if one of the coordinates has str dtype (:issue:`6775`, :pull:`7612`).

xarray/core/missing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _get_nan_block_lengths(
4848
.where(valid)
4949
.bfill(dim=dim)
5050
.where(~valid, 0)
51-
.fillna(index[-1] - valid_arange.max())
51+
.fillna(index[-1] - valid_arange.max(dim=[dim]))
5252
)
5353

5454
return nan_block_lengths

xarray/tests/test_missing.py

+42-21
Original file line numberDiff line numberDiff line change
@@ -542,19 +542,28 @@ def test_bfill_dataset(ds):
542542

543543
@requires_bottleneck
544544
@pytest.mark.parametrize(
545-
"y, lengths",
545+
"y, lengths_expected",
546546
[
547-
[np.arange(9), [[3, 3, 3, 0, 3, 3, 0, 2, 2]]],
548-
[np.arange(9) * 3, [[9, 9, 9, 0, 9, 9, 0, 6, 6]]],
549-
[[0, 2, 5, 6, 7, 8, 10, 12, 14], [[6, 6, 6, 0, 4, 4, 0, 4, 4]]],
547+
[np.arange(9), [[1, 0, 7, 7, 7, 7, 7, 7, 0], [3, 3, 3, 0, 3, 3, 0, 2, 2]]],
548+
[
549+
np.arange(9) * 3,
550+
[[3, 0, 21, 21, 21, 21, 21, 21, 0], [9, 9, 9, 0, 9, 9, 0, 6, 6]],
551+
],
552+
[
553+
[0, 2, 5, 6, 7, 8, 10, 12, 14],
554+
[[2, 0, 12, 12, 12, 12, 12, 12, 0], [6, 6, 6, 0, 4, 4, 0, 4, 4]],
555+
],
550556
],
551557
)
552-
def test_interpolate_na_nan_block_lengths(y, lengths):
553-
arr = [[np.nan, np.nan, np.nan, 1, np.nan, np.nan, 4, np.nan, np.nan]]
554-
da = xr.DataArray(arr * 2, dims=["x", "y"], coords={"x": [0, 1], "y": y})
558+
def test_interpolate_na_nan_block_lengths(y, lengths_expected):
559+
arr = [
560+
[np.nan, 1, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 4],
561+
[np.nan, np.nan, np.nan, 1, np.nan, np.nan, 4, np.nan, np.nan],
562+
]
563+
da = xr.DataArray(arr, dims=["x", "y"], coords={"x": [0, 1], "y": y})
555564
index = get_clean_interp_index(da, dim="y", use_coordinate=True)
556565
actual = _get_nan_block_lengths(da, dim="y", index=index)
557-
expected = da.copy(data=lengths * 2)
566+
expected = da.copy(data=lengths_expected)
558567
assert_equal(actual, expected)
559568

560569

@@ -660,16 +669,17 @@ def test_interpolate_na_max_gap_time_specifier(
660669
"coords",
661670
[
662671
pytest.param(None, marks=pytest.mark.xfail()),
663-
{"x": np.arange(4), "y": np.arange(11)},
672+
{"x": np.arange(4), "y": np.arange(12)},
664673
],
665674
)
666675
def test_interpolate_na_2d(coords):
676+
n = np.nan
667677
da = xr.DataArray(
668678
[
669-
[1, 2, 3, 4, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
670-
[1, 2, 3, np.nan, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
671-
[1, 2, 3, np.nan, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
672-
[1, 2, 3, 4, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
679+
[1, 2, 3, 4, n, 6, n, n, n, 10, 11, n],
680+
[n, n, 3, n, n, 6, n, n, n, 10, n, n],
681+
[n, n, 3, n, n, 6, n, n, n, 10, n, n],
682+
[n, 2, 3, 4, n, 6, n, n, n, 10, 11, n],
673683
],
674684
dims=["x", "y"],
675685
coords=coords,
@@ -678,21 +688,32 @@ def test_interpolate_na_2d(coords):
678688
actual = da.interpolate_na("y", max_gap=2)
679689
expected_y = da.copy(
680690
data=[
681-
[1, 2, 3, 4, 5, 6, 7, np.nan, np.nan, np.nan, 11],
682-
[1, 2, 3, np.nan, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
683-
[1, 2, 3, np.nan, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
684-
[1, 2, 3, 4, 5, 6, 7, np.nan, np.nan, np.nan, 11],
691+
[1, 2, 3, 4, 5, 6, n, n, n, 10, 11, n],
692+
[n, n, 3, n, n, 6, n, n, n, 10, n, n],
693+
[n, n, 3, n, n, 6, n, n, n, 10, n, n],
694+
[n, 2, 3, 4, 5, 6, n, n, n, 10, 11, n],
685695
]
686696
)
687697
assert_equal(actual, expected_y)
688698

699+
actual = da.interpolate_na("y", max_gap=1, fill_value="extrapolate")
700+
expected_y_extra = da.copy(
701+
data=[
702+
[1, 2, 3, 4, n, 6, n, n, n, 10, 11, 12],
703+
[n, n, 3, n, n, 6, n, n, n, 10, n, n],
704+
[n, n, 3, n, n, 6, n, n, n, 10, n, n],
705+
[1, 2, 3, 4, n, 6, n, n, n, 10, 11, 12],
706+
]
707+
)
708+
assert_equal(actual, expected_y_extra)
709+
689710
actual = da.interpolate_na("x", max_gap=3)
690711
expected_x = xr.DataArray(
691712
[
692-
[1, 2, 3, 4, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
693-
[1, 2, 3, 4, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
694-
[1, 2, 3, 4, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
695-
[1, 2, 3, 4, np.nan, 6, 7, np.nan, np.nan, np.nan, 11],
713+
[1, 2, 3, 4, n, 6, n, n, n, 10, 11, n],
714+
[n, 2, 3, 4, n, 6, n, n, n, 10, 11, n],
715+
[n, 2, 3, 4, n, 6, n, n, n, 10, 11, n],
716+
[n, 2, 3, 4, n, 6, n, n, n, 10, 11, n],
696717
],
697718
dims=["x", "y"],
698719
coords=coords,

0 commit comments

Comments
 (0)