Skip to content

REGR: Fix inplace updates on column to set correct values #35936

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 3 commits into from
Aug 31, 2020
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/v1.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
- Regression in :meth:`DatetimeIndex.intersection` incorrectly raising ``AssertionError`` when intersecting against a list (:issue:`35876`)
- Fix regression in updating a column inplace (e.g. using ``df['col'].fillna(.., inplace=True)``) (:issue:`35731`)
- Performance regression for :meth:`RangeIndex.format` (:issue:`35712`)
-

Expand Down
1 change: 1 addition & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@ def iset(self, loc: Union[int, slice, np.ndarray], value):
Set new item in-place. Does not consolidate. Adds new Block if not
contained in the current set of items
"""
value = extract_array(value, extract_numpy=True)
# FIXME: refactor, clearly separate broadcasting & zip-like assignment
# can prob also fix the various if tests for sparse/categorical
if self._blklocs is None and self.ndim > 1:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/extension/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ def test_fillna_frame(self, data_missing):
# Non-scalar "scalar" values.
super().test_fillna_frame(data_missing)

@pytest.mark.skip("Invalid test")
def test_fillna_fill_other(self, data):
# inplace update doesn't work correctly with patched extension arrays
# extract_array returns PandasArray, while dtype is a numpy dtype
super().test_fillna_fill_other(data_missing)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit strange, but because I switched if isinstance(.., Series): value = value._value to extract_array, this test started to fail. That is because for test_numpy.py we patch the PandasArray to not be special cased, and then extract_array returns a PandasArray, even for a numpy dtype. And thus, in iset we set a PandasArray into a consolidated numpy block ..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah im not a fan of this patching, OK with xfailing



class TestReshaping(BaseNumPyTests, base.BaseReshapingTests):
@pytest.mark.skip("Incorrect parent test")
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,17 @@ def test_to_dict_of_blocks_item_cache():
assert df.loc[0, "b"] == "foo"

assert df["b"] is ser


def test_update_inplace_sets_valid_block_values():
# https://github.com/pandas-dev/pandas/issues/33457
df = pd.DataFrame({"a": pd.Series([1, 2, None], dtype="category")})

# inplace update of a single column
df["a"].fillna(1, inplace=True)

# check we havent put a Series into any block.values
assert isinstance(df._mgr.blocks[0].values, pd.Categorical)

# smoketest for OP bug from GH#35731
assert df.isnull().sum().sum() == 0