Skip to content

Commit 80f2b8e

Browse files
Attempt better error for mismatched index, data
1 parent 56a7b3b commit 80f2b8e

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

pandas/core/internals/construction.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from pandas._libs import lib
1212
from pandas._typing import Axis, DtypeObj, Scalar
13+
from pandas.errors import MismatchedIndexValueError
1314

1415
from pandas.core.dtypes.cast import (
1516
construct_1d_arraylike_from_scalar,
@@ -743,10 +744,13 @@ def sanitize_index(data, index: Index):
743744
Sanitize an index type to return an ndarray of the underlying, pass
744745
through a non-Index.
745746
"""
746-
if len(data) > len(index):
747-
raise ValueError("Length of values is greater than length of index")
748-
if len(index) > len(data):
749-
raise ValueError("Length of index is greater than length of values")
747+
if len(data) != len(index):
748+
raise MismatchedIndexValueError(
749+
"Length of values "
750+
f"({len(data)}) "
751+
"does not match length of index "
752+
f"({len(index)})"
753+
)
750754

751755
if isinstance(data, np.ndarray):
752756

pandas/errors/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,9 @@ class InvalidIndexError(Exception):
208208
209209
.. versionadded:: 1.1.0
210210
"""
211+
212+
213+
class MismatchedIndexValueError(ValueError):
214+
"""
215+
Error raised when Index and Value are mismatched
216+
"""

pandas/tests/extension/base/setitem.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import pandas as pd
55
import pandas._testing as tm
66

7+
from pandas.errors import MismatchedIndexValueError
8+
79
from .base import BaseExtensionTests
810

911

@@ -243,9 +245,12 @@ def test_setitem_expand_with_extension(self, data):
243245
self.assert_frame_equal(result, expected)
244246

245247
def test_setitem_frame_invalid_length(self, data):
246-
df = pd.DataFrame({"A": [1] * len(data)})
247-
xpr = "Length of values does not match length of index"
248-
with pytest.raises(ValueError, match=xpr):
248+
vCount = len(data)
249+
df = pd.DataFrame({"A": [1] * vCount})
250+
xpr = (
251+
f"Length of values ({vCount}) does not match length of index (2)"
252+
)
253+
with pytest.raises(MismatchedIndexValueError, match=xpr):
249254
df["B"] = data[:5]
250255

251256
@pytest.mark.xfail(reason="GH#20441: setitem on extension types.")

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import pandas._testing as tm
2424
import pandas.core.common as com
2525
from pandas.core.indexing import IndexingError
26+
from pandas.errors import MismatchedIndexValueError
2627

2728
from pandas.tseries.offsets import BDay
2829

@@ -161,8 +162,8 @@ def test_setitem_list(self, float_frame):
161162
with pytest.raises(ValueError, match=msg):
162163
data[["A"]] = float_frame[["A", "B"]]
163164

164-
msg = "Length of values does not match length of index"
165-
with pytest.raises(ValueError, match=msg):
165+
msg = "Length of values (1) does not match length of index (2)"
166+
with pytest.raises(MismatchedIndexValueError, match=msg):
166167
data["A"] = range(len(data.index) - 1)
167168

168169
df = DataFrame(0, index=range(3), columns=["tt1", "tt2"], dtype=np.int_)

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pandas import Categorical, DataFrame, Index, Series, Timestamp, date_range
55
import pandas._testing as tm
66
from pandas.core.arrays import SparseArray
7+
from pandas.errors import MismatchedIndexValueError
78

89

910
class TestDataFrameSetItem:
@@ -106,8 +107,8 @@ def test_setitem_wrong_length_categorical_dtype_raises(self):
106107
cat = Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"])
107108
df = DataFrame(range(10), columns=["bar"])
108109

109-
msg = "Length of values does not match length of index"
110-
with pytest.raises(ValueError, match=msg):
110+
msg = "Length of values (10) does not match length of index (2)"
111+
with pytest.raises(MismatchedIndexValueError, match=msg):
111112
df["foo"] = cat
112113

113114
def test_setitem_with_sparse_value(self):

0 commit comments

Comments
 (0)