-
-
Save jaypeedevlin/fdfb88f6fd1031a819f1d46cb36384da to your computer and use it in GitHub Desktop.
pd vs np for detecting null
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
In [16]: data_pd = pd.Series(np.random.rand(10000000)) | |
In [17]: data_pd[data_pd < .2] = np.nan | |
In [18]: %timeit np.isfinite(data_pd) | |
10 loops, best of 3: 27.3 ms per loop | |
In [19]: %timeit pd.notnull(data_pd) | |
100 loops, best of 3: 11.2 ms per loop | |
In [20]: # check for equality | |
In [21]: (pd.notnull(data_pd).values == np.isfinite(data_pd)).all() | |
Out[21]: True | |
In [22]: data_np = np.random.rand(10000000) | |
In [23]: data_np[data_np < .2] = np.nan | |
In [24]: %timeit pd.notnull(data_np) | |
100 loops, best of 3: 11.2 ms per loop | |
In [25]: %timeit np.isfinite(data_np) | |
10 loops, best of 3: 24.6 ms per loop | |
In [26]: (pd.notnull(data_np) == np.isfinite(data_np)).all() | |
Out[26]: True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're doing the operations on the pandas object. The reverse is actually true if you do the operations on the ndarray, and it's not even close (numpy is ~ 25x faster)
And here's your own test case:
Running: