Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unhashable type: 'slice' for pandas

Tags:

python

pandas

I have a pandas datastructure, which I create like this:

test_inputs = pd.read_csv("../input/test.csv", delimiter=',')

Its shape

print(test_inputs.shape)

is this

(28000, 784)

I would like to print a subset of its rows, like this:

print(test_inputs[100:200, :])
print(test_inputs[100:200, :].shape)

However, I am getting:

TypeError: unhashable type: 'slice'

Any idea what could be wrong?

like image 708
octavian Avatar asked Jul 02 '17 12:07

octavian


People also ask

How do you fix a TypeError Unhashable type slice?

The “TypeError: unhashable type: 'slice'” error is raised when you try to access items from a dictionary using slicing syntax. To solve this error, make sure you refer to the items you want to access from a dictionary directly.

How do I fix Unhashable type?

This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key. The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.

How do you fix a TypeError Unhashable type dict?

The Python "TypeError: unhashable type: 'dict'" occurs when we use a dictionary as a key in another dictionary or as an element in a set . To solve the error, use a frozenset instead, or convert the dictionary into a JSON string before using it as a key.

What is Unhashable type in Python?

TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed.


1 Answers

Indexing in pandas is really confusing, as it looks like list indexing but it is not. You need to use .iloc, which is indexing by position

print(test_inputs.iloc[100:200, :])

And if you don't use column selection you can omit it

print(test_inputs.iloc[100:200])

P.S. Using .loc (or just []) is not what you want, as it would look not for the row number, but for the row index (which can be filled we anything, not even numbers, not even unique). Ranges in .loc will find rows with index value 100 and 200, and return the lines between. If you just created the DataFrame .iloc and .loc may give the same result, but using .loc in this case is a very bad practice as it will lead you to difficult to understand problem when the index will change for some reason (for example you'll select some subset of rows, and from that moment the row number and index will not be the same).

like image 122
Leonid Mednikov Avatar answered Sep 17 '22 18:09

Leonid Mednikov