Skip to content

Commit a86fc33

Browse files
mroeschkemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#47393: CI/TST: Don't require length for construct_1d_arraylike_from_scalar cast to float64
1 parent a70b494 commit a86fc33

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

pandas/core/construction.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,10 @@ def sanitize_array(
533533
if dtype is not None and is_float_dtype(data.dtype) and is_integer_dtype(dtype):
534534
# possibility of nan -> garbage
535535
try:
536-
subarr = _try_cast(data, dtype, copy, True)
536+
# GH 47391 numpy > 1.24 will raise a RuntimeError for nan -> int
537+
# casting aligning with IntCastingNaNError below
538+
with np.errstate(invalid="ignore"):
539+
subarr = _try_cast(data, dtype, copy, True)
537540
except IntCastingNaNError:
538541
warnings.warn(
539542
"In a future version, passing float-dtype values containing NaN "

pandas/core/dtypes/cast.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,9 @@ def construct_1d_arraylike_from_scalar(
19141914
value = maybe_unbox_datetimelike_tz_deprecation(value, dtype)
19151915

19161916
subarr = np.empty(length, dtype=dtype)
1917-
subarr.fill(value)
1917+
if length:
1918+
# GH 47391: numpy > 1.24 will raise filling np.nan into int dtypes
1919+
subarr.fill(value)
19181920

19191921
return subarr
19201922

pandas/core/reshape/merge.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -1201,23 +1201,27 @@ def _maybe_coerce_merge_keys(self) -> None:
12011201

12021202
# check whether ints and floats
12031203
elif is_integer_dtype(rk.dtype) and is_float_dtype(lk.dtype):
1204-
if not (lk == lk.astype(rk.dtype))[~np.isnan(lk)].all():
1205-
warnings.warn(
1206-
"You are merging on int and float "
1207-
"columns where the float values "
1208-
"are not equal to their int representation.",
1209-
UserWarning,
1210-
)
1204+
# GH 47391 numpy > 1.24 will raise a RuntimeError for nan -> int
1205+
with np.errstate(invalid="ignore"):
1206+
if not (lk == lk.astype(rk.dtype))[~np.isnan(lk)].all():
1207+
warnings.warn(
1208+
"You are merging on int and float "
1209+
"columns where the float values "
1210+
"are not equal to their int representation.",
1211+
UserWarning,
1212+
)
12111213
continue
12121214

12131215
elif is_float_dtype(rk.dtype) and is_integer_dtype(lk.dtype):
1214-
if not (rk == rk.astype(lk.dtype))[~np.isnan(rk)].all():
1215-
warnings.warn(
1216-
"You are merging on int and float "
1217-
"columns where the float values "
1218-
"are not equal to their int representation.",
1219-
UserWarning,
1220-
)
1216+
# GH 47391 numpy > 1.24 will raise a RuntimeError for nan -> int
1217+
with np.errstate(invalid="ignore"):
1218+
if not (rk == rk.astype(lk.dtype))[~np.isnan(rk)].all():
1219+
warnings.warn(
1220+
"You are merging on int and float "
1221+
"columns where the float values "
1222+
"are not equal to their int representation.",
1223+
UserWarning,
1224+
)
12211225
continue
12221226

12231227
# let's infer and see if we are ok

0 commit comments

Comments
 (0)