Skip to content

Commit

Permalink
Backport PR #47393 on branch 1.4.x (CI/TST: Don't require length for …
Browse files Browse the repository at this point in the history
…construct_1d_arraylike_from_scalar cast to float64) (#47460)

* Backport PR #47393: CI/TST: Don't require length for construct_1d_arraylike_from_scalar cast to float64

Co-authored-by: Matthew Roeschke <[email protected]>
Co-authored-by: Simon Hawkins <[email protected]>
  • Loading branch information
3 people authored Jun 22, 2022
1 parent a70b494 commit 30a3b98
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
5 changes: 4 additions & 1 deletion pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -2218,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
Expand Down
32 changes: 18 additions & 14 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 30a3b98

Please sign in to comment.