Function reference

Bottleneck provides the following functions:

reduce

nansum, nanmean, nanstd, nanvar, nanmin, nanmax, median, nanmedian, ss, nanargmin, nanargmax, anynan, allnan

non-reduce

replace

non-reduce with axis

rankdata, nanrankdata, partition, argpartition, push

moving window

move_sum, move_mean, move_std, move_var, move_min, move_max, move_argmin, move_argmax, move_median, move_rank

Reduce

Functions the reduce the input array along the specified axis.


bottleneck.nansum(a, axis=None)

Sum of array elements along given axis treating NaNs as zero.

The data type (dtype) of the output is the same as the input. On 64-bit operating systems, 32-bit input is NOT upcast to 64-bit accumulator and return values.

Parameters
aarray_like

Array containing numbers whose sum is desired. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the sum is computed. The default (axis=None) is to compute the sum of the flattened array.

Returns
yndarray

An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned.

Notes

No error is raised on overflow.

If positive or negative infinity are present the result is positive or negative infinity. But if both positive and negative infinity are present, the result is Not A Number (NaN).

Examples

>>> bn.nansum(1)
1
>>> bn.nansum([1])
1
>>> bn.nansum([1, np.nan])
1.0
>>> a = np.array([[1, 1], [1, np.nan]])
>>> bn.nansum(a)
3.0
>>> bn.nansum(a, axis=0)
array([ 2.,  1.])

When positive infinity and negative infinity are present:

>>> bn.nansum([1, np.nan, np.inf])
inf
>>> bn.nansum([1, np.nan, np.NINF])
-inf
>>> bn.nansum([1, np.nan, np.inf, np.NINF])
nan

bottleneck.nanmean(a, axis=None)

Mean of array elements along given axis ignoring NaNs.

float64 intermediate and return values are used for integer inputs.

Parameters
aarray_like

Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the means are computed. The default (axis=None) is to compute the mean of the flattened array.

Returns
yndarray

An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. float64 intermediate and return values are used for integer inputs.

See also

bottleneck.nanmedian

Median along specified axis, ignoring NaNs.

Notes

No error is raised on overflow. (The sum is computed and then the result is divided by the number of non-NaN elements.)

If positive or negative infinity are present the result is positive or negative infinity. But if both positive and negative infinity are present, the result is Not A Number (NaN).

Examples

>>> bn.nanmean(1)
1.0
>>> bn.nanmean([1])
1.0
>>> bn.nanmean([1, np.nan])
1.0
>>> a = np.array([[1, 4], [1, np.nan]])
>>> bn.nanmean(a)
2.0
>>> bn.nanmean(a, axis=0)
array([ 1.,  4.])

When positive infinity and negative infinity are present:

>>> bn.nanmean([1, np.nan, np.inf])
inf
>>> bn.nanmean([1, np.nan, np.NINF])
-inf
>>> bn.nanmean([1, np.nan, np.inf, np.NINF])
nan

bottleneck.nanstd(a, axis=None, ddof=0)

Standard deviation along the specified axis, ignoring NaNs.

float64 intermediate and return values are used for integer inputs.

Instead of a faster one-pass algorithm, a more stable two-pass algorithm is used.

An example of a one-pass algorithm:

>>> np.sqrt((a*a).mean() - a.mean()**2)

An example of a two-pass algorithm:

>>> np.sqrt(((a - a.mean())**2).mean())

Note in the two-pass algorithm the mean must be found (first pass) before the squared deviation (second pass) can be found.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the standard deviation is computed. The default (axis=None) is to compute the standard deviation of the flattened array.

ddofint, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of non-NaN elements. By default ddof is zero.

Returns
yndarray

An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. float64 intermediate and return values are used for integer inputs. If ddof is >= the number of non-NaN elements in a slice or the slice contains only NaNs, then the result for that slice is NaN.

See also

bottleneck.nanvar

Variance along specified axis ignoring NaNs

Notes

If positive or negative infinity are present the result is Not A Number (NaN).

Examples

>>> bn.nanstd(1)
0.0
>>> bn.nanstd([1])
0.0
>>> bn.nanstd([1, np.nan])
0.0
>>> a = np.array([[1, 4], [1, np.nan]])
>>> bn.nanstd(a)
1.4142135623730951
>>> bn.nanstd(a, axis=0)
array([ 0.,  0.])

When positive infinity or negative infinity are present NaN is returned:

>>> bn.nanstd([1, np.nan, np.inf])
nan

bottleneck.nanvar(a, axis=None, ddof=0)

Variance along the specified axis, ignoring NaNs.

float64 intermediate and return values are used for integer inputs.

Instead of a faster one-pass algorithm, a more stable two-pass algorithm is used.

An example of a one-pass algorithm:

>>> (a*a).mean() - a.mean()**2

An example of a two-pass algorithm:

>>> ((a - a.mean())**2).mean()

Note in the two-pass algorithm the mean must be found (first pass) before the squared deviation (second pass) can be found.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the variance is computed. The default (axis=None) is to compute the variance of the flattened array.

ddofint, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of non_NaN elements. By default ddof is zero.

Returns
yndarray

An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. float64 intermediate and return values are used for integer inputs. If ddof is >= the number of non-NaN elements in a slice or the slice contains only NaNs, then the result for that slice is NaN.

See also

bottleneck.nanstd

Standard deviation along specified axis ignoring NaNs.

Notes

If positive or negative infinity are present the result is Not A Number (NaN).

Examples

>>> bn.nanvar(1)
0.0
>>> bn.nanvar([1])
0.0
>>> bn.nanvar([1, np.nan])
0.0
>>> a = np.array([[1, 4], [1, np.nan]])
>>> bn.nanvar(a)
2.0
>>> bn.nanvar(a, axis=0)
array([ 0.,  0.])

When positive infinity or negative infinity are present NaN is returned:

>>> bn.nanvar([1, np.nan, np.inf])
nan

bottleneck.nanmin(a, axis=None)

Minimum values along specified axis, ignoring NaNs.

When all-NaN slices are encountered, NaN is returned for that slice.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the minimum is computed. The default (axis=None) is to compute the minimum of the flattened array.

Returns
yndarray

An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. The same dtype as a is returned.

See also

bottleneck.nanmax

Maximum along specified axis, ignoring NaNs.

bottleneck.nanargmin

Indices of minimum values along axis, ignoring NaNs.

Examples

>>> bn.nanmin(1)
1
>>> bn.nanmin([1])
1
>>> bn.nanmin([1, np.nan])
1.0
>>> a = np.array([[1, 4], [1, np.nan]])
>>> bn.nanmin(a)
1.0
>>> bn.nanmin(a, axis=0)
array([ 1.,  4.])

bottleneck.nanmax(a, axis=None)

Maximum values along specified axis, ignoring NaNs.

When all-NaN slices are encountered, NaN is returned for that slice.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the maximum is computed. The default (axis=None) is to compute the maximum of the flattened array.

Returns
yndarray

An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. The same dtype as a is returned.

See also

bottleneck.nanmin

Minimum along specified axis, ignoring NaNs.

bottleneck.nanargmax

Indices of maximum values along axis, ignoring NaNs.

Examples

>>> bn.nanmax(1)
1
>>> bn.nanmax([1])
1
>>> bn.nanmax([1, np.nan])
1.0
>>> a = np.array([[1, 4], [1, np.nan]])
>>> bn.nanmax(a)
4.0
>>> bn.nanmax(a, axis=0)
array([ 1.,  4.])

bottleneck.median(a, axis=None)

Median of array elements along given axis.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the median is computed. The default (axis=None) is to compute the median of the flattened array.

Returns
yndarray

An array with the same shape as a, except that the specified axis has been removed. If a is a 0d array, or if axis is None, a scalar is returned. float64 return values are used for integer inputs. NaN is returned for a slice that contains one or more NaNs.

See also

bottleneck.nanmedian

Median along specified axis ignoring NaNs.

Examples

>>> a = np.array([[10, 7, 4], [3, 2, 1]])
>>> bn.median(a)
    3.5
>>> bn.median(a, axis=0)
    array([ 6.5,  4.5,  2.5])
>>> bn.median(a, axis=1)
    array([ 7.,  2.])

bottleneck.nanmedian(a, axis=None)

Median of array elements along given axis ignoring NaNs.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the median is computed. The default (axis=None) is to compute the median of the flattened array.

Returns
yndarray

An array with the same shape as a, except that the specified axis has been removed. If a is a 0d array, or if axis is None, a scalar is returned. float64 return values are used for integer inputs.

See also

bottleneck.median

Median along specified axis.

Examples

>>> a = np.array([[np.nan, 7, 4], [3, 2, 1]])
>>> a
array([[ nan,   7.,   4.],
       [  3.,   2.,   1.]])
>>> bn.nanmedian(a)
3.0
>> bn.nanmedian(a, axis=0)
array([ 3. ,  4.5,  2.5])
>> bn.nanmedian(a, axis=1)
array([ 5.5,  2. ])

bottleneck.ss(a, axis=None)

Sum of the square of each element along the specified axis.

Parameters
aarray_like

Array whose sum of squares is desired. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the sum of squares is computed. The default (axis=None) is to sum the squares of the flattened array.

Returns
yndarray

The sum of a**2 along the given axis.

Examples

>>> a = np.array([1., 2., 5.])
>>> bn.ss(a)
30.0

And calculating along an axis:

>>> b = np.array([[1., 2., 5.], [2., 5., 6.]])
>>> bn.ss(b, axis=1)
array([ 30., 65.])

bottleneck.nanargmin(a, axis=None)

Indices of the minimum values along an axis, ignoring NaNs.

For all-NaN slices ValueError is raised. Unlike NumPy, the results can be trusted if a slice contains only NaNs and Infs.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which to operate. By default (axis=None) flattened input is used.

Returns
index_arrayndarray

An array of indices or a single index value.

See also

bottleneck.nanargmax

Indices of the maximum values along an axis.

bottleneck.nanmin

Minimum values along specified axis, ignoring NaNs.

Examples

>>> a = np.array([[np.nan, 4], [2, 3]])
>>> bn.nanargmin(a)
2
>>> a.flat[2]
2.0
>>> bn.nanargmin(a, axis=0)
array([1, 1])
>>> bn.nanargmin(a, axis=1)
array([1, 0])

bottleneck.nanargmax(a, axis=None)

Indices of the maximum values along an axis, ignoring NaNs.

For all-NaN slices ValueError is raised. Unlike NumPy, the results can be trusted if a slice contains only NaNs and Infs.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which to operate. By default (axis=None) flattened input is used.

Returns
index_arrayndarray

An array of indices or a single index value.

See also

bottleneck.nanargmin

Indices of the minimum values along an axis.

bottleneck.nanmax

Maximum values along specified axis, ignoring NaNs.

Examples

>>> a = np.array([[np.nan, 4], [2, 3]])
>>> bn.nanargmax(a)
1
>>> a.flat[1]
4.0
>>> bn.nanargmax(a, axis=0)
array([1, 0])
>>> bn.nanargmax(a, axis=1)
array([1, 1])

bottleneck.anynan(a, axis=None)

Test whether any array element along a given axis is NaN.

Returns the same output as np.isnan(a).any(axis)

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which NaNs are searched. The default (axis = None) is to search for NaNs over a flattened input array.

Returns
ybool or ndarray

A boolean or new ndarray is returned.

See also

bottleneck.allnan

Test if all array elements along given axis are NaN

Examples

>>> bn.anynan(1)
False
>>> bn.anynan(np.nan)
True
>>> bn.anynan([1, np.nan])
True
>>> a = np.array([[1, 4], [1, np.nan]])
>>> bn.anynan(a)
True
>>> bn.anynan(a, axis=0)
array([False,  True], dtype=bool)

bottleneck.allnan(a, axis=None)

Test whether all array elements along a given axis are NaN.

Returns the same output as np.isnan(a).all(axis)

Note that allnan([]) is True to match np.isnan([]).all() and all([])

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which NaNs are searched. The default (axis = None) is to search for NaNs over a flattened input array.

Returns
ybool or ndarray

A boolean or new ndarray is returned.

See also

bottleneck.anynan

Test if any array element along given axis is NaN

Examples

>>> bn.allnan(1)
False
>>> bn.allnan(np.nan)
True
>>> bn.allnan([1, np.nan])
False
>>> a = np.array([[1, np.nan], [1, np.nan]])
>>> bn.allnan(a)
False
>>> bn.allnan(a, axis=0)
array([False,  True], dtype=bool)

An empty array returns True:

>>> bn.allnan([])
True

which is similar to:

>>> all([])
True
>>> np.isnan([]).all()
True

Non-reduce

Functions that do not reduce the input array and do not take axis as input.


bottleneck.replace(a, old, new)

Replace (inplace) given scalar values of an array with new values.

The equivalent numpy function:

a[a==old] = new

Or in the case where old=np.nan:

a[np.isnan(old)] = new

Parameters
anumpy.ndarray

The input array, which is also the output array since this functions works inplace.

oldscalar

All elements in a with this value will be replaced by new.

newscalar

All elements in a with a value of old will be replaced by new.

Returns
Returns a view of the input array after performing the replacements,
if any.

Examples

Replace zero with 3 (note that the input array is modified):

>>> a = np.array([1, 2, 0])
>>> bn.replace(a, 0, 3)
>>> a
array([1, 2, 3])

Replace np.nan with 0:

>>> a = np.array([1, 2, np.nan])
>>> bn.replace(a, np.nan, 0)
>>> a
array([ 1.,  2.,  0.])

Non-reduce with axis

Functions that do not reduce the input array but operate along a specified axis.


bottleneck.rankdata(a, axis=None)

Ranks the data, dealing with ties appropriately.

Equal values are assigned a rank that is the average of the ranks that would have been otherwise assigned to all of the values within that set. Ranks begin at 1, not 0.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the elements of the array are ranked. The default (axis=None) is to rank the elements of the flattened array.

Returns
yndarray

An array with the same shape as a. The dtype is ‘float64’.

See also

bottleneck.nanrankdata

Ranks the data dealing with ties and NaNs.

Examples

>>> bn.rankdata([0, 2, 2, 3])
array([ 1. ,  2.5,  2.5,  4. ])
>>> bn.rankdata([[0, 2], [2, 3]])
array([ 1. ,  2.5,  2.5,  4. ])
>>> bn.rankdata([[0, 2], [2, 3]], axis=0)
array([[ 1.,  1.],
       [ 2.,  2.]])
>>> bn.rankdata([[0, 2], [2, 3]], axis=1)
array([[ 1.,  2.],
       [ 1.,  2.]])

bottleneck.nanrankdata(a, axis=None)

Ranks the data, dealing with ties and NaNs appropriately.

Equal values are assigned a rank that is the average of the ranks that would have been otherwise assigned to all of the values within that set. Ranks begin at 1, not 0.

NaNs in the input array are returned as NaNs.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

axis{int, None}, optional

Axis along which the elements of the array are ranked. The default (axis=None) is to rank the elements of the flattened array.

Returns
yndarray

An array with the same shape as a. The dtype is ‘float64’.

See also

bottleneck.rankdata

Ranks the data, dealing with ties and appropriately.

Examples

>>> bn.nanrankdata([np.nan, 2, 2, 3])
array([ nan,  1.5,  1.5,  3. ])
>>> bn.nanrankdata([[np.nan, 2], [2, 3]])
array([ nan,  1.5,  1.5,  3. ])
>>> bn.nanrankdata([[np.nan, 2], [2, 3]], axis=0)
array([[ nan,   1.],
       [  1.,   2.]])
>>> bn.nanrankdata([[np.nan, 2], [2, 3]], axis=1)
array([[ nan,   1.],
       [  1.,   2.]])

bottleneck.partition(a, kth, axis=-1)

Partition array elements along given axis.

A 1d array B is partitioned at array index kth if three conditions are met: (1) B[kth] is in its sorted position, (2) all elements to the left of kth are less than or equal to B[kth], and (3) all elements to the right of kth are greater than or equal to B[kth]. Note that the array elements in conditions (2) and (3) are in general unordered.

Shuffling the input array may change the output. The only guarantee is given by the three conditions above.

This functions is not protected against NaN. Therefore, you may get unexpected results if the input contains NaN.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

kthint

The value of the element at index kth will be in its sorted position. Smaller (larger) or equal values will be to the left (right) of index kth.

axis{int, None}, optional

Axis along which the partition is performed. The default (axis=-1) is to partition along the last axis.

Returns
yndarray

A partitioned copy of the input array with the same shape and type of a.

See also

bottleneck.argpartition

Indices that would partition an array

Notes

Unexpected results may occur if the input array contains NaN.

Examples

Create a numpy array:

>>> a = np.array([1, 0, 3, 4, 2])

Partition array so that the first 3 elements (indices 0, 1, 2) are the smallest 3 elements (note, as in this example, that the smallest 3 elements may not be sorted):

>>> bn.partition(a, kth=2)
array([1, 0, 2, 4, 3])

Now Partition array so that the last 2 elements are the largest 2 elements:

>>> bn.partition(a, kth=3)
array([1, 0, 2, 3, 4])

bottleneck.argpartition(a, kth, axis=-1)

Return indices that would partition array along the given axis.

A 1d array B is partitioned at array index kth if three conditions are met: (1) B[kth] is in its sorted position, (2) all elements to the left of kth are less than or equal to B[kth], and (3) all elements to the right of kth are greater than or equal to B[kth]. Note that the array elements in conditions (2) and (3) are in general unordered.

Shuffling the input array may change the output. The only guarantee is given by the three conditions above.

This functions is not protected against NaN. Therefore, you may get unexpected results if the input contains NaN.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

kthint

The value of the element at index kth will be in its sorted position. Smaller (larger) or equal values will be to the left (right) of index kth.

axis{int, None}, optional

Axis along which the partition is performed. The default (axis=-1) is to partition along the last axis.

Returns
yndarray

An array the same shape as the input array containing the indices that partition a. The dtype of the indices is numpy.intp.

See also

bottleneck.partition

Partition array elements along given axis.

Notes

Unexpected results may occur if the input array contains NaN.

Examples

Create a numpy array:

>>> a = np.array([10, 0, 30, 40, 20])

Find the indices that partition the array so that the first 3 elements are the smallest 3 elements:

>>> index = bn.argpartition(a, kth=2)
>>> index
array([0, 1, 4, 3, 2])

Let’s use the indices to partition the array (note, as in this example, that the smallest 3 elements may not be in order):

>>> a[index]
array([10, 0, 20, 40, 30])

bottleneck.push(a, n=None, axis=-1)

Fill missing values (NaNs) with most recent non-missing values.

Filling proceeds along the specified axis from small index values to large index values.

Parameters
aarray_like

Input array. If a is not an array, a conversion is attempted.

n{int, None}, optional

How far to push values. If the most recent non-NaN array element is more than n index positions away, than a NaN is returned. The default (n = None) is to push the entire length of the slice. If n is an integer it must be nonnegative.

axisint, optional

Axis along which the elements of the array are pushed. The default (axis=-1) is to push along the last axis of the input array.

Returns
yndarray

An array with the same shape and dtype as a.

See also

bottleneck.replace

Replace specified value of an array with new value.

Examples

>>> a = np.array([5, np.nan, np.nan, 6, np.nan])
>>> bn.push(a)
    array([ 5.,  5.,  5.,  6.,  6.])
>>> bn.push(a, n=1)
    array([  5.,   5.,  nan,   6.,   6.])
>>> bn.push(a, n=2)
    array([ 5.,  5.,  5.,  6.,  6.])

Moving window functions

Functions that operate along a (1d) moving window.


bottleneck.move_sum(a, window, min_count=None, axis=-1)

Moving window sum along the specified axis, optionally ignoring NaNs.

This function cannot handle input arrays that contain Inf. When the window contains Inf, the output will correctly be Inf. However, when Inf moves out of the window, the remaining output values in the slice will incorrectly be NaN.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

Returns
yndarray

The moving sum of the input array along the specified axis. The output has the same shape as the input.

Examples

>>> a = np.array([1.0, 2.0, 3.0, np.nan, 5.0])
>>> bn.move_sum(a, window=2)
array([ nan,   3.,   5.,  nan,  nan])
>>> bn.move_sum(a, window=2, min_count=1)
array([ 1.,  3.,  5.,  3.,  5.])

bottleneck.move_mean(a, window, min_count=None, axis=-1)

Moving window mean along the specified axis, optionally ignoring NaNs.

This function cannot handle input arrays that contain Inf. When the window contains Inf, the output will correctly be Inf. However, when Inf moves out of the window, the remaining output values in the slice will incorrectly be NaN.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

Returns
yndarray

The moving mean of the input array along the specified axis. The output has the same shape as the input.

Examples

>>> a = np.array([1.0, 2.0, 3.0, np.nan, 5.0])
>>> bn.move_mean(a, window=2)
array([ nan,  1.5,  2.5,  nan,  nan])
>>> bn.move_mean(a, window=2, min_count=1)
array([ 1. ,  1.5,  2.5,  3. ,  5. ])

bottleneck.move_std(a, window, min_count=None, axis=-1, ddof=0)

Moving window standard deviation along the specified axis, optionally ignoring NaNs.

This function cannot handle input arrays that contain Inf. When Inf enters the moving window, the outout becomes NaN and will continue to be NaN for the remainer of the slice.

Unlike bn.nanstd, which uses a two-pass algorithm, move_nanstd uses a one-pass algorithm called Welford’s method. The algorithm is slow but numerically stable for cases where the mean is large compared to the standard deviation.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

ddofint, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero.

Returns
yndarray

The moving standard deviation of the input array along the specified axis. The output has the same shape as the input.

Examples

>>> a = np.array([1.0, 2.0, 3.0, np.nan, 5.0])
>>> bn.move_std(a, window=2)
array([ nan,  0.5,  0.5,  nan,  nan])
>>> bn.move_std(a, window=2, min_count=1)
array([ 0. ,  0.5,  0.5,  0. ,  0. ])

bottleneck.move_var(a, window, min_count=None, axis=-1, ddof=0)

Moving window variance along the specified axis, optionally ignoring NaNs.

This function cannot handle input arrays that contain Inf. When Inf enters the moving window, the outout becomes NaN and will continue to be NaN for the remainer of the slice.

Unlike bn.nanvar, which uses a two-pass algorithm, move_nanvar uses a one-pass algorithm called Welford’s method. The algorithm is slow but numerically stable for cases where the mean is large compared to the standard deviation.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

ddofint, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero.

Returns
yndarray

The moving variance of the input array along the specified axis. The output has the same shape as the input.

Examples

>>> a = np.array([1.0, 2.0, 3.0, np.nan, 5.0])
>>> bn.move_var(a, window=2)
array([ nan,  0.25,  0.25,  nan,  nan])
>>> bn.move_var(a, window=2, min_count=1)
array([ 0. ,  0.25,  0.25,  0. ,  0. ])

bottleneck.move_min(a, window, min_count=None, axis=-1)

Moving window minimum along the specified axis, optionally ignoring NaNs.

float64 output is returned for all input data types.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

Returns
yndarray

The moving minimum of the input array along the specified axis. The output has the same shape as the input. The dtype of the output is always float64.

Examples

>>> a = np.array([1.0, 2.0, 3.0, np.nan, 5.0])
>>> bn.move_min(a, window=2)
array([ nan,   1.,   2.,  nan,  nan])
>>> bn.move_min(a, window=2, min_count=1)
array([ 1.,  1.,  2.,  3.,  5.])

bottleneck.move_max(a, window, min_count=None, axis=-1)

Moving window maximum along the specified axis, optionally ignoring NaNs.

float64 output is returned for all input data types.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

Returns
yndarray

The moving maximum of the input array along the specified axis. The output has the same shape as the input. The dtype of the output is always float64.

Examples

>>> a = np.array([1.0, 2.0, 3.0, np.nan, 5.0])
>>> bn.move_max(a, window=2)
array([ nan,   2.,   3.,  nan,  nan])
>>> bn.move_max(a, window=2, min_count=1)
array([ 1.,  2.,  3.,  3.,  5.])

bottleneck.move_argmin(a, window, min_count=None, axis=-1)

Moving window index of minimum along the specified axis, optionally ignoring NaNs.

Index 0 is at the rightmost edge of the window. For example, if the array is monotonically decreasing (increasing) along the specified axis then the output array will contain zeros (window-1).

If there is a tie in input values within a window, then the rightmost index is returned.

float64 output is returned for all input data types.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

Returns
yndarray

The moving index of minimum values of the input array along the specified axis. The output has the same shape as the input. The dtype of the output is always float64.

Examples

>>> a = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
>>> bn.move_argmin(a, window=2)
array([ nan,   1.,   1.,   1.,   1.])
>>> a = np.array([5.0, 4.0, 3.0, 2.0, 1.0])
>>> bn.move_argmin(a, window=2)
array([ nan,   0.,   0.,   0.,   0.])
>>> a = np.array([2.0, 3.0, 4.0, 1.0, 7.0, 5.0, 6.0])
>>> bn.move_argmin(a, window=3)
array([ nan,  nan,   2.,   0.,   1.,   2.,   1.])

bottleneck.move_argmax(a, window, min_count=None, axis=-1)

Moving window index of maximum along the specified axis, optionally ignoring NaNs.

Index 0 is at the rightmost edge of the window. For example, if the array is monotonically increasing (decreasing) along the specified axis then the output array will contain zeros (window-1).

If there is a tie in input values within a window, then the rightmost index is returned.

float64 output is returned for all input data types.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

Returns
yndarray

The moving index of maximum values of the input array along the specified axis. The output has the same shape as the input. The dtype of the output is always float64.

Examples

>>> a = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
>>> bn.move_argmax(a, window=2)
array([ nan,   0.,   0.,   0.,   0.])
>>> a = np.array([5.0, 4.0, 3.0, 2.0, 1.0])
>>> bn.move_argmax(a, window=2)
array([ nan,   1.,   1.,   1.,   1.])
>>> a = np.array([2.0, 3.0, 4.0, 1.0, 7.0, 5.0, 6.0])
>>> bn.move_argmax(a, window=3)
array([ nan,  nan,   0.,   1.,   0.,   1.,   2.])

bottleneck.move_median(a, window, min_count=None, axis=-1)

Moving window median along the specified axis, optionally ignoring NaNs.

float64 output is returned for all input data types.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

Returns
yndarray

The moving median of the input array along the specified axis. The output has the same shape as the input.

Examples

>>> a = np.array([1.0, 2.0, 3.0, 4.0])
>>> bn.move_median(a, window=2)
array([ nan,  1.5,  2.5,  3.5])
>>> bn.move_median(a, window=2, min_count=1)
array([ 1. ,  1.5,  2.5,  3.5])

bottleneck.move_rank(a, window, min_count=None, axis=-1)

Moving window ranking along the specified axis, optionally ignoring NaNs.

The output is normalized to be between -1 and 1. For example, with a window width of 3 (and with no ties), the possible output values are -1, 0, 1.

Ties are broken by averaging the rankings. See the examples below.

The runtime depends almost linearly on window. The more NaNs there are in the input array, the shorter the runtime.

Parameters
andarray

Input array. If a is not an array, a conversion is attempted.

windowint

The number of elements in the moving window.

min_count: {int, None}, optional

If the number of non-NaN values in a window is less than min_count, then a value of NaN is assigned to the window. By default min_count is None, which is equivalent to setting min_count equal to window.

axisint, optional

The axis over which the window is moved. By default the last axis (axis=-1) is used. An axis of None is not allowed.

Returns
yndarray

The moving ranking along the specified axis. The output has the same shape as the input. For integer input arrays, the dtype of the output is float64.

Examples

With window=3 and no ties, there are 3 possible output values, i.e. [-1., 0., 1.]:

>>> a = np.array([1, 2, 3, 9, 8, 7, 5, 6, 4])
>>> bn.move_rank(a, window=3)
    array([ nan,  nan,   1.,   1.,   0.,  -1.,  -1.,   0.,  -1.])

Ties are broken by averaging the rankings of the tied elements:

>>> a = np.array([1, 2, 3, 3, 3, 4])
>>> bn.move_rank(a, window=3)
    array([ nan,  nan,  1. ,  0.5,  0. ,  1. ])

In an increasing sequence, the moving window ranking is always equal to 1:

>>> a = np.array([1, 2, 3, 4, 5])
>>> bn.move_rank(a, window=2)
    array([ nan,   1.,   1.,   1.,   1.])