Skip to content

Commit 6939711

Browse files
committed
Bug: Raise ValueError with interpolate limit = 0
add whatsnew Add similar log to fillna Update whatsnew with issue and fillna change Add test for a negative limit
1 parent 0d3ecfa commit 6939711

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Other API Changes
241241
- ``CParserError`` has been renamed to ``ParserError`` in ``pd.read_csv`` and will be removed in the future (:issue:`12665`)
242242
- ``SparseArray.cumsum()`` and ``SparseSeries.cumsum()`` will now always return ``SparseArray`` and ``SparseSeries`` respectively (:issue:`12855`)
243243
- ``DataFrame.applymap()`` with an empty ``DataFrame`` will return a copy of the empty ``DataFrame`` instead of a ``Series`` (:issue:`8222`)
244+
- ``interpolate()`` and ``fillna()`` will raise a ``ValueError`` if the ``limit`` keyword argument is not greater than 0. (:issue:`9217`)
244245

245246
.. _whatsnew_0200.deprecations:
246247

pandas/core/generic.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -3239,7 +3239,7 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
32393239
a gap with more than this number of consecutive NaNs, it will only
32403240
be partially filled. If method is not specified, this is the
32413241
maximum number of entries along the entire axis where NaNs will be
3242-
filled.
3242+
filled. Must be greater than 0 if not None.
32433243
downcast : dict, default is None
32443244
a dict of item->dtype of what to downcast if possible,
32453245
or the string 'infer' which will try to downcast to an appropriate
@@ -3257,6 +3257,9 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
32573257
@Appender(_shared_docs['fillna'] % _shared_doc_kwargs)
32583258
def fillna(self, value=None, method=None, axis=None, inplace=False,
32593259
limit=None, downcast=None):
3260+
if is_integer(limit) and not limit > 0:
3261+
raise ValueError('`limit` keyword argument must be greater '
3262+
'than 0.')
32603263
if isinstance(value, (list, tuple)):
32613264
raise TypeError('"value" parameter must be a scalar or dict, but '
32623265
'you passed a "{0}"'.format(type(value).__name__))
@@ -3662,7 +3665,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
36623665
* 0: fill column-by-column
36633666
* 1: fill row-by-row
36643667
limit : int, default None.
3665-
Maximum number of consecutive NaNs to fill.
3668+
Maximum number of consecutive NaNs to fill. Must be greater than 0.
36663669
limit_direction : {'forward', 'backward', 'both'}, default 'forward'
36673670
If limit is specified, consecutive NaNs will be filled in this
36683671
direction.
@@ -3704,6 +3707,9 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
37043707
"""
37053708
Interpolate values according to different methods.
37063709
"""
3710+
if is_integer(limit) and not limit > 0:
3711+
raise ValueError("`limit` keyword argument must be greater "
3712+
"than 0.")
37073713

37083714
if self.ndim > 2:
37093715
raise NotImplementedError("Interpolate has not been implemented "

pandas/tests/series/test_missing.py

+10
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ def test_fillna_raise(self):
278278
self.assertRaises(TypeError, s.fillna, [1, 2])
279279
self.assertRaises(TypeError, s.fillna, (1, 2))
280280

281+
# related GH 9217, make sure limit is greater than 0
282+
for limit in [-1, 0]:
283+
s = Series([1, 2, 3, None])
284+
tm.assertRaises(ValueError, lambda: s.fillna(1, limit=limit))
285+
281286
def test_isnull_for_inf(self):
282287
s = Series(['a', np.inf, np.nan, 1.0])
283288
with pd.option_context('mode.use_inf_as_null', True):
@@ -730,6 +735,11 @@ def test_interp_limit(self):
730735
result = s.interpolate(method='linear', limit=2)
731736
assert_series_equal(result, expected)
732737

738+
# GH 9217
739+
for limit in [-1, 0]:
740+
s = pd.Series([1, 2, np.nan, np.nan, 5])
741+
tm.assertRaises(ValueError, lambda: s.interpolate(limit=limit))
742+
733743
def test_interp_limit_forward(self):
734744
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
735745

0 commit comments

Comments
 (0)