Skip to content

Support for empty slice in multi indices #8737

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
SylvainCorlay opened this issue Nov 5, 2014 · 8 comments
Closed

Support for empty slice in multi indices #8737

SylvainCorlay opened this issue Nov 5, 2014 · 8 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Milestone

Comments

@SylvainCorlay
Copy link
Contributor

It is currently possible to pass a list of labels for a given dataframe to DataFrame.loc in the case of a multi index, but the list cannot be empty unlike in the case of a non-hierarchical index.

@SylvainCorlay
Copy link
Contributor Author

Ping @SSunkara .

@SylvainCorlay SylvainCorlay changed the title Support for empty slice Support for empty slice in multi indices Nov 5, 2014
@jreback
Copy link
Contributor

jreback commented Nov 5, 2014

hmm this should work
can you give a small example (copy/pastable) of what you are doing and print of.show_versions()

@SylvainCorlay
Copy link
Contributor Author

Here it is:

import pandas as pd
import numpy as np
multi_index = pd.MultiIndex.from_product((['foo', 'bar', 'baz'], ['alpha', 'beta']))
df = pd.DataFrame(np.random.randn(5, 6), index=range(5), columns=multi_index)
df = df.sortlevel(0, axis=1)
df.loc[:, ([], slice(None))]

The version is 0.14.0.

Here is the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-15460543620a> in <module>()
      5 df = pd.DataFrame(np.random.randn(5, 6), index=range(5), columns=multi_index)
      6 df = df.sortlevel(0, axis=1)
----> 7 df.loc[:, ([], slice(None))]

/opt/anaconda20/lib/python2.7/site-packages/pandas/core/indexing.pyc in __getitem__(self, key)
   1125     def __getitem__(self, key):
   1126         if type(key) is tuple:
-> 1127             return self._getitem_tuple(key)
   1128         else:
   1129             return self._getitem_axis(key, axis=0)

/opt/anaconda20/lib/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_tuple(self, tup)
    643     def _getitem_tuple(self, tup):
    644         try:
--> 645             return self._getitem_lowerdim(tup)
    646         except IndexingError:
    647             pass

/opt/anaconda20/lib/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_lowerdim(self, tup)
    751         # we may have a nested tuples indexer here
    752         if self._is_nested_tuple_indexer(tup):
--> 753             return self._getitem_nested_tuple(tup)
    754 
    755         # we maybe be using a tuple to represent multiple dimensions here

/opt/anaconda20/lib/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_nested_tuple(self, tup)
    823 
    824             current_ndim = obj.ndim
--> 825             obj = getattr(obj, self.name)._getitem_axis(key, axis=axis, validate_iterable=True)
    826             axis += 1
    827 

/opt/anaconda20/lib/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_axis(self, key, axis, validate_iterable)
   1254             return self._getitem_iterable(key, axis=axis)
   1255         elif _is_nested_tuple(key, labels):
-> 1256             locs = labels.get_locs(key)
   1257             indexer = [ slice(None) ] * self.ndim
   1258             indexer[axis] = locs

/opt/anaconda20/lib/python2.7/site-packages/pandas/core/index.pyc in get_locs(self, tup)
   3579                 ranges.append(reduce(
   3580                     np.logical_or,[ _convert_indexer(self._get_level_indexer(x, level=i)
-> 3581                                                      ) for x in k ]))
   3582             elif k == slice(None):
   3583                 # include all from this level

TypeError: reduce() of empty sequence with no initial value

Printout of show_versions:

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.8.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-24-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.14.0
nose: 1.3.3
Cython: 0.20.1
numpy: 1.8.1
scipy: 0.14.0
statsmodels: 0.5.0
IPython: 3.0.0-dev
sphinx: 1.2.2
patsy: 0.2.1
scikits.timeseries: None
dateutil: 1.5
pytz: 2014.3
bottleneck: None
tables: 3.1.1
numexpr: 2.3.1
matplotlib: 1.3.1
openpyxl: 1.8.5
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: 0.5.5
lxml: 3.3.5
bs4: 4.3.1
html5lib: None
bq: None
apiclient: None
rpy2: 2.4.3
sqlalchemy: 0.9.4
pymysql: None
psycopg2: None

@jreback
Copy link
Contributor

jreback commented Nov 5, 2014

I see it..ok will see if can get in for 0.15.1 (releasing this weekend). thanks for the report.

@jreback jreback added Bug MultiIndex Indexing Related to indexing on series/frames, not to indexes themselves labels Nov 5, 2014
@jreback jreback added this to the 0.15.1 milestone Nov 5, 2014
@jreback
Copy link
Contributor

jreback commented Nov 5, 2014

so this should return an empty frame (with the same index, and column index metadata preserved)

e.g.

DataFrame(index=df.index,columns=multi_index.reindex([])[0])

yes?

@SylvainCorlay
Copy link
Contributor Author

This is what I was expecting from the documentation, but wasn't sure if I was doing something wrong. Hopefully next time, we will open a PR with a correction!

@jreback
Copy link
Contributor

jreback commented Nov 5, 2014

done (was pretty trivial), sadly don't always catch all of the cases. Note that you should for sure upgrade to 0.14.1 as lots of changes in mi slicing. (of course 0.15.1 even better) :)

@jreback
Copy link
Contributor

jreback commented Nov 5, 2014

closed by #8739

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants