Skip to content

Commit 27d6ec6

Browse files
committed
Bug: Raise ValueError with interpolate limit = 0
add whatsnew
1 parent 72786cc commit 27d6ec6

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

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

242243
.. _whatsnew_0200.deprecations:
243244

pandas/core/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3662,7 +3662,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
36623662
* 0: fill column-by-column
36633663
* 1: fill row-by-row
36643664
limit : int, default None.
3665-
Maximum number of consecutive NaNs to fill.
3665+
Maximum number of consecutive NaNs to fill. Must be greater than 0.
36663666
limit_direction : {'forward', 'backward', 'both'}, default 'forward'
36673667
If limit is specified, consecutive NaNs will be filled in this
36683668
direction.
@@ -3704,6 +3704,8 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
37043704
"""
37053705
Interpolate values according to different methods.
37063706
"""
3707+
if isinstance(limit, int) and not limit > 0:
3708+
raise ValueError("Interpolate `limit` must be greater than 0.")
37073709

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

pandas/tests/series/test_missing.py

+4
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@ def test_interp_limit(self):
730730
result = s.interpolate(method='linear', limit=2)
731731
assert_series_equal(result, expected)
732732

733+
# GH 9217
734+
s = pd.Series([1, 2, np.nan, np.nan, 5])
735+
tm.assertRaises(ValueError, lambda: s.interpolate(limit=0))
736+
733737
def test_interp_limit_forward(self):
734738
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
735739

0 commit comments

Comments
 (0)