@@ -94,30 +94,37 @@ def clean_fill_method(method, allow_nearest=False):
94
94
return method
95
95
96
96
97
+ # interpolation methods that dispatch to np.interp
98
+
99
+ NP_METHODS = ["linear" , "time" , "index" , "values" ]
100
+
101
+ # interpolation methods that dispatch to _interpolate_scipy_wrapper
102
+
103
+ SP_METHODS = [
104
+ "nearest" ,
105
+ "zero" ,
106
+ "slinear" ,
107
+ "quadratic" ,
108
+ "cubic" ,
109
+ "barycentric" ,
110
+ "krogh" ,
111
+ "spline" ,
112
+ "polynomial" ,
113
+ "from_derivatives" ,
114
+ "piecewise_polynomial" ,
115
+ "pchip" ,
116
+ "akima" ,
117
+ "cubicspline" ,
118
+ ]
119
+
120
+
97
121
def clean_interp_method (method : str , ** kwargs ) -> str :
98
122
order = kwargs .get ("order" )
99
- valid = [
100
- "linear" ,
101
- "time" ,
102
- "index" ,
103
- "values" ,
104
- "nearest" ,
105
- "zero" ,
106
- "slinear" ,
107
- "quadratic" ,
108
- "cubic" ,
109
- "barycentric" ,
110
- "polynomial" ,
111
- "krogh" ,
112
- "piecewise_polynomial" ,
113
- "pchip" ,
114
- "akima" ,
115
- "spline" ,
116
- "from_derivatives" ,
117
- "cubicspline" ,
118
- ]
123
+
119
124
if method in ("spline" , "polynomial" ) and order is None :
120
125
raise ValueError ("You must specify the order of the spline or polynomial." )
126
+
127
+ valid = NP_METHODS + SP_METHODS
121
128
if method not in valid :
122
129
raise ValueError (f"method must be one of { valid } . Got '{ method } ' instead." )
123
130
@@ -180,8 +187,6 @@ def interpolate_1d(
180
187
Bounds_error is currently hardcoded to False since non-scipy ones don't
181
188
take it as an argument.
182
189
"""
183
- # Treat the original, non-scipy methods first.
184
-
185
190
invalid = isna (yvalues )
186
191
valid = ~ invalid
187
192
@@ -261,50 +266,32 @@ def interpolate_1d(
261
266
# sort preserve_nans and covert to list
262
267
preserve_nans = sorted (preserve_nans )
263
268
264
- xvalues = getattr (xvalues , "values" , xvalues )
265
269
yvalues = getattr (yvalues , "values" , yvalues )
266
270
result = yvalues .copy ()
267
271
268
- if method in ["linear" , "time" , "index" , "values" ]:
272
+ # xvalues to pass to NumPy/SciPy
273
+
274
+ xvalues = getattr (xvalues , "values" , xvalues )
275
+ if method == "linear" :
276
+ inds = xvalues
277
+ else :
278
+ inds = np .asarray (xvalues )
279
+
280
+ # hack for DatetimeIndex, #1646
281
+ if needs_i8_conversion (inds .dtype ):
282
+ inds = inds .view (np .int64 )
283
+
269
284
if method in ("values" , "index" ):
270
- inds = np .asarray (xvalues )
271
- # hack for DatetimeIndex, #1646
272
- if needs_i8_conversion (inds .dtype ):
273
- inds = inds .view (np .int64 )
274
285
if inds .dtype == np .object_ :
275
286
inds = lib .maybe_convert_objects (inds )
276
- else :
277
- inds = xvalues
287
+
288
+ if method in NP_METHODS :
278
289
# np.interp requires sorted X values, #21037
279
290
indexer = np .argsort (inds [valid ])
280
291
result [invalid ] = np .interp (
281
292
inds [invalid ], inds [valid ][indexer ], yvalues [valid ][indexer ]
282
293
)
283
- result [preserve_nans ] = np .nan
284
- return result
285
-
286
- sp_methods = [
287
- "nearest" ,
288
- "zero" ,
289
- "slinear" ,
290
- "quadratic" ,
291
- "cubic" ,
292
- "barycentric" ,
293
- "krogh" ,
294
- "spline" ,
295
- "polynomial" ,
296
- "from_derivatives" ,
297
- "piecewise_polynomial" ,
298
- "pchip" ,
299
- "akima" ,
300
- "cubicspline" ,
301
- ]
302
-
303
- if method in sp_methods :
304
- inds = np .asarray (xvalues )
305
- # hack for DatetimeIndex, #1646
306
- if issubclass (inds .dtype .type , np .datetime64 ):
307
- inds = inds .view (np .int64 )
294
+ else :
308
295
result [invalid ] = _interpolate_scipy_wrapper (
309
296
inds [valid ],
310
297
yvalues [valid ],
@@ -315,8 +302,9 @@ def interpolate_1d(
315
302
order = order ,
316
303
** kwargs ,
317
304
)
318
- result [preserve_nans ] = np .nan
319
- return result
305
+
306
+ result [preserve_nans ] = np .nan
307
+ return result
320
308
321
309
322
310
def _interpolate_scipy_wrapper (
0 commit comments