3

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.

PyRsquared
  • 6,970
  • 11
  • 50
  • 86
  • 2
    I cannot reproduce your issue. I am getting all `True` values – James Dec 20 '18 at 13:45
  • It turns out this was a python version issue. I have python 2.7 and python 3.7 environments, both with the same versions of pandas as numpy (0.23.4 and 1.15.4 respectively). In python 2.7 I get the error asked in the question. In python 3.7 I don't. It would still be good to know why this happens though. – PyRsquared Dec 20 '18 at 14:09
  • I can't reproduce even on 2.7: https://repl.it/repls/VictoriousQuickwittedTruespace – jpp Dec 20 '18 at 16:56

0 Answers0