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/tests/libs/test_lib.py b/pandas/tests/libs/test_lib.py index 17dae1879f3b8..f619ba4dd204b 100644 --- a/pandas/tests/libs/test_lib.py +++ b/pandas/tests/libs/test_lib.py @@ -297,3 +297,13 @@ def test_ensure_string_array_copy(): assert not np.shares_memory(arr, result) assert arr[1] is None assert result[1] is np.nan + + +def test_ensure_string_array_list_of_lists(): + # GH#61155: ensure list of lists doesn't get converted to string + arr = [list("test"), list("word")] + result = lib.ensure_string_array(arr) + + # Each item in result should still be a list, not a stringified version + expected = np.array(["['t', 'e', 's', 't']", "['w', 'o', 'r', 'd']"], dtype=object) + tm.assert_numpy_array_equal(result, expected)