'Pandas.DataFrame.dtypes.isin() strange output

Let's start with example:

import pandas as pd
import numpy as np

test_frame = pd.DataFrame({'a':[1,2,3], 'b':['a','b','c']})
print('===print dtypes:')
print(test_frame.dtypes)
print('===first col type:')
print(test_frame.dtypes[0])
print('===type of type indicator:')
print(type(test_frame.dtypes[0]))
print('===is equal to np.int64:')
print(test_frame.dtypes[0] == np.int64)

print()
print('===isin([np.int64]):')
print(test_frame.dtypes.isin([np.int64]))

The output looks like:

===print dtypes:
a     int64
b    object
dtype: object
===first col type:
int64
===type of type indicator:
<class 'numpy.dtype[int64]'>
===is equal to np.int64:
True

===isin([np.int64]):
a    False
b    False
dtype: bool

As you can see I have a DataFrame with int64 column, python 'understands' that there is np.int64 column. But when i use .isin function, it doesn't see np.int64 column in 'a'. Actually a have working code for this case:

list(map(lambda x: x in [np.int64, np.float64], test_frame.dtypes.tolist()))

But i don't understand why .isin doesn't work like i wish. Can you please tell me why this is?



Solution 1:[1]

np.int64 is not the same as np.dtype('int64'):

test_frame.dtypes.isin([np.dtype('int64')])
#a     True
#b    False
#dtype: bool

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1