Skip to content

Commit

Permalink
Support of itrable q argument for move_quantile
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-riazanov committed Oct 2, 2022
1 parent 72677f8 commit 2c892db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 3 additions & 1 deletion bottleneck/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from . import slow
from ._pytesttester import PytestTester
from .move import (move_argmax, move_argmin, move_max, move_mean, move_median,
move_quantile, move_min, move_rank, move_std, move_sum, move_var)
move_min, move_rank, move_std, move_sum, move_var)

from .src.move_quantile import move_quantile

from .nonreduce import replace
from .nonreduce_axis import (argpartition, nanrankdata, partition, push,
Expand Down
13 changes: 13 additions & 0 deletions bottleneck/src/move_quantile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ..move import move_quantile as move_quantile_c
from collections.abc import Iterable
import numpy as np

def move_quantile(a, window, q, min_count=None, axis=-1):
if not isinstance(q, Iterable):
return move_quantile_c(a, window=window, min_count=min_count, axis=axis, q=q)
result = np.asarray(
[move_quantile_c(a=a, window=window, min_count=min_count, axis=axis, q=quantile) for quantile in q]
)
return result

move_quantile.__doc__ = move_quantile_c.__doc__
11 changes: 5 additions & 6 deletions bottleneck/src/move_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,13 +1030,13 @@ mover(char *name,
if (quantile_obj != Py_None) {
quantile = PyFloat_AsDouble(quantile_obj);
if (error_converting(quantile)) {
TYPE_ERR("`q` must be a float");
TYPE_ERR("Value(s) in `q` must be float");
return NULL;
}
if ((quantile < 0.0) || (quantile > 1.0)) {
/* Float/double specifiers %f and %lf don't work here for some reason*/
PyErr_Format(PyExc_ValueError,
"`q` must be between 0. and 1.");
"Value(s) in `q` must be between 0. and 1.");
return NULL;
}

Expand Down Expand Up @@ -1579,7 +1579,7 @@ MULTILINE STRING END */

static char move_quantile_doc[] =
/* MULTILINE STRING BEGIN
move_quantile(a, window, min_count=None, axis=-1, q=0.5)
move_quantile(a, window, q, min_count=None, axis=-1)
Moving window quantile along the specified axis, ignoring NaNs.
Expand All @@ -1593,16 +1593,15 @@ a : ndarray
Input array. If `a` is not an array, a conversion is attempted.
window : int
The number of elements in the moving window.
q : float or list of floats
Quantile(s) to compute, all values must be between 0 and 1 inclusive.
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`.
axis : int, 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.
q : float, optional
Quantile to compute, which must be between 0 and 1 inclusive.
By default q=0.5 is used, computing a moving median.
Returns
-------
Expand Down

0 comments on commit 2c892db

Please sign in to comment.