@@ -4749,6 +4749,33 @@ def test_join_str_datetime(self):
4749
4749
4750
4750
self.assertEqual(len(tst.columns), 3)
4751
4751
4752
+ def test_join_multiindex_leftright(self):
4753
+ # GH 10741
4754
+ df1 = pd.DataFrame([['a', 'x', 0.471780], ['a','y', 0.774908],
4755
+ ['a', 'z', 0.563634], ['b', 'x', -0.353756],
4756
+ ['b', 'y', 0.368062], ['b', 'z', -1.721840],
4757
+ ['c', 'x', 1], ['c', 'y', 2], ['c', 'z', 3]],
4758
+ columns=['first', 'second', 'value1']).set_index(['first', 'second'])
4759
+ df2 = pd.DataFrame([['a', 10], ['b', 20]], columns=['first', 'value2']).set_index(['first'])
4760
+
4761
+ exp = pd.DataFrame([[0.471780, 10], [0.774908, 10], [0.563634, 10],
4762
+ [-0.353756, 20], [0.368062, 20], [-1.721840, 20],
4763
+ [1.000000, np.nan], [2.000000, np.nan], [3.000000, np.nan]],
4764
+ index=df1.index, columns=['value1', 'value2'])
4765
+
4766
+ # these must be the same results (but columns are flipped)
4767
+ tm.assert_frame_equal(df1.join(df2, how='left'), exp)
4768
+ tm.assert_frame_equal(df2.join(df1, how='right'), exp[['value2', 'value1']])
4769
+
4770
+ exp_idx = pd.MultiIndex.from_product([['a', 'b'], ['x', 'y', 'z']],
4771
+ names=['first', 'second'])
4772
+ exp = pd.DataFrame([[0.471780, 10], [0.774908, 10], [0.563634, 10],
4773
+ [-0.353756, 20], [0.368062, 20], [-1.721840, 20]],
4774
+ index=exp_idx, columns=['value1', 'value2'])
4775
+
4776
+ tm.assert_frame_equal(df1.join(df2, how='right'), exp)
4777
+ tm.assert_frame_equal(df2.join(df1, how='left'), exp[['value2', 'value1']])
4778
+
4752
4779
def test_from_records_sequencelike(self):
4753
4780
df = DataFrame({'A' : np.array(np.random.randn(6), dtype = np.float64),
4754
4781
'A1': np.array(np.random.randn(6), dtype = np.float64),
@@ -10100,6 +10127,39 @@ def test_align_int_fill_bug(self):
10100
10127
expected = df2 - df2.mean()
10101
10128
assert_frame_equal(result, expected)
10102
10129
10130
+ def test_align_multiindex(self):
10131
+ # GH 10665
10132
+ # same test cases as test_align_multiindex in test_series.py
10133
+
10134
+ midx = pd.MultiIndex.from_product([range(2), range(3), range(2)],
10135
+ names=('a', 'b', 'c'))
10136
+ idx = pd.Index(range(2), name='b')
10137
+ df1 = pd.DataFrame(np.arange(12), index=midx)
10138
+ df2 = pd.DataFrame(np.arange(2), index=idx)
10139
+
10140
+ # these must be the same results (but flipped)
10141
+ res1l, res1r = df1.align(df2, join='left')
10142
+ res2l, res2r = df2.align(df1, join='right')
10143
+
10144
+ expl = df1
10145
+ tm.assert_frame_equal(expl, res1l)
10146
+ tm.assert_frame_equal(expl, res2r)
10147
+ expr = pd.DataFrame([0, 0, 1, 1, np.nan, np.nan] * 2, index=midx)
10148
+ tm.assert_frame_equal(expr, res1r)
10149
+ tm.assert_frame_equal(expr, res2l)
10150
+
10151
+ res1l, res1r = df1.align(df2, join='right')
10152
+ res2l, res2r = df2.align(df1, join='left')
10153
+
10154
+ exp_idx = pd.MultiIndex.from_product([range(2), range(2), range(2)],
10155
+ names=('a', 'b', 'c'))
10156
+ expl = pd.DataFrame([0, 1, 2, 3, 6, 7, 8, 9], index=exp_idx)
10157
+ tm.assert_frame_equal(expl, res1l)
10158
+ tm.assert_frame_equal(expl, res2r)
10159
+ expr = pd.DataFrame([0, 0, 1, 1] * 2, index=exp_idx)
10160
+ tm.assert_frame_equal(expr, res1r)
10161
+ tm.assert_frame_equal(expr, res2l)
10162
+
10103
10163
def test_where(self):
10104
10164
default_frame = DataFrame(np.random.randn(5, 3),columns=['A','B','C'])
10105
10165
0 commit comments