Python Forum
Error Message: TypeError: unhashable type: 'set'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error Message: TypeError: unhashable type: 'set'
#5
(May-08-2019, 05:32 PM)DeaD_EyE Wrote: Quick and dirty solution with less Python knowledge:
hashable_data = tuple(set(ITERABLE))
Mutable objects don't have a hash, because they can mutate.
Immutable objects doesn't change, so they have have a hash.

There is also a built-in type, called frozenset and yes, it does what it sounds like.
This is an immutable set, which has an hash.

You can make the test:

# will fail
{set(): 42}

# is ok
{frozenset(): 42}

Try this:

df_sar['sar_details_sent_norm_trigrams_unique'] = df_sar['sar_details_sent_norm_trigrams_'].apply(lambda x: frozenset([trigram for sent in x for trigram in sent]))
And you can remove the square brackets, then it's a generator expression, which is consumed by frozenset (saves memory).
Otherwise first a list from the set is created in memory, then it's applied to the dataframe.

df_sar['sar_details_sent_norm_trigrams_unique'] = df_sar['sar_details_sent_norm_trigrams_'].apply(lambda x: frozenset(trigram for sent in x for trigram in sent))

This is great - thank you.
Reply


Messages In This Thread
RE: Error Message: TypeError: unhashable type: 'set' - by twinpiques - May-22-2019, 02:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: unhashable type: 'Series' bongielondympofu 2 540 Mar-14-2024, 06:12 PM
Last Post: deanhystad
  [pandas] TypeError: list indices must be integers or slices, not str but type is int. cspower 4 885 Dec-30-2023, 09:38 AM
Last Post: Gribouillis
  GroupBy - Sum = Error [datetime64 type does not support sum operations] BSDevo 4 2,905 Oct-27-2023, 07:22 PM
Last Post: BSDevo
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,642 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Type error in Cross_val_score Vicral 0 1,840 Jul-20-2021, 12:18 PM
Last Post: Vicral
  type error array BrianPA 2 2,428 Jan-17-2021, 01:48 PM
Last Post: BrianPA
  Get error message in a GAN neural network tutorial jdude50 0 1,687 Oct-22-2020, 11:11 PM
Last Post: jdude50
  Error binding parameter 0 - probably unsupported type. illmattic 7 10,399 Jul-18-2020, 09:32 PM
Last Post: illmattic
  Series object error message abhaydd 1 4,909 Aug-11-2019, 01:29 AM
Last Post: boring_accountant
  Error Message - Akainu 2 3,290 May-24-2019, 09:09 PM
Last Post: Akainu

Forum Jump:

User Panel Messages

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