Python Forum

Full Version: TypeError: unhashable type : list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.
(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?
When you need a hashable version of a list you can use a tuple:

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