Skip to content

Commit bf6978c

Browse files
TomAugspurgerproost
authored andcommitted
DOC: Update MultiIndex.names whatsnew (pandas-dev#29572)
1 parent 3a6164b commit bf6978c

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

doc/source/user_guide/advanced.rst

+25
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,31 @@ index.
554554
Both ``rename`` and ``rename_axis`` support specifying a dictionary,
555555
``Series`` or a mapping function to map labels/names to new values.
556556

557+
When working with an ``Index`` object directly, rather than via a ``DataFrame``,
558+
:meth:`Index.set_names` can be used to change the names.
559+
560+
.. ipython:: python
561+
562+
mi = pd.MultiIndex.from_product([[1, 2], ['a', 'b']], names=['x', 'y'])
563+
mi.names
564+
565+
mi2 = mi.rename("new name", level=0)
566+
mi2
567+
568+
.. warning::
569+
570+
Prior to pandas 1.0.0, you could also set the names of a ``MultiIndex``
571+
by updating the name of a level.
572+
573+
.. code-block:: none
574+
575+
>>> mi.levels[0].name = 'name via level'
576+
>>> mi.names[0] # only works for older panads
577+
'name via level'
578+
579+
As of pandas 1.0, this will *silently* fail to update the names
580+
of the MultiIndex. Use :meth:`Index.set_names` instead.
581+
557582
Sorting a ``MultiIndex``
558583
------------------------
559584

doc/source/whatsnew/v1.0.0.rst

+25-20
Original file line numberDiff line numberDiff line change
@@ -130,34 +130,39 @@ Backwards incompatible API changes
130130

131131
.. _whatsnew_1000.api_breaking.MultiIndex._names:
132132

133-
``MultiIndex.levels`` do not hold level names any longer
134-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
133+
Avoid using names from ``MultiIndex.levels``
134+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135135

136-
- A :class:`MultiIndex` previously stored the level names as attributes of each of its
137-
:attr:`MultiIndex.levels`. From Pandas 1.0, the names are only accessed through
138-
:attr:`MultiIndex.names` (which was also possible previously). This is done in order to
139-
make :attr:`MultiIndex.levels` more similar to :attr:`CategoricalIndex.categories` (:issue:`27242`:).
136+
As part of a larger refactor to :class:`MultiIndex` the level names are now
137+
stored separately from the levels (:issue:`27242`). We recommend using
138+
:attr:`MultiIndex.names` to access the names, and :meth:`Index.set_names`
139+
to update the names.
140140

141-
*pandas 0.25.x*
141+
For backwards compatibility, you can still *access* the names via the levels.
142142

143-
.. code-block:: ipython
143+
.. ipython:: python
144144
145-
In [1]: mi = pd.MultiIndex.from_product([[1, 2], ['a', 'b']], names=['x', 'y'])
146-
Out[2]: mi
147-
MultiIndex([(1, 'a'),
148-
(1, 'b'),
149-
(2, 'a'),
150-
(2, 'b')],
151-
names=['x', 'y'])
152-
Out[3]: mi.levels[0].name
153-
'x'
145+
mi = pd.MultiIndex.from_product([[1, 2], ['a', 'b']], names=['x', 'y'])
146+
mi.levels[0].name
154147
155-
*pandas 1.0.0*
148+
However, it is no longer possible to *update* the names of the ``MultiIndex``
149+
via the name of the level. The following will **silently** fail to update the
150+
name of the ``MultiIndex``
156151

157152
.. ipython:: python
158153
159-
mi = pd.MultiIndex.from_product([[1, 2], ['a', 'b']], names=['x', 'y'])
160-
mi.levels[0].name
154+
mi.levels[0].name = "new name"
155+
mi.names
156+
157+
To update, use ``MultiIndex.set_names``, which returns a new ``MultiIndex``.
158+
159+
.. ipython:: python
160+
161+
mi2 = mi.set_names("new name", level=0)
162+
mi2.names
163+
164+
New repr for :class:`pandas.core.arrays.IntervalArray`
165+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
161166

162167
- :class:`pandas.core.arrays.IntervalArray` adopts a new ``__repr__`` in accordance with other array classes (:issue:`25022`)
163168

0 commit comments

Comments
 (0)