def strip_unknown_categories(x):
 if isinstance(x, (pd.Series, pd.DataFrame)):
  x = x.copy()
  if isinstance(x, pd.DataFrame):
   cat_mask = (x.dtypes == 'category')
   if cat_mask.any():
    cats = cat_mask[cat_mask].index
    for c in cats:
     if (not has_known_categories(x[c])):
      x[c].cat.set_categories([], inplace=True)
  elif isinstance(x, pd.Series):
   if (is_categorical_dtype(x.dtype) and (not has_known_categories(x))):
    x.cat.set_categories([], inplace=True)
  if (isinstance(x.index, pd.CategoricalIndex) and (not has_known_categories(x.index))):
   x.index = x.index.set_categories([])
 elif (isinstance(x, pd.CategoricalIndex) and (not has_known_categories(x))):
  x = x.set_categories([])
 return x

def strip_unknown_categories ( x ) : if isinstance ( x , ( pd . Series , pd . DataFrame ) ) : x = x . copy ( ) if isinstance ( x , pd . DataFrame ) : cat_mask = ( x . dtypes == 'category' ) if cat_mask . any ( ) : cats = cat_mask [ cat_mask ] . index for c in cats : if ( not has_known_categories ( x [ c ] ) ) : x [ c ] . cat . set_categories ( [ ] , inplace = True ) elif isinstance ( x , pd . Series ) : if ( is_categorical_dtype ( x . dtype ) and ( not has_known_categories ( x ) ) ) : x . cat . set_categories ( [ ] , inplace = True ) if ( isinstance ( x . index , pd . CategoricalIndex ) and ( not has_known_categories ( x . index ) ) ) : x . index = x . index . set_categories ( [ ] ) elif ( isinstance ( x , pd . CategoricalIndex ) and ( not has_known_categories ( x ) ) ) : x = x . set_categories ( [ ] ) return x
replace any unknown categoricals with empty categoricals .