Skip to content

Symmetric difference on equal MultiIndexes raises TypeError #13490

Closed
@adament

Description

@adament

Calling symmetric_difference on two equal multiindices results in a TypeError rather than an empty MultiIndex. This is surprising since calling difference on the same multiindices results in the expected empty MultiIndex.

Code Sample, a copy-pastable example if possible

a = pandas.MultiIndex.from_product([['a', 'b'], [0, 1]])
b = pandas.MultiIndex.from_product([['a', 'b'], [0, 1]])
print(a.symmetric_difference(b))

Which gives the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Miniconda3\envs\balancedbeta\lib\site-packages\pandas\indexes\base.py", line 1674, in __xor__
    return self.symmetric_difference(other)
  File "C:\Miniconda3\envs\balancedbeta\lib\site-packages\pandas\indexes\base.py", line 1911, in symmetric_difference
    return self._shallow_copy_with_infer(the_diff, **attribs)
  File "C:\Miniconda3\envs\balancedbeta\lib\site-packages\pandas\indexes\multi.py", line 387, in _shallow_copy_with_infer
    return self._shallow_copy(values, **kwargs)
  File "C:\Miniconda3\envs\balancedbeta\lib\site-packages\pandas\indexes\multi.py", line 396, in _shallow_copy
    return MultiIndex.from_tuples(values, **kwargs)
  File "C:\Miniconda3\envs\balancedbeta\lib\site-packages\pandas\indexes\multi.py", line 883, in from_tuples
    raise TypeError('Cannot infer number of levels from empty list')
TypeError: Cannot infer number of levels from empty list

Expected Output

MultiIndex(levels=[[], []], labels = [[], []])

output of pd.show_versions()

INSTALLED VERSIONS                                                          
------------------                                                          
commit: None                                                                
python: 2.7.11.final.0                                                      
python-bits: 64                                                             
OS: Windows                                                                 
OS-release: 7                                                               
machine: AMD64                                                              
processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel               
byteorder: little                                                           
LC_ALL: None                                                                
LANG: DA                                                                    

pandas: 0.18.1                                                              
nose: None                                                                  
pip: 8.1.1                                                                  
setuptools: 20.7.0                                                          
Cython: 0.23                                                                
numpy: 1.10.4                                                               
scipy: 0.17.0                                                               
statsmodels: 0.6.1                                                          
xarray: None                                                                
IPython: 4.2.0                                                              
sphinx: None                                                                
patsy: 0.4.1                                                                
dateutil: 2.5.2                                                             
pytz: 2016.3                                                                
blosc: None                                                                 
bottleneck: None                                                            
tables: 3.2.2                                                               
numexpr: 2.5.2                                                              
matplotlib: 1.5.1                                                           
openpyxl: None                                                              
xlrd: None                                                                  
xlwt: None                                                                  
xlsxwriter: None                                                            
lxml: None                                                                  
bs4: None                                                                   
html5lib: None                                                              
httplib2: None                                                              
apiclient: None                                                             
sqlalchemy: 1.0.12                                                          
pymysql: None                                                               
psycopg2: None                                                              
jinja2: 2.8                                                                 
boto: None                                                                  
pandas_datareader: None                                                     

Metadata

Metadata

Assignees

No one assigned

    Labels

    Error ReportingIncorrect or improved errors from pandasIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions