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

Add support for column specific fillna to viz #3066

Merged
merged 1 commit into from
Sep 28, 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
4 changes: 4 additions & 0 deletions superset/connectors/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def uid(self):
def column_names(self):
return sorted([c.column_name for c in self.columns])

@property
def columns_types(self):
return {c.column_name: c.type for c in self.columns}

@property
def main_dttm_col(self):
return "timestamp"
Expand Down
19 changes: 18 additions & 1 deletion superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class BaseViz(object):
verbose_name = "Base Viz"
credits = ""
is_timeseries = False
default_fillna = 0

def __init__(self, datasource, form_data):
if not datasource:
Expand All @@ -61,6 +62,21 @@ def __init__(self, datasource, form_data):
self.status = None
self.error_message = None

def get_fillna_for_type(self, col_type):
"""Returns the value for use as filler for a specific Column.type"""
if col_type:
if col_type == 'TEXT' or col_type.startswith('VARCHAR'):
return ' NULL'
return self.default_fillna

def get_fillna_for_columns(self, columns=None):
"""Returns a dict or scalar that can be passed to DataFrame.fillna"""
if columns is None:
return self.default_fillna
columns_types = self.datasource.columns_types
fillna = {c: self.get_fillna_for_type(columns_types.get(c)) for c in columns}
return fillna

def get_df(self, query_obj=None):
"""Returns a pandas dataframe based on the query object"""
if not query_obj:
Expand Down Expand Up @@ -102,7 +118,8 @@ def get_df(self, query_obj=None):
if self.datasource.offset:
df[DTTM_ALIAS] += timedelta(hours=self.datasource.offset)
df.replace([np.inf, -np.inf], np.nan)
df = df.fillna(0)
fillna = self.get_fillna_for_columns(df.columns)
df = df.fillna(fillna)
return df

def get_extra_filters(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,5 +767,16 @@ def test_slice_query_endpoint(self):
assert 'language' in resp
self.logout();

def test_viz_get_fillna_for_columns(self):
slc = self.get_slice("Girls", db.session)
q = slc.viz.query_obj()
results = slc.viz.datasource.query(q)
fillna_columns = slc.viz.get_fillna_for_columns(results.df.columns)
self.assertDictEqual(
fillna_columns,
{'name': ' NULL', 'sum__num': 0}
)


if __name__ == '__main__':
unittest.main()