From 987ae5c484d04e55704a9bbdebbfd96787b0a4f8 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Thu, 22 Dec 2016 01:05:56 -0500 Subject: [PATCH] BUG: Patch maybe_convert_objects uint64 handling 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. --- pandas/src/inference.pyx | 2 +- pandas/tests/types/test_inference.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pandas/src/inference.pyx b/pandas/src/inference.pyx index 2f829417f9bb2..97d786bf82b7c 100644 --- a/pandas/src/inference.pyx +++ b/pandas/src/inference.pyx @@ -810,7 +810,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0, floats[i] = val complexes[i] = val if not seen_null: - seen_uint = seen_uint or (val > npy_int64_max) + seen_uint = seen_uint or (int(val) > npy_int64_max) seen_sint = seen_sint or (val < 0) if seen_uint and seen_sint: diff --git a/pandas/tests/types/test_inference.py b/pandas/tests/types/test_inference.py index f83ad51c2f648..fb8f3ca0b5b58 100644 --- a/pandas/tests/types/test_inference.py +++ b/pandas/tests/types/test_inference.py @@ -260,6 +260,13 @@ def test_maybe_convert_objects_uint64(self): exp = np.array([2**63], dtype=np.uint64) tm.assert_numpy_array_equal(lib.maybe_convert_objects(arr), exp) + # NumPy bug: can't compare uint64 to int64, as that + # results in both casting to float64, so we should + # make sure that this function is robust against it + arr = np.array([np.uint64(2**63)], dtype=object) + exp = np.array([2**63], dtype=np.uint64) + tm.assert_numpy_array_equal(lib.maybe_convert_objects(arr), exp) + arr = np.array([2, -1], dtype=object) exp = np.array([2, -1], dtype=np.int64) tm.assert_numpy_array_equal(lib.maybe_convert_objects(arr), exp)