Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb01c72

Browse files
pums974Alexandre Poux
andauthored
Use complex nan by default when interpolating out of bounds (#6019)
* use complex nan by default when interpolating out of bounds * update whats-new.rst * remove unecessary complexity * analyse `dtype.kind` instead of using `np.iscomplexobj` Co-authored-by: Alexandre Poux <[email protected]>
1 parent 23d345b commit fb01c72

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ New Features
2626

2727
Breaking changes
2828
~~~~~~~~~~~~~~~~
29-
29+
- Use complex nan when interpolating complex values out of bounds by default (instead of real nan) (:pull:`6019`).
30+
By `Alexandre Poux <https://github.com/pums974>`_.
3031

3132
Deprecations
3233
~~~~~~~~~~~~

xarray/core/missing.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ def __init__(self, xi, yi, method="linear", fill_value=None, period=None):
8282
self._xi = xi
8383
self._yi = yi
8484

85+
nan = np.nan if yi.dtype.kind != "c" else np.nan + np.nan * 1j
86+
8587
if fill_value is None:
86-
self._left = np.nan
87-
self._right = np.nan
88+
self._left = nan
89+
self._right = nan
8890
elif isinstance(fill_value, Sequence) and len(fill_value) == 2:
8991
self._left = fill_value[0]
9092
self._right = fill_value[1]
@@ -143,10 +145,12 @@ def __init__(
143145
self.cons_kwargs = kwargs
144146
self.call_kwargs = {}
145147

148+
nan = np.nan if yi.dtype.kind != "c" else np.nan + np.nan * 1j
149+
146150
if fill_value is None and method == "linear":
147-
fill_value = np.nan, np.nan
151+
fill_value = nan, nan
148152
elif fill_value is None:
149-
fill_value = np.nan
153+
fill_value = nan
150154

151155
self.f = interp1d(
152156
xi,

xarray/tests/test_interp.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,3 +907,16 @@ def test_coord_attrs(x, expect_same_attrs):
907907

908908
has_same_attrs = ds.interp(x=x).x.attrs == base_attrs
909909
assert expect_same_attrs == has_same_attrs
910+
911+
912+
@requires_scipy
913+
def test_interp1d_complex_out_of_bounds():
914+
"""Ensure complex nans are used by default"""
915+
da = xr.DataArray(
916+
np.exp(0.3j * np.arange(4)),
917+
[("time", np.arange(4))],
918+
)
919+
920+
expected = da.interp(time=3.5, kwargs=dict(fill_value=np.nan + np.nan * 1j))
921+
actual = da.interp(time=3.5)
922+
assert_identical(actual, expected)

xarray/tests/test_missing.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,3 +687,25 @@ def test_interpolate_na_2d(coords):
687687
coords=coords,
688688
)
689689
assert_equal(actual, expected_x)
690+
691+
692+
@requires_scipy
693+
def test_interpolators_complex_out_of_bounds():
694+
"""Ensure complex nans are used for complex data"""
695+
696+
xi = np.array([-1, 0, 1, 2, 5], dtype=np.float64)
697+
yi = np.exp(1j * xi)
698+
x = np.array([-2, 1, 6], dtype=np.float64)
699+
700+
expected = np.array(
701+
[np.nan + np.nan * 1j, np.exp(1j), np.nan + np.nan * 1j], dtype=yi.dtype
702+
)
703+
704+
for method, interpolator in [
705+
("linear", NumpyInterpolator),
706+
("linear", ScipyInterpolator),
707+
]:
708+
709+
f = interpolator(xi, yi, method=method)
710+
actual = f(x)
711+
assert_array_equal(actual, expected)

0 commit comments

Comments
 (0)