Skip to content

Commit 6f041e6

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 Changes assertion to ValueError
1 parent 3d6fcdc commit 6f041e6

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

doc/source/whatsnew/v0.20.0.txt

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

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3262,7 +3262,7 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
32623262
a gap with more than this number of consecutive NaNs, it will only
32633263
be partially filled. If method is not specified, this is the
32643264
maximum number of entries along the entire axis where NaNs will be
3265-
filled.
3265+
filled. Must be greater than 0 if not None.
32663266
downcast : dict, default is None
32673267
a dict of item->dtype of what to downcast if possible,
32683268
or the string 'infer' which will try to downcast to an appropriate
@@ -3281,6 +3281,7 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
32813281
def fillna(self, value=None, method=None, axis=None, inplace=False,
32823282
limit=None, downcast=None):
32833283
inplace = validate_bool_kwarg(inplace, 'inplace')
3284+
32843285
if isinstance(value, (list, tuple)):
32853286
raise TypeError('"value" parameter must be a scalar or dict, but '
32863287
'you passed a "{0}"'.format(type(value).__name__))
@@ -3292,7 +3293,6 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
32923293
axis = 0
32933294
axis = self._get_axis_number(axis)
32943295
method = missing.clean_fill_method(method)
3295-
32963296
from pandas import DataFrame
32973297
if value is None:
32983298
if method is None:
@@ -3687,7 +3687,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
36873687
* 0: fill column-by-column
36883688
* 1: fill row-by-row
36893689
limit : int, default None.
3690-
Maximum number of consecutive NaNs to fill.
3690+
Maximum number of consecutive NaNs to fill. Must be greater than 0.
36913691
limit_direction : {'forward', 'backward', 'both'}, default 'forward'
36923692
If limit is specified, consecutive NaNs will be filled in this
36933693
direction.

pandas/core/internals.py

+3
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) and limit < 1:
376+
raise ValueError('`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

+5-2
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) and limit < 1:
174+
raise ValueError('`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

+10
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ def test_fillna_raise(self):
295295
self.assertRaises(TypeError, s.fillna, [1, 2])
296296
self.assertRaises(TypeError, s.fillna, (1, 2))
297297

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

@@ -865,6 +870,11 @@ def test_interp_limit(self):
865870
result = s.interpolate(method='linear', limit=2)
866871
assert_series_equal(result, expected)
867872

873+
# GH 9217
874+
for limit in [-1, 0]:
875+
s = pd.Series([1, 2, np.nan, np.nan, 5])
876+
tm.assertRaises(ValueError, lambda: s.interpolate(limit=limit))
877+
868878
def test_interp_limit_forward(self):
869879
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
870880

0 commit comments

Comments
 (0)