From 204de095a810749c32c712a97d5c6c42ef66e05d Mon Sep 17 00:00:00 2001 From: Bert Palm <bert.palm@ufz.de> Date: Thu, 3 Dec 2020 14:53:10 +0100 Subject: [PATCH] fixed count - monkey-patch roller - see comment --- saqc/lib/rolling.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/saqc/lib/rolling.py b/saqc/lib/rolling.py index 4a5516ff8..3266cbafe 100644 --- a/saqc/lib/rolling.py +++ b/saqc/lib/rolling.py @@ -371,4 +371,24 @@ def customRoller(obj, window, min_periods=None, # aka minimum non-nan values # is, because we cannot throw out values by ourself in the indexer, because min_periods also evaluates NA values # in its count and we have no control over the actual values, just their indexes. theirs.update(min_periods=x.min_periods) - return obj.rolling(indexer, center=None, **theirs) + roller = obj.rolling(indexer, center=None, **theirs) + + # ----- count hack ------- + # Unfortunately pandas calls count differently if a BaseIndexer + # instance is given. IMO, the intention behind this is to call + # count different for dt-like windows, but if a user pass a own + # indexer we also end up in this case /: + # The only possibility is to monkey-patch pandas... + def new_count(): + self = roller + if not x.is_freq_type: + result = obj.notna().astype(int) + theirs.update(min_periods=min_periods or 0) + return customRoller(result, window, **theirs, **ours).sum() + return self._old_count() + + roller._old_count = roller.count + roller.count = new_count + # ----- count hack ------- + + return roller -- GitLab