Skip to content

BUG/TST: fillna limit checks and tests #27077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ Missing

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

MultiIndex
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,6 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
"""
inplace = validate_bool_kwarg(inplace, 'inplace')

if not self._can_hold_na:
if inplace:
return self
else:
return self.copy()

mask = isna(self.values)
if limit is not None:
if not is_integer(limit):
Expand All @@ -361,6 +355,12 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
"is currently limited to 2")
mask[mask.cumsum(self.ndim - 1) > limit] = False

if not self._can_hold_na:
if inplace:
return self
else:
return self.copy()

# fillna, but if we cannot coerce, then try again as an ObjectBlock
try:
values, _ = self._try_coerce_args(self.values, value)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,22 @@ def test_fillna_skip_certain_blocks(self):
# it works!
df.fillna(np.nan)

@pytest.mark.parametrize("type", [int, float])
def test_fillna_positive_limit(self, type):
df = DataFrame(np.random.randn(10, 4)).astype(type)

msg = "Limit must be greater than 0"
with pytest.raises(ValueError, match=msg):
df.fillna(0, limit=-5)

@pytest.mark.parametrize("type", [int, float])
def test_fillna_integer_limit(self, type):
df = DataFrame(np.random.randn(10, 4)).astype(type)

msg = "Limit must be an integer"
with pytest.raises(ValueError, match=msg):
df.fillna(0, limit=0.5)

def test_fillna_inplace(self):
df = DataFrame(np.random.randn(10, 4))
df[1][:4] = np.nan
Expand Down