Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ def array(
return data

if isinstance(dtype, ExtensionDtype):
if dtype == StringDtype() and isinstance(data, (list, tuple)):
for i in data:
if isinstance(i, (list, tuple, np.ndarray)):
raise TypeError("Values must be a 1D list-like")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check seems extremely expensive for an edge case. Maybe it's possible to patch the function that performs the conversion in _from_sequence.

cls = dtype.construct_array_type()
return cls._from_sequence(data, dtype=dtype, copy=copy)

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ def test_nd_raises(data):
pd.array(data, dtype="int64")


@pytest.mark.parametrize("data", [[["a"], ["b"]]])
def test_not_1D_like_raises(data):
with pytest.raises(TypeError, match="Values must be a 1D list-like"):
pd.array(data, dtype=pd.StringDtype())


def test_scalar_raises():
with pytest.raises(ValueError, match="Cannot pass scalar '1'"):
pd.array(1)
Expand Down
Loading