Skip to content

BUG: Fix bug in maybe_convert_objects with None and nullable #50043

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

Merged
merged 2 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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