Skip to content

Commit 13926e5

Browse files
BUG: preserve (object) dtype in factorize (pandas-dev#60118)
* BUG: preserve (object) dtype in factorize * add fallback for float16
1 parent 1908f2e commit 13926e5

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

pandas/core/base.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from pandas.core.dtypes.generic import (
4545
ABCDataFrame,
4646
ABCIndex,
47+
ABCMultiIndex,
4748
ABCSeries,
4849
)
4950
from pandas.core.dtypes.missing import (
@@ -1287,13 +1288,18 @@ def factorize(
12871288
if uniques.dtype == np.float16:
12881289
uniques = uniques.astype(np.float32)
12891290

1290-
if isinstance(self, ABCIndex):
1291-
# preserve e.g. MultiIndex
1291+
if isinstance(self, ABCMultiIndex):
1292+
# preserve MultiIndex
12921293
uniques = self._constructor(uniques)
12931294
else:
12941295
from pandas import Index
12951296

1296-
uniques = Index(uniques)
1297+
try:
1298+
uniques = Index(uniques, dtype=self.dtype)
1299+
except NotImplementedError:
1300+
# not all dtypes are supported in Index that are allowed for Series
1301+
# e.g. float16 or bytes
1302+
uniques = Index(uniques)
12971303
return codes, uniques
12981304

12991305
_shared_docs["searchsorted"] = """

pandas/tests/test_algos.py

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def test_factorize_complex(self):
6565
expected_uniques = np.array([(1 + 0j), (2 + 0j), (2 + 1j)], dtype=complex)
6666
tm.assert_numpy_array_equal(uniques, expected_uniques)
6767

68-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)", strict=False)
6968
def test_factorize(self, index_or_series_obj, sort):
7069
obj = index_or_series_obj
7170
result_codes, result_uniques = obj.factorize(sort=sort)

0 commit comments

Comments
 (0)