Skip to content

Commit f72d1df

Browse files
authored
Merge branch 'main' into fix-issue-61221
2 parents 1f9cd53 + 29e0146 commit f72d1df

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/_libs/lib.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,10 @@ cpdef ndarray[object] ensure_string_array(
777777
return out
778778
arr = arr.to_numpy(dtype=object)
779779
elif not util.is_array(arr):
780-
arr = np.array(arr, dtype="object")
780+
# GH#61155: Guarantee a 1-d result when array is a list of lists
781+
input_arr = arr
782+
arr = np.empty(len(arr), dtype="object")
783+
arr[:] = input_arr
781784

782785
result = np.asarray(arr, dtype="object")
783786

pandas/tests/libs/test_lib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,13 @@ def test_ensure_string_array_copy():
297297
assert not np.shares_memory(arr, result)
298298
assert arr[1] is None
299299
assert result[1] is np.nan
300+
301+
302+
def test_ensure_string_array_list_of_lists():
303+
# GH#61155: ensure list of lists doesn't get converted to string
304+
arr = [list("test"), list("word")]
305+
result = lib.ensure_string_array(arr)
306+
307+
# Each item in result should still be a list, not a stringified version
308+
expected = np.array(["['t', 'e', 's', 't']", "['w', 'o', 'r', 'd']"], dtype=object)
309+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)