Skip to content

Commit e8f3593

Browse files
authored
BUG : Allow axis = 'columns' with limit and no method to work on fillna. (#42856)
* solves issue #40989 by raising NotImplementedError and modifying docs * Testing the NotImplementedError created to solve issue #40989 * pre-commit error fixed * added modified generic.py * added modified test_fillna.py * reverted changes for test_frame.py * added another test * added into bug fixes in whatsnew
1 parent 4268618 commit e8f3593

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ Indexing
303303

304304
Missing
305305
^^^^^^^
306-
-
306+
- Bug in :meth:`DataFrame.fillna` with limit and no method ignores axis='columns' or ``axis = 1`` (:issue:`40989`)
307307
-
308308

309309
MultiIndex

pandas/core/generic.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -6312,10 +6312,21 @@ def fillna(
63126312
return result if not inplace else None
63136313

63146314
elif not is_list_like(value):
6315-
new_data = self._mgr.fillna(
6316-
value=value, limit=limit, inplace=inplace, downcast=downcast
6317-
)
6315+
if not self._mgr.is_single_block and axis == 1:
6316+
6317+
result = self.T.fillna(value=value, limit=limit).T
6318+
6319+
# need to downcast here because of all of the transposes
6320+
result._mgr = result._mgr.downcast()
6321+
6322+
new_data = result
6323+
else:
6324+
6325+
new_data = self._mgr.fillna(
6326+
value=value, limit=limit, inplace=inplace, downcast=downcast
6327+
)
63186328
elif isinstance(value, ABCDataFrame) and self.ndim == 2:
6329+
63196330
new_data = self.where(self.notna(), value)._data
63206331
else:
63216332
raise ValueError(f"invalid fill value with a {type(value)}")

pandas/tests/frame/methods/test_fillna.py

+54
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,60 @@ def test_fillna_pos_args_deprecation(self):
574574
expected = DataFrame({"a": [1, 2, 3, 0]}, dtype=float)
575575
tm.assert_frame_equal(result, expected)
576576

577+
def test_fillna_with_columns_and_limit(self):
578+
# GH40989
579+
df = DataFrame(
580+
[
581+
[np.nan, 2, np.nan, 0],
582+
[3, 4, np.nan, 1],
583+
[np.nan, np.nan, np.nan, 5],
584+
[np.nan, 3, np.nan, 4],
585+
],
586+
columns=list("ABCD"),
587+
)
588+
result = df.fillna(axis=1, value=100, limit=1)
589+
result2 = df.fillna(axis=1, value=100, limit=2)
590+
591+
expected = DataFrame(
592+
{
593+
"A": Series([100, 3, 100, 100], dtype="float64"),
594+
"B": [2, 4, np.nan, 3],
595+
"C": [np.nan, 100, np.nan, np.nan],
596+
"D": Series([0, 1, 5, 4], dtype="float64"),
597+
},
598+
index=[0, 1, 2, 3],
599+
)
600+
expected2 = DataFrame(
601+
{
602+
"A": Series([100, 3, 100, 100], dtype="float64"),
603+
"B": Series([2, 4, 100, 3], dtype="float64"),
604+
"C": [100, 100, np.nan, 100],
605+
"D": Series([0, 1, 5, 4], dtype="float64"),
606+
},
607+
index=[0, 1, 2, 3],
608+
)
609+
610+
tm.assert_frame_equal(result, expected)
611+
tm.assert_frame_equal(result2, expected2)
612+
613+
def test_fillna_inplace_with_columns_limit_and_value(self):
614+
# GH40989
615+
df = DataFrame(
616+
[
617+
[np.nan, 2, np.nan, 0],
618+
[3, 4, np.nan, 1],
619+
[np.nan, np.nan, np.nan, 5],
620+
[np.nan, 3, np.nan, 4],
621+
],
622+
columns=list("ABCD"),
623+
)
624+
625+
expected = df.fillna(axis=1, value=100, limit=1)
626+
assert expected is not df
627+
628+
df.fillna(axis=1, value=100, limit=1, inplace=True)
629+
tm.assert_frame_equal(df, expected)
630+
577631

578632
def test_fillna_nonconsolidated_frame():
579633
# https://github.com/pandas-dev/pandas/issues/36495

0 commit comments

Comments
 (0)