Skip to content

Commit

Permalink
BUG: preserve (object) dtype in factorize (#60118)
Browse files Browse the repository at this point in the history
* BUG: preserve (object) dtype in factorize

* add fallback for float16

(cherry picked from commit 13926e5)
  • Loading branch information
jorisvandenbossche committed Nov 4, 2024
1 parent aedb17a commit 6449eaa
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCIndex,
ABCMultiIndex,
ABCSeries,
)
from pandas.core.dtypes.missing import (
Expand Down Expand Up @@ -1198,13 +1199,18 @@ def factorize(
if uniques.dtype == np.float16:
uniques = uniques.astype(np.float32)

if isinstance(self, ABCIndex):
# preserve e.g. MultiIndex
if isinstance(self, ABCMultiIndex):
# preserve MultiIndex
uniques = self._constructor(uniques)
else:
from pandas import Index

uniques = Index(uniques)
try:
uniques = Index(uniques, dtype=self.dtype)
except NotImplementedError:
# not all dtypes are supported in Index that are allowed for Series
# e.g. float16 or bytes
uniques = Index(uniques)
return codes, uniques

_shared_docs[
Expand Down

0 comments on commit 6449eaa

Please sign in to comment.