Python Forum
TypeError: unhashable type : list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: unhashable type : list
#1
I am getting the following

Error:
TypeError: unhashable type: 'list'
for this line

df_pre = df_pre.rename(columns={error_cols: 'p{}_{}'.format(q, error_cols)})

which is contained in this function

def get_metrics(df, q, error_cols):
    df_pre = df[df.order_delivery_leg == 'pre-accept']
    df_pre = df_pre.groupby(['region_name'])[error_cols].quantile(q).reset_index()
    df_pre = df_pre.rename(columns={error_cols: 'p{}_{}'.format(q, error_cols)})

    return df_pre
When I call it in my main module using this function

pre_startP25, pickup_startP25, del_startP25 = prepare_data.get_metrics(start,0.25,
                                                                       ['time_to_actual_pickup_error',
                                                                       'time_to_actual_dropoff_error'])
Is the way I am renaming the column incorrect?
Reply
#2
error_cols is a list. you try to use it as a key in a dict:
{error_cols: 'p{}_{}'.format(q, error_cols)}

list is mutable object and thus not hashable. it cannot be used as key in dict.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jul-20-2018, 07:48 AM)buran Wrote: error_cols is a list. you try to use it as a key in a dict:
{error_cols: 'p{}_{}'.format(q, error_cols)}

list is mutable object and thus not hashable. it cannot be used as key in dict.

Thanks, any thoughts on a workaround for something like this?
Reply
#4
When you need a hashable version of a list you can use a tuple:

{tuple(error_cols): 'p{}_{}'.format(q, error_cols)}
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unhashable error - Histogram code lsbpython 1 959 Aug-07-2022, 04:02 PM
Last Post: Yoriz
  search a list or tuple for a specific type ot class Skaperen 8 1,855 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,209 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,769 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,099 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,710 May-07-2022, 08:07 AM
Last Post: menator01
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,505 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 2,967 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,331 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,260 Jan-30-2021, 07:11 AM
Last Post: alloydog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020