Python Forum
TypeError: unhashable type: 'Series'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: unhashable type: 'Series'
#1
I get the TypeError: unhashable type: 'Series' when running the code. The dataset does have a column called 'Date' The code is below
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv")
date = input("What date would you like to view? (dd/mm/yyyy)\nEnter:  ")
df1 = df.loc(df["Date"] == date)
print(df1)
However the line below works;
[df1 = (df.loc[df.Date == date])

Attached Files

Thumbnail(s)
   
Reply
#2
Try like this:

df = pd.read_csv("/home/pedro/myPython/pandas/csv_files/get_data_by_date.csv")
date = input("What date would you like to view? (dd/mm/yyyy)\nEnter:  ")
date # returns '03/09/23'
df['Date'][2] # returns '03/09/23'
df.loc[df['Date'] == date] # returns the row where Date = '03/09/23'
Reply
#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


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 886 Dec-30-2023, 09:38 AM
Last Post: Gribouillis
  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
  TypeError: 'Series' object cannot be interpreted as an integer evelynow 2 17,736 Sep-11-2019, 02:43 PM
Last Post: timmahoney
  Error Message: TypeError: unhashable type: 'set' twinpiques 4 19,018 May-22-2019, 02:31 PM
Last Post: twinpiques
  AUCPR of individual features using Random Forest (Error: unhashable Type) melissa 1 3,325 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