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 16 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 @@ -587,6 +587,7 @@ Indexing
- 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.reindex` raising ``IndexingError`` wrongly for empty :class:`DataFrame` with ``tolerance`` not None or ``method="nearest"`` (:issue:`27315`)
- Bug in :meth:`DataFrame.loc.__setitem__` changing dtype when indexer was completely ``False`` (:issue:`37550`)

Missing
^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,10 @@ def _setitem_with_indexer_split_path(self, indexer, value):
if isinstance(value, ABCDataFrame):
self._setitem_with_indexer_frame_value(indexer, value)

elif lplane_indexer == 0:
# We get here in one case via .loc with a all-False mask
pass

elif np.ndim(value) == 2:
self._setitem_with_indexer_2d_value(indexer, value)

Expand All @@ -1683,10 +1687,6 @@ def _setitem_with_indexer_split_path(self, indexer, value):
"when setting with an iterable"
)

elif lplane_indexer == 0 and len(value) == len(self.obj.index):
Copy link
Member

Choose a reason for hiding this comment

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

i think we need to retain the check for len(value) since we only want to catch boolean masking (though clearly we dont have tests with a mismatched len(value))

probably also need to check that pi is a listlike with matching length too.

Copy link
Member

Choose a reason for hiding this comment

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

indexers.check_setitem_lengths may be useful here

Copy link
Member Author

Choose a reason for hiding this comment

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

What would we want to do with empty indexers then?

Copy link
Member Author

@phofl phofl Nov 17, 2020

Choose a reason for hiding this comment

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

The reason I thought this would be ok is, that empty indexers look here the same as all False boolean indexers. Value in is here empty too, so we have that mismatch

Copy link
Member

Choose a reason for hiding this comment

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

i think ser.loc[[]] = [1, 2, 3] should raise kind of like ser.loc[[1]] = [1, 2, 3, 4] would

Copy link
Member Author

Choose a reason for hiding this comment

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

Problem here is:

In case of [] we have the following parameters:

lplane_indexer=0
indexer = (array([], dtype=int64), array([1])
value = array([])

In case of [False] we get exactyl the same parametrization. We cant really decide here whether we got a boolean indexer or not.

Additionally,

df = DataFrame({"a": ["a"], "b": [1], "c": [1]})
df.loc[list([]), ["b"]] = 10 - df["c"]
df.loc[list([]), ["b"]] = 11

does not raise on master either. Was not introduced here

Copy link
Member

Choose a reason for hiding this comment

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

That is a tough one. Let's get another opinion. @jreback: what do you expect to happen with ser.loc[[]] = [1, 2, 3]?

Copy link
Member Author

Choose a reason for hiding this comment

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

ping @jreback ?

Copy link
Contributor

Choose a reason for hiding this comment

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

coming back to this, yeah i think if we don't have equal len indexer we should always raise, so @jbrockmendel example should raise

# We get here in one case via .loc with a all-False mask
pass

elif len(ilocs) == len(value):
# We are setting multiple columns in a single row.
for loc, v in zip(ilocs, value):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ def setitem(self, indexer, value):

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
14 changes: 14 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,17 @@ 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)

@pytest.mark.parametrize("func", [list, np.array, Series])
@pytest.mark.parametrize("value", [[], [False]])
Copy link
Member

Choose a reason for hiding this comment

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

it isnt clear to me that this is correct with []. why is it included?

Copy link
Member Author

Choose a reason for hiding this comment

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

Shouldn't an empty indexer do nothing to the underlying object?

def test_setitem_loc_only_false_indexer_dtype_changed(self, func, value):
# GH#37550
# Dtype is only changed when value to set is a Series and indexer is
# empty/bool all False
df = DataFrame({"a": ["a"], "b": [1], "c": [1]})
df.loc[func(value), ["b"]] = 10 - df["c"]
expected = DataFrame({"a": ["a"], "b": [1], "c": [1]})
tm.assert_frame_equal(df, expected)

df.loc[[False], ["b"]] = 9
Copy link
Member

Choose a reason for hiding this comment

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

should [False] be func(value) as above? if not, this might belong as a separate test

tm.assert_frame_equal(df, expected)