Skip to content

Improve error message if attempting to construct multidimensional series #50651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,9 @@ def _sanitize_ndim(
if isinstance(data, np.ndarray):
if allow_2d:
return result
raise ValueError("Data must be 1-dimensional")
raise ValueError(
f"Data must be 1-dimensional, got ndarray of shape {data.shape} instead"
)
if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype):
# i.e. PandasDtype("O")

Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ def test_constructor(self, datetime_series):
assert not Series().index._is_all_dates

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

mixed.name = "Series"
Expand Down