Why is isinstance() returning different values for the same data depending on implementation?

Viewed 74

I have a pandas dataframe df where I want to check the datatypes

import pandas as pd
import numpy as np

df
>>> a1    a2    a3    a4
0   5     10.08 22.9  50.45
1   25    13.80 43.0  NaN
2   2     8.30  15.0  17.00
print(df.dtypes)
>>>
a1        int64
a2     float64
a3     float64
a4     float64
dtype: object

I have a function which checks the datatypes, namely whether a value is a scalar or not

def isScalar(a):
    if isinstance(a, (int, float, np.int64, np.float64)):
        return True
    else:
        return False

Now, if I run

df.applymap(isScalar)
>>> a1      a2      a3      a3      
0   False   True    True    True
1   False   True    True    True
2   False   True    True    True

we see that the a1 column is all False, which contradicts if I run individual values through isScalar()

print(type(df.a1[1]))
>>> numpy.int64
print(isScalar(df.a1[1]))
>>> True

What is going on here? The values of a1 returned by df.applymap(isScalar) should all be True.

0 Answers
Related