Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check pandas variable type
#1
I am trying to check variable's data type is list or dataframe.
When I got type(df_c) is Out[155]: pandas.core.frame.DataFrame.
Checking it again by type(df_c) == 'pandas.core.frame.DataFrame', it showed False, not True. <-- I think it should print "True" rather than "False".

import pandas as pd

c=[1,2,3]

df_c=pd.DataFrame(c)

type(df_c)
Out[155]: pandas.core.frame.DataFrame

type(df_c) == 'pandas.core.frame.DataFrame'
Out[156]: False
Reply
#2
you compare type of the object with str object - that's why it's false.
As per the docs and also PEP8 recommendation, the isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.

import pandas as pd
 
spam = [1,2,3]
df = pd.DataFrame(spam)

# better use isinstance()
print(isinstance(df, pd.DataFrame))
print(isinstance(spam, list))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Oh! I just got a lesson by isinstance this function.
Thank for your assistance.

(Jun-12-2020, 08:47 AM)buran Wrote: you compare type of the object with str object - that's why it's false.
As per the docs and also PEP8 recommendation, the isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.

import pandas as pd
 
spam = [1,2,3]
df = pd.DataFrame(spam)

# better use isinstance()
print(isinstance(df, pd.DataFrame))
print(isinstance(spam, list))
Reply
#4
just for brevity, although not recommended using type will work like this
print(type(df_c) == pd.DataFrame)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  isinstance() always return true for object type check Yoki91 2 2,498 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  Python - change variable type during program execution ple 1 2,331 Apr-12-2020, 08:43 AM
Last Post: buran
  Type hinting - return type based on parameter micseydel 2 2,425 Jan-14-2020, 01:20 AM
Last Post: micseydel
  How to check hardware type? MuntyScruntfundle 4 2,970 Feb-16-2019, 12:23 PM
Last Post: snippsat
  Storing MySQL BIT Data Type data in python variable krushna 2 3,431 Dec-31-2018, 01:28 AM
Last Post: krushna
  how i can check the input type? Firdaos 3 2,798 Dec-13-2018, 11:39 PM
Last Post: wavic
  how to check for file type in a folder SoulsKeeper 4 4,870 Sep-15-2018, 02:48 PM
Last Post: ichabod801
  What would be a way to check if a variable or class existed in an if? Klar 8 5,020 Dec-28-2017, 08:19 PM
Last Post: nilamo
  check if value of passed variable has uppercase characters in it. wfsteadman 3 3,194 Sep-01-2017, 05:52 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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