MCPcopy Create free account
hub / github.com/apache/arrow / test_cast

Function test_cast

python/pyarrow/tests/test_compute.py:2231–2267  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2229
2230
2231def test_cast():
2232 arr = pa.array([1, 2, 3, 4], type='int64')
2233 options = pc.CastOptions(pa.int8())
2234
2235 with pytest.raises(TypeError):
2236 pc.cast(arr, target_type=None)
2237
2238 with pytest.raises(ValueError):
2239 pc.cast(arr, 'int32', options=options)
2240
2241 with pytest.raises(ValueError):
2242 pc.cast(arr, safe=True, options=options)
2243
2244 assert pc.cast(arr, options=options) == pa.array(
2245 [1, 2, 3, 4], type='int8')
2246
2247 arr = pa.array([2 ** 63 - 1], type='int64')
2248 allow_overflow_options = pc.CastOptions(
2249 pa.int32(), allow_int_overflow=True)
2250
2251 with pytest.raises(pa.ArrowInvalid):
2252 pc.cast(arr, 'int32')
2253
2254 assert pc.cast(arr, 'int32', safe=False) == pa.array([-1], type='int32')
2255
2256 assert pc.cast(arr, options=allow_overflow_options) == pa.array(
2257 [-1], type='int32')
2258
2259 arr = pa.array(
2260 [datetime.datetime(2010, 1, 1), datetime.datetime(2015, 1, 1)])
2261 expected = pa.array([1262304000000, 1420070400000], type='timestamp[ms]')
2262 assert pc.cast(arr, 'timestamp[ms]') == expected
2263
2264 arr = pa.array([[1, 2], [3, 4, 5]], type=pa.large_list(pa.int8()))
2265 expected = pa.array([["1", "2"], ["3", "4", "5"]],
2266 type=pa.list_(pa.utf8()))
2267 assert pc.cast(arr, expected.type) == expected
2268
2269
2270@pytest.mark.parametrize('value_type', [pa.date32(), pa.date64()])

Callers

nothing calls this directly

Calls 4

CastOptionsMethod · 0.80
datetimeMethod · 0.80
arrayMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected