Skip to content

Commit

Permalink
BUG: raise accurate exception from Series.interpolate (pandas-dev#24014)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmusolino committed Feb 9, 2019
1 parent 04df22f commit de5b5d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
7 changes: 2 additions & 5 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,8 @@ def check_int_bool(self, inplace):
fill_value=fill_value,
coerce=coerce,
downcast=downcast)
# try an interp method
try:
m = missing.clean_interp_method(method, **kwargs)
except ValueError:
m = None
# validate the interp method
m = missing.clean_interp_method(method, **kwargs)

if m is not None:
r = check_int_bool(self, inplace)
Expand Down
25 changes: 19 additions & 6 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,19 +1064,32 @@ def test_interp_limit(self):
# GH 9217, make sure limit is an int and greater than 0
methods = ['linear', 'time', 'index', 'values', 'nearest', 'zero',
'slinear', 'quadratic', 'cubic', 'barycentric', 'krogh',
'polynomial', 'spline', 'piecewise_polynomial', None,
'polynomial', 'spline', 'piecewise_polynomial',
'from_derivatives', 'pchip', 'akima']
s = pd.Series([1, 2, np.nan, np.nan, 5])
msg = (r"Limit must be greater than 0|"
"time-weighted interpolation only works on Series or"
r" DataFrames with a DatetimeIndex|"
r"invalid method '(polynomial|spline|None)' to interpolate|"
"Limit must be an integer")
r"Limit must be an integer|"
r"You must specify the order of the spline")
for limit in [-1, 0, 1., 2.]:
for method in methods:
with pytest.raises(ValueError, match=msg):
s.interpolate(limit=limit, method=method)

def test_interp_invalid_method(self):
s = Series([1, 3, np.nan, 12, np.nan, 25])

invalid_methods = [None, 'nonexistent_method']
for method in invalid_methods:
msg = "method must be one of.*\\. Got '{}' instead".format(method)
with pytest.raises(ValueError, match=msg):
s.interpolate(method=method)
# When an invalid method and invalid limit (such as -1) are
# provided, the error message reflects the invalid method.
with pytest.raises(ValueError, match=msg):
s.interpolate(method=method, limit=-1)

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

Expand Down Expand Up @@ -1277,7 +1290,7 @@ def test_interp_limit_no_nans(self):
@pytest.mark.parametrize("method", ['polynomial', 'spline'])
def test_no_order(self, method):
s = Series([0, 1, np.nan, 3])
msg = "invalid method '{}' to interpolate".format(method)
msg = "You must specify the order of the spline or polynomial"
with pytest.raises(ValueError, match=msg):
s.interpolate(method=method)

Expand Down Expand Up @@ -1315,10 +1328,10 @@ def test_spline_interpolation(self):

@td.skip_if_no_scipy
def test_spline_error(self):
# see gh-10633
# see GH-10633, GH-24014
s = pd.Series(np.arange(10) ** 2)
s[np.random.randint(0, 9, 3)] = np.nan
msg = "invalid method 'spline' to interpolate"
msg = "You must specify the order of the spline or polynomial"
with pytest.raises(ValueError, match=msg):
s.interpolate(method='spline')

Expand Down

0 comments on commit de5b5d2

Please sign in to comment.