Skip to content

Commit f8b0b9f

Browse files
Inevitable-MarzipanWillAyd
authored andcommitted
BUG/TST: fillna limit checks and tests (#27077)
1 parent b80df7b commit f8b0b9f

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ Missing
693693

694694
- Fixed misleading exception message in :meth:`Series.interpolate` if argument ``order`` is required, but omitted (:issue:`10633`, :issue:`24014`).
695695
- Fixed class type displayed in exception message in :meth:`DataFrame.dropna` if invalid ``axis`` parameter passed (:issue:`25555`)
696+
- A ``ValueError`` will now be thrown by :meth:`DataFrame.fillna` when ``limit`` is not a positive integer (:issue:`27042`)
696697
-
697698

698699
MultiIndex

pandas/core/internals/blocks.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,6 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
344344
"""
345345
inplace = validate_bool_kwarg(inplace, 'inplace')
346346

347-
if not self._can_hold_na:
348-
if inplace:
349-
return self
350-
else:
351-
return self.copy()
352-
353347
mask = isna(self.values)
354348
if limit is not None:
355349
if not is_integer(limit):
@@ -361,6 +355,12 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
361355
"is currently limited to 2")
362356
mask[mask.cumsum(self.ndim - 1) > limit] = False
363357

358+
if not self._can_hold_na:
359+
if inplace:
360+
return self
361+
else:
362+
return self.copy()
363+
364364
# fillna, but if we cannot coerce, then try again as an ObjectBlock
365365
try:
366366
values, _ = self._try_coerce_args(self.values, value)

pandas/tests/frame/test_missing.py

+16
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,22 @@ def test_fillna_skip_certain_blocks(self):
516516
# it works!
517517
df.fillna(np.nan)
518518

519+
@pytest.mark.parametrize("type", [int, float])
520+
def test_fillna_positive_limit(self, type):
521+
df = DataFrame(np.random.randn(10, 4)).astype(type)
522+
523+
msg = "Limit must be greater than 0"
524+
with pytest.raises(ValueError, match=msg):
525+
df.fillna(0, limit=-5)
526+
527+
@pytest.mark.parametrize("type", [int, float])
528+
def test_fillna_integer_limit(self, type):
529+
df = DataFrame(np.random.randn(10, 4)).astype(type)
530+
531+
msg = "Limit must be an integer"
532+
with pytest.raises(ValueError, match=msg):
533+
df.fillna(0, limit=0.5)
534+
519535
def test_fillna_inplace(self):
520536
df = DataFrame(np.random.randn(10, 4))
521537
df[1][:4] = np.nan

0 commit comments

Comments
 (0)