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

[line chart] add 'min_periods' control related to rolling windows #3397

Merged
merged 2 commits into from
Aug 30, 2017
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
13 changes: 13 additions & 0 deletions superset/assets/javascripts/explore/stores/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,19 @@ export const controls = {
'relative to the time granularity selected',
},

min_periods: {
type: 'TextControl',
label: 'Min Periods',
isInt: true,
description: (
'The minimum number of rolling periods required to show ' +
'a value. For instance if you do a cumulative sum on 7 days ' +
'you may want your "Min Period" to be 7, so that all data points ' +
'shown are the total of 7 periods. This will hide the "ramp up" ' +
'taking place over the first 7 periods'
),
},

series: {
type: 'SelectControl',
label: 'Series',
Expand Down
7 changes: 3 additions & 4 deletions superset/assets/javascripts/explore/stores/visTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ export const sections = {
'that allow for advanced analytical post processing ' +
'of query results',
controlSetRows: [
['rolling_type', 'rolling_periods'],
['time_compare'],
['rolling_type', 'rolling_periods', 'min_periods'],
['time_compare', null],
['num_period_compare', 'period_ratio_type'],
['resample_how', 'resample_rule'],
['resample_fillmethod'],
['resample_how', 'resample_rule', 'resample_fillmethod'],
],
},
],
Expand Down
16 changes: 11 additions & 5 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,18 +886,25 @@ def process_data(self, df):
dft = df.T
df = (dft / dft.sum()).T

rolling_periods = fd.get("rolling_periods")
rolling_type = fd.get("rolling_type")
rolling_periods = int(fd.get("rolling_periods") or 0)
min_periods = int(fd.get("min_periods") or 0)

if rolling_type in ('mean', 'std', 'sum') and rolling_periods:
kwargs = dict(
arg=df,
window=rolling_periods,
min_periods=min_periods)
if rolling_type == 'mean':
df = pd.rolling_mean(df, int(rolling_periods), min_periods=0)
df = pd.rolling_mean(**kwargs)
elif rolling_type == 'std':
df = pd.rolling_std(df, int(rolling_periods), min_periods=0)
df = pd.rolling_std(**kwargs)
elif rolling_type == 'sum':
df = pd.rolling_sum(df, int(rolling_periods), min_periods=0)
df = pd.rolling_sum(**kwargs)
elif rolling_type == 'cumsum':
df = df.cumsum()
if min_periods:
df = df[min_periods:]

num_period_compare = fd.get("num_period_compare")
if num_period_compare:
Expand All @@ -911,7 +918,6 @@ def process_data(self, df):
df = df / df.shift(num_period_compare)

df = df[num_period_compare:]

return df

def get_data(self, df):
Expand Down