Skip to content

Commit 7263573

Browse files
committed
Add a test for safe cast from NumPy float64 array to integer
1 parent 7a36fcc commit 7263573

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

python/pyarrow/tests/test_pandas.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,14 +3278,28 @@ def test_error_sparse(self):
32783278

32793279

32803280
def test_safe_cast_from_float_with_nans_to_int():
3281-
# TODO(kszucs): write tests for creating Date32 and Date64 arrays, see
3282-
# ARROW-4258 and https://github.com/apache/arrow/pull/3395
32833281
values = pd.Series([1, 2, None, 4])
32843282
arr = pa.Array.from_pandas(values, type=pa.int32(), safe=True)
32853283
expected = pa.array([1, 2, None, 4], type=pa.int32())
32863284
assert arr.equals(expected)
32873285

32883286

3287+
def test_safe_cast_from_float_with_fraction_to_int_fails():
3288+
values = pd.Series([1.0, 2.5, 3.0], dtype='float32')
3289+
with pytest.raises(pa.ArrowInvalid, match="loat.*truncated"):
3290+
pa.Array.from_pandas(values, type=pa.int32(), safe=True)
3291+
3292+
values = pd.Series([1.1, 2.0, 3.0], dtype='float64')
3293+
with pytest.raises(pa.ArrowInvalid, match="loat.*truncated"):
3294+
pa.Array.from_pandas(values, type=pa.int32(), safe=True)
3295+
3296+
# safe=False should allow it (with truncation)
3297+
values = pd.Series([1.5, 2.9, 3.1], dtype='float64')
3298+
arr = pa.Array.from_pandas(values, type=pa.int32(), safe=False)
3299+
expected = pa.array([1, 2, 3], type=pa.int32())
3300+
assert arr.equals(expected)
3301+
3302+
32893303
def _fully_loaded_dataframe_example():
32903304
index = pd.MultiIndex.from_arrays([
32913305
pd.date_range('2000-01-01', periods=5).repeat(2),

0 commit comments

Comments
 (0)