def clear_known_categories(x, cols=None, index=True):
 if isinstance(x, (pd.Series, pd.DataFrame)):
  x = x.copy()
  if isinstance(x, pd.DataFrame):
   mask = (x.dtypes == 'category')
   if (cols is None):
    cols = mask[mask].index
   elif (not mask.loc[cols].all()):
    raise ValueError('Not all columns are categoricals')
   for c in cols:
    x[c].cat.set_categories([UNKNOWN_CATEGORIES], inplace=True)
  elif isinstance(x, pd.Series):
   if is_categorical_dtype(x.dtype):
    x.cat.set_categories([UNKNOWN_CATEGORIES], inplace=True)
  if (index and isinstance(x.index, pd.CategoricalIndex)):
   x.index = x.index.set_categories([UNKNOWN_CATEGORIES])
 elif isinstance(x, pd.CategoricalIndex):
  x = x.set_categories([UNKNOWN_CATEGORIES])
 return x

def clear_known_categories ( x , cols = None , index = True ) : if isinstance ( x , ( pd . Series , pd . DataFrame ) ) : x = x . copy ( ) if isinstance ( x , pd . DataFrame ) : mask = ( x . dtypes == 'category' ) if ( cols is None ) : cols = mask [ mask ] . index elif ( not mask . loc [ cols ] . all ( ) ) : raise ValueError ( 'Not all columns are categoricals' ) for c in cols : x [ c ] . cat . set_categories ( [ UNKNOWN_CATEGORIES ] , inplace = True ) elif isinstance ( x , pd . Series ) : if is_categorical_dtype ( x . dtype ) : x . cat . set_categories ( [ UNKNOWN_CATEGORIES ] , inplace = True ) if ( index and isinstance ( x . index , pd . CategoricalIndex ) ) : x . index = x . index . set_categories ( [ UNKNOWN_CATEGORIES ] ) elif isinstance ( x , pd . CategoricalIndex ) : x = x . set_categories ( [ UNKNOWN_CATEGORIES ] ) return x
set categories to be unknown .