1

I have been receiving "TypeError: 'Series' objects are mutable, thus they cannot be hashed." And while I understand what it means, I can't seem to work out a solution. I have tried converting the series to a tuple (as seen below) since tuples are immutable. I am receiving the same error message. Any idea how I can fix this problem?

import pandas as pd

data_file = pd.read_csv("file:///C:/Users/carte/OneDrive/Desktop/TEST.csv")

Jordan = []
Carter = []
Reece = []
Liam = []
Bryce = []
David = []

Jordan = data_file['Jordan']
Carter = data_file['Carter']
Reece = data_file['Reece']
Liam = data_file['Liam']
Bryce = data_file['Bryce']
David = data_file['David']

invite = []
invite.append(Carter)
invite.append(Jordan)
invite.append(Reece)
invite.append(David)
invite.append(Liam)
invite.append(Bryce)

tuple(invite)

invite = list(dict.fromkeys(invite))
print(invite)

For some context, my code is supposed to take all of the names on each list and remove the duplicates before printing a single list at the end.

  • 1
    Tuples themselves are immutable, but if they contain a mutable object, then you're going to run into the same problem. – Makoto Feb 18 '19 at 23:15
  • Can you flesh out your question with the actual stacktrace (error) and a snippet or example of what `TEST.csv` looks like? – Chris Larson Feb 18 '19 at 23:17
  • Possible duplicate of ["Series objects are mutable and cannot be hashed" error](https://stackoverflow.com/questions/29700552/series-objects-are-mutable-and-cannot-be-hashed-error) – Edeki Okoh Feb 19 '19 at 00:03
  • 1
    Jordan = data-file['Jordan'] is creating a [Series](https://stackoverflow.com/questions/29700552/series-objects-are-mutable-and-cannot-be-hashed-error) and as the error says you cannot has a series. Follow the link to separate the column data into list correctly. – Edeki Okoh Feb 19 '19 at 00:05
  • `tuple(invite)` doesn't actually do anything since you don't assign it to anything. – Mark Ransom Feb 19 '19 at 01:18

2 Answers2

1

As Makoto suggests, the issue is not the type of invite, but the type of what's going into invite. It looks like you're trying to unpack each instance of pandas.Series to fill the list before de-duplicating with dict.fromkeys(). For this, you should use list.extend() rather than list.append(). The former expands a list with all of the elements inside its argument, while the latter adds a single new element to a list (Difference between append vs. extend list methods in Python). For example:

invite = []
invite.extend(Carter)
invite.extend(Jordan)

You might also need to manually convert the pandas.Series into lists, which would look something like this:

invite = []
invite.extend(Carter.to_list())
invite.extend(Jordan.to_list())

Note that creating empty lists before extracting data from the CSV should be unnecessary.

pydsigner
  • 2,177
  • 1
  • 20
  • 31
0

There's probably a number of different ways to do this, so here's one option and this cuts down on the amount of code you need significantly.

# Assuming 'invites' is a list of strings that needs to be reduced to uniques
# This method will maintain the order of your list, but remove duplicates
uniques = list({key:'' for key in invites}.keys())

# Using set() will remove duplicates but will not preserve the order of the entries
uniques = list(set(invites))
NL23codes
  • 699
  • 7
  • 27