Skip to content

REGR: ensure numpy fixed-size binary/string arrays are converted to object dtype for Index creation #57995

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ Performance improvements

Bug fixes
~~~~~~~~~
- Fixed bug in :class:`Index` Index constructor was not converting FLS to object. (:issue:`57645`)
- Fixed bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
- Fixed bug in :meth:`DataFrameGroupBy.apply` that was returning a completely empty DataFrame when all return values of ``func`` were ``None`` instead of returning an empty DataFrame with the original columns and dtypes. (:issue:`57775`)
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
- Fixed bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
- Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)

Categorical
^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLi
values = list(values)
elif isinstance(values, ABCIndex):
return values._values
elif isinstance(values, ABCSeries):
elif isinstance(values, ABCSeries) and values.dtype.kind != "S":
return values._values

if isinstance(values, list) and dtype in [np.object_, object]:
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/methods/test_set_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,17 @@ def test_set_index_raise_on_len(
with pytest.raises(ValueError, match=msg):
df.set_index(["A", df.A, box(values)], drop=drop, append=append)

def test_set_index_with_FLS_Dtype(self):
arr = np.array(["apple", "banana", "orange", "grape"], dtype="S6")

# Attempt to create a DataFrame with an array with FLS Dtype
df = DataFrame(Series(arr), columns=["fruits"])

# Create Index that converts FLS Dtype to object
expected = Index(data=Series(arr), name="fruits")
df.set_index("fruits", inplace=True)
tm.assert_index_equal(df.index, expected)


class TestSetIndexCustomLabelType:
def test_set_index_custom_label_type(self):
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ def test_constructor_datetimes_mixed_tzs(self):
expected = Index([dt1, dt2], dtype=object)
tm.assert_index_equal(result, expected)

def test_FLS_to_object_conversion(self):
# Create NumPy array of fixed-length strings
arr = np.array(["apple", "banana", "orange", "grape"], dtype="S6")
# Create expected array for index
expected_arr = np.array(
[b"apple", b"banana", b"orange", b"grape"], dtype=object
)
# Create Index that converts FLS Dtype to object
index = Index(data=Series(arr), name="fruits")
expected = Index(data=Series(expected_arr), name="fruits")
tm.assert_index_equal(index, expected)


class TestDtypeEnforced:
# check we don't silently ignore the dtype keyword
Expand Down
Loading