ToonoW commented on issue #4984: Pivot table not working - unhashable type: 
'dict'
URL: 
https://github.com/apache/incubator-superset/issues/4984#issuecomment-388745849
 
 
   I read souce code, find the reason.
   
   The reason is superset change the data struct of `metric`. So in some 
situation it is work, but some not.
   
   Before change, the type of `metric` is `str`, but after change the type of 
`metric` is `dict`.
     
   For example.
   Before
   ```json
   'avg__2004'
   ```
   
   After
   ```json
   {'expressionType': 'SIMPLE', 'column': {'column_name': '2004', 
'verbose_name': None, 'description': None, 'expression': '', 'filterable': 
False, 'groupby': False, 'is_dttm': False, 'type': 'BIGINT', 'optionName': 
'_col_2004'}, 'aggregate': 'AVG', 'sqlExpression': None, 'hasCustomLabel': 
False, 'fromFormData': True, 'label': 'AVG(2004)', 'optionName': 
'metric_681b3cp6kvh_sa14ez05lx'}
   ```
   
   #### Solution
   Change the file `viz.py` get metric code of per `get_data` function. We 
should make `metric` is a `str`.
   
   Like this.
   
   ```python
   class CountryMapViz(BaseViz):
   
       """A country centric"""
   
       viz_type = 'country_map'
       verbose_name = _('Country Map')
       is_timeseries = False
       credits = 'From bl.ocks.org By john-guerra'
   
       def query_obj(self):
           qry = super(CountryMapViz, self).query_obj()
           qry['metrics'] = [
               self.form_data['metric']]
           qry['groupby'] = [self.form_data['entity']]
           return qry
   
       def get_data(self, df):
           fd = self.form_data
           cols = [fd.get('entity')]
           metric = fd.get('metric')
           if isinstance(metric, dict):
               metric = metric['label']
           cols += [metric]
           ndf = df[cols]
           df = ndf
           df.columns = ['country_id', 'metric']
           d = df.to_dict(orient='records')
           return d
   ```
   
   Then `CountryMapViz` is OK.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org
For additional commands, e-mail: notifications-h...@superset.apache.org

Reply via email to