Skip to content

Commit 1d5ce5b

Browse files
authored
BUG: Fix bug in maybe_convert_objects with None and nullable (#50043)
1 parent d88e0fb commit 1d5ce5b

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

pandas/_libs/lib.pyx

+6-3
Original file line numberDiff line numberDiff line change
@@ -2446,7 +2446,7 @@ def maybe_convert_objects(ndarray[object] objects,
24462446
seen.int_ = True
24472447
floats[i] = <float64_t>val
24482448
complexes[i] = <double complex>val
2449-
if not seen.null_:
2449+
if not seen.null_ or convert_to_nullable_integer:
24502450
seen.saw_int(val)
24512451

24522452
if ((seen.uint_ and seen.sint_) or
@@ -2616,10 +2616,13 @@ def maybe_convert_objects(ndarray[object] objects,
26162616
result = complexes
26172617
elif seen.float_:
26182618
result = floats
2619-
elif seen.int_:
2619+
elif seen.int_ or seen.uint_:
26202620
if convert_to_nullable_integer:
26212621
from pandas.core.arrays import IntegerArray
2622-
result = IntegerArray(ints, mask)
2622+
if seen.uint_:
2623+
result = IntegerArray(uints, mask)
2624+
else:
2625+
result = IntegerArray(ints, mask)
26232626
else:
26242627
result = floats
26252628
elif seen.nan_:

pandas/tests/dtypes/test_inference.py

+12
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,18 @@ def test_maybe_convert_objects_nullable_integer(self, exp):
863863

864864
tm.assert_extension_array_equal(result, exp)
865865

866+
@pytest.mark.parametrize(
867+
"dtype, val", [("int64", 1), ("uint64", np.iinfo(np.int64).max + 1)]
868+
)
869+
def test_maybe_convert_objects_nullable_none(self, dtype, val):
870+
# GH#50043
871+
arr = np.array([val, None, 3], dtype="object")
872+
result = lib.maybe_convert_objects(arr, convert_to_nullable_integer=True)
873+
expected = IntegerArray(
874+
np.array([val, 0, 3], dtype=dtype), np.array([False, True, False])
875+
)
876+
tm.assert_extension_array_equal(result, expected)
877+
866878
@pytest.mark.parametrize(
867879
"convert_to_masked_nullable, exp",
868880
[

0 commit comments

Comments
 (0)