Skip to content

ENH: .isnull and .notnull have been added as methods to Index to make this more consistent with the Series API #15300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1356,8 +1356,16 @@ Modifying and Computations
Index.unique
Index.nunique
Index.value_counts

Missing Values
~~~~~~~~~~~~~~
.. autosummary::
:toctree: generated/

Index.fillna
Index.dropna
Index.isnull
Index.notnull

Conversion
~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Other enhancements

- ``pd.read_excel`` now preserves sheet order when using ``sheetname=None`` (:issue:`9930`)
- Multiple offset aliases with decimal points are now supported (e.g. '0.5min' is parsed as '30s') (:issue:`8419`)

- ``.isnull()`` and ``.notnull()`` have been added to ``Index`` object to make them more consistent with the ``Series`` API (:issue:`15300`)
- ``pd.read_gbq`` method now allows query configuration preferences (:issue:`14742`)

- New ``UnsortedIndexError`` (subclass of ``KeyError``) raised when indexing/slicing into an
Expand Down
38 changes: 36 additions & 2 deletions pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,7 @@ def repeat(self, repeats, *args, **kwargs):
nv.validate_repeat(args, kwargs)
return self._shallow_copy(self._values.repeat(repeats))

def where(self, cond, other=None):
"""
_index_shared_docs['where'] = """
.. versionadded:: 0.19.0

Return an Index of same shape as self and whose corresponding
Expand All @@ -577,6 +576,9 @@ def where(self, cond, other=None):
cond : boolean same length as self
other : scalar, or array-like
"""

@Appender(_index_shared_docs['where'])
def where(self, cond, other=None):
if other is None:
other = self._na_value
values = np.where(cond, self.values, other)
Expand Down Expand Up @@ -1662,6 +1664,38 @@ def hasnans(self):
else:
return False

def isnull(self):
"""
Detect missing values

.. versionadded:: 0.20.0

Returns
-------
a boolean array of whether my values are null

See also
--------
pandas.isnull : pandas version
"""
return self._isnan

def notnull(self):
"""
Reverse of isnull

.. versionadded:: 0.20.0

Returns
-------
a boolean array of whether my values are not null

See also
--------
pandas.notnull : pandas version
"""
return ~self.isnull()

def putmask(self, mask, value):
"""
return a new Index of the values set with the mask
Expand Down
13 changes: 1 addition & 12 deletions pandas/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,8 @@ def _can_reindex(self, indexer):
""" always allow reindexing """
pass

@Appender(_index_shared_docs['where'])
def where(self, cond, other=None):
"""
.. versionadded:: 0.19.0

Return an Index of same shape as self and whose corresponding
entries are from self where cond is True and otherwise are from
other.

Parameters
----------
cond : boolean same length as self
other : scalar, or array-like
"""
if other is None:
other = self._na_value
values = np.where(cond, self.values, other)
Expand Down
27 changes: 26 additions & 1 deletion pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pandas import (Series, Index, Float64Index, Int64Index, UInt64Index,
RangeIndex, MultiIndex, CategoricalIndex, DatetimeIndex,
TimedeltaIndex, PeriodIndex, notnull)
TimedeltaIndex, PeriodIndex, notnull, isnull)
from pandas.types.common import needs_i8_conversion
from pandas.util.testing import assertRaisesRegexp

Expand Down Expand Up @@ -879,3 +879,28 @@ def test_fillna(self):
expected[1] = True
self.assert_numpy_array_equal(idx._isnan, expected)
self.assertTrue(idx.hasnans)

def test_nulls(self):
# this is really a smoke test for the methods
# as these are adequantely tested for function elsewhere

for name, index in self.indices.items():
if len(index) == 0:
self.assert_numpy_array_equal(
index.isnull(), np.array([], dtype=bool))
elif isinstance(index, MultiIndex):
idx = index.copy()
msg = "isnull is not defined for MultiIndex"
with self.assertRaisesRegexp(NotImplementedError, msg):
idx.isnull()
else:

if not index.hasnans:
self.assert_numpy_array_equal(
index.isnull(), np.zeros(len(index), dtype=bool))
self.assert_numpy_array_equal(
index.notnull(), np.ones(len(index), dtype=bool))
else:
result = isnull(index)
self.assert_numpy_array_equal(index.isnull(), result)
self.assert_numpy_array_equal(index.notnull(), ~result)
13 changes: 1 addition & 12 deletions pandas/tseries/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,19 +786,8 @@ def repeat(self, repeats, *args, **kwargs):
return self._shallow_copy(self.asi8.repeat(repeats),
freq=freq)

@Appender(_index_shared_docs['where'])
def where(self, cond, other=None):
"""
.. versionadded:: 0.19.0

Return an Index of same shape as self and whose corresponding
entries are from self where cond is True and otherwise are from
other.

Parameters
----------
cond : boolean same length as self
other : scalar, or array-like
"""
other = _ensure_datetimelike_to_i8(other)
values = _ensure_datetimelike_to_i8(self)
result = np.where(cond, values, other).astype('i8')
Expand Down