Skip to content

Issue36124 display of int enums #36820

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
6 changes: 5 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,8 @@ def maybe_convert_numeric(ndarray[object] values, set na_values,
def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
bint safe=False, bint convert_datetime=False,
bint convert_timedelta=False,
bint convert_to_nullable_integer=False):
bint convert_to_nullable_integer=False,
bint convert_intenum=False):
"""
Type inference function-- convert object array to proper dtype

Expand Down Expand Up @@ -2217,6 +2218,9 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False,
seen.object_ = True
break
elif util.is_integer_object(val):
if getattr(val, 'name', None) is not None and not convert_intenum:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am not sure why you need this extra argument at all? the getattr (or a hasattr check) would prob be ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact it could be in the elif itself

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jreback, I will take a look at this when I get a chance later this week.

seen.object_ = True
break
seen.int_ = True
floats[i] = <float64_t>val
complexes[i] = <double complex>val
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ cdef inline bint is_integer_object(object obj) nogil:

Notes
-----
This counts np.timedelta64 objects as integers.
This counts np.timedelta64 and IntEnums objects as integers.
"""
return (not PyBool_Check(obj) and PyArray_IsIntegerScalar(obj)
and not is_timedelta64_object(obj))
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from collections import namedtuple
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from enum import IntEnum
from fractions import Fraction
from io import StringIO
from numbers import Number
Expand Down Expand Up @@ -589,6 +590,24 @@ def test_maybe_convert_objects_bool_nan(self):
out = lib.maybe_convert_objects(ind.values, safe=1)
tm.assert_numpy_array_equal(out, exp)

def test_maybe_convert_objects_intenum(self):
class Colors(IntEnum):
red = 1
blue = 2

ind = Index([Colors.red, Colors.blue], dtype=object)
expected = np.array([Colors.red, Colors.blue], dtype=object)
result = lib.maybe_convert_objects(ind.values)

# by default, we should not convert IntEnums to ints
tm.assert_numpy_array_equal(result, expected)

expected = np.array([1, 2], dtype=np.int64)
result = lib.maybe_convert_objects(ind.values, convert_intenum=True)

# still coverts to int if convert_intenum set to True
tm.assert_numpy_array_equal(result, expected)

def test_mixed_dtypes_remain_object_array(self):
# GH14956
array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object)
Expand Down