Skip to content

Commit

Permalink
Adding basic resampling capabilities from pandas
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jan 6, 2016
1 parent 3a3e7c8 commit ade9175
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions panoramix/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ def __init__(self, viz):
"The time granularity for the visualization. Note that you "
"can define arbitrary expression that return a DATETIME "
"column in the table editor")),
'resample_rule': FreeFormSelectField(
'Resample Rule', default='',
choices=self.choicify(('1M', '1H', '1D', '7D', '1M', '1Y')),
description=("Pandas resample rule")),
'resample_how': FreeFormSelectField(
'Resample How', default='',
choices=self.choicify(('', 'avg', 'sum',)),
description=("Pandas resample how")),
'resample_fillmethod': FreeFormSelectField(
'Resample Fill Method', default='',
choices=self.choicify(('', 'ffill', 'bfill')),
description=("Pandas resample fill method")),
'since': FreeFormSelectField(
'Since', default="7 days ago",
choices=self.choicify([
Expand Down
14 changes: 13 additions & 1 deletion panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from flask import flash, request, Markup
from markdown import markdown
from pandas.io.json import dumps, to_json
from pandas.io.json import dumps
from werkzeug.datastructures import ImmutableMultiDict
from werkzeug.urls import Href
import numpy as np
Expand Down Expand Up @@ -629,6 +629,7 @@ class NVD3TimeSeriesViz(NVD3Viz):
('rolling_type', 'rolling_periods'),
'time_compare',
'num_period_compare',
('resample_how', 'resample_rule',), 'resample_fillmethod'
),
},
)
Expand All @@ -646,6 +647,17 @@ def get_df(self, query_obj=None):
columns=form_data.get('groupby'),
values=form_data.get('metrics'))

fm = form_data.get("resample_fillmethod")
if not fm:
fm = None
how = form_data.get("resample_how")
rule = form_data.get("resample_rule")
if how and rule:
df = df.resample(rule, how=how, fill_method=fm)
if not fm:
df = df.fillna(0)


if self.sort_series:
dfs = df.sum()
dfs.sort(ascending=False)
Expand Down

0 comments on commit ade9175

Please sign in to comment.