jon
jon

Reputation: 359

TypeError: unhashable type: 'Int64Index'

The section of my code that is causing me problems is

def Half_Increase(self):
    self.keg_count=summer17.iloc[self.result_rows,2].values[0]
    self.keg_count +=1
    summer17[self.result_rows,2] = self.keg_count
    print(keg_count)

So this function is to be executed when a button widget is pressed. It's supposed to get the value from a specific cell in a dataframe, add 1 to it, and then return the new value to the dataframe. (I'm not entirely sure if this is the proper way to do this.)

I get the following error

Exception in Tkinter callback
Traceback (most recent call last):

  File "C:\Python3.6\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "beerfest_program_v0.3.py", line 152, in Half_Increase
    summer17[self.result_rows,2] = self.keg_count
  File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2331, in __setitem__
    self._set_item(key, value)
  File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2397, in _set_item
    value = self._sanitize_column(key, value)
  File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2596, in _sanitize_column
    if broadcast and key in self.columns and value.ndim == 1:
  File "C:\Python3.6\lib\site-packages\pandas\core\indexes\base.py", line 1640, in __contains__
    hash(key)
  File "C:\Python3.6\lib\site-packages\pandas\core\indexes\base.py", line 1667, in __hash__
    raise TypeError("unhashable type: %r" % type(self).__name__)
TypeError: unhashable type: 'Int64Index'

I'm guessing this has something to do with the variable types not matching but I've looked and cant find how to remedy this.

Upvotes: 2

Views: 11132

Answers (1)

jezrael
jezrael

Reputation: 863501

I think you need iloc:

summer17.iloc[result_rows,2] += 1

Sample:

summer17 = pd.DataFrame({'a':[1,2,3],
                         'b':[3,4,5],
                         'c':[5,9,7]})
#if reselt_rows is scalar
result_rows = 1

print(summer17)
   a  b  c
0  1  3  5
1  2  4  9
2  3  5  7

summer17.iloc[result_rows,2] += 1
print(summer17)
   a  b   c
0  1  3   5
1  2  4  10
2  3  5   7

It is same as:

#get value
keg_count=summer17.iloc[result_rows,2]
#increment
keg_count +=1
#set value
summer17.iloc[result_rows,2] = keg_count
print(summer17)
   a  b   c
0  1  3   5
1  2  4  10
2  3  5   7

But if result_rows is list or 1d array:

result_rows = [1,2]

#get all values per positions defined in result_rows 
#filter only first value by values[0]
keg_count=summer17.iloc[result_rows,2].values[0]
#increment 
keg_count +=1
#set all values of result_rows by incremented value 
summer17.iloc[result_rows,2] = keg_count
print(summer17)
   a  b   c
0  1  3   5
1  2  4  10
2  3  5  10

Upvotes: 2

Related Questions