Skip to content

Commit 92e8a25

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 move limit check closer to where the variable is used
1 parent f93714b commit 92e8a25

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

doc/source/whatsnew/v0.20.0.txt

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

pandas/core/generic.py

Lines changed: 3 additions & 3 deletions
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,7 @@ 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+
32703271
if isinstance(value, (list, tuple)):
32713272
raise TypeError('"value" parameter must be a scalar or dict, but '
32723273
'you passed a "{0}"'.format(type(value).__name__))
@@ -3278,7 +3279,6 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
32783279
axis = 0
32793280
axis = self._get_axis_number(axis)
32803281
method = missing.clean_fill_method(method)
3281-
32823282
from pandas import DataFrame
32833283
if value is None:
32843284
if method is None:
@@ -3673,7 +3673,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
36733673
* 0: fill column-by-column
36743674
* 1: fill row-by-row
36753675
limit : int, default None.
3676-
Maximum number of consecutive NaNs to fill.
3676+
Maximum number of consecutive NaNs to fill. Must be greater than 0.
36773677
limit_direction : {'forward', 'backward', 'both'}, default 'forward'
36783678
If limit is specified, consecutive NaNs will be filled in this
36793679
direction.

pandas/core/internals.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ def fillna(self, value, limit=None, inplace=False, downcast=None,
372372
original_value = value
373373
mask = isnull(self.values)
374374
if limit is not None:
375+
if is_integer(limit):
376+
assert limit > 0, ('`limit` keyword argument must be greater '
377+
'than 0.')
375378
if self.ndim > 2:
376379
raise NotImplementedError("number of dimensions for 'fillna' "
377380
"is currently limited to 2")

pandas/core/missing.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
is_float_dtype, is_datetime64_dtype,
1313
is_datetime64tz_dtype, is_integer_dtype,
1414
_ensure_float64, is_scalar,
15-
needs_i8_conversion)
15+
needs_i8_conversion, is_integer)
1616
from pandas.types.missing import isnull
1717

1818

@@ -169,7 +169,10 @@ def _interp_limit(invalid, fw_limit, bw_limit):
169169
# the beginning (see issues #9218 and #10420)
170170
violate_limit = sorted(start_nans)
171171

172-
if limit:
172+
if limit is not None:
173+
if is_integer(limit):
174+
assert limit > 0, ('`limit` keyword argument must be greater '
175+
'than 0.')
173176
if limit_direction == 'forward':
174177
violate_limit = sorted(start_nans | set(_interp_limit(invalid,
175178
limit, 0)))

pandas/tests/series/test_missing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ def test_fillna_raise(self):
297297
self.assertRaises(TypeError, s.fillna, [1, 2])
298298
self.assertRaises(TypeError, s.fillna, (1, 2))
299299

300+
# related GH 9217, make sure limit is greater than 0
301+
for limit in [-1, 0]:
302+
s = Series([1, 2, 3, None])
303+
tm.assertRaises(AssertionError, lambda: s.fillna(1, limit=limit))
304+
300305
def test_fillna_nat(self):
301306
series = Series([0, 1, 2, tslib.iNaT], dtype='M8[ns]')
302307

@@ -867,6 +872,11 @@ def test_interp_limit(self):
867872
result = s.interpolate(method='linear', limit=2)
868873
assert_series_equal(result, expected)
869874

875+
# GH 9217
876+
for limit in [-1, 0]:
877+
s = pd.Series([1, 2, np.nan, np.nan, 5])
878+
tm.assertRaises(AssertionError, lambda: s.interpolate(limit=limit))
879+
870880
def test_interp_limit_forward(self):
871881
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
872882

0 commit comments

Comments
 (0)