Skip to content

Instantly share code, notes, and snippets.

@himat
Created March 9, 2020 18:10
Show Gist options
  • Save himat/232f32c60a952bbf8c785ff1844b9c52 to your computer and use it in GitHub Desktop.
Save himat/232f32c60a952bbf8c785ff1844b9c52 to your computer and use it in GitHub Desktop.
[Don't use `is` to check for NaN in Python] Always use `np.isnan(.)` #python
>>> import pandas as pd
>>> import numpy as np
>>>
>>> s = pd.Series([10, 11, 12])
>>>
>>> s[2] = np.nan
>>> s[2] is np.nan
False
# A NaN type can get coerced which is what happened above, and when this happens, a separate instance is created that thus has a different id(.) which maks the `is` check fail
>>> type(np.nan)
float
>>> type(s[2])
float64
# Always use `np.isnan(.)`
>>> np.isnan(s[2])
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment