Skip to content

Commit b45327f

Browse files
REGR: Fix inplace updates on column to set correct values (#35936)
1 parent 132e191 commit b45327f

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Regression in :meth:`DatetimeIndex.intersection` incorrectly raising ``AssertionError`` when intersecting against a list (:issue:`35876`)
18+
- Fix regression in updating a column inplace (e.g. using ``df['col'].fillna(.., inplace=True)``) (:issue:`35731`)
1819
- Performance regression for :meth:`RangeIndex.format` (:issue:`35712`)
1920
-
2021

pandas/core/internals/managers.py

+1
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ def iset(self, loc: Union[int, slice, np.ndarray], value):
10251025
Set new item in-place. Does not consolidate. Adds new Block if not
10261026
contained in the current set of items
10271027
"""
1028+
value = extract_array(value, extract_numpy=True)
10281029
# FIXME: refactor, clearly separate broadcasting & zip-like assignment
10291030
# can prob also fix the various if tests for sparse/categorical
10301031
if self._blklocs is None and self.ndim > 1:

pandas/tests/extension/test_numpy.py

+6
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ def test_fillna_frame(self, data_missing):
348348
# Non-scalar "scalar" values.
349349
super().test_fillna_frame(data_missing)
350350

351+
@pytest.mark.skip("Invalid test")
352+
def test_fillna_fill_other(self, data):
353+
# inplace update doesn't work correctly with patched extension arrays
354+
# extract_array returns PandasArray, while dtype is a numpy dtype
355+
super().test_fillna_fill_other(data_missing)
356+
351357

352358
class TestReshaping(BaseNumPyTests, base.BaseReshapingTests):
353359
@pytest.mark.skip("Incorrect parent test")

pandas/tests/frame/test_block_internals.py

+14
Original file line numberDiff line numberDiff line change
@@ -644,3 +644,17 @@ def test_to_dict_of_blocks_item_cache():
644644
assert df.loc[0, "b"] == "foo"
645645

646646
assert df["b"] is ser
647+
648+
649+
def test_update_inplace_sets_valid_block_values():
650+
# https://github.com/pandas-dev/pandas/issues/33457
651+
df = pd.DataFrame({"a": pd.Series([1, 2, None], dtype="category")})
652+
653+
# inplace update of a single column
654+
df["a"].fillna(1, inplace=True)
655+
656+
# check we havent put a Series into any block.values
657+
assert isinstance(df._mgr.blocks[0].values, pd.Categorical)
658+
659+
# smoketest for OP bug from GH#35731
660+
assert df.isnull().sum().sum() == 0

0 commit comments

Comments
 (0)