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

Function test_cumulative_sum

python/pyarrow/tests/test_compute.py:3570–3619  ·  view source on GitHub ↗
(start, skip_nulls)

Source from the content-addressed store, hash-verified

3568@pytest.mark.parametrize('start', (1.25, 10.5, -10.5))
3569@pytest.mark.parametrize('skip_nulls', (True, False))
3570def test_cumulative_sum(start, skip_nulls):
3571 # Exact tests (e.g., integral types)
3572 start_int = int(start)
3573 starts = [None, start_int, pa.scalar(start_int, type=pa.int8()),
3574 pa.scalar(start_int, type=pa.int64())]
3575 for strt in starts:
3576 arrays = [
3577 pa.array([1, 2, 3]),
3578 pa.array([0, None, 20, 30]),
3579 pa.chunked_array([[0, None], [20, 30]])
3580 ]
3581 expected_arrays = [
3582 pa.array([1, 3, 6]),
3583 pa.array([0, None, 20, 50])
3584 if skip_nulls else pa.array([0, None, None, None]),
3585 pa.chunked_array([[0, None, 20, 50]])
3586 if skip_nulls else pa.chunked_array([[0, None, None, None]])
3587 ]
3588 for i, arr in enumerate(arrays):
3589 result = pc.cumulative_sum(arr, start=strt, skip_nulls=skip_nulls)
3590 # Add `start` offset to expected array before comparing
3591 expected = pc.add(expected_arrays[i], strt if strt is not None
3592 else 0)
3593 assert result.equals(expected)
3594
3595 starts = [None, start, pa.scalar(start, type=pa.float32()),
3596 pa.scalar(start, type=pa.float64())]
3597 for strt in starts:
3598 arrays = [
3599 pa.array([1.125, 2.25, 3.03125]),
3600 pa.array([1, np.nan, 2, -3, 4, 5]),
3601 pa.array([1, np.nan, None, 3, None, 5])
3602 ]
3603 expected_arrays = [
3604 np.array([1.125, 3.375, 6.40625]),
3605 np.array([1, np.nan, np.nan, np.nan, np.nan, np.nan]),
3606 np.array([1, np.nan, None, np.nan, None, np.nan])
3607 if skip_nulls else np.array([1, np.nan, None, None, None, None])
3608 ]
3609 for i, arr in enumerate(arrays):
3610 result = pc.cumulative_sum(arr, start=strt, skip_nulls=skip_nulls)
3611 # Add `start` offset to expected array before comparing
3612 expected = pc.add(expected_arrays[i], strt if strt is not None
3613 else 0)
3614 np.testing.assert_array_almost_equal(result.to_numpy(
3615 zero_copy_only=False), expected.to_numpy(zero_copy_only=False))
3616
3617 for strt in ['a', pa.scalar('arrow'), 1.1]:
3618 with pytest.raises(pa.ArrowInvalid):
3619 pc.cumulative_sum([1, 2, 3], start=strt)
3620
3621
3622@pytest.mark.numpy

Callers

nothing calls this directly

Calls 4

chunked_arrayMethod · 0.80
equalsMethod · 0.80
scalarMethod · 0.45
arrayMethod · 0.45

Tested by

no test coverage detected