From 7edf076a4430dd02dc066cc3ff92346a33a0544b Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Mon, 28 Oct 2024 16:27:59 -0400 Subject: [PATCH 1/3] initial fix --- pandas/core/dtypes/cast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 6ba07b1761557..6a5be2f5014e2 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1410,7 +1410,7 @@ def np_find_common_type(*dtypes: np.dtype) -> np.dtype: """ try: common_dtype = np.result_type(*dtypes) - if common_dtype.kind in "mMSU": + if common_dtype.kind in "mMSU" or common_dtype not in dtypes: # NumPy promotion currently (1.25) misbehaves for for times and strings, # so fall back to object (find_common_dtype did unless there # was only one dtype) From 160a4e33c4d5b8fb517487e00cd45ac04a9bf3c7 Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Mon, 28 Oct 2024 19:44:03 -0400 Subject: [PATCH 2/3] generalized cast and adjusted test cases --- pandas/core/dtypes/cast.py | 6 +++++- .../tests/dtypes/cast/test_find_common_type.py | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 6a5be2f5014e2..0bb8d73bdc508 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1410,7 +1410,11 @@ def np_find_common_type(*dtypes: np.dtype) -> np.dtype: """ try: common_dtype = np.result_type(*dtypes) - if common_dtype.kind in "mMSU" or common_dtype not in dtypes: + # GH 59609 + # Float point precision error when converting int64 and uint64 + if common_dtype.kind in "mMSU" or common_dtype.kind not in [ + dtype.kind for dtype in dtypes + ]: # NumPy promotion currently (1.25) misbehaves for for times and strings, # so fall back to object (find_common_dtype did unless there # was only one dtype) diff --git a/pandas/tests/dtypes/cast/test_find_common_type.py b/pandas/tests/dtypes/cast/test_find_common_type.py index 83ef7382fbe8a..d2ea6c8ef8c85 100644 --- a/pandas/tests/dtypes/cast/test_find_common_type.py +++ b/pandas/tests/dtypes/cast/test_find_common_type.py @@ -31,13 +31,15 @@ ((np.float16, np.float32), np.float32), ((np.float16, np.int16), np.float32), ((np.float32, np.int16), np.float32), - ((np.uint64, np.int64), np.float64), ((np.int16, np.float64), np.float64), ((np.float16, np.int64), np.float64), # Into others. ((np.complex128, np.int32), np.complex128), ((object, np.float32), object), ((object, np.int16), object), + # GH 59609 + # Float point precision error when converting int64 and uint64 + ((np.uint64, np.int64), object), # Bool with int. ((np.dtype("bool"), np.int64), object), ((np.dtype("bool"), np.int32), object), @@ -156,8 +158,17 @@ def test_interval_dtype(left, right): # i.e. numeric if right.subtype.kind in ["i", "u", "f"]: # both numeric -> common numeric subtype - expected = IntervalDtype(np.float64, "right") - assert result == expected + if ( + left.subtype.kind == "i" + and right.subtype.kind == "u" + or left.subtype.kind == "u" + and right.subtype.kind == "i" + ): + assert result == object + else: + expected = IntervalDtype(np.float64, "right") + assert result == expected + else: assert result == object From fd47414f6697f8ef52db8c0f20911ce5f9e89ba6 Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Mon, 28 Oct 2024 19:49:44 -0400 Subject: [PATCH 3/3] cleaned test case --- pandas/tests/dtypes/cast/test_find_common_type.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pandas/tests/dtypes/cast/test_find_common_type.py b/pandas/tests/dtypes/cast/test_find_common_type.py index d2ea6c8ef8c85..47e37fef52201 100644 --- a/pandas/tests/dtypes/cast/test_find_common_type.py +++ b/pandas/tests/dtypes/cast/test_find_common_type.py @@ -158,12 +158,7 @@ def test_interval_dtype(left, right): # i.e. numeric if right.subtype.kind in ["i", "u", "f"]: # both numeric -> common numeric subtype - if ( - left.subtype.kind == "i" - and right.subtype.kind == "u" - or left.subtype.kind == "u" - and right.subtype.kind == "i" - ): + if (left.subtype.kind, right.subtype.kind) in [("i", "u"), ("u", "i")]: assert result == object else: expected = IntervalDtype(np.float64, "right")