Skip to content

Commit 4959b78

Browse files
fix np.ndarray.astype runtimewarning
1 parent 1863adb commit 4959b78

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ def convert_dtypes(
960960
if len(arr) < len(input_array) and not is_nan_na():
961961
# In the presence of NaNs, we cannot convert to IntegerDtype
962962
pass
963-
elif (arr.astype(int) == arr).all():
963+
elif np.array_equal(arr, np.trunc(arr), equal_nan=True):
964964
inferred_dtype = target_int_dtype
965965
else:
966966
inferred_dtype = input_array.dtype
@@ -987,7 +987,7 @@ def convert_dtypes(
987987
if len(arr) < len(input_array) and not is_nan_na():
988988
# In the presence of NaNs, we can't convert to IntegerDtype
989989
inferred_dtype = inferred_float_dtype
990-
elif (arr.astype(int) == arr).all():
990+
elif np.array_equal(arr, np.trunc(arr)):
991991
inferred_dtype = pandas_dtype_func("Int64")
992992
else:
993993
inferred_dtype = inferred_float_dtype

pandas/tests/frame/methods/test_convert_dtypes.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,30 @@ def test_convert_dtype_pyarrow_timezone_preserve(self):
228228
result = df.convert_dtypes(dtype_backend="pyarrow")
229229
expected = df.copy()
230230
tm.assert_frame_equal(result, expected)
231+
232+
def test_convert_dtype_pyarrow_int64_limits_warning(self):
233+
# GH 58485
234+
pytest.importorskip("pyarrow")
235+
data = {
236+
"a": [
237+
-9223372036854775808,
238+
4611686018427387904,
239+
9223372036854775807,
240+
None,
241+
],
242+
"b": [
243+
-9223372036854775808,
244+
4611686018427387904,
245+
9223372036854775807,
246+
None,
247+
],
248+
"c": [
249+
-9223372036854775808,
250+
4611686018427387904,
251+
9223372036854775807,
252+
None,
253+
],
254+
}
255+
result = pd.DataFrame(data).convert_dtypes(dtype_backend="pyarrow")
256+
expected = pd.DataFrame(data, dtype="int64[pyarrow]")
257+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)