Skip to content

Commit c53df5f

Browse files
Backport PR #34407 on branch 1.1.x: REGR: revert "CLN: _consolidate_inplace less" / fix regression in fillna() (#38115)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 6845248 commit c53df5f

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

doc/source/whatsnew/v1.1.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
- Fixed regression in metadata propagation for ``groupby`` iterator (:issue:`37343`)
2222
- Fixed regression in indexing on a :class:`Series` with ``CategoricalDtype`` after unpickling (:issue:`37631`)
2323
- Fixed regression in ``df.groupby(..).rolling(..)`` with the resulting :class:`MultiIndex` when grouping by a label that is in the index (:issue:`37641`)
24+
- Fixed regression in :meth:`DataFrame.fillna` not filling ``NaN`` after other operations such as :meth:`DataFrame.pivot` (:issue:`36495`).
2425

2526
.. ---------------------------------------------------------------------------
2627

pandas/core/generic.py

+6
Original file line numberDiff line numberDiff line change
@@ -3484,6 +3484,8 @@ class animal locomotion
34843484
if axis == 1:
34853485
return self[key]
34863486

3487+
self._consolidate_inplace()
3488+
34873489
index = self.index
34883490
if isinstance(index, MultiIndex):
34893491
loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
@@ -6011,6 +6013,8 @@ def fillna(
60116013
inplace = validate_bool_kwarg(inplace, "inplace")
60126014
value, method = validate_fillna_kwargs(value, method)
60136015

6016+
self._consolidate_inplace()
6017+
60146018
# set the default here, so functions examining the signaure
60156019
# can detect if something was set (e.g. in groupby) (GH9221)
60166020
if axis is None:
@@ -6449,6 +6453,8 @@ def replace(
64496453
if not is_bool(regex) and to_replace is not None:
64506454
raise AssertionError("'to_replace' must be 'None' if 'regex' is not a bool")
64516455

6456+
self._consolidate_inplace()
6457+
64526458
if value is None:
64536459
# passing a single value that is scalar like
64546460
# when value is None (GH5319), for compat

pandas/core/internals/managers.py

+6
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ def apply(self: T, f, align_keys=None, **kwargs) -> T:
417417
def quantile(
418418
self,
419419
axis: int = 0,
420+
consolidate: bool = True,
420421
transposed: bool = False,
421422
interpolation="linear",
422423
qs=None,
@@ -430,6 +431,8 @@ def quantile(
430431
Parameters
431432
----------
432433
axis: reduction axis, default 0
434+
consolidate: bool, default True. Join together blocks having same
435+
dtype
433436
transposed: bool, default False
434437
we are holding transposed data
435438
interpolation : type of interpolation, default 'linear'
@@ -444,6 +447,9 @@ def quantile(
444447
# simplify some of the code here and in the blocks
445448
assert self.ndim >= 2
446449

450+
if consolidate:
451+
self._consolidate_inplace()
452+
447453
def get_axe(block, qs, axes):
448454
# Because Series dispatches to DataFrame, we will always have
449455
# block.ndim == 2

pandas/tests/frame/test_missing.py

+11
Original file line numberDiff line numberDiff line change
@@ -717,3 +717,14 @@ def test_fill_corner(self, float_frame, float_string_frame):
717717

718718
# TODO(wesm): unused?
719719
result = empty_float.fillna(value=0) # noqa
720+
721+
722+
def test_fillna_nonconsolidated_frame():
723+
# https://github.com/pandas-dev/pandas/issues/36495
724+
df = DataFrame(
725+
[[1, 1, 1, 1.0], [2, 2, 2, 2.0], [3, 3, 3, 3.0]],
726+
columns=["i1", "i2", "i3", "f1"],
727+
)
728+
df_nonconsol = df.pivot("i1", "i2")
729+
result = df_nonconsol.fillna(0)
730+
assert result.isna().sum().sum() == 0

0 commit comments

Comments
 (0)