From 4aa4a49bddfa843700cb18870019bfeb94e20084 Mon Sep 17 00:00:00 2001 From: antareepsarkar Date: Sat, 22 Nov 2025 12:09:14 +0530 Subject: [PATCH 1/3] 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) From 21b02a1314f791dbb7e606a605780fe56e045bb2 Mon Sep 17 00:00:00 2001 From: antareepsarkar Date: Tue, 25 Nov 2025 22:41:15 +0530 Subject: [PATCH 2/3] Raise TypeError when array not like 1D in pandas.array --- pandas/core/arrays/string_arrow.py | 3 +++ pandas/core/construction.py | 4 ---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 3dc0115fc4770..e62fffe7baa8e 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -213,6 +213,9 @@ def _from_sequence( elif isinstance(scalars, (pa.Array, pa.ChunkedArray)): pa_arr = pc.cast(scalars, pa.large_string()) else: + ndarr = np.array(scalars) + if ndarr.ndim != 1: + raise TypeError("Values must be a 1D list-like") # convert non-na-likes to str result = lib.ensure_string_array(scalars, copy=copy) pa_arr = pa.array(result, type=pa.large_string(), from_pandas=True) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index a0f11088fa810..5868bdaa1225b 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -322,10 +322,6 @@ 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) From d22d8211dcb459f38bd8c284f564453a30592485 Mon Sep 17 00:00:00 2001 From: antareepsarkar Date: Wed, 26 Nov 2025 12:21:47 +0530 Subject: [PATCH 3/3] raise TypeError when array not like 1D in pandas.array --- pandas/core/arrays/string_arrow.py | 3 --- pandas/core/construction.py | 7 +++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index e62fffe7baa8e..3dc0115fc4770 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -213,9 +213,6 @@ def _from_sequence( elif isinstance(scalars, (pa.Array, pa.ChunkedArray)): pa_arr = pc.cast(scalars, pa.large_string()) else: - ndarr = np.array(scalars) - if ndarr.ndim != 1: - raise TypeError("Values must be a 1D list-like") # convert non-na-likes to str result = lib.ensure_string_array(scalars, copy=copy) pa_arr = pa.array(result, type=pa.large_string(), from_pandas=True) 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)