diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index de7d9af731010..8bc696285c939 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -769,7 +769,10 @@ cpdef ndarray[object] ensure_string_array( return out arr = arr.to_numpy(dtype=object) elif not util.is_array(arr): - arr = np.array(arr, dtype="object") + # GH#61155: Guarantee a 1-d result when array is a list of lists + input_arr = arr + arr = np.empty(len(arr), dtype="object") + arr[:] = input_arr result = np.asarray(arr, dtype="object") diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index b3aa782341c77..b5bca7675c350 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -655,6 +655,8 @@ def _from_sequence( # zero_copy_only to True which caused problems see GH#52076 scalars = np.array(scalars) # convert non-na-likes to str, and nan-likes to StringDtype().na_value + if isinstance(scalars, list) and all(isinstance(x, list) for x in scalars): + scalars = [str(x) for x in scalars] result = lib.ensure_string_array(scalars, na_value=na_value, copy=copy) # Manually creating new array avoids the validation step in the __init__, so is diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 852049804a4f5..855425c3cb636 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4912,6 +4912,10 @@ def values(self) -> ArrayLike: :meth:`Index.to_numpy`, depending on whether you need a reference to the underlying data or a NumPy array. + .. versionchanged:: 3.0.0 + + The returned array is read-only. + Returns ------- array: numpy.ndarray or ExtensionArray diff --git a/pandas/tests/arrays/test_string_array.py b/pandas/tests/arrays/test_string_array.py new file mode 100644 index 0000000000000..66f432a73d79d --- /dev/null +++ b/pandas/tests/arrays/test_string_array.py @@ -0,0 +1,4 @@ +import pandas as pd + +print(pd.array([list("test"), list("words")], dtype="string")) +print(pd.array([list("test"), list("word")], dtype="string"))