isinstance() does not detect type for pandas dataframe column

Viewed 2669

I am trying to using isinstance() at the beginning of a loop to check the type for each column in a data frame.

I have a lot of columns but isinstance universally fails. For example;

test['Total Revenue'].dtypes

returns

dtype('int64')

But using isinstance returns

isinstance(test['Total Revenue'].dtypes, (int, np.integer))
False

isinstance only recognizes my columns as an object

isinstance(test['Total Revenue'].dtypes, object)
True

My gut tells me it has something to do with how isinstance is accessing the value, but I'm stuck

1 Answers

pd.Series.dtypes returns an instance of the np.dtype class, which is also a sub class of object. So isinstance(series.dtypes, int) will of course fails, but isinstance(pd.Series([1,2,3]).dtypes,np.dtype) is True.

I think you want

test['Total Revenue'].dtypes == 'int64'

or

pd.Series([1,2,3,]).dtypes == np.int
Related