def _get_codes_for_values(values, categories):
from pandas.core.algorithms import _get_data_algo, _hashtables<EOL>dtype_equal = is_dtype_equal(values.dtype, categories.dtype)<EOL>if dtype_equal:<EOL><INDENT>values = getattr(values, '<STR_LIT>', values)<EOL>categories = getattr(categories, '<STR_LIT>', categories)<EOL><DEDENT>elif (is_extension_array_dtype(categories.dtype) and<EOL>is_object_dtype(values)):<EOL><INDENT>try:<EOL><INDENT>values = (<EOL>categories.dtype.construct_array_type()._from_sequence(values)<EOL>)<EOL><DEDENT>except Exception:<EOL><INDENT>values = ensure_object(values)<EOL>categories = ensure_object(categories)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>values = ensure_object(values)<EOL>categories = ensure_object(categories)<EOL><DEDENT>(hash_klass, vec_klass), vals = _get_data_algo(values, _hashtables)<EOL>(_, _), cats = _get_data_algo(categories, _hashtables)<EOL>t = hash_klass(len(cats))<EOL>t.map_locations(cats)<EOL>return coerce_indexer_dtype(t.lookup(vals), cats)<EOL>
utility routine to turn values into codes given the specified categories