def test_remove_unused_levels_with_nan(): # GH 37510 idx = Index([(1, np.nan), (3, 4)]).rename(["id1", "id2"]) idx = idx.set_levels(["a", np.nan], level="id1") idx = idx.remove_unused_levels() result = idx.levels expected = FrozenList([["a", np.nan], [4]]) assert str(result) == str(expected)
def add_category(index: pd.Index, category: Any, level: int = 0) -> pd.Index: "Add `category` to categorical `index` at specified `level`." def add_cat(index, cats): if isinstance(index, pd.CategoricalIndex): cats = [item for item in cats if item not in index.categories] index = index.add_categories(cats) return index if isinstance(category, str): category = [category] if isinstance(index, pd.MultiIndex): index = index.set_levels(add_cat(index.levels[level], category), level=level) else: index = add_cat(index, category) return index