#!/usr/bin/env python # coding: utf-8 # # Demonstration of some properties related to `NaN` # # * `np.nan != np.nan`, `math.nan != math`. # * use `np.isnan` or `math.nan` # In[1]: import math import numpy as np # In[2]: mt_nan = math.nan # In[3]: np_nan = np.nan # In[4]: np.isnan(mt_nan) # In[5]: np.isnan(np_nan) # In[6]: math.isnan(mt_nan) # In[7]: math.isnan(np_nan) # In[8]: math.isnan == np.isnan # In[9]: np_nan == np_nan # In[10]: mt_nan == mt_nan # ## behavior of `pd.DataFrame` wst. `None` # In[11]: import pandas as pd # `None` is converted to `NaN` when the column type is `float` # In[12]: df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': 1.4}]) display(df) # In[13]: display(df.dtypes) # In[14]: assert not (df['b'].values[0] is None) # In[15]: assert np.isnan(df['b'].values[0]) # `None` is unchanged when the column type is `object` # In[16]: df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': 's'}]) display(df) display(df.dtypes) # In[17]: assert df['b'].values[0] is None # In[18]: import unittest # np.isnan isn't applicable to `None` type # In[19]: try: np.isnan(df['b'].values[0]) except TypeError as err: print('✓ TypeError is raised!') print(err) # try another case of `object column` # In[20]: df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': [1, 2]}]) display(df) # In[21]: display(df.dtypes) # In[22]: assert df['b'].values[0] is None