Skip to content

columns comparison with Enum column name triggers TypeError #22551

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
s-wakaba opened this issue Aug 31, 2018 · 7 comments · Fixed by #47715
Closed

columns comparison with Enum column name triggers TypeError #22551

s-wakaba opened this issue Aug 31, 2018 · 7 comments · Fixed by #47715
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions

Comments

@s-wakaba
Copy link

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
@WillAyd
Copy link
Member

WillAyd commented Aug 31, 2018

Hmm is there any reason why you wouldn't just use the .name of your Enum? Would seem like a much safer approach not sure we want to (or rather can) support indexing by arbitrary objects

@WillAyd WillAyd added Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action labels Aug 31, 2018
@s-wakaba
Copy link
Author

s-wakaba commented Sep 1, 2018

My motivation is the same with using Symbol() objects as property keys in Javascript (ES6). It can be an explicit key for both coding and debugging and can also avoid name conflict.

@jkfm
Copy link

jkfm commented Oct 16, 2018

I have the same problem, and would also prefer not to use the .name-property to ensure specificity of the key.

Or rather: If the reason for this error is an unintended effect of a change in 0.22 to 0.23, what would speak against restoring the original behavior?

@hayd
Copy link
Contributor

hayd commented Apr 24, 2019

@sdementen
Copy link
Contributor

I get a similar issue but I am not indexing at all the dataframe or the series, just comparing it

from uuid import uuid4

import pandas as pd

col = uuid4()

sr = pd.Series([4, 5, 6], name=col)
(sr == sr)
# raise TypeError: 'UUID' object is not callable

df = pd.DataFrame({col: [1, 2, 3]})
(df == df)
# raise TypeError: 'UUID' object is not callable

@jbrockmendel jbrockmendel added the Numeric Operations Arithmetic, Comparison, and Logical operations label Sep 21, 2020
@mroeschke
Copy link
Member

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

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action Numeric Operations Arithmetic, Comparison, and Logical operations labels Jun 22, 2021
@AnjaTRPES
Copy link

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)?

mroeschke pushed a commit that referenced this issue Jul 16, 2022
* TST

* TST

* TST

* updated strign formatting

* updated formatting

* updating the test place within files

* removing an additional parentheses

* removing the  buggy file that was pushed

* removing the  buggy file that was pushed

* fixing initial file

* fixing order of import

* fixing space

* fixing space

* fixing space

* series

* series

* series

* series

* syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants