55
55
)
56
56
from pandas .core .dtypes .common import (
57
57
ensure_int64 ,
58
- ensure_object ,
59
58
ensure_platform_int ,
60
59
is_categorical_dtype ,
61
60
is_datetime64_dtype ,
96
95
import pandas .core .algorithms as algorithms
97
96
from pandas .core .algorithms import (
98
97
factorize ,
99
- get_data_algo ,
100
98
take_nd ,
101
99
unique1d ,
102
100
)
@@ -2760,8 +2758,6 @@ def _get_codes_for_values(values, categories: Index) -> np.ndarray:
2760
2758
2761
2759
If `values` is known to be a Categorical, use recode_for_categories instead.
2762
2760
"""
2763
- dtype_equal = is_dtype_equal (values .dtype , categories .dtype )
2764
-
2765
2761
if values .ndim > 1 :
2766
2762
flat = values .ravel ()
2767
2763
codes = _get_codes_for_values (flat , categories )
@@ -2773,30 +2769,9 @@ def _get_codes_for_values(values, categories: Index) -> np.ndarray:
2773
2769
# Categorical(array[Period, Period], categories=PeriodIndex(...))
2774
2770
cls = categories .dtype .construct_array_type ()
2775
2771
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 )
2800
2775
2801
2776
2802
2777
def recode_for_categories (
0 commit comments