@@ -92,26 +92,36 @@ def make_interpolate_example_data(shape, frac_nan, seed=12345, non_uniform=False
92
92
return da , df
93
93
94
94
95
+ @pytest .mark .parametrize ("fill_value" , [None , np .nan , 47.11 ])
96
+ @pytest .mark .parametrize (
97
+ "method" , ["linear" , "nearest" , "zero" , "slinear" , "quadratic" , "cubic" ]
98
+ )
95
99
@requires_scipy
96
- def test_interpolate_pd_compat () :
100
+ def test_interpolate_pd_compat (method , fill_value ) -> None :
97
101
shapes = [(8 , 8 ), (1 , 20 ), (20 , 1 ), (100 , 100 )]
98
102
frac_nans = [0 , 0.5 , 1 ]
99
- methods = ["linear" , "nearest" , "zero" , "slinear" , "quadratic" , "cubic" ]
100
103
101
- for shape , frac_nan , method in itertools .product (shapes , frac_nans , methods ):
104
+ for shape , frac_nan in itertools .product (shapes , frac_nans ):
102
105
da , df = make_interpolate_example_data (shape , frac_nan )
103
106
104
107
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
106
111
expected = df .interpolate (
107
112
method = method ,
108
113
axis = da .get_axis_num (dim ),
114
+ limit_direction = "both" ,
115
+ fill_value = fill_value ,
109
116
)
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
115
125
116
126
np .testing .assert_allclose (actual .values , expected .values )
117
127
0 commit comments