Skip to content

Commit 7b5d864

Browse files
committed
Updating error message and testcase
1 parent 89d5c7c commit 7b5d864

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

pandas/core/indexing.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,10 +1989,8 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
19891989
return self._setitem_with_indexer((pi, info_axis[0]), value[0])
19901990

19911991
raise ValueError(
1992-
f"Length mismatch when setting Dataframe with an iterable: "
1993-
f"{len(pi)} keys vs {len(value)} values. "
1994-
f"Keys: {pi}, Values: {value}. "
1995-
f"Both must have the same length."
1992+
f"Length mismatch when setting Dataframe with an iterable."
1993+
f"Keys: {pi}, Values: {value}"
19961994
)
19971995

19981996
elif lplane_indexer == 0 and len(value) == len(self.obj.index):
@@ -2020,10 +2018,8 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
20202018

20212019
else:
20222020
raise ValueError(
2023-
f"Length mismatch when setting Dataframe with an iterable: "
2024-
f"{len(pi)} keys vs {len(value)} values. "
2025-
f"Keys: {pi}, Values: {value}. "
2026-
f"Both must have the same length."
2021+
f"Length mismatch when setting Dataframe with an iterable."
2022+
f"Keys: {pi}, Values: {value}"
20272023
)
20282024

20292025
else:

pandas/tests/indexing/test_indexing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_setitem_ndarray_1d(self):
4646
df["bar"] = np.zeros(10, dtype=complex)
4747

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

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

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

@@ -1036,7 +1036,7 @@ def test_scalar_setitem_with_nested_value(value):
10361036
df = DataFrame({"A": [1, 2, 3]})
10371037
msg = "|".join(
10381038
[
1039-
"Must have equal len keys and value",
1039+
"Length mismatch when setting Dataframe with an iterable",
10401040
"setting an array element with a sequence",
10411041
]
10421042
)
@@ -1046,7 +1046,9 @@ def test_scalar_setitem_with_nested_value(value):
10461046
# TODO For object dtype this happens as well, but should we rather preserve
10471047
# the nested data and set as such?
10481048
df = DataFrame({"A": [1, 2, 3], "B": np.array([1, "a", "b"], dtype=object)})
1049-
with pytest.raises(ValueError, match="Must have equal len keys and value"):
1049+
with pytest.raises(
1050+
ValueError, match="Length mismatch when setting Dataframe with an iterable"
1051+
):
10501052
df.loc[0, "B"] = value
10511053
# if isinstance(value, np.ndarray):
10521054
# assert (df.loc[0, "B"] == value).all()

0 commit comments

Comments
 (0)