Skip to content

Commit

Permalink
test_interpolate_pd_compat with range of fill_value's (pydata#8189)
Browse files Browse the repository at this point in the history
* ENH: test_interpolate_pd_compat with range of fill_value's
* add whats-new.rst entry
  • Loading branch information
kmuehlbauer authored Sep 24, 2023
1 parent a4f80b2 commit 05b3a21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ Internal Changes
than `.reduce`, as the start of a broader effort to move non-reducing
functions away from ```.reduce``, (:pull:`8114`).
By `Maximilian Roos <https://github.com/max-sixty>`_.
- Test range of fill_value's in test_interpolate_pd_compat (:issue:`8146`, :pull:`8189`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.


.. _whats-new.2023.08.0:

Expand Down
28 changes: 19 additions & 9 deletions xarray/tests/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,36 @@ def make_interpolate_example_data(shape, frac_nan, seed=12345, non_uniform=False
return da, df


@pytest.mark.parametrize("fill_value", [None, np.nan, 47.11])
@pytest.mark.parametrize(
"method", ["linear", "nearest", "zero", "slinear", "quadratic", "cubic"]
)
@requires_scipy
def test_interpolate_pd_compat():
def test_interpolate_pd_compat(method, fill_value) -> None:
shapes = [(8, 8), (1, 20), (20, 1), (100, 100)]
frac_nans = [0, 0.5, 1]
methods = ["linear", "nearest", "zero", "slinear", "quadratic", "cubic"]

for shape, frac_nan, method in itertools.product(shapes, frac_nans, methods):
for shape, frac_nan in itertools.product(shapes, frac_nans):
da, df = make_interpolate_example_data(shape, frac_nan)

for dim in ["time", "x"]:
actual = da.interpolate_na(method=method, dim=dim, fill_value=np.nan)
actual = da.interpolate_na(method=method, dim=dim, fill_value=fill_value)
# need limit_direction="both" here, to let pandas fill
# in both directions instead of default forward direction only
expected = df.interpolate(
method=method,
axis=da.get_axis_num(dim),
limit_direction="both",
fill_value=fill_value,
)
# Note, Pandas does some odd things with the left/right fill_value
# for the linear methods. This next line inforces the xarray
# fill_value convention on the pandas output. Therefore, this test
# only checks that interpolated values are the same (not nans)
expected.values[pd.isnull(actual.values)] = np.nan

if method == "linear":
# Note, Pandas does not take left/right fill_value into account
# for the numpy linear methods.
# see https://github.com/pandas-dev/pandas/issues/55144
# This aligns the pandas output with the xarray output
expected.values[pd.isnull(actual.values)] = np.nan
expected.values[actual.values == fill_value] = fill_value

np.testing.assert_allclose(actual.values, expected.values)

Expand Down

0 comments on commit 05b3a21

Please sign in to comment.