Skip to content

Bug in iloc.setitem orienting IntegerArray into the wrong direction #41288

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 2 commits into from
May 5, 2021
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
2 changes: 1 addition & 1 deletion pandas/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def check_setitem_lengths(indexer, value, values) -> bool:
# a) not necessarily 1-D indexers, e.g. tuple
# b) boolean indexers e.g. BoolArray
if is_list_like(value):
if len(indexer) != len(value):
if len(indexer) != len(value) and values.ndim == 1:
Copy link
Member

Choose a reason for hiding this comment

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

would the ndim check here be necessary if EAs all supported 2D?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we are passing here through with something like

df = pd.DataFrame(data={
    'col1': [1, 2, 3, 4],
    'col2': [3, 4, 5, 6],
    'col3': [6, 7, 8, 9],
})
rhs = np.array([1, 2, 3])
df.iloc[[1]] = rhs

too. This should set row-wise, but the check assumes it sets column-wise

# boolean with truth values == len of the value is ok too
if not (
isinstance(indexer, np.ndarray)
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,12 +970,7 @@ def setitem(self, indexer, value):
values[indexer] = value

elif is_ea_value:
# GH#38952
if values.ndim == 1:
values[indexer] = value
else:
# TODO(EA2D): special case not needed with 2D EA
values[indexer] = value.to_numpy(values.dtype).reshape(-1, 1)
values[indexer] = value

else:
# error: Argument 1 to "setitem_datetimelike_compat" has incompatible type
Expand Down
20 changes: 14 additions & 6 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
PeriodDtype,
)

import pandas as pd
from pandas import (
Categorical,
DataFrame,
Expand Down Expand Up @@ -792,22 +793,29 @@ def test_setitem_slice_position(self):
tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize("indexer", [tm.setitem, tm.iloc])
@pytest.mark.parametrize("box", [Series, np.array, list])
@pytest.mark.parametrize("box", [Series, np.array, list, pd.array])
@pytest.mark.parametrize("n", [1, 2, 3])
def test_setitem_broadcasting_rhs(self, n, box, indexer):
def test_setitem_slice_indexer_broadcasting_rhs(self, n, box, indexer):
# GH#40440
# TODO: Add pandas array as box after GH#40933 is fixed
df = DataFrame([[1, 3, 5]] + [[2, 4, 6]] * n, columns=["a", "b", "c"])
indexer(df)[1:] = box([10, 11, 12])
expected = DataFrame([[1, 3, 5]] + [[10, 11, 12]] * n, columns=["a", "b", "c"])
tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize("box", [Series, np.array, list, pd.array])
@pytest.mark.parametrize("n", [1, 2, 3])
def test_setitem_list_indexer_broadcasting_rhs(self, n, box):
# GH#40440
df = DataFrame([[1, 3, 5]] + [[2, 4, 6]] * n, columns=["a", "b", "c"])
df.iloc[list(range(1, n + 1))] = box([10, 11, 12])
expected = DataFrame([[1, 3, 5]] + [[10, 11, 12]] * n, columns=["a", "b", "c"])
tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize("indexer", [tm.setitem, tm.iloc])
@pytest.mark.parametrize("box", [Series, np.array, list])
@pytest.mark.parametrize("box", [Series, np.array, list, pd.array])
@pytest.mark.parametrize("n", [1, 2, 3])
def test_setitem_broadcasting_rhs_mixed_dtypes(self, n, box, indexer):
def test_setitem_slice_broadcasting_rhs_mixed_dtypes(self, n, box, indexer):
# GH#40440
# TODO: Add pandas array as box after GH#40933 is fixed
df = DataFrame(
[[1, 3, 5], ["x", "y", "z"]] + [[2, 4, 6]] * n, columns=["a", "b", "c"]
)
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ def test_iloc_setitem_ea_inplace(self, frame_or_series, box, using_array_manager
else:
values = obj[0].values

obj.iloc[:2] = box(arr[2:])
if frame_or_series is Series:
obj.iloc[:2] = box(arr[2:])
else:
obj.iloc[:2, 0] = box(arr[2:])

expected = frame_or_series(np.array([3, 4, 3, 4], dtype="i8"))
tm.assert_equal(obj, expected)

Expand Down