Skip to content

Commit 4435b6e

Browse files
committed
ENH: .isnull and .notnull have been added as methods to Index to make this more consistent with the Series API
1 parent 9ddba8d commit 4435b6e

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

doc/source/api.rst

+8
Original file line numberDiff line numberDiff line change
@@ -1356,8 +1356,16 @@ Modifying and Computations
13561356
Index.unique
13571357
Index.nunique
13581358
Index.value_counts
1359+
1360+
Missing Values
1361+
~~~~~~~~~~~~~~
1362+
.. autosummary::
1363+
:toctree: generated/
1364+
13591365
Index.fillna
13601366
Index.dropna
1367+
Index.isnull
1368+
Index.notnull
13611369

13621370
Conversion
13631371
~~~~~~~~~~

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Other enhancements
125125

126126
- ``pd.read_excel`` now preserves sheet order when using ``sheetname=None`` (:issue:`9930`)
127127
- Multiple offset aliases with decimal points are now supported (e.g. '0.5min' is parsed as '30s') (:issue:`8419`)
128-
128+
- ``.isnull()`` and ``.notnull()`` have been added to ``Index`` object to make them more consistent with the ``Series`` API (:issue:``)
129129
- ``pd.read_gbq`` method now allows query configuration preferences (:issue:`14742`)
130130

131131
- New ``UnsortedIndexError`` (subclass of ``KeyError``) raised when indexing/slicing into an

pandas/indexes/base.py

+28
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,34 @@ def hasnans(self):
16621662
else:
16631663
return False
16641664

1665+
def isnull(self):
1666+
"""
1667+
Detect missing values
1668+
1669+
Returns
1670+
-------
1671+
a boolean array of whether my values are null
1672+
1673+
See also
1674+
--------
1675+
pandas.isnull : pandas version
1676+
"""
1677+
return self._isnan
1678+
1679+
def notnull(self):
1680+
"""
1681+
Reverse of isnull
1682+
1683+
Returns
1684+
-------
1685+
a boolean array of whether my values are not null
1686+
1687+
See also
1688+
--------
1689+
pandas.notnull : pandas version
1690+
"""
1691+
return ~self.isnull()
1692+
16651693
def putmask(self, mask, value):
16661694
"""
16671695
return a new Index of the values set with the mask

pandas/tests/indexes/common.py

+22
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,25 @@ def test_fillna(self):
879879
expected[1] = True
880880
self.assert_numpy_array_equal(idx._isnan, expected)
881881
self.assertTrue(idx.hasnans)
882+
883+
def test_nulls(self):
884+
# this is really a smoke test for the methods
885+
# as these are adequantely tested for function elsewhere
886+
887+
for name, index in self.indices.items():
888+
if len(index) == 0:
889+
self.assert_numpy_array_equal(index.isnull(), np.array([], dtype=bool))
890+
elif isinstance(index, MultiIndex):
891+
idx = index.copy()
892+
msg = "isnull is not defined for MultiIndex"
893+
with self.assertRaisesRegexp(NotImplementedError, msg):
894+
idx.isnull()
895+
else:
896+
897+
if not index.hasnans:
898+
self.assert_numpy_array_equal(index.isnull(), np.zeros(len(index), dtype=bool))
899+
self.assert_numpy_array_equal(index.notnull(), np.ones(len(index), dtype=bool))
900+
else:
901+
result = isnull(index)
902+
self.assert_numpy_array_equal(index.isnull(), result)
903+
self.assert_numpy_array_equal(index.notnull(), ~result)

0 commit comments

Comments
 (0)