Separate MultiIndex names from levels by topper-123 · Pull Request #27242 · pandas-dev/pandas (original) (raw)

As I see it, this will for practical purposes keep the names in two locations

No, they'll be stored in one place and accessible from two locations. This is the same as on master, only the source of truth and the referring location are swapped.

But if someone does

mi = pd.MultiIndex.from_product([[1, 2], ['a', 'b']], names=['x', 'y']) lev = mi.levels[0] mi.set_names('z', level=0)

then

mi.names[0], lev.name 'z', 'x'

So the names will be stored in two places (or users should not store individual levels seperately, which they can't be expected to know). So for this reason I think it's the most most practical to make a clean cut.

EDIT: Ok I got an idea: What if we deprecate levels and use the name categories instead? In that case we could do (after implementing #27138):

@property def levels(self) -> FrozenList[Index]: warnings.warn(...) return FrozenList(pd.Index(lev.categories, name=name) for name, lev in zip(self.names, self._data))

@property def categories(self) -> FrozenList[Index]: return FrozenList(lev.categories for lev in self._data)

This would also make the API for MultiIndex be more similar to CategoricalIndex.