Skip to content

BUG: TypeError when calling unique on empty MultiIndex #20308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Liam3851 opened this issue Mar 12, 2018 · 3 comments · Fixed by #20571
Closed

BUG: TypeError when calling unique on empty MultiIndex #20308

Liam3851 opened this issue Mar 12, 2018 · 3 comments · Fixed by #20571

Comments

@Liam3851
Copy link
Contributor

Code Sample, a copy-pastable example if possible

import pandas as pd                                   
import numpy as np                                    
x = pd.MultiIndex.from_tuples([(1, 2), (3, 4)], names=['a', 'b'])
y = x[np.array([False, False])] # multiindex y is now empty
y.unique()

-->
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-290d71fd5bc3> in <module>()
      3 x = pd.MultiIndex.from_tuples([(1, 2), (3, 4)]. names=['a', 'b'])
      4 y = x[np.array([False, False])]
----> 5 y.unique()

C:\projects\pandas-dk\pandas\core\indexes\multi.py in unique(self, level)
   1072
   1073         if level is None:
-> 1074             return super(MultiIndex, self).unique()
   1075         else:
   1076             level = self._get_level_number(level)

C:\projects\pandas-dk\pandas\core\indexes\base.py in unique(self, level)
   3971             self._validate_index_level(level)
   3972         result = super(Index, self).unique()
-> 3973         return self._shallow_copy(result)
   3974
   3975     @Appender(base._shared_docs['drop_duplicates'] % _index_doc_kwargs)

C:\projects\pandas-dk\pandas\core\indexes\multi.py in _shallow_copy(self, values, **kwargs)
    559             # discards freq
    560             kwargs.pop('freq', None)
--> 561             return MultiIndex.from_tuples(values, **kwargs)
    562         return self.view()
    563

C:\projects\pandas-dk\pandas\core\indexes\multi.py in from_tuples(cls, tuples, sortorder, names)
   1316             if names is None:
   1317                 msg = 'Cannot infer number of levels from empty list'
-> 1318                 raise TypeError(msg)
   1319             arrays = [[]] * len(names)
   1320         elif isinstance(tuples, (np.ndarray, Index)):

TypeError: Cannot infer number of levels from empty list

Problem description

Looks similar to #16777. Calling unique on an empty single-level index works fine, but doing so on an empty MultiIndex fails. In versions prior to 0.19 MultiIndex.unique on an empty MultiIndex worked (returning an empty array), but now that the array is being wrapped in an index this is causing a failure.

Expected Output

MultiIndex(levels=[[1, 3], [2, 4]],
           labels=[[], []],
           names=['a', 'b'])

Output of pd.show_versions()

INSTALLED VERSIONS

commit: b669112
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 62 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0.dev0+457.gb669112
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.0
scipy: 1.0.0
pyarrow: 0.8.0
xarray: 0.10.0
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.10
s3fs: 0.1.2
fastparquet: 0.1.4
pandas_gbq: None
pandas_datareader: None

@Liam3851
Copy link
Contributor Author

After looking at this a bit more it looks like the problem is with MultiIndex._shallow_copy with values specified. If values is an empty list, MultiIndex.from_tuples loses track of what the levels are and you end up with this error.

@jreback
Copy link
Contributor

jreback commented Mar 14, 2018

hmm this seems broken, #20062 would impact this (slightly)

@jreback jreback modified the milestones: No action, Next Major Release Mar 14, 2018
@toobaz
Copy link
Member

toobaz commented Mar 31, 2018

Consequence of #20570

toobaz added a commit to toobaz/pandas that referenced this issue Mar 31, 2018
@jreback jreback modified the milestones: Next Major Release, 0.23.0 Apr 1, 2018
toobaz added a commit that referenced this issue Apr 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants