def _categorize(categories, df):
    if '.index' in categories:
        index = pd.CategoricalIndex(
                  pd.Categorical.from_codes(df.index.values, categories['.index']))
    else:
        index = df.index
    if isinstance(df, pd.Series):
        if df.name in categories:
            cat = pd.Categorical.from_codes(df.values, categories[df.name])
            return pd.Series(cat, index=index)
        else:
            return df

    else:
        return pd.DataFrame(
                dict((col, pd.Categorical.from_codes(df[col].values, categories[col])
                           if col in categories
                           else df[col].values)
                    for col in df.columns),
                columns=df.columns,
                index=index)
Categorize columns in dataframe
dataset/ETHPy150Open dask/dask/dask/dataframe/categorical.py/_categorize