code: def _encode_categorical_existing(X_col, nonmissings, 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
  
      if issubclass(X_col.dtype.type, np.floating):
          missings = np.isnan(X_col)
          if missings.any():
              nonmissings = ~missings
              X_col = X_col[nonmissings]
      elif X_col.dtype.type is np.object_:
          X_col = _densify_object_ndarray(X_col)
  
      uniques, indexes = np.unique(X_col, return_inverse=True)
  
      if issubclass(X_col.dtype.type, np.floating):
          uniques = uniques.astype(np.float64, copy=False)
      uniques = uniques.astype(np.unicode_, copy=False)
  
      mapping = np.fromiter(
          (categories.get(val, -1) for val in uniques),
          np.int64,
          count=len(uniques),
      )
      encoded = mapping[indexes]
  
      if (mapping < 0).any():
          if nonmissings is not None:
              encoded_tmp = np.zeros(len(nonmissings), dtype=np.int64)
              np.place(encoded_tmp, nonmissings, encoded)
              bad = np.full(len(nonmissings), None, dtype=np.object_)
              np.place(
                  bad, encoded_tmp < 0, uniques[indexes[encoded < 0]]
              )
              encoded = encoded_tmp
          else:
              bad = np.full(len(encoded), None, dtype=np.object_)
              unknowns = encoded < 0
              np.place(bad, unknowns, uniques[indexes[unknowns]])
      else:
          bad = None
          if nonmissings is not None:
              encoded_tmp = np.zeros(len(nonmissings), dtype=np.int64)
              np.place(encoded_tmp, nonmissings, encoded)
              encoded = encoded_tmp
  
      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 3 for calling a global function with example usage: _encode_categorical_existing(X_col, nonmissings, categories) and returns: encoded,  bad