Skip to content

Commit 502eca6

Browse files
authored
Improve error message if attempting to construct multidimensional series (#50651)
* Update construction.py * Update test_constructors.py * Update test_constructors.py * Update construction.py * Update test_constructors.py
1 parent 0597b15 commit 502eca6

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

pandas/core/construction.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,9 @@ def _sanitize_ndim(
666666
if isinstance(data, np.ndarray):
667667
if allow_2d:
668668
return result
669-
raise ValueError("Data must be 1-dimensional")
669+
raise ValueError(
670+
f"Data must be 1-dimensional, got ndarray of shape {data.shape} instead"
671+
)
670672
if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype):
671673
# i.e. PandasDtype("O")
672674

pandas/tests/series/test_constructors.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ def test_constructor(self, datetime_series):
172172
assert not Series().index._is_all_dates
173173

174174
# exception raised is of type ValueError GH35744
175-
with pytest.raises(ValueError, match="Data must be 1-dimensional"):
175+
with pytest.raises(
176+
ValueError,
177+
match=r"Data must be 1-dimensional, got ndarray of shape \(3, 3\) instead",
178+
):
176179
Series(np.random.randn(3, 3), index=np.arange(3))
177180

178181
mixed.name = "Series"

0 commit comments

Comments
 (0)