Skip to content

BUG: rename fails when using level #43659

Closed
@bustawin

Description

@bustawin

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

arrays = [
        ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
        ["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
s = pd.Series(np.random.randn(8), index=index)
s.rename(index={'one': 'yes'}, level='second', errors='raise')

Issue Description

rename does never find index values when using the level parameter to replace. When adding errors='raise', we get the following error:

Traceback (most recent call last):
  File "/Users/bustawin/repos/venvs/bicing/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-15-54b8de4a3511>", line 1, in <module>
    s.rename(index={'one': 'yes'}, level=1, errors='raise')
  File "/Users/bustawin/repos/venvs/bicing/lib/python3.9/site-packages/pandas/core/series.py", line 4518, in rename
    return super().rename(
  File "/Users/bustawin/repos/venvs/bicing/lib/python3.9/site-packages/pandas/core/generic.py", line 1154, in rename
    raise KeyError(f"{missing_labels} not found in axis")
KeyError: "['one'] not found in axis"

The example dataframe is taken from the multi-index guide.

Expected Behavior

The above should have produced the following dataframe:

first  second
bar    yes       0.220890
       two      -1.497161
baz    yes       0.589343
       two      -0.604643
foo    yes       0.090448
       two       0.373825
qux    yes       1.366876
       two       1.460868

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.9.7.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.3
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.1.3
setuptools : 57.0.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : 2.9.1 (dt dec pq3 ext lo64)
jinja2 : 3.0.1
IPython : 7.27.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 2021.08.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.3.24
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions