Skip to content

Updating error message to include more details about dataframe #61929

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,8 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
return self._setitem_with_indexer((pi, info_axis[0]), value[0])

raise ValueError(
"Must have equal len keys and value when setting with an iterable"
f"Length mismatch when setting Dataframe with an iterable. "
f"Keys: {pi}, Values: {value}"
)

elif lplane_indexer == 0 and len(value) == len(self.obj.index):
Expand Down Expand Up @@ -2017,7 +2018,8 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):

else:
raise ValueError(
"Must have equal len keys and value when setting with an iterable"
f"Length mismatch when setting Dataframe with an iterable. "
f"Keys: {pi}, Values: {value}"
)

else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ def test_setitem_loc_empty_indexer_raises_with_non_empty_value(self, box):
indexer = box([], dtype="object")
else:
indexer = box([])
msg = "Must have equal len keys and value when setting with an iterable"
msg = "Length mismatch when setting Dataframe with an iterable"
with pytest.raises(ValueError, match=msg):
df.loc[indexer, ["b"]] = [1]

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/multiindex/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def test_multiindex_assignment_single_dtype(self):
tm.assert_series_equal(df.loc[4, "c"], exp)

# invalid assignments
msg = "Must have equal len keys and value when setting with an iterable"
msg = "Length mismatch when setting Dataframe with an iterable"
with pytest.raises(ValueError, match=msg):
df.loc[4, "c"] = [0, 1, 2, 3]

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/multiindex/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def test_per_axis_per_level_setitem(self):
[[100], [100, 100]], dtype="int64"
)

msg = "Must have equal len keys and value when setting with an iterable"
msg = "Length mismatch when setting Dataframe with an iterable"
with pytest.raises(ValueError, match=msg):
df.loc[(slice(None), 1), (slice(None), ["foo"])] = np.array(
[100, 100, 100, 100], dtype="int64"
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_setitem_ndarray_1d(self):
df["bar"] = np.zeros(10, dtype=complex)

# invalid
msg = "Must have equal len keys and value when setting with an iterable"
msg = "Length mismatch when setting Dataframe with an iterable"
with pytest.raises(ValueError, match=msg):
df.loc[df.index[2:5], "bar"] = np.array([2.33j, 1.23 + 0.1j, 2.2, 1.0])

Expand All @@ -67,7 +67,7 @@ def test_setitem_ndarray_1d_2(self):
df["foo"] = np.zeros(10, dtype=np.float64)
df["bar"] = np.zeros(10, dtype=complex)

msg = "Must have equal len keys and value when setting with an iterable"
msg = "Length mismatch when setting Dataframe with an iterable"
with pytest.raises(ValueError, match=msg):
df[2:5] = np.arange(1, 4) * 1j

Expand Down Expand Up @@ -1036,7 +1036,7 @@ def test_scalar_setitem_with_nested_value(value):
df = DataFrame({"A": [1, 2, 3]})
msg = "|".join(
[
"Must have equal len keys and value",
"Length mismatch when setting Dataframe with an iterable",
"setting an array element with a sequence",
]
)
Expand All @@ -1046,7 +1046,9 @@ def test_scalar_setitem_with_nested_value(value):
# TODO For object dtype this happens as well, but should we rather preserve
# the nested data and set as such?
df = DataFrame({"A": [1, 2, 3], "B": np.array([1, "a", "b"], dtype=object)})
with pytest.raises(ValueError, match="Must have equal len keys and value"):
with pytest.raises(
ValueError, match="Length mismatch when setting Dataframe with an iterable"
):
df.loc[0, "B"] = value
# if isinstance(value, np.ndarray):
# assert (df.loc[0, "B"] == value).all()
Expand Down
Loading