-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 3 commits
9df9c89
6e0ec24
26b7dae
11d27b1
a527342
722cf9d
345b3c1
8c57c09
3b92d31
97203de
2f7bf13
eb3b204
9d59de4
b02e629
b27c825
8edc7d0
e57e407
6506df5
8cdef72
c14d7b2
01d7ffc
da94459
85120c9
bae602e
824c927
6871ae9
56f4f76
d334c7a
f12ef06
131d168
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
else: | ||
# e.g. we are bool dtype and value is nan | ||
# TODO: watch out for case with listlike value and scalar/empty indexer | ||
if is_list_like(value) and is_empty_indexer(indexer, np.array(value)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you put this as another elif case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return self | ||
dtype, _ = maybe_promote(np.array(value).dtype) | ||
return self.astype(dtype).setitem(indexer, value) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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