Finding peaks in pandas series with non integer index

499 views Asked by At

I have the following series and trying to find the index of the peaks which should be [1,8.5] or the peak value which should be [279,139]. the used threshold is 100. I tried many ways but, it always ignores the series index and returns [1,16].

0.5       0
1.0     279
1.5     256
2.0      84
2.5      23
3.0      11
3.5       3
4.0       2
4.5       7
5.0       5
5.5       4
6.0       4
6.5      10
7.0      30
7.5      88
8.0     133
8.5     139
9.0      84
9.5      55
10.0     26
10.5     10
11.0      8
11.5      4
12.0      4
12.5      1
13.0      0
13.5      0
14.0      1
14.5      0

I tried this code

thresh = 100
peak_idx, _ = find_peaks(out.value_counts(sort=False), height=thresh)
plt.plot(out.value_counts(sort=False).index[peak_idx], out.value_counts(sort=False)[peak_idx], 'r.')
out.value_counts(sort=False).plot.bar()
plt.show()
peak_idx

here is the output array([ 1, 16], dtype=int64)

enter image description here

1

There are 1 answers

1
Cristofor On BEST ANSWER

You are doing it right the only thing that you misunderstood is that find_peaks finds the indexes of the peaks, not peaks themselves.

Here is the documentation that clearly states that:

Returns

peaksndarray

    Indices of peaks in x that satisfy all given conditions.

Reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html

Try this code here:

thresh = 100
y = [0,279,256, 84, 23, 11,  3,  2,  7,  5,  4,  4, 10, 30, 88,133,139, 84, 55, 26, 10,  8,  4,  4,  1,  0,  0,  1,  0]
x = [0.5 ,1.0 ,1.5 ,2.0 ,2.5 ,3.0 ,3.5 ,4.0 ,4.5 ,5.0 ,5.5 ,6.0 ,6.5 ,7.0 ,7.5 ,8.0 ,8.5 ,9.0 ,9.5 ,10.0,10.5,11.0,11.5,12.0,12.5,13.0,13.5,14.0,14.5]
peak_idx, _ = find_peaks(x, height=thresh)
out_values = [x[peak] for peak in peak_idx]

Here out_vaules will contain what you want