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 all 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.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ Indexing
- Bug in :meth:`DataFrame.loc.__getitem__` incorrectly raising ``KeyError`` when selecting a single column with a boolean key (:issue:`44322`).
- Bug in setting :meth:`DataFrame.iloc` with a single ``ExtensionDtype`` column and setting 2D values e.g. ``df.iloc[:] = df.values`` incorrectly raising (:issue:`44514`)
- Bug in indexing on columns with ``loc`` or ``iloc`` using a slice with a negative step with ``ExtensionDtype`` columns incorrectly raising (:issue:`44551`)
- Bug in :meth:`DataFrame.loc.__setitem__` changing dtype when indexer was completely ``False`` (:issue:`37550`)
- Bug in :meth:`IntervalIndex.get_indexer_non_unique` returning boolean mask instead of array of integers for a non unique and non monotonic index (:issue:`44084`)
- Bug in :meth:`IntervalIndex.get_indexer_non_unique` not handling targets of ``dtype`` 'object' with NaNs correctly (:issue:`44482`)
-
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,8 @@ def ravel(i):
# we have a frame, with multiple indexers on both axes; and a
# series, so need to broadcast (see GH5206)
if sum_aligners == self.ndim and all(is_sequence(_) for _ in indexer):
if is_empty_indexer(indexer[0], ser._values):
return ser._values.copy()
Copy link
Member

Choose a reason for hiding this comment

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

evidently it works, but this seems like a weird place to handle this. is my intuition wrong 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.

This ensures, that an indexer like (np.array([]), np.array([1])) is handled the same as (np.array([]), 1), hence I handled it here.

ser = ser.reindex(obj.axes[0][indexer[0]], copy=True)._values

# single indexer
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,33 @@ def test_setitem_boolean_mask(self, mask_type, float_frame):
expected.values[np.array(mask)] = np.nan
tm.assert_frame_equal(result, expected)

@pytest.mark.xfail(reason="Currently empty indexers are treated as all False")
@pytest.mark.parametrize("box", [list, np.array, Series])
def test_setitem_loc_empty_indexer_raises_with_non_empty_value(self, box):
# GH#37672
df = DataFrame({"a": ["a"], "b": [1], "c": [1]})
if box == Series:
Copy link
Member

Choose a reason for hiding this comment

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

nitpick "box is Series", i can change this in my next CLN branch

indexer = box([], dtype="object")
else:
indexer = box([])
msg = "Must have equal len keys and value when setting with an iterable"
with pytest.raises(ValueError, match=msg):
df.loc[indexer, ["b"]] = [1]

@pytest.mark.parametrize("box", [list, np.array, Series])
def test_setitem_loc_only_false_indexer_dtype_changed(self, box):
# 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]})
indexer = box([False])
df.loc[indexer, ["b"]] = 10 - df["c"]
expected = DataFrame({"a": ["a"], "b": [1], "c": [1]})
tm.assert_frame_equal(df, expected)

df.loc[indexer, ["b"]] = 9
tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize("indexer", [tm.setitem, tm.loc])
def test_setitem_boolean_mask_aligning(self, indexer):
# GH#39931
Expand Down