Skip to content

Commit 9143df4

Browse files
committed
FIX pandas-dev#57645: Cannot use numpy FLS as indicies since pandas 2.2.1
While using the function set_index with parameter inplace=True, the function would try and create a new index where its dtype would be a FLS S{value} dtype, which was not recognized by the function _dtype_to_subclass and raised a NotImplementedError. That said , by adding a verification that recognizes FLS dtype , the index is created successfully and the function executes properly.
1 parent 283a2dc commit 9143df4

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

+9
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,15 @@ Performance improvements
373373

374374
Bug fixes
375375
~~~~~~~~~
376+
- Fixed bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
377+
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
378+
- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
379+
- Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
380+
- 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`)
381+
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
382+
- Fixed bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
383+
- Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)
384+
- Fixed bug in :class:`Index` Index constructor did not allow FLS as indicies. (:issue:`57645`)
376385

377386
Categorical
378387
^^^^^^^^^^^

pandas/core/indexes/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,9 @@ def _dtype_to_subclass(cls, dtype: DtypeObj):
625625
# NB: assuming away MultiIndex
626626
return Index
627627

628-
elif issubclass(dtype.type, str) or is_numeric_dtype(dtype):
628+
elif (
629+
dtype.kind == "S" or issubclass(dtype.type, str) or is_numeric_dtype(dtype)
630+
):
629631
return Index
630632

631633
raise NotImplementedError(dtype)

pandas/tests/frame/methods/test_set_index.py

+14
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,20 @@ def test_set_index_raise_on_len(
617617
with pytest.raises(ValueError, match=msg):
618618
df.set_index(["A", df.A, box(values)], drop=drop, append=append)
619619

620+
def test_set_index_with_FLS_Dtype(self):
621+
string_length = 6
622+
in_dtype, df_name = f"S{string_length}", "fruit"
623+
data = ["apple", "banana", "orange", "grape"]
624+
625+
# Create array with FLS(|S{value}) dtype
626+
arr = np.array(data, dtype=in_dtype)
627+
df = DataFrame(Series(arr), columns=[df_name])
628+
629+
# This will create a new Index with FLS dtype
630+
expected = Index(data=Series(arr), name=df_name)
631+
df.set_index(df_name, inplace=True)
632+
tm.assert_index_equal(df.index, expected)
633+
620634

621635
class TestSetIndexCustomLabelType:
622636
def test_set_index_custom_label_type(self):

pandas/tests/io/test_parquet.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1027,9 +1027,7 @@ def test_columns_dtypes_not_invalid(self, pa):
10271027

10281028
# bytes
10291029
df.columns = [b"foo", b"bar"]
1030-
with pytest.raises(NotImplementedError, match="|S3"):
1031-
# Bytes fails on read_parquet
1032-
check_round_trip(df, pa)
1030+
check_round_trip(df, pa)
10331031

10341032
# python object
10351033
df.columns = [

0 commit comments

Comments
 (0)