5555)
5656from pandas .core .dtypes .common import (
5757 ensure_int64 ,
58- ensure_object ,
5958 ensure_platform_int ,
6059 is_categorical_dtype ,
6160 is_datetime64_dtype ,
9695import pandas .core .algorithms as algorithms
9796from pandas .core .algorithms import (
9897 factorize ,
99- get_data_algo ,
10098 take_nd ,
10199 unique1d ,
102100)
@@ -2760,8 +2758,6 @@ def _get_codes_for_values(values, categories: Index) -> np.ndarray:
27602758
27612759 If `values` is known to be a Categorical, use recode_for_categories instead.
27622760 """
2763- dtype_equal = is_dtype_equal (values .dtype , categories .dtype )
2764-
27652761 if values .ndim > 1 :
27662762 flat = values .ravel ()
27672763 codes = _get_codes_for_values (flat , categories )
@@ -2773,30 +2769,9 @@ def _get_codes_for_values(values, categories: Index) -> np.ndarray:
27732769 # Categorical(array[Period, Period], categories=PeriodIndex(...))
27742770 cls = categories .dtype .construct_array_type ()
27752771 values = maybe_cast_to_extension_array (cls , values )
2776- if not isinstance (values , cls ):
2777- # exception raised in _from_sequence
2778- values = ensure_object (values )
2779- # error: Incompatible types in assignment (expression has type
2780- # "ndarray", variable has type "Index")
2781- categories = ensure_object (categories ) # type: ignore[assignment]
2782- elif not dtype_equal :
2783- values = ensure_object (values )
2784- # error: Incompatible types in assignment (expression has type "ndarray",
2785- # variable has type "Index")
2786- categories = ensure_object (categories ) # type: ignore[assignment]
2787-
2788- if isinstance (categories , ABCIndex ):
2789- return coerce_indexer_dtype (categories .get_indexer_for (values ), categories )
2790-
2791- # Only hit here when we've already coerced to object dtypee.
2792-
2793- hash_klass , vals = get_data_algo (values )
2794- # pandas/core/arrays/categorical.py:2661: error: Argument 1 to "get_data_algo" has
2795- # incompatible type "Index"; expected "Union[ExtensionArray, ndarray]" [arg-type]
2796- _ , cats = get_data_algo (categories ) # type: ignore[arg-type]
2797- t = hash_klass (len (cats ))
2798- t .map_locations (cats )
2799- return coerce_indexer_dtype (t .lookup (vals ), cats )
2772+
2773+ codes = categories .get_indexer_for (values )
2774+ return coerce_indexer_dtype (codes , categories )
28002775
28012776
28022777def recode_for_categories (
0 commit comments