Skip to content

Commit de5b5d2

Browse files
committed
BUG: raise accurate exception from Series.interpolate (pandas-dev#24014)
1 parent 04df22f commit de5b5d2

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

pandas/core/internals/blocks.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,8 @@ def check_int_bool(self, inplace):
11151115
fill_value=fill_value,
11161116
coerce=coerce,
11171117
downcast=downcast)
1118-
# try an interp method
1119-
try:
1120-
m = missing.clean_interp_method(method, **kwargs)
1121-
except ValueError:
1122-
m = None
1118+
# validate the interp method
1119+
m = missing.clean_interp_method(method, **kwargs)
11231120

11241121
if m is not None:
11251122
r = check_int_bool(self, inplace)

pandas/tests/series/test_missing.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,19 +1064,32 @@ def test_interp_limit(self):
10641064
# GH 9217, make sure limit is an int and greater than 0
10651065
methods = ['linear', 'time', 'index', 'values', 'nearest', 'zero',
10661066
'slinear', 'quadratic', 'cubic', 'barycentric', 'krogh',
1067-
'polynomial', 'spline', 'piecewise_polynomial', None,
1067+
'polynomial', 'spline', 'piecewise_polynomial',
10681068
'from_derivatives', 'pchip', 'akima']
10691069
s = pd.Series([1, 2, np.nan, np.nan, 5])
10701070
msg = (r"Limit must be greater than 0|"
10711071
"time-weighted interpolation only works on Series or"
10721072
r" DataFrames with a DatetimeIndex|"
1073-
r"invalid method '(polynomial|spline|None)' to interpolate|"
1074-
"Limit must be an integer")
1073+
r"Limit must be an integer|"
1074+
r"You must specify the order of the spline")
10751075
for limit in [-1, 0, 1., 2.]:
10761076
for method in methods:
10771077
with pytest.raises(ValueError, match=msg):
10781078
s.interpolate(limit=limit, method=method)
10791079

1080+
def test_interp_invalid_method(self):
1081+
s = Series([1, 3, np.nan, 12, np.nan, 25])
1082+
1083+
invalid_methods = [None, 'nonexistent_method']
1084+
for method in invalid_methods:
1085+
msg = "method must be one of.*\\. Got '{}' instead".format(method)
1086+
with pytest.raises(ValueError, match=msg):
1087+
s.interpolate(method=method)
1088+
# When an invalid method and invalid limit (such as -1) are
1089+
# provided, the error message reflects the invalid method.
1090+
with pytest.raises(ValueError, match=msg):
1091+
s.interpolate(method=method, limit=-1)
1092+
10801093
def test_interp_limit_forward(self):
10811094
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
10821095

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

@@ -1315,10 +1328,10 @@ def test_spline_interpolation(self):
13151328

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

0 commit comments

Comments
 (0)