Skip to content

BUG: loc.setitem modifying values with empty indexer #51193

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 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,7 @@ Indexing
- Bug in :meth:`Series.loc` raising error for out of bounds end of slice indexer (:issue:`50161`)
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
- Bug in :meth:`DataFrame.loc` raising ``IndexError`` when setting values for a pyarrow-backed column with a non-scalar indexer (:issue:`50085`)
- Bug in :meth:`DataFrame.loc` modifying object when setting incompatible value with an empty indexer (:issue:`45981`)
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`)
- Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when re-indexing ``columns`` and ``index`` (:issue:`48190`)
- Bug in :meth:`DataFrame.iloc` raising ``IndexError`` when indexer is a :class:`Series` with numeric extension array dtype (:issue:`49521`)
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ def is_null_slice(obj) -> bool:
)


def is_empty_slice(obj) -> bool:
"""
We have an empty slice, e.g. no values are selected.
"""
return (
isinstance(obj, slice)
and obj.start is not None
and obj.stop is not None
and obj.start == obj.stop
)


def is_true_slices(line) -> list[bool]:
"""
Find non-trivial slices in "line": return a list of booleans with same length.
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,13 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None:

is_full_setter = com.is_null_slice(pi) or com.is_full_slice(pi, len(self.obj))

if is_full_setter:
is_null_setter = com.is_empty_slice(pi) or is_array_like(pi) and len(pi) == 0

if is_null_setter:
# no-op, don't cast dtype later
return

elif is_full_setter:

try:
self.obj._mgr.column_setitem(
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ def test_getitem_bool_int_key():
ser.loc[0]


@pytest.mark.parametrize("val", [{}, {"b": "x"}])
@pytest.mark.parametrize("indexer", [[], [False, False], slice(0, -1), np.array([])])
def test_setitem_empty_indexer(indexer, val):
# GH#45981
df = DataFrame({"a": [1, 2], **val})
expected = df.copy()
df.loc[indexer] = 1.5
tm.assert_frame_equal(df, expected)


class TestDeprecatedIndexers:
@pytest.mark.parametrize("key", [{1}, {1: 1}])
def test_getitem_dict_and_set_deprecated(self, key):
Expand Down