Skip to content

REGR: fillna not filling NaNs after pivot without explicitly listing pivot values #36667

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

Closed
Closed
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/v1.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Fixed regressions
- Fixed regression when adding a :meth:`timedelta_range` to a :class:``Timestamp`` raised an ``ValueError`` (:issue:`35897`)
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a tuple (:issue:`35534`)
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
- Fixed regression where :meth:`DataFrame.fillna` not filling ``NaN`` after :meth:`DataFrame.pivot` operation (:issue:`36495`)
- Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`, :issue:`35802`)
- Fixed regression in :meth:`DataFrame.replace` inconsistent replace when using a float in the replace method (:issue:`35376`)
- Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`, :issue:`36377`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ def apply(
result_blocks: List[Block] = []
# fillna: Series/DataFrame is responsible for making sure value is aligned

self._consolidate_inplace()

aligned_args = {k: kwargs[k] for k in align_keys}

for b in self.blocks:
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/frame/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,31 @@ def test_fill_corner(self, float_frame, float_string_frame):

# TODO(wesm): unused?
result = empty_float.fillna(value=0) # noqa

def test_fillna_after_pivot(self):
# https://github.com/pandas-dev/pandas/issues/36495
df = DataFrame(
[
[1, 1, 1, 1.0],
[2, 2, 2, 2.0],
[3, 3, 3, 3.0],
],
columns=["i1", "i2", "i3", "f1"],
)

result = df.pivot("i1", "i2").fillna(0)

expected = DataFrame(
[
[1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
[0.0, 2.0, 0.0, 0.0, 2.0, 0.0],
[0.0, 0.0, 3.0, 0.0, 0.0, 3.0],
],
index=pd.Int64Index([1, 2, 3], dtype="int64", name="i1"),
columns=pd.MultiIndex.from_tuples(
[("i3", 1), ("i3", 2), ("i3", 3), ("f1", 1), ("f1", 2), ("f1", 3)],
names=[None, "i2"],
),
)

tm.assert_frame_equal(result, expected)