Skip to content

Commit aeb52a4

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 069f9a4 commit aeb52a4

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ Bug fixes
308308
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
309309
- Fixed bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
310310
- Fixed bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)
311+
- Fixed bug in :class:`Index` Index constructor did not allow FLS as indicies. (:issue:`57645`)
311312

312313
Categorical
313314
^^^^^^^^^^^

pandas/core/indexes/base.py

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

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

632634
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
@@ -1023,9 +1023,7 @@ def test_columns_dtypes_not_invalid(self, pa):
10231023

10241024
# bytes
10251025
df.columns = [b"foo", b"bar"]
1026-
with pytest.raises(NotImplementedError, match="|S3"):
1027-
# Bytes fails on read_parquet
1028-
check_round_trip(df, pa)
1026+
check_round_trip(df, pa)
10291027

10301028
# python object
10311029
df.columns = [

0 commit comments

Comments
 (0)