Skip to content

Commit 868d114

Browse files
gfyoungjreback
authored andcommitted
BUG: Patch maybe_convert_objects uint64 handling (#14951)
Makes method robust against known NumPy bug that you can't compare uint64 against int64 because they are casted to float64 during the comparison, causing truncation.
1 parent 35beea8 commit 868d114

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pandas/src/inference.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
810810
floats[i] = <float64_t> val
811811
complexes[i] = <double complex> val
812812
if not seen_null:
813-
seen_uint = seen_uint or (val > npy_int64_max)
813+
seen_uint = seen_uint or (int(val) > npy_int64_max)
814814
seen_sint = seen_sint or (val < 0)
815815

816816
if seen_uint and seen_sint:

pandas/tests/types/test_inference.py

+7
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ def test_maybe_convert_objects_uint64(self):
260260
exp = np.array([2**63], dtype=np.uint64)
261261
tm.assert_numpy_array_equal(lib.maybe_convert_objects(arr), exp)
262262

263+
# NumPy bug: can't compare uint64 to int64, as that
264+
# results in both casting to float64, so we should
265+
# make sure that this function is robust against it
266+
arr = np.array([np.uint64(2**63)], dtype=object)
267+
exp = np.array([2**63], dtype=np.uint64)
268+
tm.assert_numpy_array_equal(lib.maybe_convert_objects(arr), exp)
269+
263270
arr = np.array([2, -1], dtype=object)
264271
exp = np.array([2, -1], dtype=np.int64)
265272
tm.assert_numpy_array_equal(lib.maybe_convert_objects(arr), exp)

0 commit comments

Comments
 (0)