Skip to content

dropna method added to Index. #7799

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
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
10 changes: 10 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ def is_(self, other):
# use something other than None to be clearer
return self._id is getattr(other, '_id', Ellipsis)

def dropna(self):
"""
Return Index without null values

Returns
-------
dropped : Index
"""
return self[~isnull(self.values)]

def _reset_identity(self):
"""Initializes or resets ``_id`` attribute with new object"""
self._id = _Identity()
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,13 @@ def test_nan_first_take_datetime(self):
exp = Index([idx[-1], idx[0], idx[1]])
tm.assert_index_equal(res, exp)

def test_dropna(self):
idx = Index([np.nan, 'a', np.nan, np.nan, 'b', 'c', np.nan],
name='idx')
expected = Index(['a', 'b', 'c'], name='idx')
result = idx.dropna()
tm.assert_index_equal(result, expected)


class TestFloat64Index(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down Expand Up @@ -1049,6 +1056,13 @@ def test_astype_from_object(self):
tm.assert_equal(result.dtype, expected.dtype)
tm.assert_index_equal(result, expected)

def test_dropna(self):
idx = Index([np.nan, 1.0, np.nan, np.nan, 2.0, 3.0, np.nan],
name='idx')
expected = Index([1.0, 2.0, 3.0], name='idx')
result = idx.dropna()
tm.assert_index_equal(result, expected)


class TestInt64Index(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down Expand Up @@ -1474,6 +1488,12 @@ def test_slice_keep_name(self):
idx = Int64Index([1, 2], name='asdf')
self.assertEqual(idx.name, idx[1:].name)

def test_dropna_does_nothing(self):
idx = Index([1, 2, 3], name='idx')
expected = Index([1, 2, 3], name='idx')
result = idx.dropna()
tm.assert_index_equal(result, expected)


class TestMultiIndex(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down Expand Up @@ -2824,6 +2844,12 @@ def test_level_setting_resets_attributes(self):
# if this fails, probably didn't reset the cache correctly.
assert not ind.is_monotonic

def test_dropna_does_nothing(self):
idx = MultiIndex.from_tuples([('bar', 'two')])
expected = idx
result = idx.dropna()
tm.assert_index_equal(result, expected)


def test_get_combined_index():
from pandas.core.index import _get_combined_index
Expand Down