Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def var(self, ddof=1):
""" Compute variance of elements within window """
return self._known_aggregation(aggregations.Var(ddof=ddof))
# Compute example
state = agg.initial(self.root.example, grouper=grouper_example)
if hasattr(grouper_example, 'iloc'):
grouper_example = grouper_example.iloc[:0]
elif isinstance(grouper_example, (np.ndarray, pd.Index)):
grouper_example = grouper_example[:0]
_, example = agg.on_new(state,
self.root.example.iloc[:0],
grouper=grouper_example)
if self.n is not None:
diff = aggregations.diff_iloc
window = self.n
elif self.value is not None:
diff = aggregations.diff_loc
window = self.value
outstream = stream.accumulate(aggregations.windowed_groupby_accumulator,
agg=agg,
start=None,
returns_state=True,
diff=diff,
window=window)
for typ, s_type in _stream_types[stream_type]:
if isinstance(example, typ):
return s_type(outstream, example)
return Streaming(outstream, example, stream_type=stream_type)
def apply(self, func):
""" Apply an arbitrary function over each window of data """
result = self._known_aggregation(aggregations.Full())
return result.map_partitions(func, result)
def count(self):
""" Count elements within window """
return self._known_aggregation(aggregations.Count())
def sum(self):
""" Sum frame """
return self.accumulate_partitions(aggregations.accumulator,
agg=aggregations.Sum(),
start=None, stream_type='updating',
returns_state=True)
if hasattr(grouper_example, 'iloc'):
grouper_example = grouper_example.iloc[:0]
elif isinstance(grouper_example, (np.ndarray, pd.Index)):
grouper_example = grouper_example[:0]
_, example = agg.on_new(state,
self.root.example.iloc[:0],
grouper=grouper_example)
if self.n is not None:
diff = aggregations.diff_iloc
window = self.n
elif self.value is not None:
diff = aggregations.diff_loc
window = self.value
outstream = stream.accumulate(aggregations.windowed_groupby_accumulator,
agg=agg,
start=None,
returns_state=True,
diff=diff,
window=window)
for typ, s_type in _stream_types[stream_type]:
if isinstance(example, typ):
return s_type(outstream, example)
return Streaming(outstream, example, stream_type=stream_type)
def _known_aggregation(self, agg):
if self.n is not None:
diff = aggregations.diff_iloc
window = self.n
elif self.value is not None:
diff = aggregations.diff_loc
window = self.value
return self.root.accumulate_partitions(aggregations.window_accumulator,
diff=diff,
window=window,
agg=agg,
start=None,
returns_state=True)
def count(self):
""" Groupby-count """
return self._accumulate(aggregations.GroupbyCount)
def size(self):
""" Number of elements within window """
return self._known_aggregation(aggregations.Size())
else:
stream = self.root.stream
grouper_example = self.grouper
agg = Agg(self.index, grouper=self.grouper, **kwargs)
# Compute example
state = agg.initial(self.root.example, grouper=grouper_example)
if hasattr(grouper_example, 'iloc'):
grouper_example = grouper_example.iloc[:0]
elif isinstance(grouper_example, (np.ndarray, pd.Index)):
grouper_example = grouper_example[:0]
_, example = agg.on_new(state,
self.root.example.iloc[:0],
grouper=grouper_example)
outstream = stream.accumulate(aggregations.groupby_accumulator,
agg=agg,
start=None,
returns_state=True)
for typ, s_type in _stream_types[stream_type]:
if isinstance(example, typ):
return s_type(outstream, example)
return Streaming(outstream, example, stream_type=stream_type)