diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 5868bdaa1225b..2ce6ddd7f3ff3 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -321,6 +321,13 @@ def array( return data.copy() return data + if dtype == StringDtype(): + ndarr = np.array(data) + if ndarr.ndim != 1: + raise TypeError("Values must be a 1D list-like") + cls = dtype.construct_array_type() + return cls._from_sequence(data, dtype=dtype, copy=copy) + if isinstance(dtype, ExtensionDtype): cls = dtype.construct_array_type() return cls._from_sequence(data, dtype=dtype, copy=copy) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index c327d1b647bce..1f526a7916416 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -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)