Skip to content

Commit cfc1c92

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 change if statements to assert
1 parent be3f2ae commit cfc1c92

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ Other API Changes
323323
- ``SparseArray.cumsum()`` and ``SparseSeries.cumsum()`` will now always return ``SparseArray`` and ``SparseSeries`` respectively (:issue:`12855`)
324324
- ``DataFrame.applymap()`` with an empty ``DataFrame`` will return a copy of the empty ``DataFrame`` instead of a ``Series`` (:issue:`8222`)
325325
- ``.loc`` has compat with ``.ix`` for accepting iterators, and NamedTuples (:issue:`15120`)
326+
- ``interpolate()`` and ``fillna()`` will raise a ``ValueError`` if the ``limit`` keyword argument is not greater than 0. (:issue:`9217`)
326327
- ``pd.read_csv()`` will now issue a ``ParserWarning`` whenever there are conflicting values provided by the ``dialect`` parameter and the user (:issue:`14898`)
327328
- ``pd.read_csv()`` will now raise a ``ValueError`` for the C engine if the quote character is larger than than one byte (:issue:`11592`)
328329
- ``inplace`` arguments now require a boolean value, else a ``ValueError`` is thrown (:issue:`14189`)

pandas/core/generic.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -3248,7 +3248,7 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
32483248
a gap with more than this number of consecutive NaNs, it will only
32493249
be partially filled. If method is not specified, this is the
32503250
maximum number of entries along the entire axis where NaNs will be
3251-
filled.
3251+
filled. Must be greater than 0 if not None.
32523252
downcast : dict, default is None
32533253
a dict of item->dtype of what to downcast if possible,
32543254
or the string 'infer' which will try to downcast to an appropriate
@@ -3267,6 +3267,10 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
32673267
def fillna(self, value=None, method=None, axis=None, inplace=False,
32683268
limit=None, downcast=None):
32693269
inplace = validate_bool_kwarg(inplace, 'inplace')
3270+
3271+
if is_integer(limit):
3272+
assert limit > 0, ('`limit` keyword argument must be greater '
3273+
'than 0.')
32703274
if isinstance(value, (list, tuple)):
32713275
raise TypeError('"value" parameter must be a scalar or dict, but '
32723276
'you passed a "{0}"'.format(type(value).__name__))
@@ -3278,7 +3282,6 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
32783282
axis = 0
32793283
axis = self._get_axis_number(axis)
32803284
method = missing.clean_fill_method(method)
3281-
32823285
from pandas import DataFrame
32833286
if value is None:
32843287
if method is None:
@@ -3673,7 +3676,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
36733676
* 0: fill column-by-column
36743677
* 1: fill row-by-row
36753678
limit : int, default None.
3676-
Maximum number of consecutive NaNs to fill.
3679+
Maximum number of consecutive NaNs to fill. Must be greater than 0.
36773680
limit_direction : {'forward', 'backward', 'both'}, default 'forward'
36783681
If limit is specified, consecutive NaNs will be filled in this
36793682
direction.
@@ -3717,6 +3720,10 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
37173720
"""
37183721
inplace = validate_bool_kwarg(inplace, 'inplace')
37193722

3723+
if is_integer(limit):
3724+
assert limit > 0, ("`limit` keyword argument must be greater "
3725+
"than 0.")
3726+
37203727
if self.ndim > 2:
37213728
raise NotImplementedError("Interpolate has not been implemented "
37223729
"on Panel and Panel 4D objects.")

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(AssertionError, 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(AssertionError, 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)