Skip to content

Commit 0c4bb89

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 0e219d7 commit 0c4bb89

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

doc/source/whatsnew/v0.20.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Other API Changes
246246
- ``SparseArray.cumsum()`` and ``SparseSeries.cumsum()`` will now always return ``SparseArray`` and ``SparseSeries`` respectively (:issue:`12855`)
247247
- ``DataFrame.applymap()`` with an empty ``DataFrame`` will return a copy of the empty ``DataFrame`` instead of a ``Series`` (:issue:`8222`)
248248
- ``.loc`` has compat with ``.ix`` for accepting iterators, and NamedTuples (:issue:`15120`)
249+
- ``interpolate()`` and ``fillna()`` will raise a ``ValueError`` if the ``limit`` keyword argument is not greater than 0. (:issue:`9217`)
249250
- ``pd.read_csv()`` will now issue a ``ParserWarning`` whenever there are conflicting values provided by the ``dialect`` parameter and the user (:issue:`14898`)
250251
- ``pd.read_csv()`` will now raise a ``ValueError`` for the C engine if the quote character is larger than than one byte (:issue:`11592`)
251252
- ``inplace`` arguments now require a boolean value, else a ``ValueError`` is thrown (:issue:`14189`)
@@ -369,4 +370,4 @@ Bug Fixes
369370
- Bug in ``Series`` constructor when both ``copy=True`` and ``dtype`` arguments are provided (:issue:`15125`)
370371
- Bug in ``pd.read_csv()`` for the C engine where ``usecols`` were being indexed incorrectly with ``parse_dates`` (:issue:`14792`)
371372

372-
- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
373+
- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)

pandas/core/generic.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -3254,7 +3254,7 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
32543254
a gap with more than this number of consecutive NaNs, it will only
32553255
be partially filled. If method is not specified, this is the
32563256
maximum number of entries along the entire axis where NaNs will be
3257-
filled.
3257+
filled. Must be greater than 0 if not None.
32583258
downcast : dict, default is None
32593259
a dict of item->dtype of what to downcast if possible,
32603260
or the string 'infer' which will try to downcast to an appropriate
@@ -3273,6 +3273,10 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
32733273
def fillna(self, value=None, method=None, axis=None, inplace=False,
32743274
limit=None, downcast=None):
32753275
inplace = validate_bool_kwarg(inplace, 'inplace')
3276+
3277+
if is_integer(limit):
3278+
assert limit > 0, ('`limit` keyword argument must be greater '
3279+
'than 0.')
32763280
if isinstance(value, (list, tuple)):
32773281
raise TypeError('"value" parameter must be a scalar or dict, but '
32783282
'you passed a "{0}"'.format(type(value).__name__))
@@ -3284,7 +3288,6 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
32843288
axis = 0
32853289
axis = self._get_axis_number(axis)
32863290
method = missing.clean_fill_method(method)
3287-
32883291
from pandas import DataFrame
32893292
if value is None:
32903293
if method is None:
@@ -3679,7 +3682,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
36793682
* 0: fill column-by-column
36803683
* 1: fill row-by-row
36813684
limit : int, default None.
3682-
Maximum number of consecutive NaNs to fill.
3685+
Maximum number of consecutive NaNs to fill. Must be greater than 0.
36833686
limit_direction : {'forward', 'backward', 'both'}, default 'forward'
36843687
If limit is specified, consecutive NaNs will be filled in this
36853688
direction.
@@ -3723,6 +3726,10 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
37233726
"""
37243727
inplace = validate_bool_kwarg(inplace, 'inplace')
37253728

3729+
if is_integer(limit):
3730+
assert limit > 0, ("`limit` keyword argument must be greater "
3731+
"than 0.")
3732+
37263733
if self.ndim > 2:
37273734
raise NotImplementedError("Interpolate has not been implemented "
37283735
"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)