Skip to content

Commit

Permalink
Merge pull request #68 from mistercrunch/cumsum
Browse files Browse the repository at this point in the history
Adding cumsum to rolling functions
  • Loading branch information
mistercrunch committed Nov 30, 2015
2 parents 525bd50 + 03e13cb commit 618e117
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions panoramix/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def __init__(self, viz):
"Limits the number of time series that get displayed")),
'rolling_type': SelectField(
'Rolling',
default='mean',
choices=[(s, s) for s in ['mean', 'sum', 'std']],
default='None',
choices=[(s, s) for s in ['None', 'mean', 'sum', 'std', 'cumsum']],
description=(
"Defines a rolling window function to apply")),
'rolling_periods': IntegerField(
Expand Down
14 changes: 9 additions & 5 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,17 +508,21 @@ def get_df(self, query_obj=None):

rolling_periods = form_data.get("rolling_periods")
rolling_type = form_data.get("rolling_type")
if rolling_periods and rolling_type:

if rolling_type in ('mean', 'std', 'sum') and rolling_periods:
if rolling_type == 'mean':
df = pd.rolling_mean(df, int(rolling_periods), min_periods=1)
df = pd.rolling_mean(df, int(rolling_periods), min_periods=0)
elif rolling_type == 'std':
df = pd.rolling_std(df, int(rolling_periods), min_periods=1)
df = pd.rolling_std(df, int(rolling_periods), min_periods=0)
elif rolling_type == 'sum':
df = pd.rolling_sum(df, int(rolling_periods), min_periods=1)
df = pd.rolling_sum(df, int(rolling_periods), min_periods=0)
elif rolling_type == 'cumsum':
df = df.cumsum()
return df

def to_series(self, df, classed='', title_suffix=''):
series = df.to_dict('series')

chart_data = []
for name in df.T.index.tolist():
ys = series[name]
Expand All @@ -542,7 +546,7 @@ def to_series(self, df, classed='', title_suffix=''):
"color": color,
"classed": classed,
"values": [
{'x': ds, 'y': ys[i]}
{'x': ds, 'y': ys[ds]}
for i, ds in enumerate(df.timestamp)]
}
chart_data.append(d)
Expand Down

0 comments on commit 618e117

Please sign in to comment.