237

when my function f is called with a variable I want to check if var is a pandas dataframe:

def f(var):
    if var == pd.DataFrame():
        print "do stuff"

I guess the solution might be quite simple but even with

def f(var):
    if var.values != None:
        print "do stuff"

I can't get it to work like expected.

Max Ghenis
  • 14,783
  • 16
  • 84
  • 132
trbck
  • 5,187
  • 6
  • 26
  • 29
  • 1
    Your code says "if `var` is equal to an empty dataframe". What you really want is "if the type of `var` is equal to the type `pd.DataFrame`". You can check that using `isinstance` – Katriel Feb 11 '13 at 09:17

3 Answers3

350

Use isinstance, nothing else:

if isinstance(x, pd.DataFrame):
    ... # do something

PEP8 says explicitly that isinstance is the preferred way to check types

No:  type(x) is pd.DataFrame
No:  type(x) == pd.DataFrame
Yes: isinstance(x, pd.DataFrame)

And don't even think about

if obj.__class__.__name__ = 'DataFrame':
    expect_problems_some_day()

isinstance handles inheritance (see What are the differences between type() and isinstance()?). For example, it will tell you if a variable is a string (either str or unicode), because they derive from basestring)

if isinstance(obj, basestring):
    i_am_string(obj)

Specifically for pandas DataFrame objects:

import pandas as pd
isinstance(var, pd.DataFrame)
cs95
  • 379,657
  • 97
  • 704
  • 746
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
155

Use the built-in isinstance() function.

import pandas as pd

def f(var):
    if isinstance(var, pd.DataFrame):
        print("do stuff")
cs95
  • 379,657
  • 97
  • 704
  • 746
Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97
  • 4
    How can you generalise this to the case in which a user may use the function you define, but didn't `import pandas as pd`, but instead just `import pandas`? Just perform an `or` on both possibilities, or is there something more sophisticated I don't know of? – n1k31t4 Jun 18 '17 at 22:14
  • 1
    A potential solution could be to put the import statement inside the function so there is no chance of a user importing pandas using some other method. To speed things up (to avoid importing the entire panda library for a simple check) you could just use something like `import pandas.DataFrame as panda_type` and then inside then check the array type using `isinstance(var, panda_type)` – pacificgilly1992 Dec 31 '18 at 20:13
4

Or you can use the simplest solution: type(x).

If it is Data Frame it will output pandas.core.frame.DataFrame.

cottontail
  • 10,268
  • 18
  • 50
  • 51