-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaggregators.py
More file actions
25 lines (18 loc) · 762 Bytes
/
aggregators.py
File metadata and controls
25 lines (18 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from typing import List
import numpy as np
import pandas as pd
class Aggregator:
def __init__(self, sample_rate: str='S'):
self.sample_rate = sample_rate
def aggregate(self, batches: List[pd.Series]) -> pd.Series:
"""Combines arbitrarily many time series into one,
summing up the hits"""
mini = np.min([b.index.min() for b in batches])
maxi = np.max([b.index.max() for b in batches])
# create a date range covering all the series
date_rng = pd.date_range(start=mini, end=maxi, freq=self.sample_rate)
hits = np.zeros(len(date_rng))
ts = pd.Series(data=hits, index=date_rng)
for b in batches:
ts = ts.combine(b, func=np.add, fill_value=0)
return ts