API: Allow dictionary argument in rename_axis to change some names of MultiIndex · Issue #19978 · pandas-dev/pandas (original) (raw)
Code Sample, a copy-pastable example if possible
mi = pd.MultiIndex.from_product([['a','b','c'],[1,2]], names=['ll','nn']) df = pd.DataFrame({'x': [i for i in range(len(mi))], 'y' : [i*10 for i in range(len(mi))]}, index=mi) df.rename(columns={'x':'z'}) # This works to change name of one column df.rename_axis({'x' : 'z'}, axis='columns') # This also works to change name of one column (but you get a deprecation warning) df.rename(index={'nn':'zz'}) # This does not work to change name of one level of a MultiIndex df.rename_axis({'nn' : 'zz'}, axis='index') # This does not work to change name of one level of a MultiIndex
Problem description
Ideally, if you call df.rename_axis(dict, axis='index')
, only the names specified in the dictionary would be changed in the corresponding MultiIndex
Expected Output
For the expression
df.rename_axis({'nn' : 'zz'}, axis='index')
the output should be:
x y
zz ll
1 a 0 0
b 1 10
c 2 20
2 a 3 30
b 4 40
c 5 50
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None
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.22.0
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.10
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.1
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
In [ ]: