Skip to content

CLN: simplify interpolate_2d and callers #36624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ def fillna(self, value=None, method=None, limit=None):

# TODO: dispatch when self.categories is EA-dtype
values = np.asarray(self).reshape(-1, len(self))
values = interpolate_2d(values, method, 0, None, value).astype(
values = interpolate_2d(values, method, 0, None).astype(
self.categories.dtype
)[0]
codes = _get_codes_for_values(values, self.categories)
Expand Down
11 changes: 4 additions & 7 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,12 +1181,15 @@ def interpolate(
m = None

if m is not None:
if fill_value is not None:
# similar to validate_fillna_kwargs
raise ValueError("Cannot pass both fill_value and method")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for codecov?


return self._interpolate_with_fill(
method=m,
axis=axis,
inplace=inplace,
limit=limit,
fill_value=fill_value,
coerce=coerce,
downcast=downcast,
)
Expand Down Expand Up @@ -1214,7 +1217,6 @@ def _interpolate_with_fill(
axis: int = 0,
inplace: bool = False,
limit: Optional[int] = None,
fill_value: Optional[Any] = None,
coerce: bool = False,
downcast: Optional[str] = None,
) -> List["Block"]:
Expand All @@ -1232,16 +1234,11 @@ def _interpolate_with_fill(

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

# We only get here for non-ExtensionBlock
fill_value = convert_scalar_for_putitemlike(fill_value, self.values.dtype)

values = missing.interpolate_2d(
values,
method=method,
axis=axis,
limit=limit,
fill_value=fill_value,
dtype=self.dtype,
)

blocks = [self.make_block_same_class(values, ndim=self.ndim)]
Expand Down
13 changes: 2 additions & 11 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,6 @@ def interpolate_2d(
method="pad",
axis=0,
limit=None,
fill_value=None,
dtype: Optional[DtypeObj] = None,
):
"""
Perform an actual interpolation of values, values will be make 2-d if
Expand All @@ -563,18 +561,11 @@ def interpolate_2d(
raise AssertionError("cannot interpolate on a ndim == 1 with axis != 0")
values = values.reshape(tuple((1,) + values.shape))

if fill_value is None:
mask = None
else: # todo create faster fill func without masking
mask = mask_missing(transf(values), fill_value)

method = clean_fill_method(method)
if method == "pad":
values = transf(pad_2d(transf(values), limit=limit, mask=mask, dtype=dtype))
values = transf(pad_2d(transf(values), limit=limit))
else:
values = transf(
backfill_2d(transf(values), limit=limit, mask=mask, dtype=dtype)
)
values = transf(backfill_2d(transf(values), limit=limit))

# reshape back
if ndim == 1:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ def test_interp_invalid_method(self, invalid_method):
with pytest.raises(ValueError, match=msg):
s.interpolate(method=invalid_method, limit=-1)

def test_interp_invalid_method_and_value(self):
# GH#36624
ser = Series([1, 3, np.nan, 12, np.nan, 25])

msg = "Cannot pass both fill_value and method"
with pytest.raises(ValueError, match=msg):
ser.interpolate(fill_value=3, method="pad")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill_value is not in the Series.interpolate api. This is accepted through kwargs from the public api? Then fill_value is accepted and passed along internally? I assume none of the scipy interpolate functions accept fill_value? or won't in the future!

do we really need the ValueError if fill_value is passed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need the ValueError if fill_value is passed?

In master:

>>> ser = pd.Series([1, 2, np.nan, 4])
>>> ser.interpolate(method="pad", fill_value=99)
0    1.0
1    2.0
2    NaN
3    4.0
dtype: float64

I'm pretty confident this isn't intentional.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but on master and this PR

>>> pd.__version__
'1.2.0.dev0+499.g3b12293cea'
>>> ser = pd.Series([1, 2, np.nan, 4])
>>> ser.interpolate(fill_value=99)
0    1.0
1    2.0
2    3.0
3    4.0
dtype: float64

>>> ser.interpolate(humpty_dumpty=99)
0    1.0
1    2.0
2    3.0
3    4.0
dtype: float64

why the ValueError when method passed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. it may be that we can disallow fill_value at the top of Series.interpolate. making **kwargs explicit would be nice. But i haven't thoroughly checked if thats possible.
  2. BlockManager.interpolate also gets called from fillna, so we need to rule out fill_value at the place where this PR does it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks ok to me; i think we are accepting too much here accidently.

@jbrockmendel can you add a whatsnew note in any case (just saying passing invalid keyword args to .interpolate() will raise (or can do it after when all non-named keyword args are banned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so a ValueError may be appropriate for fill_na, but the test is for Series.interpolate. Why not TypeError: interpolate() got an unexpected keyword argument 'fill_value'?


def test_interp_limit_forward(self):
s = Series([1, 3, np.nan, np.nan, np.nan, 11])

Expand Down