Skip to content

Commit 2d5a4e7

Browse files
CLN: clean and deduplicate in core.missing.interpolate_1d (#34744)
* interpolate_1d returns function * CLN: clean and deduplicate in core.missing.interpolate_1d
1 parent 2e90c7f commit 2d5a4e7

File tree

1 file changed

+45
-57
lines changed

1 file changed

+45
-57
lines changed

pandas/core/missing.py

+45-57
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,37 @@ def clean_fill_method(method, allow_nearest=False):
9494
return method
9595

9696

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+
97121
def clean_interp_method(method: str, **kwargs) -> str:
98122
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+
119124
if method in ("spline", "polynomial") and order is None:
120125
raise ValueError("You must specify the order of the spline or polynomial.")
126+
127+
valid = NP_METHODS + SP_METHODS
121128
if method not in valid:
122129
raise ValueError(f"method must be one of {valid}. Got '{method}' instead.")
123130

@@ -180,8 +187,6 @@ def interpolate_1d(
180187
Bounds_error is currently hardcoded to False since non-scipy ones don't
181188
take it as an argument.
182189
"""
183-
# Treat the original, non-scipy methods first.
184-
185190
invalid = isna(yvalues)
186191
valid = ~invalid
187192

@@ -261,50 +266,32 @@ def interpolate_1d(
261266
# sort preserve_nans and covert to list
262267
preserve_nans = sorted(preserve_nans)
263268

264-
xvalues = getattr(xvalues, "values", xvalues)
265269
yvalues = getattr(yvalues, "values", yvalues)
266270
result = yvalues.copy()
267271

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+
269284
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)
274285
if inds.dtype == np.object_:
275286
inds = lib.maybe_convert_objects(inds)
276-
else:
277-
inds = xvalues
287+
288+
if method in NP_METHODS:
278289
# np.interp requires sorted X values, #21037
279290
indexer = np.argsort(inds[valid])
280291
result[invalid] = np.interp(
281292
inds[invalid], inds[valid][indexer], yvalues[valid][indexer]
282293
)
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:
308295
result[invalid] = _interpolate_scipy_wrapper(
309296
inds[valid],
310297
yvalues[valid],
@@ -315,8 +302,9 @@ def interpolate_1d(
315302
order=order,
316303
**kwargs,
317304
)
318-
result[preserve_nans] = np.nan
319-
return result
305+
306+
result[preserve_nans] = np.nan
307+
return result
320308

321309

322310
def _interpolate_scipy_wrapper(

0 commit comments

Comments
 (0)