I'm coding some tests to verify if some columns of a pandas dataframe are integer (or floats for other columns), but with authorized NaN values.
I'm currently trying with assert statements:
import pandas as pd
import pandas.api.types as ptypes
df = pd.read_excel('the_df', na_values='')
try:
assert(ptypes.is_numeric_dtype(df['a_column']))
except AssertionError:
return False
else:
return True
But this method returns False when there are NaN and I can't figure out how to fix this. How can I proceed ?
Thanks.
CodePudding user response:
Check for the dtype, not the entire array:
import pandas as pd
import pandas.api.types as ptypes
df = pd.read_excel('the_df', na_values='')
return ptypes.is_numeric_dtype(df['a_column'].dtype)
(You don't need the try-except-else chain: the above achieves the exact same thing: it returns True
or False
).