Python Forum
TypeError: unhashable type : list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: TypeError: unhashable type : list (/thread-11664.html)



TypeError: unhashable type : list - yankeephan87 - Jul-20-2018

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?


RE: TypeError: unhashable type : list - buran - Jul-20-2018

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.


RE: TypeError: unhashable type : list - yankeephan87 - Jul-20-2018

(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?


RE: TypeError: unhashable type : list - ichabod801 - Jul-20-2018

When you need a hashable version of a list you can use a tuple:

{tuple(error_cols): 'p{}_{}'.format(q, error_cols)}