Skip to content

Commit b78877a

Browse files
committed
fix(convert_numeric): allow direct conversion to python integer
1 parent c8c6e76 commit b78877a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

pandas/_libs/lib.pyx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,18 +2426,24 @@ def maybe_convert_numeric(
24262426

24272427
val = int(val)
24282428
seen.saw_int(val)
2429+
pyints[i] = val
24292430

24302431
if val >= 0:
24312432
if val <= oUINT64_MAX:
24322433
uints[i] = val
2433-
else:
2434+
elif seen.coerce_numeric:
24342435
seen.float_ = True
2436+
else:
2437+
seen.overflow_ = True
24352438

24362439
if oINT64_MIN <= val <= oINT64_MAX:
24372440
ints[i] = val
24382441

24392442
if val < oINT64_MIN or (seen.sint_ and seen.uint_):
2440-
seen.float_ = True
2443+
if seen.coerce_numeric:
2444+
seen.float_ = True
2445+
else:
2446+
seen.overflow_ = True
24412447

24422448
elif util.is_bool_object(val):
24432449
floats[i] = uints[i] = ints[i] = bools[i] = val

pandas/tests/dtypes/test_inference.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,26 @@ def test_convert_int_overflow(self, value):
729729
result = lib.maybe_convert_objects(arr)
730730
tm.assert_numpy_array_equal(arr, result)
731731

732+
@pytest.mark.parametrize(
733+
"value, expected_value",
734+
[
735+
(-(1 << 65), -(1 << 65)),
736+
(1 << 65, 1 << 65),
737+
(str(1 << 65), 1 << 65),
738+
(f"-{1 << 65}", -(1 << 65)),
739+
],
740+
)
741+
@pytest.mark.parametrize("coerce_numeric", [False, True])
742+
def test_convert_numeric_overflow(self, value, expected_value, coerce_numeric):
743+
arr = np.array([value], dtype=object)
744+
expected = np.array([expected_value], dtype=float if coerce_numeric else object)
745+
result, _ = lib.maybe_convert_numeric(
746+
arr,
747+
set(),
748+
coerce_numeric=coerce_numeric,
749+
)
750+
tm.assert_numpy_array_equal(result, expected)
751+
732752
@pytest.mark.parametrize("val", [None, np.nan, float("nan")])
733753
@pytest.mark.parametrize("dtype", ["M8[ns]", "m8[ns]"])
734754
def test_maybe_convert_objects_nat_inference(self, val, dtype):

0 commit comments

Comments
 (0)