Search code examples
pythonpython-3.xpandasscipy

Apply find_peaks() function using groupby() in a pandas dataframe


I have a timeseries data that is grouped by in ID column.

I tried using the following code:

def find_peaks_in_group(group):
    peaks, peak_properties = find_peaks(group['RFPower'], prominence=1, height=0.7)
    group['peaks'] = False
    group.loc[peaks, 'peaks'] = True
    group['peak_heights'] = 0.0
    group.loc[peaks, 'peak_heights'] = peak_properties['peak_heights']
    return group

result_df=df.groupby('ID', group_keys=False).apply(find_peaks_in_group)

However Im getting a KeyError:

None of [Int64Index([76116, 76134, 76146, 76150, 76155, 76161, 76165, 76171, 76177, 76180, 76182, 76185, 76187, 76192, 76196, 76201, 76220, 76224, 76230, 76235, 76240, 76242, 76247, 76252, 76257, 76276, 76282, 76286, 76292, 76295, 76297, 76302, 76307, 76337, 76348, 76350, 76362, 76364, 77324, 93723, 94851, 94855], dtype='int64')] are in the [index]"

I even tried removing the group_keys=False and resetting the index to no avail. Any suggestions?


Solution

  • i think peaks is list of np.array(index) returned on find_peaks function so you have to use peaks[0] or change returned values of find_peaks