Skip to content

Commit

Permalink
TST: be less lazy about empty MultiIndex set difference, check names
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Oct 21, 2011
1 parent c93ded7 commit 1b30cfc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,12 +1531,16 @@ def diff(self, other):
"""
self._assert_can_do_setop(other)

result_names = self.names if self.names == other.names else None

if self.equals(other):
return self[:0]
return MultiIndex(levels=[[]]*self.nlevels,
labels=[[]]*self.nlevels,
names=result_names)

difference = sorted(set(self.values) - set(other.values))
return MultiIndex.from_tuples(difference, sortorder=0,
names=self.names)
names=result_names)

def _assert_can_do_setop(self, other):
if not isinstance(other, MultiIndex):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,10 +907,17 @@ def test_diff(self):
self.assert_(result.equals(expected))
self.assertEqual(result.names, self.index.names)

# empty difference
result = first - first
expected = first[:0]
self.assert_(result.equals(expected))

# names not the same
chunklet = self.index[-3:]
chunklet.names = ['foo', 'baz']
result = first - chunklet
self.assertEqual(result.names, [None, None])

def test_argsort(self):
result = self.index.argsort()
expected = self.index.get_tuple_index().argsort()
Expand Down

0 comments on commit 1b30cfc

Please sign in to comment.