code: def _encode_pandas_categorical_existing(
      X_col, pd_categories, categories
  ):
      # called under: predict
  
      # TODO: add special case handling if there is only 1 sample to make that faster
      # if we have just 1 sample, we can avoid making the mapping below
  
      mapping = np.fromiter(
          (categories.get(val, -1) for val in pd_categories),
          np.int64,
          count=len(pd_categories),
      )
  
      if len(mapping) <= len(categories):
          mapping_cmp = np.arange(1, len(mapping) + 1, dtype=np.int64)
          if np.array_equal(mapping, mapping_cmp):
              # avoid overflows for np.int8
              X_col = X_col.astype(dtype=np.int64, copy=False)
              X_col = X_col + 1
              return X_col, None
      else:
          mapping_cmp = np.arange(
              1, len(categories) + 1, dtype=np.int64
          )
          if np.array_equal(mapping[0 : len(mapping_cmp)], mapping_cmp):
              unknowns = len(categories) <= X_col
              bad = np.full(len(X_col), None, dtype=np.object_)
              bad[unknowns] = pd_categories[X_col[unknowns]]
              # avoid overflows for np.int8
              X_col = X_col.astype(dtype=np.int64, copy=False)
              X_col = X_col + 1
              X_col[unknowns] = -1
              return X_col, bad
  
      mapping = np.insert(mapping, 0, 0)
      encoded = mapping[X_col + 1]
  
      bad = None
      unknowns = encoded < 0
      if unknowns.any():
          bad = np.full(len(X_col), None, dtype=np.object_)
          bad[unknowns] = pd_categories[X_col[unknowns]]
  
      return encoded, bad
  
imports: from collections import Counter
  from itertools import count
  import numpy
  import numpy.ma
  import logging
label_desc: Use image node_id 5 for calling a global function with example usage: _encode_pandas_categorical_existing(X_col, pd_categories, categories) and returns: encoded,  bad, X_col,  None, X_col,  bad