Skip to content
Open
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
6 changes: 6 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ def array(
return data.copy()
return data

# to avoid returning an array of string representation of objects.
if dtype == StringDtype():
ndarr = np.array(data)
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this for the following reasons:

  1. It's weird to convert to np.array while calling pd.array and not use it.
  2. The conversion in np.array can raise, e.g., when we call np.array([[1, 2], ["a"]]).

Copy link
Author

Choose a reason for hiding this comment

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

Is this change alright? Do I need to make any changes?

if ndarr.ndim != 1:
raise TypeError("Values must be a 1D list-like")

if isinstance(dtype, ExtensionDtype):
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"]]])
Copy link
Member

Choose a reason for hiding this comment

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

Can you add some more cases? For example, some of #63112:

int_2d = [[1], [2]]
float_2d = [[1.0], [2.0]]
string_2d = [["a"], ["b"]]
mixed_2d = [[1, 2], ["a", "b"]]

Copy link
Author

@antareepsarkar antareepsarkar Dec 6, 2025

Choose a reason for hiding this comment

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

Thanks for the review.
I did the modifications.

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