Created
March 9, 2020 18:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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