From 689fe59ff78e527b7f9ae3447d5e7e1a8a6acee3 Mon Sep 17 00:00:00 2001 From: Dane Pitkin Date: Wed, 10 May 2023 16:45:51 -0400 Subject: [PATCH] Add tests for GH-35040 and GH-34901 --- python/pyarrow/tests/test_scalars.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/python/pyarrow/tests/test_scalars.py b/python/pyarrow/tests/test_scalars.py index fc3c5e549eb33..f6bb168a6c189 100644 --- a/python/pyarrow/tests/test_scalars.py +++ b/python/pyarrow/tests/test_scalars.py @@ -295,7 +295,7 @@ def test_cast(): pa.scalar('foo').cast('int32') -def test_timestamp_to_string_cast(): +def test_cast_timestamp_to_string(): # GH-35370 pytest.importorskip("pytz") import pytz @@ -304,6 +304,26 @@ def test_timestamp_to_string_cast(): assert ts.cast(pa.string()) == pa.scalar('2000-01-01 00:00:00.000000000Z') +def test_cast_float_to_int(): + # GH-35040 + float_scalar = pa.scalar(1.5, type=pa.float64()) + unsafe_cast = float_scalar.cast(pa.int64(), safe=False) + expected_unsafe_cast = pa.scalar(1, type=pa.int64()) + assert unsafe_cast == expected_unsafe_cast + with pytest.raises(pa.ArrowInvalid): + float_scalar.cast(pa.int64(), safe=True) + + +def test_cast_int_to_float(): + # GH-34901 + int_scalar = pa.scalar(18014398509481983, type=pa.int64()) + unsafe_cast = int_scalar.cast(pa.float64(), safe=False) + expected_unsafe_cast = pa.scalar(18014398509481983.0, type=pa.float64()) + assert unsafe_cast == expected_unsafe_cast + with pytest.raises(pa.ArrowInvalid): + int_scalar.cast(pa.float64(), safe=True) + + @pytest.mark.pandas def test_timestamp(): import pandas as pd