def cartesian_product_for_groupers(result, args, names):
def f(a):<EOL><INDENT>if isinstance(a, (CategoricalIndex, Categorical)):<EOL><INDENT>categories = a.categories<EOL>a = Categorical.from_codes(np.arange(len(categories)),<EOL>categories=categories,<EOL>ordered=a.ordered)<EOL><DEDENT>return a<EOL><DEDENT>index = pd.MultiIndex.from_product(map(f, args), names=names)<EOL>return result.reindex(index).sort_index()<EOL>
Reindex to a cartesian production for the groupers,
    preserving the nature (Categorical) of each grouper