Skip to content

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

Open
@willofferfit

Description

@willofferfit

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

Metadata

Metadata

Assignees

Labels

BugConstructorsSeries/DataFrame/Index/pd.array ConstructorsDtype ConversionsUnexpected or buggy dtype conversions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions