def func(values):
    return values.mean()
funcmeans, bin_edges, binnumber = stats.binned_statistic(x=np.arange(len(A)
    ), values=A, statistic=func, bins=breaks)
assert np.allclose(means, funcmeans)
Vectorizing a Numpy slice operation