Use isinstance , nothing more:
if isinstance(x, pd.DataFrame): ...
PEP8 explicitly says that isinstance is the preferred type checking method
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 the variable is a string ( str or unicode ), because they are derived from basestring )
if isinstance(obj, basestring): i_am_string(obj)
Especially for pandas DataFrame objects:
import pandas as pd isinstance(var, pd.DataFrame)
Jakub M. Feb 11 '13 at 9:23 2013-02-11 09:23
source share