Skip to content

BUG: Series.rename raises error on values accepted by Series constructor. #27813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lgmsantos opened this issue Aug 8, 2019 · 0 comments · Fixed by #27814
Closed

BUG: Series.rename raises error on values accepted by Series constructor. #27813

lgmsantos opened this issue Aug 8, 2019 · 0 comments · Fixed by #27814
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Series Series data structure
Milestone

Comments

@lgmsantos
Copy link
Contributor

Sample

import pandas as pd

class MyIndexer:
    pass

i1 = MyIndexer()
s = pd.Series([1, 2, 3], name=i1) # allowed
s.rename(i1) # raises error

The error stack trace is the following:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    s.rename(i1) # raises error
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/series.py", line 3736, in rename
    return super(Series, self).rename(index=index, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 1091, in rename
    level=level)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 171, in rename_axis
    obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 2004, in _transform_index
    items = [func(x) for x in index]
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 2004, in <listcomp>
    items = [func(x) for x in index]
TypeError: 'MyIndexer' object is not callable

Description

Series.rename handle anything that isn't a scalar or list-like as a mapping.

Proposed change

Change the following code (from Series.rename):

non_mapping = is_scalar(index) or (is_list_like(index) and not is_dict_like(index))
if non_mapping:
    return self._set_name(index, inplace=kwargs.get("inplace"))
return super().rename(index=index, **kwargs)

to

if callable(index) or is_dict_like(index):
    return super().rename(index=index, **kwargs)
else:
    return self._set_name(index, inplace=kwargs.get("inplace"))

so anything that isn't a dict or a callable will be treated the same way as a scalar or list-like.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-55-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: pt_BR.UTF-8

pandas: 0.24.2
pytest: 3.6.0
pip: 19.1.1
setuptools: 41.0.0
Cython: 0.26.1
numpy: 1.16.4
scipy: 1.3.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: 0.5.1
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.1.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: 4.2.1
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@gfyoung gfyoung added Bug Indexing Related to indexing on series/frames, not to indexes themselves Series Series data structure labels Aug 9, 2019
@TomAugspurger TomAugspurger added this to the 0.25.1 milestone Aug 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Series Series data structure
Projects
None yet
3 participants