Skip to content

Commit e31a057

Browse files
committed
dropna method added to Index.
ref pandas-dev#6194 ref pandas-dev#7784
1 parent 6e67f0a commit e31a057

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

pandas/core/index.py

+10
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ def is_(self, other):
215215
# use something other than None to be clearer
216216
return self._id is getattr(other, '_id', Ellipsis)
217217

218+
def dropna(self):
219+
"""
220+
Return Index without null values
221+
222+
Returns
223+
-------
224+
dropped : Index
225+
"""
226+
return self[~isnull(self.values)]
227+
218228
def _reset_identity(self):
219229
"""Initializes or resets ``_id`` attribute with new object"""
220230
self._id = _Identity()

pandas/tests/test_index.py

+25
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,13 @@ def test_nan_first_take_datetime(self):
905905
exp = Index([idx[-1], idx[0], idx[1]])
906906
tm.assert_index_equal(res, exp)
907907

908+
def test_dropna(self):
909+
idx = Index([np.nan, 'a', np.nan, np.nan, 'b', 'c', np.nan],
910+
name='idx')
911+
expected = Index(['a', 'b', 'c'], name='idx')
912+
result = idx.dropna()
913+
tm.assert_index_equal(result, expected)
914+
908915

909916
class TestFloat64Index(tm.TestCase):
910917
_multiprocess_can_split_ = True
@@ -1049,6 +1056,12 @@ def test_astype_from_object(self):
10491056
tm.assert_equal(result.dtype, expected.dtype)
10501057
tm.assert_index_equal(result, expected)
10511058

1059+
def test_dropna(self):
1060+
idx = Float64Index([np.nan, 1.0, np.nan, np.nan, 2.0, 3.0, np.nan])
1061+
expected = Float64Index([1.0, 2.0, 3.0])
1062+
result = idx.dropna()
1063+
tm.assert_index_equal(result, expected)
1064+
10521065

10531066
class TestInt64Index(tm.TestCase):
10541067
_multiprocess_can_split_ = True
@@ -1474,6 +1487,12 @@ def test_slice_keep_name(self):
14741487
idx = Int64Index([1, 2], name='asdf')
14751488
self.assertEqual(idx.name, idx[1:].name)
14761489

1490+
def test_dropna_does_nothing(self):
1491+
idx = Int64Index([1, 2, 3], name='idx')
1492+
expected = Int64Index([1, 2, 3], name='idx')
1493+
result = idx.dropna()
1494+
tm.assert_index_equal(result, expected)
1495+
14771496

14781497
class TestMultiIndex(tm.TestCase):
14791498
_multiprocess_can_split_ = True
@@ -2824,6 +2843,12 @@ def test_level_setting_resets_attributes(self):
28242843
# if this fails, probably didn't reset the cache correctly.
28252844
assert not ind.is_monotonic
28262845

2846+
def test_dropna_does_nothing(self):
2847+
idx = MultiIndex.from_tuples([('bar', 'two')])
2848+
expected = idx
2849+
result = idx.dropna()
2850+
tm.assert_index_equal(result, expected)
2851+
28272852

28282853
def test_get_combined_index():
28292854
from pandas.core.index import _get_combined_index

0 commit comments

Comments
 (0)