Skip to content
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

BUG: Can't store IntEnum members inside a pd.DataFrame column of object dtype. If you use pd.Series it works #59380

Open
2 of 3 tasks
willofferfit opened this issue Aug 1, 2024 · 2 comments
Assignees
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors Dtype Conversions Unexpected or buggy dtype conversions

Comments

@willofferfit
Copy link

willofferfit commented Aug 1, 2024

Pandas version checks

  • 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 main branch of pandas.

Reproducible Example

from enum import IntEnum, auto

import pandas as pd


class Color(IntEnum):
    BLUE = auto()
    RED = auto()


colors = [Color.BLUE, Color.RED]

print("Test pd.DataFrame:")
df = pd.DataFrame(colors, columns=["color"], dtype=object)
check_type = lambda type_, iterable: all(isinstance(color, type_) for color in iterable)
print("Are they ints?", check_type(int, df["color"]))  # True
print("Are they colors?", check_type(Color, df["color"]))  # False

print("\nTest pd.Series:")
series = pd.Series(colors, dtype=object)
print("Are they ints?", check_type(int, series))  # True
print("Are they colors?", check_type(Color, series))  # True

Issue Description

If you try to store a member of an IntEnum in a pd.DataFrame, it'll get converted to int, even if the column has object dtype.

There is an old issue (#36124) about this that had been labeled with Enhancement, but it is clearly a bug. A PR had been made (#36820), but was abandoned.

Expected Behavior

No casting should have been done for an object dtype column. And behaviors should be the same for pd.DataFrame and pd.Series. If backwards compatibility is desired, the abandoned PR had suggested a valid approach.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.10.6.final.0
python-bits : 64
OS : Darwin
OS-release : 23.5.0
Version : Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 63.2.0
pip : 22.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.13.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.3.1
gcsfs : None
matplotlib : 3.8.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.13.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@willofferfit willofferfit added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 1, 2024
@willofferfit willofferfit changed the title BUG: Can't store IntEnum members inside a pd.DataFrame column of object dtype BUG: Can't store IntEnum members inside a pd.DataFrame column of object dtype. If you use pd.Series it works Aug 1, 2024
@willofferfit willofferfit changed the title BUG: Can't store IntEnum members inside a pd.DataFrame column of object dtype. If you use pd.Series it works BUG: Can't store IntEnum members inside a pd.DataFrame column of object dtype. If you use pd.Series it works Aug 1, 2024
@sdalmia11
Copy link

take

@rhshadrach
Copy link
Member

Thanks for the report. Confirmed on main, further investigations and PRs to fix are welcome. This alternative works:

df = pd.DataFrame({"color": colors}, dtype=object)

@rhshadrach rhshadrach added Dtype Conversions Unexpected or buggy dtype conversions Constructors Series/DataFrame/Index/pd.array Constructors and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

No branches or pull requests

3 participants