Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time resampling as in Pandas #99

Merged
merged 2 commits into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 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 Expand Up @@ -426,11 +438,12 @@ class QueryForm(OmgWtForm):
TextField("Super", default=''))
for fieldset in viz.fieldsetizer():
for ff in fieldset['fields']:
if isinstance(ff, string_types):
ff = [ff]
for s in ff:
if s:
setattr(QueryForm, s, px_form_fields[s])
if ff:
if isinstance(ff, string_types):
ff = [ff]
for s in ff:
if s:
setattr(QueryForm, s, px_form_fields[s])


# datasource type specific form elements
Expand Down
4 changes: 3 additions & 1 deletion panoramix/templates/panoramix/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
{% endif %}
<div class="fieldset_content">
{% for fieldname in fieldset.fields %}
{% if not fieldname.__iter__ %}
{% if not fieldname %}
<hr/>
{% elif not fieldname.__iter__ %}
{{ panofield(fieldname) }}
{% else %}
<div class="row">
Expand Down
17 changes: 15 additions & 2 deletions 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 @@ -98,7 +98,7 @@ def flat_form_fields(cls):
for obj in d['fields']:
if isinstance(obj, (tuple, list)):
l |= {a for a in obj}
else:
elif obj:
l.add(obj)
return l

Expand Down Expand Up @@ -629,6 +629,8 @@ class NVD3TimeSeriesViz(NVD3Viz):
('rolling_type', 'rolling_periods'),
'time_compare',
'num_period_compare',
None,
('resample_how', 'resample_rule',), 'resample_fillmethod'
),
},
)
Expand All @@ -646,6 +648,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