Skip to content

Commit 29e0146

Browse files
Manju080mroeschkepre-commit-ci[bot]rhshadrach
authored
BUG: Impossible creation of array with dtype=string (#61263)
* DOC: Update warning in Index.values docstring to clarify index modification issues (#60954) * DOC: Update warning in Index.values docstring to clarify index modification issues (#60954) with changes * Update pandas/core/indexes/base.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> * DOC : Fixing the whitespace which was causing error * Fixed docstring validation and formatting issues * BUG: Fix array creation for string dtype with inconsistent list lengths (#61155) * BUG: Fix array creation for string dtype with inconsistent list lengths (#61155) * BUG fix GH#61155 v2 * BUG fix GH#61155 with test case for list of lists handling * Fix formatting in test_string_array.py (pre-commit autofix) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add test for list of lists handling in ensure_string_array (GH#61155) * fixing checks * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update pandas/tests/libs/test_lib.py Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> * Remove pandas/tests/arrays/test_string_array.py as requested * wrong fiel base.py * Remove check for nested lists in scalars in string_.py first try * Revert unintended changes to base.py --------- Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com>
1 parent e637b42 commit 29e0146

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/_libs/lib.pyx

+4-1
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

+10
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)