Python Forum
TypeError: unhashable type: 'Series'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: unhashable type: 'Series'
#3
The reason "df.loc(df["Date"] == date)" doesn't work is because that is not how you use loc. If you read the documentation you'll see that your second example is the correct syntax for using loc, other than the surrounding parenthesis that do nothing.

https://pandas.pydata.org/pandas-docs/st...e.loc.html

Are you looking for more explanation than "You are using it wrong"? I can take a shot.

DataFrame.loc is not a function, it is an object. In particular it is a _LocIndexer object. You can see that if you write something like this
import pandas as pd
df = pd.DataFrame()
print(df.loc)
Output:
<pandas.core.indexing._LocIndexer object at 0x0000017ACE809030>
This class has a special method named __getitem__() that gets called when you use square brackets like this: df.loc[df.Date == date]. __getitem__() is one of many Python dunder methods (or magic methods) that are used to associate operators like [], >, + with methods __getitem__, __gt__, __add__.

There is another dunder method __call__ that gets called when you follow an instance by (). This is what happened in your first example. When you did this: df.loc(df["Date"] == date) you were actually doing this: df.loc.__call__(df["Date"] == date).

Your program crashed because you accidentally called loc.__call__() when you meant to call loc.__getitem__().
Pedroski55 likes this post
Reply


Messages In This Thread
RE: TypeError: unhashable type: 'Series' - by deanhystad - Mar-14-2024, 06:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [pandas] TypeError: list indices must be integers or slices, not str but type is int. cspower 4 962 Dec-30-2023, 09:38 AM
Last Post: Gribouillis
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,706 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  TypeError: 'Series' object cannot be interpreted as an integer evelynow 2 17,828 Sep-11-2019, 02:43 PM
Last Post: timmahoney
  Error Message: TypeError: unhashable type: 'set' twinpiques 4 19,202 May-22-2019, 02:31 PM
Last Post: twinpiques
  AUCPR of individual features using Random Forest (Error: unhashable Type) melissa 1 3,356 Jul-10-2017, 12:48 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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