Treemap visualization with multi group by filed raises KeyError
Pandas unused levels of a MultiIndex would cause KeyError while getting nested data in Treemap visualization type.
Expected results

Actual results
raise KeyError
2019-06-14 19:17:24,223:INFO:root:SELECT name AS name, date AS date, price AS price, COUNT(*) AS count
FROM ttt GROUP BY name, date, price ORDER BY count DESC
LIMIT 10000 OFFSET 0
2019-06-14 19:17:24,238:DEBUG:root:[stats_logger] (incr) loaded_from_source
2019-06-14 19:17:24,245:ERROR:root:'2019-06-02'
Traceback (most recent call last):
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/superset/views/core.py", line 1149, in generate_json
payload = viz_obj.get_payload()
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/superset/viz.py", line 371, in get_payload
payload['data'] = self.get_data(df)
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/superset/viz.py", line 765, in get_data
for metric in df.columns]
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/superset/viz.py", line 765, in <listcomp>
for metric in df.columns]
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/superset/viz.py", line 759, in _nest
for l in df.index.levels[0]]
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/superset/viz.py", line 759, in <listcomp>
for l in df.index.levels[0]]
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/superset/viz.py", line 759, in _nest
for l in df.index.levels[0]]
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/superset/viz.py", line 759, in <listcomp>
for l in df.index.levels[0]]
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/pandas/core/indexing.py", line 1500, in __getitem__
return self._getitem_axis(maybe_callable, axis=axis)
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/pandas/core/indexing.py", line 1913, in _getitem_axis
return self._get_label(key, axis=axis)
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/pandas/core/indexing.py", line 141, in _get_label
return self.obj._xs(label, axis=axis)
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/pandas/core/generic.py", line 3583, in xs
drop_level=drop_level)
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 2571, in get_loc_level
indexer = self._get_level_indexer(key, level=level)
File "/Users/lusong/code/superset_venv/venv/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 2666, in _get_level_indexer
raise KeyError(key)
KeyError: '2019-06-02'
2019-06-14 19:17:24,250:INFO:werkzeug:127.0.0.1 - - [14/Jun/2019 19:17:24] "POST /superset/explore_json/ HTTP/1.1" 500 -
Screenshots

How to reproduce the bug
- Create a new table by 'Upload a CSV', file content list as below
date,name,price,cnt
2019-06-01,apple,11,10
2019-06-01,pen,3,4
2019-06-02,apple,10,11
2019-06-02,pineapple,30,2
- Edit table, mark
date,name,priceGroupable - Create a chart using Treemap visualization type
- Edit chart, set Group by fileds in
name,date,pricesequence
Environment
- superset version:
0.28.1 - python version:
3.7.1 - node.js version:
v8.11.3 - npm version:
6.4.1
Checklist
- [x] I have checked the superset logs for python stacktraces and included it here as text if there are any.
- [x] I have reproduced the issue with at least the latest released version of superset.
- [x] I have checked the issue tracker for the same issue and I haven't found one similar.
Additional context
After get data by pen label, the date value 2019-06-02 is acturally unused and should remove before get next level's data.
In [2]: import pandas as pd
In [3]: data = [
...: ['2019-06-01','apple',11,10],
...: ['2019-06-01','pen',3,4],
...: ['2019-06-02','apple',10,11],
...: ['2019-06-02','pineapple',30,2]
...: ]
In [4]: df = pd.DataFrame(data, columns=['date', 'name', 'price', 'cnt'])
In [5]: df.set_index(['name', 'date', 'price'], inplace=True, drop=False)
In [6]: df
Out[6]:
cnt
name date price
apple 2019-06-01 11 10
pen 2019-06-01 3 4
apple 2019-06-02 10 11
pineapple 2019-06-02 30 2
In [7]: df.loc['pen']
Out[7]:
cnt
date price
2019-06-01 3 4
In [8]: print(df.loc['pen'].index.levels[0].tolist())
['2019-06-01', '2019-06-02']
In [10]: print(df.loc['pen'].index.get_level_values(0).tolist())
['2019-06-01']
pandas MultiIndex issue https://github.com/pandas-dev/pandas/issues/8893 https://github.com/pandas-dev/pandas/issues/2770
Issue-Label Bot is automatically applying the label #bug to this issue, with a confidence of 0.96. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!
Links: app homepage, dashboard and code for this bot.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. For admin, please label this issue .pinned to prevent stale bot from closing the issue.
Had the same issue, made following changes in viz.py of superset. `class TreemapViz(BaseViz):
viz_type = 'treemap'
verbose_name = _('Treemap')
credits = '<a href="https://d3js.org">d3.js</a>'
is_timeseries = False
def _nest(self, metric, df):
nlevels = df.index.nlevels
if nlevels == 1:
result = [{'name': n, 'value': v}
for n, v in zip(df.index, df[metric])]
else:
result = [{'name': l, 'children': self._nest(metric, df.loc[l])}
for l in df.index.unique(0)]
return result
def get_data(self, df):
df = df.set_index(self.form_data.get('groupby'))
chart_data = [{'name': metric, 'children': self._nest(metric, df)}
for metric in df.columns]
return chart_data`
Instead of for l in df.index.levels[0]] use for l in df.index.unique(0)].