Skip to content

Commit 96545d0

Browse files
committed
BUG: enable joins to work between multiindex and non-multi. close #2024
1 parent 6c6dae7 commit 96545d0

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

pandas/core/index.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -1762,12 +1762,17 @@ def append(self, other):
17621762
-------
17631763
appended : Index
17641764
"""
1765-
if isinstance(other, (list, tuple)):
1766-
to_concat = (self.values,) + tuple(k.values for k in other)
1767-
else:
1768-
to_concat = self.values, other.values
1765+
if not isinstance(other, (list, tuple)):
1766+
other = [other]
1767+
1768+
to_concat = (self.values,) + tuple(k.values for k in other)
17691769
new_tuples = np.concatenate(to_concat)
1770-
return MultiIndex.from_tuples(new_tuples, names=self.names)
1770+
1771+
# if all(isinstance(x, MultiIndex) for x in other):
1772+
try:
1773+
return MultiIndex.from_tuples(new_tuples, names=self.names)
1774+
except:
1775+
return Index(new_tuples)
17711776

17721777
def argsort(self, *args, **kwargs):
17731778
return self.values.argsort()
@@ -2408,14 +2413,7 @@ def diff(self, other):
24082413
names=result_names)
24092414

24102415
def _assert_can_do_setop(self, other):
2411-
if not isinstance(other, MultiIndex):
2412-
if len(other) == 0:
2413-
return True
2414-
raise TypeError('can only call with other hierarchical '
2415-
'index objects')
2416-
2417-
if self.nlevels != other.nlevels:
2418-
raise AssertionError('Must have same number of levels')
2416+
pass
24192417

24202418
def insert(self, loc, item):
24212419
"""

pandas/tests/test_index.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,8 @@ def test_diff(self):
14711471
self.assert_(len(result) == 0)
14721472

14731473
# raise Exception called with non-MultiIndex
1474-
self.assertRaises(Exception, first.diff, first._tuple_index)
1474+
result = first.diff(first._tuple_index)
1475+
self.assertTrue(result.equals(first[:0]))
14751476

14761477
def test_from_tuples(self):
14771478
self.assertRaises(Exception, MultiIndex.from_tuples, [])

pandas/tools/tests/test_merge.py

+10
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ def test_join_inner_multiindex(self):
407407

408408
# _assert_same_contents(expected, expected2.ix[:, expected.columns])
409409

410+
def test_join_hierarchical_mixed(self):
411+
df = DataFrame([(1,2,3), (4,5,6)], columns = ['a','b','c'])
412+
new_df = df.groupby(['a']).agg({'b': [np.mean, np.sum]})
413+
other_df = DataFrame([(1,2,3), (7,10,6)], columns = ['a','b','d'])
414+
other_df.set_index('a', inplace=True)
415+
416+
result = merge(new_df, other_df, left_index=True, right_index=True)
417+
self.assertTrue(('b', 'mean') in result)
418+
self.assertTrue('b' in result)
419+
410420
def test_join_float64_float32(self):
411421
a = DataFrame(randn(10,2), columns=['a','b'])
412422
b = DataFrame(randn(10,1), columns=['c']).astype(np.float32)

0 commit comments

Comments
 (0)