Skip to content

Commit 05b3a21

Browse files
authored
test_interpolate_pd_compat with range of fill_value's (#8189)
* ENH: test_interpolate_pd_compat with range of fill_value's * add whats-new.rst entry
1 parent a4f80b2 commit 05b3a21

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Internal Changes
108108
than `.reduce`, as the start of a broader effort to move non-reducing
109109
functions away from ```.reduce``, (:pull:`8114`).
110110
By `Maximilian Roos <https://github.com/max-sixty>`_.
111+
- Test range of fill_value's in test_interpolate_pd_compat (:issue:`8146`, :pull:`8189`).
112+
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
113+
111114

112115
.. _whats-new.2023.08.0:
113116

xarray/tests/test_missing.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,36 @@ def make_interpolate_example_data(shape, frac_nan, seed=12345, non_uniform=False
9292
return da, df
9393

9494

95+
@pytest.mark.parametrize("fill_value", [None, np.nan, 47.11])
96+
@pytest.mark.parametrize(
97+
"method", ["linear", "nearest", "zero", "slinear", "quadratic", "cubic"]
98+
)
9599
@requires_scipy
96-
def test_interpolate_pd_compat():
100+
def test_interpolate_pd_compat(method, fill_value) -> None:
97101
shapes = [(8, 8), (1, 20), (20, 1), (100, 100)]
98102
frac_nans = [0, 0.5, 1]
99-
methods = ["linear", "nearest", "zero", "slinear", "quadratic", "cubic"]
100103

101-
for shape, frac_nan, method in itertools.product(shapes, frac_nans, methods):
104+
for shape, frac_nan in itertools.product(shapes, frac_nans):
102105
da, df = make_interpolate_example_data(shape, frac_nan)
103106

104107
for dim in ["time", "x"]:
105-
actual = da.interpolate_na(method=method, dim=dim, fill_value=np.nan)
108+
actual = da.interpolate_na(method=method, dim=dim, fill_value=fill_value)
109+
# need limit_direction="both" here, to let pandas fill
110+
# in both directions instead of default forward direction only
106111
expected = df.interpolate(
107112
method=method,
108113
axis=da.get_axis_num(dim),
114+
limit_direction="both",
115+
fill_value=fill_value,
109116
)
110-
# Note, Pandas does some odd things with the left/right fill_value
111-
# for the linear methods. This next line inforces the xarray
112-
# fill_value convention on the pandas output. Therefore, this test
113-
# only checks that interpolated values are the same (not nans)
114-
expected.values[pd.isnull(actual.values)] = np.nan
117+
118+
if method == "linear":
119+
# Note, Pandas does not take left/right fill_value into account
120+
# for the numpy linear methods.
121+
# see https://github.com/pandas-dev/pandas/issues/55144
122+
# This aligns the pandas output with the xarray output
123+
expected.values[pd.isnull(actual.values)] = np.nan
124+
expected.values[actual.values == fill_value] = fill_value
115125

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

0 commit comments

Comments
 (0)