Skip to content

Commit

Permalink
dropna method added to Index.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexual authored and qwhelan committed Mar 19, 2015
1 parent 35d0893 commit 7ea39cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,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
30 changes: 30 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,14 @@ def get_reindex_type(target):
self.assertEqual(reindexed.levels[0].dtype.type, np.int64)
self.assertEqual(reindexed.levels[1].dtype.type, np.float64)

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)


def test_groupby(self):
idx = Index(range(5))
groups = idx.groupby(np.array([1,1,2,2,2]))
Expand Down Expand Up @@ -1513,6 +1521,12 @@ def test_astype_from_object(self):
tm.assert_equal(result.dtype, expected.dtype)
tm.assert_index_equal(result, expected)

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


class TestInt64Index(Numeric, tm.TestCase):
_holder = Int64Index
Expand Down Expand Up @@ -2085,6 +2099,14 @@ def test_time_loc(self): # GH8667

tm.assert_array_equal(ts.index.get_loc(key), i)
tm.assert_series_equal(ts[key], ts.iloc[i])
=======
def test_dropna_does_nothing(self):
idx = Int64Index([1, 2, 3], name='idx')
expected = Int64Index([1, 2, 3], name='idx')
result = idx.dropna()
tm.assert_index_equal(result, expected)

>>>>>>> dropna method added to Index.

left, right = ts.copy(), ts.copy()
left[key] *= -10
Expand Down Expand Up @@ -3890,6 +3912,7 @@ def test_level_setting_resets_attributes(self):
# if this fails, probably didn't reset the cache correctly.
assert not ind.is_monotonic

<<<<<<< HEAD
def test_isin(self):
values = [('foo', 2), ('bar', 3), ('quux', 4)]

Expand Down Expand Up @@ -3988,6 +4011,13 @@ def test_groupby(self):
groups = self.index.groupby(self.index)
exp = dict((key, [key]) for key in self.index)
tm.assert_dict_equal(groups, exp)
=======
def test_dropna_does_nothing(self):
idx = MultiIndex.from_tuples([('bar', 'two')])
expected = idx
result = idx.dropna()
tm.assert_index_equal(result, expected)
>>>>>>> dropna method added to Index.


def test_get_combined_index():
Expand Down

0 comments on commit 7ea39cc

Please sign in to comment.