Skip to content

Commit e261b24

Browse files
Attempt better error for mismatched index, data
1 parent 73f549a commit e261b24

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.errors import MismatchedIndexValueError
5+
46
import pandas as pd
5-
import pandas._testing as tm
7+
from pandas import _testing as tm
68

79
from .base import BaseExtensionTests
810

@@ -244,8 +246,10 @@ def test_setitem_expand_with_extension(self, data):
244246

245247
def test_setitem_frame_invalid_length(self, data):
246248
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):
249+
xpr = (
250+
f"Length of values \({len(data[:5])}\) "
251+
f"does not match length of index \({len(df)}\)")
252+
with pytest.raises(MismatchedIndexValueError, match=xpr):
249253
df["B"] = data[:5]
250254

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

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from pandas._libs import iNaT
8+
from pandas.errors import MismatchedIndexValueError
89

910
from pandas.core.dtypes.common import is_float_dtype, is_integer
1011

@@ -16,12 +17,12 @@
1617
MultiIndex,
1718
Series,
1819
Timestamp,
20+
_testing as tm,
1921
date_range,
2022
isna,
2123
notna,
2224
)
23-
import pandas._testing as tm
24-
import pandas.core.common as com
25+
from pandas.core import common as com
2526
from pandas.core.indexing import IndexingError
2627

2728
from pandas.tseries.offsets import BDay
@@ -160,10 +161,12 @@ def test_setitem_list(self, float_frame):
160161
msg = "Columns must be same length as key"
161162
with pytest.raises(ValueError, match=msg):
162163
data[["A"]] = float_frame[["A", "B"]]
163-
164-
msg = "Length of values does not match length of index"
165-
with pytest.raises(ValueError, match=msg):
166-
data["A"] = range(len(data.index) - 1)
164+
newcolumndata = range(len(data.index) - 1)
165+
msg = (
166+
f"Length of values \({len(newcolumndata)}\) "
167+
f"does not match length of index \({len(data)}\)")
168+
with pytest.raises(MismatchedIndexValueError, match=msg):
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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.errors import MismatchedIndexValueError
5+
46
from pandas.core.dtypes.dtypes import DatetimeTZDtype, IntervalDtype, PeriodDtype
57

68
from pandas import (
@@ -117,8 +119,11 @@ def test_setitem_wrong_length_categorical_dtype_raises(self):
117119
cat = Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"])
118120
df = DataFrame(range(10), columns=["bar"])
119121

120-
msg = "Length of values does not match length of index"
121-
with pytest.raises(ValueError, match=msg):
122+
msg = (
123+
f"Length of values \({len(cat)}\) "
124+
f"does not match length of index \({len(df)}\)"
125+
)
126+
with pytest.raises(MismatchedIndexValueError, match=msg):
122127
df["foo"] = cat
123128

124129
def test_setitem_with_sparse_value(self):

0 commit comments

Comments
 (0)