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

Function test_array_slice

python/pyarrow/tests/test_array.py:526–564  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

524
525
526def test_array_slice():
527 arr = pa.array(range(10))
528
529 sliced = arr.slice(2)
530 expected = pa.array(range(2, 10))
531 assert sliced.equals(expected)
532
533 sliced2 = arr.slice(2, 4)
534 expected2 = pa.array(range(2, 6))
535 assert sliced2.equals(expected2)
536
537 # 0 offset
538 assert arr.slice(0).equals(arr)
539
540 # Slice past end of array
541 assert len(arr.slice(len(arr))) == 0
542 assert len(arr.slice(len(arr) + 2)) == 0
543 assert len(arr.slice(len(arr) + 2, 100)) == 0
544
545 with pytest.raises(IndexError):
546 arr.slice(-1)
547
548 with pytest.raises(ValueError):
549 arr.slice(2, -1)
550
551 # Test slice notation
552 assert arr[2:].equals(arr.slice(2))
553 assert arr[2:5].equals(arr.slice(2, 3))
554 assert arr[-5:].equals(arr.slice(len(arr) - 5))
555
556 n = len(arr)
557 for start in range(-n * 2, n * 2):
558 for stop in range(-n * 2, n * 2):
559 res = arr[start:stop]
560 res.validate()
561 expected = arr.to_pylist()[start:stop]
562 assert res.to_pylist() == expected
563 if np is not None:
564 assert res.to_numpy().tolist() == expected
565
566
567@pytest.mark.parametrize("scalar_type", [

Callers

nothing calls this directly

Calls 5

lenFunction · 0.85
equalsMethod · 0.80
arrayMethod · 0.45
sliceMethod · 0.45
validateMethod · 0.45

Tested by

no test coverage detected