Skip to content

API: Allow MultiIndex.rename() to accept a dict as an argument #20421

Closed
@Dr-Irv

Description

@Dr-Irv

Code Sample, a copy-pastable example if possible

In [5]: mi=pd.MultiIndex.from_product([list('abc'), list('xy'), [1,2]],names=['abc','xy','num'])

In [6]: mi.names
Out[6]: FrozenList(['abc', 'xy', 'num'])

In [7]: mi.rename(['def','v'], level=[0,2]).names
Out[7]: FrozenList(['def', 'xy', 'v'])


In [8]: mi.rename({'xy' : 'foo'})
---------------------------------------------------------------------------
# Traceback omitted

ValueError: Length of names must match number of levels in MultiIndex.

Problem description

In coordination with how DataFrame.rename() supports using a dictionary to rename columns, I think it would be worthwhile to allow MultiIndex.rename() to also support a dictionary argument. The value of this is that you then don't need to know the level numbers of each of the index names.

If implemented, this would also help with issue #17334, since we would then implement MultiIndex.rename with its own docstring.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 01882ba
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0.dev0+657.g01882ba5b
pytest: 3.4.0
pip: 9.0.1
setuptools: 38.5.1
Cython: 0.25.1
numpy: 1.14.1
scipy: 1.0.0
pyarrow: 0.8.0
xarray: None
IPython: 6.2.1
sphinx: 1.7.1
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2018.3
blosc: 1.5.1
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.2.0
openpyxl: 2.5.0
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.5
pymysql: 0.8.0
psycopg2: None
jinja2: 2.10
s3fs: 0.1.3
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions