Inner join with MultiIndex fails if no overlap

pandas, python

Solution

It is easier to ask forgiveness than permission:

import pandas as pd
d1 = pd.DataFrame({'i1': [1, 2, 2], 'i2': [1, 1, 2], 'a': [10, 20, 30]}
                  ).set_index(['i1', 'i2'])
d2 = pd.DataFrame(
    {'i1': [3, 3], 'i2': [1, 2], 'b': [40, 50]}).set_index(['i1', 'i2'])
try:
    d1.join(d2, how='inner')
except Exception as err:
    # Change this to however you wish to handle this case.
    print(err)

Problem

I can't get "empty" inner joins to work with a MultiIndex. Under 0.10.1, I have: ``` d1 = pd.DataFrame({ 'i1': [1, 2, 2], 'i2': [1, 1, 2], 'a': [10,20,30]}).set_index(['i1', 'i2']) d2 = pd.DataFrame({ 'i1': [3, 3], 'i2': [1, 2], 'b': [40, 50]}).set_index(['i1', 'i2']) d1.join(d2, how='inner') ``` which gives me ``` Exception: Cannot infer number of levels from empty list ``` Is there any good way around this? I'd like to be able to tell in advance if the intersection is empty, so I can avoid the exception.

Original source