Skip to content

BUG: Impossible creation of array with dtype=string #61263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3474043
DOC: Update warning in Index.values docstring to clarify index modifi…
Manju080 Mar 6, 2025
d070b06
DOC: Update warning in Index.values docstring to clarify index modifi…
Manju080 Mar 7, 2025
b00ba12
Update pandas/core/indexes/base.py
Manju080 Mar 8, 2025
390c8be
DOC : Fixing the whitespace which was causing error
Manju080 Mar 10, 2025
e58f383
Fixed docstring validation and formatting issues
Manju080 Mar 11, 2025
a505d35
BUG: Fix array creation for string dtype with inconsistent list lengt…
Manju080 Apr 9, 2025
6f5c4d4
BUG: Fix array creation for string dtype with inconsistent list lengt…
Manju080 Apr 9, 2025
fc4653d
BUG fix GH#61155 v2
Manju080 Apr 15, 2025
ae36cf7
BUG fix GH#61155 with test case for list of lists handling
Manju080 Apr 15, 2025
fb965e7
Fix formatting in test_string_array.py (pre-commit autofix)
Manju080 Apr 16, 2025
d3bbeaf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 16, 2025
4bf8a07
Add test for list of lists handling in ensure_string_array (GH#61155)
Manju080 May 6, 2025
e81e1da
Merge branch 'bugfix-61155' of https://github.com/Manju080/pandas int…
Manju080 May 7, 2025
0ca4a18
fixing checks
Manju080 May 7, 2025
8a4a54d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2025
90a74ef
Update pandas/tests/libs/test_lib.py
Manju080 May 8, 2025
4db751d
Remove pandas/tests/arrays/test_string_array.py as requested
Manju080 May 8, 2025
9979a8d
wrong fiel base.py
Manju080 May 8, 2025
71f7adc
Remove check for nested lists in scalars in string_.py first try
Manju080 May 8, 2025
c2f7c39
Revert unintended changes to base.py
Manju080 May 10, 2025
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
5 changes: 4 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/libs/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading