From 4aa4a49bddfa843700cb18870019bfeb94e20084 Mon Sep 17 00:00:00 2001 From: antareepsarkar Date: Sat, 22 Nov 2025 12:09:14 +0530 Subject: [PATCH] BUG: raise TypeError when array not like 1D in pandas.array --- pandas/core/construction.py | 4 ++++ pandas/tests/arrays/test_array.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 5868bdaa1225b..a0f11088fa810 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -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") 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)