Here's one approach -
np.split(a,np.flatnonzero(np.diff(a)>d)+1)
As a function to display a list of lists -
def splitme(a,d) : return list(map(list,np.split(a,np.flatnonzero(np.diff(a)>d)+1)))
For performance, I would suggest using zip to get the start, stop the indexes and then chop, thereby avoiding np.split , which could be a bottleneck -
def splitme_zip(a,d) : m = np.concatenate(([True],a[1:] > a[:-1] + d,[True])) idx = np.flatnonzero(m) l = a.tolist() return [l[i:j] for i,j in zip(idx[:-1],idx[1:])]
If you need the output as a list of arrays, skip the list conversion using .tolist / map(list,) .
Run Examples -
In [122]: a = np.array([1,2,3,5,7,10,13,16,20]) In [123]: splitme(a,1) Out[123]: [[1, 2, 3], [5], [7], [10], [13], [16], [20]] In [124]: splitme(a,2) Out[124]: [[1, 2, 3, 5, 7], [10], [13], [16], [20]] In [125]: splitme(a,3) Out[125]: [[1, 2, 3, 5, 7, 10, 13, 16], [20]]
Runtime Test -
In [180]: a = np.sort(np.random.randint(1,10000*2,(10000))) In [181]: s = pd.Series(a) In [182]: d = 3 In [183]: %timeit pandas_way(s,d)