Skip to content

Commit e4502bf

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 e4502bf

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
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:`15300`)
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

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

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

pandas/tests/indexes/common.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pandas import (Series, Index, Float64Index, Int64Index, UInt64Index,
99
RangeIndex, MultiIndex, CategoricalIndex, DatetimeIndex,
10-
TimedeltaIndex, PeriodIndex, notnull)
10+
TimedeltaIndex, PeriodIndex, notnull, isnull)
1111
from pandas.types.common import needs_i8_conversion
1212
from pandas.util.testing import assertRaisesRegexp
1313

@@ -879,3 +879,28 @@ 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(
890+
index.isnull(), np.array([], dtype=bool))
891+
elif isinstance(index, MultiIndex):
892+
idx = index.copy()
893+
msg = "isnull is not defined for MultiIndex"
894+
with self.assertRaisesRegexp(NotImplementedError, msg):
895+
idx.isnull()
896+
else:
897+
898+
if not index.hasnans:
899+
self.assert_numpy_array_equal(
900+
index.isnull(), np.zeros(len(index), dtype=bool))
901+
self.assert_numpy_array_equal(
902+
index.notnull(), np.ones(len(index), dtype=bool))
903+
else:
904+
result = isnull(index)
905+
self.assert_numpy_array_equal(index.isnull(), result)
906+
self.assert_numpy_array_equal(index.notnull(), ~result)

0 commit comments

Comments
 (0)