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 4 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 @@ -465,6 +465,7 @@ Indexing
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`MultiIndex` with a level named "0" (:issue:`37194`)
- Bug in :meth:`Series.__getitem__` when using an unsigned integer array as an indexer giving incorrect results or segfaulting instead of raising ``KeyError`` (:issue:`37218`)
- Bug in :meth:`Index.where` incorrectly casting numeric values to strings (:issue:`37591`)
- Bug in :meth:`DataFrame.loc.__setitem__` changed dtype when indexer was completely ``False`` (:issue:`37550`)
Copy link
Member

Choose a reason for hiding this comment

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

changed -> changing

these should usually be participles

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, will follow this in the future


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 @@ -920,10 +920,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.array(value)):
Copy link
Member

Choose a reason for hiding this comment

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

any way to avoid the np.array call? i think that makes a copy if we have eg a list

Copy link
Member Author

@phofl phofl Nov 8, 2020

Choose a reason for hiding this comment

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

I don't think so, is_empty_indexer requires an ndarray, while value may be a list.

Copy link
Member

Choose a reason for hiding this comment

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

the test is for loc.__setitem__. is that (or i guess iloc.__setitem__) the only way we can get here? it may make sense to do this in setitem_with_indexer

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that would be possible I think.
Sport would be in

for i, idx in enumerate(indexer):

Ran the tests in indexing, frame/indexing and series/indexing locally, this seems to work. Let me know, if we should move this

Copy link
Contributor

Choose a reason for hiding this comment

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

you can use np.asarray, also can we check len of the indexer here first to short-cut?

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 used asarray. Indexer is a tuple containing an empty list, len does not work here unfortunately.

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
7 changes: 7 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ def test_iloc_setitem_bool_indexer(self, klass):
expected = DataFrame({"flag": ["x", "y", "z"], "value": [2, 3, 4]})
tm.assert_frame_equal(df, expected)

def test_setitem_only_false_indexer_dtype_changed(self):
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 put this below in the class TestDataFrameSetItemBooleanMask?

which part(s) of the line df.loc[[False], ["b"]] = 10 - df["c"] are relevant to the bugfix here? e.g. does it matter that the right hand side is a Series? Would this be testing the right thing if it were series.loc[[False]]?

Copy link
Member Author

Choose a reason for hiding this comment

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

df.loc[[False], ["b"]] = 10 did not trigger the error. We need a Series on the rhs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved the test

# GH: 37550
df = DataFrame({"a": ["a"], "b": [1], "c": [1]})
df.loc[[False], ["b"]] = 10 - df["c"]
expected = DataFrame({"a": ["a"], "b": [1], "c": [1]})
tm.assert_frame_equal(df, expected)


class TestDataFrameSetItemSlicing:
def test_setitem_slice_position(self):
Expand Down