diff --git a/pandas/core/series.py b/pandas/core/series.py index 7f2039c998f53..f1217e97aef5d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -97,6 +97,7 @@ from pandas.core.indexes.timedeltas import TimedeltaIndex from pandas.core.indexing import check_bool_indexer from pandas.core.internals import SingleBlockManager +from pandas.core.internals.construction import sanitize_index from pandas.core.shared_docs import _shared_docs from pandas.core.sorting import ensure_key_mapped, nargsort from pandas.core.strings import StringMethods @@ -319,17 +320,7 @@ def __init__( data = [data] index = ibase.default_index(len(data)) elif is_list_like(data): - - # a scalar numpy array is list-like but doesn't - # have a proper length - try: - if len(index) != len(data): - raise ValueError( - f"Length of passed values is {len(data)}, " - f"index implies {len(index)}." - ) - except TypeError: - pass + sanitize_index(data, index) # create/copy the manager if isinstance(data, SingleBlockManager): diff --git a/pandas/tests/extension/base/constructors.py b/pandas/tests/extension/base/constructors.py index 5c9e5dcf3ae24..9dbfd2a5589c0 100644 --- a/pandas/tests/extension/base/constructors.py +++ b/pandas/tests/extension/base/constructors.py @@ -74,7 +74,7 @@ def test_dataframe_from_series(self, data): assert isinstance(result._mgr.blocks[0], ExtensionBlock) def test_series_given_mismatched_index_raises(self, data): - msg = "Length of passed values is 3, index implies 5" + msg = r"Length of values \(3\) does not match length of index \(5\)" with pytest.raises(ValueError, match=msg): pd.Series(data[:3], index=[0, 1, 2, 3, 4]) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index c7bd38bbd00b9..e35f37944e7da 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -576,7 +576,7 @@ def test_constructor_index_mismatch(self, input): # GH 19342 # test that construction of a Series with an index of different length # raises an error - msg = "Length of passed values is 3, index implies 4" + msg = r"Length of values \(3\) does not match length of index \(4\)" with pytest.raises(ValueError, match=msg): Series(input, index=np.arange(4)) @@ -592,7 +592,7 @@ def test_constructor_broadcast_list(self): # GH 19342 # construction with single-element container and index # should raise - msg = "Length of passed values is 1, index implies 3" + msg = r"Length of values \(1\) does not match length of index \(3\)" with pytest.raises(ValueError, match=msg): Series(["foo"], index=["a", "b", "c"])