Skip to content

BUG: Fix bug in loc setitem changing the dtype when condition is False #37672

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 30 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9df9c89
Fix bug in setitem when indexer was only false changed dtype
phofl Nov 3, 2020
6e0ec24
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Nov 6, 2020
26b7dae
Add space
phofl Nov 6, 2020
11d27b1
Move condition to elif
phofl Nov 8, 2020
a527342
Move test
phofl Nov 8, 2020
722cf9d
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Nov 9, 2020
345b3c1
Adjust test
phofl Nov 9, 2020
8c57c09
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Nov 10, 2020
3b92d31
Fix whatsnew
phofl Nov 14, 2020
97203de
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Nov 14, 2020
2f7bf13
Make test more clear
phofl Nov 14, 2020
eb3b204
Use asarray
phofl Nov 15, 2020
9d59de4
Parametrize test
phofl Nov 16, 2020
b02e629
Move if condition
phofl Nov 17, 2020
b27c825
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Nov 17, 2020
8edc7d0
Remove import
phofl Nov 17, 2020
e57e407
Rename parametrization
phofl Nov 17, 2020
6506df5
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Nov 17, 2020
8cdef72
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Dec 21, 2020
c14d7b2
Move whatsnew
phofl Dec 21, 2020
01d7ffc
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Jan 21, 2021
da94459
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl Apr 17, 2021
85120c9
Fix dep warning
phofl Apr 17, 2021
bae602e
Fix corner case
phofl Apr 17, 2021
824c927
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl May 14, 2021
6871ae9
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl May 23, 2021
56f4f76
Merge remote-tracking branch 'upstream/master' into 37550
phofl Dec 10, 2021
d334c7a
Move check
phofl Dec 10, 2021
f12ef06
Split tests
phofl Dec 10, 2021
131d168
Merge remote-tracking branch 'upstream/master' into 37550
phofl Dec 10, 2021
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ Indexing
- Bug in :meth:`DataFrame.loc` returned requested key plus missing values when ``loc`` was applied to single level from :class:`MultiIndex` (:issue:`27104`)
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`CategoricalIndex` using a listlike indexer containing NA values (:issue:`37722`)
- Bug in :meth:`DataFrame.xs` ignored ``droplevel=False`` for columns (:issue:`19056`)
- Bug in :meth:`DataFrame.loc.__setitem__` changing dtype when indexer was completely ``False`` (:issue:`37550`)

Missing
^^^^^^^
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,10 +928,11 @@ def setitem(self, indexer, value):

elif lib.is_scalar(value) and not isna(value):
dtype, _ = infer_dtype_from_scalar(value, pandas_dtype=True)

elif is_list_like(value) and is_empty_indexer(indexer, np.asarray(value)):
Copy link
Member

Choose a reason for hiding this comment

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

im not sure this is the right place to catch this, since i think we are specifically interested in all-False boolean indexers coming through loc. does this get here via setitem_with_indexer, and if so, can you point out specifically which branch(es)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Through _setitem_with_indexer_split_path -> _setitem_with_indexer_2d_value -> _setitem_single_column

as mentioned in #37672 (comment) we could do that in setitem_with_indexer if this is better

Copy link
Member

Choose a reason for hiding this comment

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

In _setitem_with_indexer_split_path L 1686-1688 we have a no-op block that is for all-false boolean masks. could move it up to before the 2D check. would that do the trick?

Copy link
Member Author

Choose a reason for hiding this comment

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

One possible solution is moving up the lplane_indexer==0 condition and removing the value part. Probably the shortest and does not seem to break tests. Don't know if this kills behavior not tested, but if indexer has len=0, we probably do not want to set values anyway

return self
else:
# e.g. we are bool dtype and value is nan
# TODO: watch out for case with listlike value and scalar/empty indexer
# TODO: watch out for case with listlike value and scalar indexer
dtype, _ = maybe_promote(np.array(value).dtype)
return self.astype(dtype).setitem(indexer, value)

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,14 @@ def test_setitem_boolean_mask(self, mask_type, float_frame):
expected = df.copy()
expected.values[np.array(mask)] = np.nan
tm.assert_frame_equal(result, expected)

def test_setitem_loc_only_false_indexer_dtype_changed(self):
# GH#37550
# Dtype is only changed when value to set is a Series
df = DataFrame({"a": ["a"], "b": [1], "c": [1]})
df.loc[[False], ["b"]] = 10 - df["c"]
Copy link
Member

Choose a reason for hiding this comment

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

can you make clear either in the test name and/or comment that/whether the type of indexer for ["b"] is relevant

Copy link
Member Author

Choose a reason for hiding this comment

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

Added comment and parametrized test

expected = DataFrame({"a": ["a"], "b": [1], "c": [1]})
tm.assert_frame_equal(df, expected)

df.loc[[False], ["b"]] = 10 - 1
Copy link
Member

Choose a reason for hiding this comment

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

you can simplify to 9

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx

tm.assert_frame_equal(df, expected)