NaN
¶np.nan != np.nan
, math.nan != math
.np.isnan
or math.nan
import math
import numpy as np
mt_nan = math.nan
np_nan = np.nan
np.isnan(mt_nan)
True
np.isnan(np_nan)
True
math.isnan(mt_nan)
True
math.isnan(np_nan)
True
math.isnan == np.isnan
False
np_nan == np_nan
False
mt_nan == mt_nan
False
pd.DataFrame
wst. None
¶import pandas as pd
None
is converted to NaN
when the column type is float
df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': 1.4}])
display(df)
a | b | |
---|---|---|
0 | 1 | NaN |
1 | 2 | 1.4 |
display(df.dtypes)
a int64 b float64 dtype: object
assert not (df['b'].values[0] is None)
assert np.isnan(df['b'].values[0])
None
is unchanged when the column type is object
df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': 's'}])
display(df)
display(df.dtypes)
a | b | |
---|---|---|
0 | 1 | None |
1 | 2 | s |
a int64 b object dtype: object
assert df['b'].values[0] is None
import unittest
np.isnan isn't applicable to None
type
try:
np.isnan(df['b'].values[0])
except TypeError as err:
print('✓ TypeError is raised!')
print(err)
✓ TypeError is raised! ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
try another case of object column
df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': [1, 2]}])
display(df)
a | b | |
---|---|---|
0 | 1 | None |
1 | 2 | [1, 2] |
display(df.dtypes)
a int64 b object dtype: object
assert df['b'].values[0] is None