Repository: pandas-dev/pandas
Pull request: BUG: Fixed __join_multi always returning indexers (#34074)
- [x] closes #34074
- [x] tests added / passed
- [ ] passes `black pandas`
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Problem statement:
BUG: Multijoining indexes always returns indexers, ignores function parameters
- [x] I have checked that this issue has not already been reported.

- [x] I have confirmed this bug exists on the latest version of pandas.

- [x] (optional) I have confirmed this bug exists on the master branch of pandas.

---

#### Code Sample

```python
>>>frame = pd.DataFrame([(1,2,3,4),(4,5,6,7)],columns=["i1", "i2", "i3","val"]).set_index(["i1","i2","i3"])
>>>frame2 = pd.DataFrame([(1,2,3),(4,5,6)],columns=["i1", "i2", "val"]).set_index(["i1","i2"])
>>>frame.index.join(frame2.index,return_indexers=False)
(MultiIndex([(1, 2, 3),
            (4, 5, 6)],
           names=['i1', 'i2', 'i3']), None, None)
```

#### Problem description
Parameter return_indexers is ignored in some cases of __join_multi

#### Expected Output
```python
>>>frame.index.join(frame2.index,return_indexers=False)
MultiIndex([(1, 2, 3),
            (4, 5, 6)],
           names=['i1', 'i2', 'i3'])
```

Hints from issue discussion:

Commit message: BUG: (#34074) Test for return indexers function parameter

Patch to review:
diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst
index 9c424f70b1ee0..66558bcf9b592 100644
--- a/doc/source/whatsnew/v1.1.0.rst
+++ b/doc/source/whatsnew/v1.1.0.rst
@@ -665,7 +665,7 @@ MultiIndex
         # Common elements are now guaranteed to be ordered by the left side
         left.intersection(right, sort=False)
 
--
+- Bug when joining 2 Multi-indexes, without specifying level with different columns. Return-indexers parameter is ignored. (:issue:`34074`)
 
 I/O
 ^^^
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index cf17ce9db6b1a..0f170738f92d9 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -3529,7 +3529,10 @@ def _join_multi(self, other, how, return_indexers=True):
 
             multi_join_idx = multi_join_idx.remove_unused_levels()
 
-            return multi_join_idx, lidx, ridx
+            if return_indexers:
+                return multi_join_idx, lidx, ridx
+            else:
+                return multi_join_idx
 
         jl = list(overlap)[0]
 
diff --git a/pandas/tests/indexes/multi/test_join.py b/pandas/tests/indexes/multi/test_join.py
index 062fb92c44552..3deecabbc0496 100644
--- a/pandas/tests/indexes/multi/test_join.py
+++ b/pandas/tests/indexes/multi/test_join.py
@@ -96,10 +96,19 @@ def test_join_multi_wrong_order():
     midx1 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["a", "b"])
     midx2 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["b", "a"])
 
-    join_idx, lidx, ridx = midx1.join(midx2, return_indexers=False)
+    join_idx, lidx, ridx = midx1.join(midx2, return_indexers=True)
 
     exp_ridx = np.array([-1, -1, -1, -1], dtype=np.intp)
 
     tm.assert_index_equal(midx1, join_idx)
     assert lidx is None
     tm.assert_numpy_array_equal(ridx, exp_ridx)
+
+
+def test_join_multi_return_indexers():
+    # GH 34074
+
+    midx1 = pd.MultiIndex.from_product([[1, 2], [3, 4], [5, 6]], names=["a", "b", "c"])
+    midx2 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["a", "b"])
+
+    assert type(midx1.join(midx2, return_indexers=False)) != tuple


Code review comments:
diff_hunk: @@ -96,10 +96,19 @@ def test_join_multi_wrong_order():
     midx1 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["a", "b"])
     midx2 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["b", "a"])
 
-    join_idx, lidx, ridx = midx1.join(midx2, return_indexers=False)
+    join_idx, lidx, ridx = midx1.join(midx2, return_indexers=True)
 
     exp_ridx = np.array([-1, -1, -1, -1], dtype=np.intp)
 
     tm.assert_index_equal(midx1, join_idx)
     assert lidx is None
     tm.assert_numpy_array_equal(ridx, exp_ridx)
+
+
+def test_join_multi_return_indexers():
+    # GH 34074
+
+    midx1 = pd.MultiIndex.from_product([[1, 2], [3, 4], [5, 6]], names=["a", "b", "c"])
+    midx2 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["a", "b"])
+
+    assert type(midx1.join(midx2, return_indexers=False)) != tuple
path: pandas/tests/indexes/multi/test_join.py
text: @user1:
Could this be made more explicit?

Just a suggestion:
```python
result = midx1.join(midx2, return_indexers=False)
expected = <construct expected result here>
tm.assert_index_equal(result, expected)
```

As an aside, see [PEP8](https://www.python.org/dev/peps/pep-0008/) for how it's advised to write object type comparisons