MCPcopy
hub / github.com/pandas-dev/pandas / _set_names

Method _set_names

pandas/core/indexes/multi.py:1662–1716  ·  view source on GitHub ↗

Set new names on index. Each name has to be a hashable type. Parameters ---------- values : str or sequence name(s) to set level : int, level name, or sequence of int/level names (default None) If the index is a MultiIndex (hierarchic

(self, names, *, level=None)

Source from the content-addressed store, hash-verified

1660 return FrozenList(self._names)
1661
1662 def _set_names(self, names, *, level=None) -> None:
1663 """
1664 Set new names on index. Each name has to be a hashable type.
1665
1666 Parameters
1667 ----------
1668 values : str or sequence
1669 name(s) to set
1670 level : int, level name, or sequence of int/level names (default None)
1671 If the index is a MultiIndex (hierarchical), level(s) to set (None
1672 for all levels). Otherwise level must be None
1673
1674 Raises
1675 ------
1676 TypeError if each name is not hashable.
1677
1678 Notes
1679 -----
1680 sets names on levels. WARNING: mutates!
1681
1682 Note that you generally want to set this *after* changing levels, so
1683 that it only acts on copies
1684 """
1685 # GH 15110
1686 # Don't allow a single string for names in a MultiIndex
1687 if names is not None and not is_list_like(names):
1688 raise ValueError("Names should be list-like for a MultiIndex")
1689 names = list(names)
1690
1691 if level is not None and len(names) != len(level):
1692 raise ValueError("Length of names must match length of level.")
1693 if level is None and len(names) != self.nlevels:
1694 raise ValueError(
1695 "Length of names must match number of levels in MultiIndex."
1696 )
1697
1698 if level is None:
1699 level = range(self.nlevels)
1700 else:
1701 level = (self._get_level_number(lev) for lev in level)
1702
1703 # set the name
1704 for lev, name in zip(level, names, strict=True):
1705 if name is not None:
1706 # GH 20527
1707 # All items in 'names' need to be hashable:
1708 if not is_hashable(name):
1709 raise TypeError(
1710 f"{type(self).__name__}.name must be a hashable type"
1711 )
1712 self._names[lev] = name
1713
1714 # If .levels has been accessed, the .name of each level in our cache
1715 # will be stale.
1716 self._reset_cache("levels")
1717
1718 names = property(
1719 fset=_set_names,

Callers 2

_set_levelsMethod · 0.95
__new__Method · 0.45

Calls 3

_get_level_numberMethod · 0.95
is_hashableFunction · 0.85
_reset_cacheMethod · 0.80

Tested by

no test coverage detected