Skip to content
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

Fix incosistent behavior of 2d-indexed loc-set to Series #59333 #60052

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
7 changes: 5 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2371,12 +2371,15 @@ 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 all(is_sequence(_) or isinstance(_, slice) for _ in indexer):
rhshadrach marked this conversation as resolved.
Show resolved Hide resolved
ser_values = ser.reindex(obj.axes[0][indexer[0]])._values

# single indexer
if len(indexer) > 1 and not multiindex_indexer:
len_indexer = len(indexer[1])
if isinstance(indexer[1], slice):
len_indexer = len(obj.loc[indexer[1]].axes[1])
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 you can use axes[1][indexer[1]] instead of .loc.

Copy link
Author

@anzber anzber Nov 5, 2024

Choose a reason for hiding this comment

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

I've made changes requested, but failed test appeared

else:
len_indexer = len(indexer[1])
ser_values = (
np.tile(ser_values, len_indexer).reshape(len_indexer, -1).T
)
Expand Down
40 changes: 40 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3297,3 +3297,43 @@ def test_loc_reindexing_of_empty_index(self):
df.loc[Series([False] * 4, index=df.index, name=0), 0] = df[0]
expected = DataFrame(index=[1, 1, 2, 2], data=["1", "1", "2", "2"])
tm.assert_frame_equal(df, expected)

def test_loc_set_series_to_multiple_columns(self):
anzber marked this conversation as resolved.
Show resolved Hide resolved
# GH 59933
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=list("ABC"))
df.loc[0:3, ["A", "B", "C"]] = Series([10, 20, 30])
expected = DataFrame([[10, 10, 10], [20, 20, 20]], columns=list("ABC"))
tm.assert_frame_equal(df, expected)

df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=list("ABC"))
df.loc[:, ["A", "B", "C"]] = Series([10, 20, 30])
expected = DataFrame([[10, 10, 10], [20, 20, 20]], columns=list("ABC"))
tm.assert_frame_equal(df, expected)

df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("ABC"))
df.loc[df["A"] > 0, ["A", "B", "C"]] = Series([10, 20, 30])
expected = DataFrame(
[[10, 10, 10], [20, 20, 20], [30, 30, 30]], columns=list("ABC")
)
tm.assert_frame_equal(df, expected)

df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("ABC"))
df.loc[:, ["A", "B", "C"]] = Series([10, 20, 30])
expected = DataFrame(
[[10, 10, 10], [20, 20, 20], [30, 30, 30]], columns=list("ABC")
)
tm.assert_frame_equal(df, expected)

df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("ABC"))
df.loc[0:4, ["A", "B", "C"]] = Series([10, 20, 30])
expected = DataFrame(
[[10, 10, 10], [20, 20, 20], [30, 30, 30]], columns=list("ABC")
)
tm.assert_frame_equal(df, expected)

df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("ABC"))
df.loc[:, "A":"C"] = Series([10, 20, 30])
expected = DataFrame(
[[10, 10, 10], [20, 20, 20], [30, 30, 30]], columns=list("ABC")
)
tm.assert_frame_equal(df, expected)