Skip to content

columns comparison with Enum column name triggers TypeError #22551

Closed
@s-wakaba

Description

@s-wakaba

Code Sample, a copy-pastable example if possible

from enum import Enum
import pandas as pd

Cols = Enum('Cols', 'col1 col2')

q1 = pd.DataFrame({Cols.col1: [1, 2, 3]})
q2 = pd.DataFrame({Cols.col1: [1, 2, 3]})

print((q1[Cols.col1] == q2[Cols.col1]).all())

Problem description

On pandas 0.23.x, comparison of columns in DataFrames which have enum.Enum column names triggers off a TypeError exception. it looks that pandas calls user defined Enum class as a function.

Traceback (most recent call last):
  File "./pandas_enum.py", line 9, in <module>
    print((q1[Cols.col1] == q2[Cols.col1]).all())
  File "/.../lib/python3.6/site-packages/pandas/core/ops.py", line 1217, in wrapper
    name=res_name).rename(res_name)
  File "/.../lib/python3.6/site-packages/pandas/core/series.py", line 3318, in rename
    return super(Series, self).rename(index=index, **kwargs)
  File "/.../lib/python3.6/site-packages/pandas/core/generic.py", line 973, in rename
    level=level)
  File "/.../lib/python3.6/site-packages/pandas/core/internals.py", line 3340, in rename_axis
    obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
  File "/.../lib/python3.6/site-packages/pandas/core/internals.py", line 5298, in _transform_index
    items = [func(x) for x in index]
  File "/.../lib/python3.6/site-packages/pandas/core/internals.py", line 5298, in <listcomp>
    items = [func(x) for x in index]
TypeError: 'Cols' object is not callable

Expected Output

just print True. Pandas 0.22.0 does the expected behavior. Pandas 0.23.0 and later has this problem.

Output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-327.4.5.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: None
pip: 18.0
setuptools: 40.2.0
Cython: None
numpy: 1.15.1
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions