question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pivot table not working - unhashable type: 'dict'

See original GitHub issue

Superset version

Superset 0.23.0dev

Expected results

Pivot table should display the table with results.

Actual results

unhashable type: ‘dict’

image

Steps to reproduce

  • Install superset, load sample data as given in the installation guide
  • Create pivot table with any table and choose any metric except Count(*) . Because its working only for Count(*) metric.
  • Run Query

PS: The same query working for other chart types with same parameters.

Stacktrace:

2018-05-11 17:17:36,253:DEBUG:root:[stats_logger] (incr) explore_json
2018-05-11 17:17:36,375:DEBUG:parsedatetime:eval now with context - False, False
2018-05-11 17:17:36,376:INFO:root:Cache key: 0ee55f4b95926a7373cfbf36300e1d71
2018-05-11 17:17:36,380:INFO:root:Database.get_sqla_engine(). Masked URL: sqlite:////home/gancha/.superset/superset.db
2018-05-11 17:17:36,388:INFO:root:SELECT gender AS gender, SUM(num) AS "SUM(num)" 
FROM birth_names 
WHERE ds >= '1918-05-11 00:00:00.000000' AND ds <= '2018-05-11 17:17:36.000000' GROUP BY gender ORDER BY "SUM(num)" DESC
 LIMIT 10000 OFFSET 0
2018-05-11 17:17:36,405:INFO:root:Database.get_sqla_engine(). Masked URL: sqlite:////home/gancha/.superset/superset.db
2018-05-11 17:17:36,464:DEBUG:root:[stats_logger] (incr) loaded_from_source
2018-05-11 17:17:40,465:ERROR:root:unhashable type: 'dict'
Traceback (most recent call last):
  File "/home/gancha/superset/superset/views/core.py", line 1107, in generate_json
    payload = viz_obj.get_payload()
  File "/home/gancha/superset/superset/viz.py", line 371, in get_payload
    payload['data'] = self.get_data(df)
  File "/home/gancha/superset/superset/viz.py", line 670, in get_data
    margins=self.form_data.get('pivot_margins'),
  File "/home/gancha/superset/venv/lib/python2.7/site-packages/pandas/core/frame.py", line 4468, in pivot_table
    margins_name=margins_name)
  File "/home/gancha/superset/venv/lib/python2.7/site-packages/pandas/core/reshape/pivot.py", line 57, in pivot_table
    if i not in data:
  File "/home/gancha/superset/venv/lib/python2.7/site-packages/pandas/core/generic.py", line 1075, in __contains__
    return key in self._info_axis
  File "/home/gancha/superset/venv/lib/python2.7/site-packages/pandas/core/indexes/base.py", line 1694, in __contains__
    hash(key)
TypeError: unhashable type: 'dict'    

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
bgriffencommented, May 18, 2018

Did your original query work when doing heatmaps? I have a table which has row and column and a value at that row and column (all ints) and when I do X=row, Y=col, metric=value, I get unhashable dict error just as above. If this is unrelated, I can open another issue but it is basically the exact same but for histogram charts.

1reaction
ToonoWcommented, May 14, 2018

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

'avg__2004'

After

{'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.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pivot table not working - unhashable type: 'dict' #4984 - GitHub
Create pivot table with any table and choose any metric except Count(*) . Because its working only for Count(*) metric. Run Query. PS:...
Read more >
TypeError: Unhashable type numpy.ndarray on pivot_table
Any ideas how to resolve this? Do I need to change anything in my columns? python-3.x · pandas · pivot-table.
Read more >
How to Handle Unhashable Type List Exceptions in Python
The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.
Read more >
Python: TypeError: unhashable type: 'list' - Net-Informations.Com
This error shows that the my_dict key [1,2,3] is List and List is not a hashable type in Python . Dictionary keys must...
Read more >
Dictionaries In Python - Nbshare Notebooks
Dictionaries in Python. You can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces ({}).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found