Skip to content

Commit ce943b5

Browse files
authored
CLN: re-use sanitize_index (#38912)
1 parent 4a1d3d7 commit ce943b5

File tree

3 files changed

+5
-14
lines changed

3 files changed

+5
-14
lines changed

pandas/core/series.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
from pandas.core.indexes.timedeltas import TimedeltaIndex
9898
from pandas.core.indexing import check_bool_indexer
9999
from pandas.core.internals import SingleBlockManager
100+
from pandas.core.internals.construction import sanitize_index
100101
from pandas.core.shared_docs import _shared_docs
101102
from pandas.core.sorting import ensure_key_mapped, nargsort
102103
from pandas.core.strings import StringMethods
@@ -319,17 +320,7 @@ def __init__(
319320
data = [data]
320321
index = ibase.default_index(len(data))
321322
elif is_list_like(data):
322-
323-
# a scalar numpy array is list-like but doesn't
324-
# have a proper length
325-
try:
326-
if len(index) != len(data):
327-
raise ValueError(
328-
f"Length of passed values is {len(data)}, "
329-
f"index implies {len(index)}."
330-
)
331-
except TypeError:
332-
pass
323+
sanitize_index(data, index)
333324

334325
# create/copy the manager
335326
if isinstance(data, SingleBlockManager):

pandas/tests/extension/base/constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_dataframe_from_series(self, data):
7474
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
7575

7676
def test_series_given_mismatched_index_raises(self, data):
77-
msg = "Length of passed values is 3, index implies 5"
77+
msg = r"Length of values \(3\) does not match length of index \(5\)"
7878
with pytest.raises(ValueError, match=msg):
7979
pd.Series(data[:3], index=[0, 1, 2, 3, 4])
8080

pandas/tests/series/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def test_constructor_index_mismatch(self, input):
576576
# GH 19342
577577
# test that construction of a Series with an index of different length
578578
# raises an error
579-
msg = "Length of passed values is 3, index implies 4"
579+
msg = r"Length of values \(3\) does not match length of index \(4\)"
580580
with pytest.raises(ValueError, match=msg):
581581
Series(input, index=np.arange(4))
582582

@@ -592,7 +592,7 @@ def test_constructor_broadcast_list(self):
592592
# GH 19342
593593
# construction with single-element container and index
594594
# should raise
595-
msg = "Length of passed values is 1, index implies 3"
595+
msg = r"Length of values \(1\) does not match length of index \(3\)"
596596
with pytest.raises(ValueError, match=msg):
597597
Series(["foo"], index=["a", "b", "c"])
598598

0 commit comments

Comments
 (0)