question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

columns comparison with Enum column name triggers TypeError

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mroeschkecommented, Jun 22, 2021

Looks to work on master. Could use a test

In [23]: 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())
True
0reactions
AnjaTRPEScommented, Jul 23, 2021

Hey! I am a first-time contributor and would like to help on this. My experience with tests is pretty non-existant, so as far as I understand it one should convert the code-example to a function, and then add this to the appropriate pandas/test suite (maybe to pandas/test/dtypes)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

"unexpected type" error when comparing enum values
The Type part is the name of a type - it can't be qualified by a variable value ( event ). You could...
Read more >
Documentation: 15: ALTER TYPE - PostgreSQL
This form renames a value of an enum type. The value's place in the enum's ordering is not affected. An error will occur...
Read more >
pyspark.sql module - Apache Spark
When schema is None , it will try to infer the schema (column names and types) from data , which should be an...
Read more >
The Type Hierarchy - SQLAlchemy 1.4 Documentation
The rudimental types have “CamelCase” names such as String , Numeric , Integer , and DateTime . All of the immediate subclasses of ......
Read more >
MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
Creating and Using ENUM Columns. An enumeration value must be a quoted string literal. For example, you can create a table with an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found