Skip to content

Commit 8856b24

Browse files
committed
dropna method added to Index.
ref pandas-dev#6194 ref pandas-dev#7784
1 parent a59a7ea commit 8856b24

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
@@ -907,6 +907,13 @@ def test_nan_first_take_datetime(self):
907907
exp = Index([idx[-1], idx[0], idx[1]])
908908
tm.assert_index_equal(res, exp)
909909

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

911918
class TestFloat64Index(tm.TestCase):
912919
_multiprocess_can_split_ = True
@@ -1051,6 +1058,12 @@ def test_astype_from_object(self):
10511058
tm.assert_equal(result.dtype, expected.dtype)
10521059
tm.assert_index_equal(result, expected)
10531060

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

10551068
class TestInt64Index(tm.TestCase):
10561069
_multiprocess_can_split_ = True
@@ -1476,6 +1489,12 @@ def test_slice_keep_name(self):
14761489
idx = Int64Index([1, 2], name='asdf')
14771490
self.assertEqual(idx.name, idx[1:].name)
14781491

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

14801499
class TestMultiIndex(tm.TestCase):
14811500
_multiprocess_can_split_ = True
@@ -2948,6 +2967,12 @@ def test_level_setting_resets_attributes(self):
29482967
# if this fails, probably didn't reset the cache correctly.
29492968
assert not ind.is_monotonic
29502969

2970+
def test_dropna_does_nothing(self):
2971+
idx = MultiIndex.from_tuples([('bar', 'two')])
2972+
expected = idx
2973+
result = idx.dropna()
2974+
tm.assert_index_equal(result, expected)
2975+
29512976

29522977
def test_get_combined_index():
29532978
from pandas.core.index import _get_combined_index

0 commit comments

Comments
 (0)