Skip to content

Commit

Permalink
BUG: Fix bug in maybe_convert_objects with None and nullable (#50043)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl committed Dec 3, 2022
1 parent d88e0fb commit 1d5ce5b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pandas/_libs/lib.pyx
Expand Up @@ -2446,7 +2446,7 @@ def maybe_convert_objects(ndarray[object] objects,
seen.int_ = True
floats[i] = <float64_t>val
complexes[i] = <double complex>val
if not seen.null_:
if not seen.null_ or convert_to_nullable_integer:
seen.saw_int(val)

if ((seen.uint_ and seen.sint_) or
Expand Down Expand Up @@ -2616,10 +2616,13 @@ def maybe_convert_objects(ndarray[object] objects,
result = complexes
elif seen.float_:
result = floats
elif seen.int_:
elif seen.int_ or seen.uint_:
if convert_to_nullable_integer:
from pandas.core.arrays import IntegerArray
result = IntegerArray(ints, mask)
if seen.uint_:
result = IntegerArray(uints, mask)
else:
result = IntegerArray(ints, mask)
else:
result = floats
elif seen.nan_:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Expand Up @@ -863,6 +863,18 @@ def test_maybe_convert_objects_nullable_integer(self, exp):

tm.assert_extension_array_equal(result, exp)

@pytest.mark.parametrize(
"dtype, val", [("int64", 1), ("uint64", np.iinfo(np.int64).max + 1)]
)
def test_maybe_convert_objects_nullable_none(self, dtype, val):
# GH#50043
arr = np.array([val, None, 3], dtype="object")
result = lib.maybe_convert_objects(arr, convert_to_nullable_integer=True)
expected = IntegerArray(
np.array([val, 0, 3], dtype=dtype), np.array([False, True, False])
)
tm.assert_extension_array_equal(result, expected)

@pytest.mark.parametrize(
"convert_to_masked_nullable, exp",
[
Expand Down

0 comments on commit 1d5ce5b

Please sign in to comment.