Skip to content

Commit 59aaa8e

Browse files
BUG: raise TypeError when array not like 1D in pandas.array
1 parent 08c0b78 commit 59aaa8e

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

pandas/core/construction.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ def array(
321321
return data.copy()
322322
return data
323323

324+
# to avoid returning an array of string representation of objects.
325+
if dtype == StringDtype():
326+
ndarr = np.array(data)
327+
if ndarr.ndim != 1:
328+
raise TypeError("Values must be a 1D list-like")
329+
324330
if isinstance(dtype, ExtensionDtype):
325331
cls = dtype.construct_array_type()
326332
return cls._from_sequence(data, dtype=dtype, copy=copy)

pandas/tests/arrays/test_array.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,12 @@ def test_nd_raises(data):
460460
pd.array(data, dtype="int64")
461461

462462

463+
@pytest.mark.parametrize("data", [[["a"], ["b"]]])
464+
def test_not_1D_like_raises(data):
465+
with pytest.raises(TypeError, match="Values must be a 1D list-like"):
466+
pd.array(data, dtype=pd.StringDtype())
467+
468+
463469
def test_scalar_raises():
464470
with pytest.raises(ValueError, match="Cannot pass scalar '1'"):
465471
pd.array(1)

0 commit comments

Comments
 (0)