@@ -854,8 +854,23 @@ def test_series_pad_backfill_limit(self):
854
854
assert_series_equal (result , expected )
855
855
856
856
857
- class TestSeriesInterpolateData ():
857
+ @pytest .fixture (params = ['linear' , 'index' , 'values' , 'nearest' , 'slinear' ,
858
+ 'zero' , 'quadratic' , 'cubic' , 'barycentric' , 'krogh' ,
859
+ 'polynomial' , 'spline' , 'piecewise_polynomial' ,
860
+ 'from_derivatives' , 'pchip' , 'akima' , ])
861
+ def nontemporal_method (request ):
862
+ """ Fixture that returns an (method name, required kwargs) pair.
863
+
864
+ This fixture does not include method 'time' as a parameterization; that
865
+ method requires a Series with a DatetimeIndex, and is generally tested
866
+ separately from these non-temporal methods.
867
+ """
868
+ method = request .param
869
+ kwargs = dict (order = 1 ) if method in ('spline' , 'polynomial' ) else dict ()
870
+ return method , kwargs
871
+
858
872
873
+ class TestSeriesInterpolateData ():
859
874
def test_interpolate (self , datetime_series , string_series ):
860
875
ts = Series (np .arange (len (datetime_series ), dtype = float ),
861
876
datetime_series .index )
@@ -875,12 +890,12 @@ def test_interpolate(self, datetime_series, string_series):
875
890
time_interp = ord_ts_copy .interpolate (method = 'time' )
876
891
tm .assert_series_equal (time_interp , ord_ts )
877
892
878
- # try time interpolation on a non-TimeSeries
879
- # Only raises ValueError if there are NaNs.
880
- non_ts = string_series . copy ()
881
- non_ts [ 0 ] = np .NaN
882
- msg = ("time-weighted interpolation only works on Series or DataFrames "
883
- " with a DatetimeIndex" )
893
+ def test_interpolate_time_raises_for_non_timeseries ( self ):
894
+ # When method='time' is used on a non-TimeSeries that contains a null
895
+ # value, a ValueError should be raised.
896
+ non_ts = Series ([ 0 , 1 , 2 , np .NaN ])
897
+ msg = ("time-weighted interpolation only works on Series.* "
898
+ "with a DatetimeIndex" )
884
899
with pytest .raises (ValueError , match = msg ):
885
900
non_ts .interpolate (method = 'time' )
886
901
@@ -1061,21 +1076,35 @@ def test_interp_limit(self):
1061
1076
result = s .interpolate (method = 'linear' , limit = 2 )
1062
1077
assert_series_equal (result , expected )
1063
1078
1064
- # GH 9217, make sure limit is an int and greater than 0
1065
- methods = ['linear' , 'time' , 'index' , 'values' , 'nearest' , 'zero' ,
1066
- 'slinear' , 'quadratic' , 'cubic' , 'barycentric' , 'krogh' ,
1067
- 'polynomial' , 'spline' , 'piecewise_polynomial' , None ,
1068
- 'from_derivatives' , 'pchip' , 'akima' ]
1069
- s = pd .Series ([1 , 2 , np .nan , np .nan , 5 ])
1070
- msg = (r"Limit must be greater than 0|"
1071
- "time-weighted interpolation only works on Series or"
1072
- r" DataFrames with a DatetimeIndex|"
1073
- r"invalid method '(polynomial|spline|None)' to interpolate|"
1074
- "Limit must be an integer" )
1075
- for limit in [- 1 , 0 , 1. , 2. ]:
1076
- for method in methods :
1077
- with pytest .raises (ValueError , match = msg ):
1078
- s .interpolate (limit = limit , method = method )
1079
+ @pytest .mark .parametrize ("limit" , [- 1 , 0 ])
1080
+ def test_interpolate_invalid_nonpositive_limit (self , nontemporal_method ,
1081
+ limit ):
1082
+ # GH 9217: make sure limit is greater than zero.
1083
+ s = pd .Series ([1 , 2 , np .nan , 4 ])
1084
+ method , kwargs = nontemporal_method
1085
+ with pytest .raises (ValueError , match = "Limit must be greater than 0" ):
1086
+ s .interpolate (limit = limit , method = method , ** kwargs )
1087
+
1088
+ def test_interpolate_invalid_float_limit (self , nontemporal_method ):
1089
+ # GH 9217: make sure limit is an integer.
1090
+ s = pd .Series ([1 , 2 , np .nan , 4 ])
1091
+ method , kwargs = nontemporal_method
1092
+ limit = 2.0
1093
+ with pytest .raises (ValueError , match = "Limit must be an integer" ):
1094
+ s .interpolate (limit = limit , method = method , ** kwargs )
1095
+
1096
+ @pytest .mark .parametrize ("invalid_method" , [None , 'nonexistent_method' ])
1097
+ def test_interp_invalid_method (self , invalid_method ):
1098
+ s = Series ([1 , 3 , np .nan , 12 , np .nan , 25 ])
1099
+
1100
+ msg = "method must be one of.* Got '{}' instead" .format (invalid_method )
1101
+ with pytest .raises (ValueError , match = msg ):
1102
+ s .interpolate (method = invalid_method )
1103
+
1104
+ # When an invalid method and invalid limit (such as -1) are
1105
+ # provided, the error message reflects the invalid method.
1106
+ with pytest .raises (ValueError , match = msg ):
1107
+ s .interpolate (method = invalid_method , limit = - 1 )
1079
1108
1080
1109
def test_interp_limit_forward (self ):
1081
1110
s = Series ([1 , 3 , np .nan , np .nan , np .nan , 11 ])
@@ -1276,11 +1305,20 @@ def test_interp_limit_no_nans(self):
1276
1305
@td .skip_if_no_scipy
1277
1306
@pytest .mark .parametrize ("method" , ['polynomial' , 'spline' ])
1278
1307
def test_no_order (self , method ):
1308
+ # see GH-10633, GH-24014
1279
1309
s = Series ([0 , 1 , np .nan , 3 ])
1280
- msg = "invalid method '{}' to interpolate" . format ( method )
1310
+ msg = "You must specify the order of the spline or polynomial"
1281
1311
with pytest .raises (ValueError , match = msg ):
1282
1312
s .interpolate (method = method )
1283
1313
1314
+ @td .skip_if_no_scipy
1315
+ @pytest .mark .parametrize ('order' , [- 1 , - 1.0 , 0 , 0.0 , np .nan ])
1316
+ def test_interpolate_spline_invalid_order (self , order ):
1317
+ s = Series ([0 , 1 , np .nan , 3 ])
1318
+ msg = "order needs to be specified and greater than 0"
1319
+ with pytest .raises (ValueError , match = msg ):
1320
+ s .interpolate (method = 'spline' , order = order )
1321
+
1284
1322
@td .skip_if_no_scipy
1285
1323
def test_spline (self ):
1286
1324
s = Series ([1 , 2 , np .nan , 4 , 5 , np .nan , 7 ])
@@ -1313,19 +1351,6 @@ def test_spline_interpolation(self):
1313
1351
expected1 = s .interpolate (method = 'spline' , order = 1 )
1314
1352
assert_series_equal (result1 , expected1 )
1315
1353
1316
- @td .skip_if_no_scipy
1317
- def test_spline_error (self ):
1318
- # see gh-10633
1319
- s = pd .Series (np .arange (10 ) ** 2 )
1320
- s [np .random .randint (0 , 9 , 3 )] = np .nan
1321
- msg = "invalid method 'spline' to interpolate"
1322
- with pytest .raises (ValueError , match = msg ):
1323
- s .interpolate (method = 'spline' )
1324
-
1325
- msg = "order needs to be specified and greater than 0"
1326
- with pytest .raises (ValueError , match = msg ):
1327
- s .interpolate (method = 'spline' , order = 0 )
1328
-
1329
1354
def test_interp_timedelta64 (self ):
1330
1355
# GH 6424
1331
1356
df = Series ([1 , np .nan , 3 ],
0 commit comments