Skip to content

Commit 4bab74c

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
CLN: simplify interpolate_2d and callers (pandas-dev#36624)
1 parent ff4df0c commit 4bab74c

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ def fillna(self, value=None, method=None, limit=None):
16431643

16441644
# TODO: dispatch when self.categories is EA-dtype
16451645
values = np.asarray(self).reshape(-1, len(self))
1646-
values = interpolate_2d(values, method, 0, None, value).astype(
1646+
values = interpolate_2d(values, method, 0, None).astype(
16471647
self.categories.dtype
16481648
)[0]
16491649
codes = _get_codes_for_values(values, self.categories)

pandas/core/internals/blocks.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1181,12 +1181,15 @@ def interpolate(
11811181
m = None
11821182

11831183
if m is not None:
1184+
if fill_value is not None:
1185+
# similar to validate_fillna_kwargs
1186+
raise ValueError("Cannot pass both fill_value and method")
1187+
11841188
return self._interpolate_with_fill(
11851189
method=m,
11861190
axis=axis,
11871191
inplace=inplace,
11881192
limit=limit,
1189-
fill_value=fill_value,
11901193
coerce=coerce,
11911194
downcast=downcast,
11921195
)
@@ -1214,7 +1217,6 @@ def _interpolate_with_fill(
12141217
axis: int = 0,
12151218
inplace: bool = False,
12161219
limit: Optional[int] = None,
1217-
fill_value: Optional[Any] = None,
12181220
coerce: bool = False,
12191221
downcast: Optional[str] = None,
12201222
) -> List["Block"]:
@@ -1232,16 +1234,11 @@ def _interpolate_with_fill(
12321234

12331235
values = self.values if inplace else self.values.copy()
12341236

1235-
# We only get here for non-ExtensionBlock
1236-
fill_value = convert_scalar_for_putitemlike(fill_value, self.values.dtype)
1237-
12381237
values = missing.interpolate_2d(
12391238
values,
12401239
method=method,
12411240
axis=axis,
12421241
limit=limit,
1243-
fill_value=fill_value,
1244-
dtype=self.dtype,
12451242
)
12461243

12471244
blocks = [self.make_block_same_class(values, ndim=self.ndim)]

pandas/core/missing.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,6 @@ def interpolate_2d(
545545
method="pad",
546546
axis=0,
547547
limit=None,
548-
fill_value=None,
549-
dtype: Optional[DtypeObj] = None,
550548
):
551549
"""
552550
Perform an actual interpolation of values, values will be make 2-d if
@@ -563,18 +561,11 @@ def interpolate_2d(
563561
raise AssertionError("cannot interpolate on a ndim == 1 with axis != 0")
564562
values = values.reshape(tuple((1,) + values.shape))
565563

566-
if fill_value is None:
567-
mask = None
568-
else: # todo create faster fill func without masking
569-
mask = mask_missing(transf(values), fill_value)
570-
571564
method = clean_fill_method(method)
572565
if method == "pad":
573-
values = transf(pad_2d(transf(values), limit=limit, mask=mask, dtype=dtype))
566+
values = transf(pad_2d(transf(values), limit=limit))
574567
else:
575-
values = transf(
576-
backfill_2d(transf(values), limit=limit, mask=mask, dtype=dtype)
577-
)
568+
values = transf(backfill_2d(transf(values), limit=limit))
578569

579570
# reshape back
580571
if ndim == 1:

pandas/tests/series/methods/test_interpolate.py

+8
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ def test_interp_invalid_method(self, invalid_method):
340340
with pytest.raises(ValueError, match=msg):
341341
s.interpolate(method=invalid_method, limit=-1)
342342

343+
def test_interp_invalid_method_and_value(self):
344+
# GH#36624
345+
ser = Series([1, 3, np.nan, 12, np.nan, 25])
346+
347+
msg = "Cannot pass both fill_value and method"
348+
with pytest.raises(ValueError, match=msg):
349+
ser.interpolate(fill_value=3, method="pad")
350+
343351
def test_interp_limit_forward(self):
344352
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
345353

0 commit comments

Comments
 (0)