From a86fc3306d663c69b26bc30296c2380177d3cd5f Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Wed, 22 Jun 2022 05:07:11 -0700 Subject: [PATCH 1/3] Backport PR #47393: CI/TST: Don't require length for construct_1d_arraylike_from_scalar cast to float64 --- pandas/core/construction.py | 5 ++++- pandas/core/dtypes/cast.py | 4 +++- pandas/core/reshape/merge.py | 32 ++++++++++++++++++-------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 2595cff5c43c4..957fcf4ac10fc 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -533,7 +533,10 @@ def sanitize_array( if dtype is not None and is_float_dtype(data.dtype) and is_integer_dtype(dtype): # possibility of nan -> garbage try: - subarr = _try_cast(data, dtype, copy, True) + # GH 47391 numpy > 1.24 will raise a RuntimeError for nan -> int + # casting aligning with IntCastingNaNError below + with np.errstate(invalid="ignore"): + subarr = _try_cast(data, dtype, copy, True) except IntCastingNaNError: warnings.warn( "In a future version, passing float-dtype values containing NaN " diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index e70fd443d61e8..af6b701092162 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1914,7 +1914,9 @@ def construct_1d_arraylike_from_scalar( value = maybe_unbox_datetimelike_tz_deprecation(value, dtype) subarr = np.empty(length, dtype=dtype) - subarr.fill(value) + if length: + # GH 47391: numpy > 1.24 will raise filling np.nan into int dtypes + subarr.fill(value) return subarr diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 3a39713f18d65..d3ec9fec4640d 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1201,23 +1201,27 @@ def _maybe_coerce_merge_keys(self) -> None: # check whether ints and floats elif is_integer_dtype(rk.dtype) and is_float_dtype(lk.dtype): - if not (lk == lk.astype(rk.dtype))[~np.isnan(lk)].all(): - warnings.warn( - "You are merging on int and float " - "columns where the float values " - "are not equal to their int representation.", - UserWarning, - ) + # GH 47391 numpy > 1.24 will raise a RuntimeError for nan -> int + with np.errstate(invalid="ignore"): + if not (lk == lk.astype(rk.dtype))[~np.isnan(lk)].all(): + warnings.warn( + "You are merging on int and float " + "columns where the float values " + "are not equal to their int representation.", + UserWarning, + ) continue elif is_float_dtype(rk.dtype) and is_integer_dtype(lk.dtype): - if not (rk == rk.astype(lk.dtype))[~np.isnan(rk)].all(): - warnings.warn( - "You are merging on int and float " - "columns where the float values " - "are not equal to their int representation.", - UserWarning, - ) + # GH 47391 numpy > 1.24 will raise a RuntimeError for nan -> int + with np.errstate(invalid="ignore"): + if not (rk == rk.astype(lk.dtype))[~np.isnan(rk)].all(): + warnings.warn( + "You are merging on int and float " + "columns where the float values " + "are not equal to their int representation.", + UserWarning, + ) continue # let's infer and see if we are ok From 9151f60209ebb08ea6604ad1aaa68498e7acb25e Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 22 Jun 2022 15:47:57 +0100 Subject: [PATCH 2/3] another ignore --- pandas/core/dtypes/cast.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index af6b701092162..4d40b206853df 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -2220,7 +2220,10 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any: if isinstance(element, np.ndarray) and element.dtype.kind == "f": # If all can be losslessly cast to integers, then we can hold them # We do something similar in putmask_smart - casted = element.astype(dtype) + + # GH 47391 numpy > 1.24 will raise a RuntimeError for nan -> int + with np.errstate(invalid="ignore"): + casted = element.astype(dtype) comp = casted == element if comp.all(): return element From c545fd5614f856eb68a74ec444d1b850c465ce1e Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 22 Jun 2022 15:48:19 +0100 Subject: [PATCH 3/3] lint --- 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 4d40b206853df..1f7789e72be2c 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -2220,7 +2220,7 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any: if isinstance(element, np.ndarray) and element.dtype.kind == "f": # If all can be losslessly cast to integers, then we can hold them # We do something similar in putmask_smart - + # GH 47391 numpy > 1.24 will raise a RuntimeError for nan -> int with np.errstate(invalid="ignore"): casted = element.astype(dtype)