I want a function that can take a series and a set of silos and basically round to the nearest silo. For instance:
my_series = [ 1, 1.5, 2, 2.3, 2.6, 3]
def my_function(my_series, bins):
...
my_function(my_series, bins=[1,2,3])
> [1,2,2,3,3,3]
It looks like Numpy Digitize is meant to be executed, but it generates the wrong values (asterisks for the wrong values):
np.digitize(my_series, bins= [1,2,3], right=False)
> [1, 1*, 2, 2*, 2*, 3]
The reason this is incorrect is clear from the documentation:
Each returned index i is such that bins [i-1] <= x <[i] if the boxes are monotonically increasing or bins [i-1]> x> = bins [i] if the bin is monotonically decreasing. If the values in x go beyond the boxes, 0 or len (baskets) are returned, respectively. If the right is true, then the right bit is closed so that the index i is such that the bins [i-1] x <= bins [i] or bins [i-1]> = x> bins [i] '', if the cells are monotonous increasing or decreasing accordingly.
I can get closer to what I want if I enter in values decreasing and setting "rights" to True ...
np.digitize(my_series, bins= [3,2,1], right=True)
> [3, 2, 2, 1, 1, 1]
(1) (3). , 3 , , . .