mirror of
https://github.com/apache/arrow.git
synced 2026-10-29 00:56:31 +08:00
Add some basic casting unit tests in Python
Change-Id: I3aacb21ca4f4090cee83da8e730c2a19ca2d0d99
This commit is contained in:
1 parent
c1b4570961
commit
1d07b75626
3 files changed
+68
-4
No files matched your search
@@ -324,9 +324,6 @@ TEST_F(TestCast, UnsupportedTarget) {
|
||||
}
|
||||
|
||||
TEST_F(TestCast, DateTimeZeroCopy) {
|
||||
CastOptions options;
|
||||
options.allow_int_overflow = false;
|
||||
|
||||
vector<bool> is_valid = {true, false, true, true, true};
|
||||
|
||||
std::shared_ptr<Array> arr;
|
||||
|
||||
@@ -211,6 +211,73 @@ def test_list_from_arrays():
|
||||
assert result.equals(expected)
|
||||
|
||||
|
||||
def _check_cast_case(case, safe=True):
|
||||
in_data, in_type, out_data, out_type = case
|
||||
|
||||
in_arr = pa.Array.from_pandas(in_data, type=in_type)
|
||||
|
||||
casted = in_arr.cast(out_type, safe=safe)
|
||||
expected = pa.Array.from_pandas(out_data, type=out_type)
|
||||
assert casted.equals(expected)
|
||||
|
||||
|
||||
def test_cast_integers_safe():
|
||||
safe_cases = [
|
||||
(np.array([0, 1, 2, 3], dtype='i1'), pa.int8(),
|
||||
np.array([0, 1, 2, 3], dtype='i4'), pa.int32()),
|
||||
(np.array([0, 1, 2, 3], dtype='i1'), pa.int8(),
|
||||
np.array([0, 1, 2, 3], dtype='u4'), pa.uint16()),
|
||||
(np.array([0, 1, 2, 3], dtype='i1'), pa.int8(),
|
||||
np.array([0, 1, 2, 3], dtype='u1'), pa.uint8()),
|
||||
(np.array([0, 1, 2, 3], dtype='i1'), pa.int8(),
|
||||
np.array([0, 1, 2, 3], dtype='f8'), pa.float64())
|
||||
]
|
||||
|
||||
for case in safe_cases:
|
||||
_check_cast_case(case)
|
||||
|
||||
unsafe_cases = [
|
||||
(np.array([50000], dtype='i4'), pa.int32(), pa.int16()),
|
||||
(np.array([70000], dtype='i4'), pa.int32(), pa.uint16()),
|
||||
(np.array([-1], dtype='i4'), pa.int32(), pa.uint16()),
|
||||
(np.array([50000], dtype='u2'), pa.uint16(), pa.int16())
|
||||
]
|
||||
for in_data, in_type, out_type in unsafe_cases:
|
||||
in_arr = pa.Array.from_pandas(in_data, type=in_type)
|
||||
|
||||
with pytest.raises(pa.ArrowInvalid):
|
||||
in_arr.cast(out_type)
|
||||
|
||||
|
||||
def test_cast_integers_unsafe():
|
||||
# We let NumPy do the unsafe casting
|
||||
unsafe_cases = [
|
||||
(np.array([50000], dtype='i4'), pa.int32(),
|
||||
np.array([50000], dtype='i2'), pa.int16()),
|
||||
(np.array([70000], dtype='i4'), pa.int32(),
|
||||
np.array([70000], dtype='u2'), pa.uint16()),
|
||||
(np.array([-1], dtype='i4'), pa.int32(),
|
||||
np.array([-1], dtype='u2'), pa.uint16()),
|
||||
(np.array([50000], dtype='u2'), pa.uint16(),
|
||||
np.array([50000], dtype='i2'), pa.int16())
|
||||
]
|
||||
|
||||
for case in unsafe_cases:
|
||||
_check_cast_case(case, safe=False)
|
||||
|
||||
|
||||
def test_cast_signed_to_unsigned():
|
||||
safe_cases = [
|
||||
(np.array([0, 1, 2, 3], dtype='i1'), pa.uint8(),
|
||||
np.array([0, 1, 2, 3], dtype='u1'), pa.uint8()),
|
||||
(np.array([0, 1, 2, 3], dtype='i2'), pa.uint16(),
|
||||
np.array([0, 1, 2, 3], dtype='u2'), pa.uint16())
|
||||
]
|
||||
|
||||
for case in safe_cases:
|
||||
_check_cast_case(case)
|
||||
|
||||
|
||||
def test_simple_type_construction():
|
||||
result = pa.lib.TimestampType()
|
||||
with pytest.raises(TypeError):
|
||||
|
||||
@@ -288,7 +288,7 @@ class TestPandasConversion(unittest.TestCase):
|
||||
schema = pa.schema([field])
|
||||
self._check_pandas_roundtrip(df, expected_schema=schema)
|
||||
|
||||
def test_all_nulls_to_numeric(self):
|
||||
def test_all_nulls_cast_numeric(self):
|
||||
arr = np.array([None], dtype=object)
|
||||
|
||||
def _check_type(t):
|
||||
|
||||
Reference in new issue
Block a user