Skip to content

Commit 84e83b1

Browse files
Improved error message for invalid construction (#35190)
1 parent 76bdaea commit 84e83b1

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,7 @@ Other
11701170
- Bug in :class:`Tick` comparisons raising ``TypeError`` when comparing against timedelta-like objects (:issue:`34088`)
11711171
- Bug in :class:`Tick` multiplication raising ``TypeError`` when multiplying by a float (:issue:`34486`)
11721172
- Passing a `set` as `names` argument to :func:`pandas.read_csv`, :func:`pandas.read_table`, or :func:`pandas.read_fwf` will raise ``ValueError: Names should be an ordered collection.`` (:issue:`34946`)
1173+
- Improved error message for invalid construction of list when creating a new index (:issue:`35190`)
11731174

11741175
.. ---------------------------------------------------------------------------
11751176

pandas/core/internals/construction.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,12 @@ def sanitize_index(data, index: Index):
744744
through a non-Index.
745745
"""
746746
if len(data) != len(index):
747-
raise ValueError("Length of values does not match length of index")
747+
raise ValueError(
748+
"Length of values "
749+
f"({len(data)}) "
750+
"does not match length of index "
751+
f"({len(index)})"
752+
)
748753

749754
if isinstance(data, np.ndarray):
750755

pandas/tests/extension/base/setitem.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ def test_setitem_expand_with_extension(self, data):
244244

245245
def test_setitem_frame_invalid_length(self, data):
246246
df = pd.DataFrame({"A": [1] * len(data)})
247-
xpr = "Length of values does not match length of index"
247+
xpr = (
248+
rf"Length of values \({len(data[:5])}\) "
249+
rf"does not match length of index \({len(df)}\)"
250+
)
248251
with pytest.raises(ValueError, match=xpr):
249252
df["B"] = data[:5]
250253

pandas/tests/frame/indexing/test_indexing.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,13 @@ def test_setitem_list(self, float_frame):
160160
msg = "Columns must be same length as key"
161161
with pytest.raises(ValueError, match=msg):
162162
data[["A"]] = float_frame[["A", "B"]]
163-
164-
msg = "Length of values does not match length of index"
163+
newcolumndata = range(len(data.index) - 1)
164+
msg = (
165+
rf"Length of values \({len(newcolumndata)}\) "
166+
rf"does not match length of index \({len(data)}\)"
167+
)
165168
with pytest.raises(ValueError, match=msg):
166-
data["A"] = range(len(data.index) - 1)
169+
data["A"] = newcolumndata
167170

168171
df = DataFrame(0, index=range(3), columns=["tt1", "tt2"], dtype=np.int_)
169172
df.loc[1, ["tt1", "tt2"]] = [1, 2]

pandas/tests/frame/indexing/test_setitem.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ def test_setitem_wrong_length_categorical_dtype_raises(self):
117117
cat = Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"])
118118
df = DataFrame(range(10), columns=["bar"])
119119

120-
msg = "Length of values does not match length of index"
120+
msg = (
121+
rf"Length of values \({len(cat)}\) "
122+
rf"does not match length of index \({len(df)}\)"
123+
)
121124
with pytest.raises(ValueError, match=msg):
122125
df["foo"] = cat
123126

0 commit comments

Comments
 (0)