def push(array, n, axis): from bottleneck import push if is_duck_dask_array(array): return dask_array_ops.push(array, n, axis) else: return push(array, n, axis)
def test_push(): """Test push""" ns = (0, 1, 2, 3, 4, 5, None) a = np.array([np.nan, 1, 2, np.nan, np.nan, np.nan, np.nan, 3, np.nan]) for n in ns: actual = bn.push(a.copy(), n=n) desired = bn.slow.push(a.copy(), n=n) assert_array_equal(actual, desired, "failed on n=%s" % str(n))
def test_push_2(): "Test push #2." ns = (np.inf, -1, 0, 1, 2, 3, 4, 5, 1.1, 1.5, 1.9) arr = np.array([np.nan, 1, 2, np.nan, np.nan, np.nan, np.nan, 3, np.nan]) for n in ns: actual = bn.push(arr.copy(), n=n) desired = bn.slow.push(arr.copy(), n=n) assert_array_equal(actual, desired, "failed on n=%s" % str(n))
def test_push(): "Test push" ns = (0, 1, 2, 3, 4, 5) a = np.array([np.nan, 1, 2, np.nan, np.nan, np.nan, np.nan, 3, np.nan]) for n in ns: actual = bn.push(a.copy(), n=n) desired = bn.slow.push(a.copy(), n=n) assert_array_equal(actual, desired, "failed on n=%s" % str(n))
def _bfill(arr, n=None, axis=-1): '''inverse of ffill''' import bottleneck as bn arr = np.flip(arr, axis=axis) # fill arr = bn.push(arr, axis=axis, n=n) # reverse back to original return np.flip(arr, axis=axis)
def test_push_dask(): import bottleneck import dask.array array = np.array([np.nan, 1, 2, 3, np.nan, np.nan, np.nan, np.nan, 4, 5, np.nan, 6]) for n in [None, 1, 2, 3, 4, 5, 11]: expected = bottleneck.push(array, axis=0, n=n) for c in range(1, 11): with raise_if_dask_computes(): actual = push(dask.array.from_array(array, chunks=c), axis=0, n=n) np.testing.assert_equal(actual, expected) # some chunks of size-1 with NaN with raise_if_dask_computes(): actual = push( dask.array.from_array(array, chunks=(1, 2, 3, 2, 2, 1, 1)), axis=0, n=n ) np.testing.assert_equal(actual, expected)
def time_push(self, dtype, shape): bn.push(self.arr)
import numpy as np import bottleneck as bn a = np.array([1, 2, np.nan, 4, 5]) print(bn.nansum(a)) print(bn.move_median(a, window=2, min_count=1)) bn.bench() bn.push()