diff --git a/saqc/funcs/functions.py b/saqc/funcs/functions.py index 8503567c2a677c6ba8490de97262703c03dfde6e..9e22e3fd7ae8b0b66210a4cf8e790a70ddb9decf 100644 --- a/saqc/funcs/functions.py +++ b/saqc/funcs/functions.py @@ -17,6 +17,7 @@ from scipy.cluster.hierarchy import linkage, fcluster from saqc.lib.tools import groupConsecutives, seasonalMask, FreqIndexer, customRolling +from saqc.lib.ts_operators import count from saqc.funcs.proc_functions import proc_fork, proc_drop, proc_projectFlags from saqc.funcs.modelling import modelling_mask @@ -1123,8 +1124,8 @@ def flagChangePoints(data, field, flagger, stat_func, thresh_func, bwd_window, m stat_arr, thresh_arr = _slidingWindowSearch(data_arr, bwd_start, fwd_end, stat_func, thresh_func, var_len) result_arr = stat_arr > thresh_arr - - data_ser[result_arr].rolling(agg_range, closed='both', min_periods=0).count() + detected = pd.Series(True, index=data_ser[result_arr].index) + cp_cluster = customRolling(detected, agg_range, count, closed='both', min_periods=1, center=True) flagger = flagger.setFlags(field, loc=result_arr) return data, flagger \ No newline at end of file diff --git a/saqc/lib/tools.py b/saqc/lib/tools.py index 1915a3ca1746eddb0bf4448e6a15f664ef2eac93..b3396373a4c751156920895fcfe613f49b20906c 100644 --- a/saqc/lib/tools.py +++ b/saqc/lib/tools.py @@ -438,7 +438,7 @@ class PeriodsIndexer(BaseIndexer): return start, end -def customRolling(to_roll, winsz, func, roll_mask, min_periods=1, center=False, closed=None, raw=True, engine=None, +def customRolling(to_roll, winsz, func, roll_mask=None, min_periods=1, center=False, closed=None, raw=True, engine=None, forward=False): """ A wrapper around pandas.rolling.apply(), that allows for skipping func application on @@ -453,12 +453,12 @@ def customRolling(to_roll, winsz, func, roll_mask, min_periods=1, center=False, func : Callable Function to be rolled with. If the funcname matches a .rolling attribute, the associated method of .rolling will be used instead of .apply(func) (=faster) - roll_mask : {numpy.array[bool], None} + roll_mask : {None, numpy.array[bool]}, default None A mask, indicating the rolling windows, `func` shall be applied on. Has to be of same length as `to_roll`. roll_mask[i] = False indicates, that the window with right end point to_roll.index[i] shall be skipped. - Pass None if you want values to be masked. + Pass None(default) if you want no values to be masked. min_periods : int, default 1 Gets passed on to the min_periods parameter of pandas.Rolling. (Note, that rolling with freq string defined window size and `min_periods`=None, @@ -502,14 +502,13 @@ def customRolling(to_roll, winsz, func, roll_mask, min_periods=1, center=False, center=center, closed=closed) - i_roll = i_roll.rolling(indexer, + i_roller = i_roll.rolling(indexer, min_periods=min_periods, center=center, - closed=closed).apply(func, raw=raw, engine=engine) - return pd.Series(i_roll.values, index=to_roll.index) + closed=closed) if hasattr(i_roller, func.__name__): - i_roll = getattr(i_roller, func.__name__) + i_roll = getattr(i_roller, func.__name__)() else: i_roll = i_roller.apply(func, raw=raw, engine=engine)