Simple Yet Fast Wave Segmentation Based on Zero Value
import pandas as pddefseg_zerobased (df, threshold):""" df : The dataframe that you want to proceed (make sure you use IS NOT NUll query); dataframe format/structure ______________________________________________ |DataSavedTime(datetime64) | Item/value(float) | | | | thres (default=0.1) : the threshold value for wave selection (in seconds) """ idx_group = ( df["DataSavedTime"] .diff() .dt.total_seconds() .gt(threshold) .cumsum() ) df["id"] = df.groupby(idx_group, as_index=False).ngroup() return df