Skip to content
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

Slicing multiple DataFrame columns doesn't work with boolean column names #11119

Closed
louispotok opened this issue Sep 15, 2015 · 4 comments
Closed
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@louispotok
Copy link
Contributor

Setup:

import pandas as pd
df = pd.DataFrame({True: [1,2,5,9],
                   False: [6,1,13,8]})

df[True] and df[False] work fine; but df[[True,False]] throws an exception:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-a28f55cd0379> in <module>()
----> 1 df[[True,False]]

/Users/username/pandasenv/lib/python2.7/site-packages/pandas/core/frame.pyc in __getitem__(self, key)
   1772         if isinstance(key, (Series, np.ndarray, Index, list)):
   1773             # either boolean or fancy integer index
-> 1774             return self._getitem_array(key)
   1775         elif isinstance(key, DataFrame):
   1776             return self._getitem_frame(key)

/Users/username/pandasenv/lib/python2.7/site-packages/pandas/core/frame.pyc in _getitem_array(self, key)
   1809             elif len(key) != len(self.index):
   1810                 raise ValueError('Item wrong length %d instead of %d.' %
-> 1811                                  (len(key), len(self.index)))
   1812             # _check_bool_indexer will throw exception if Series key cannot
   1813             # be reindexed to match DataFrame rows

ValueError: Item wrong length 2 instead of 4.

On the other hand, this works fine:

df = pd.DataFrame({'True': [1,2,5,9],
                   'False': [6,1,13,8]})
df[['True','False']]

Probably relevant to: #9595

@jreback
Copy link
Contributor

jreback commented Sep 16, 2015

I'll mark this as a bug, though using a boolean item as an indexer is pretty odd.

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves Dtype Conversions Unexpected or buggy dtype conversions labels Sep 16, 2015
@jreback jreback added this to the Next Major Release milestone Sep 16, 2015
@louispotok
Copy link
Contributor Author

I don't know if it's odd. I often end up with boolean column names after groupbys and unstacking -- pandas allows values to move very fluidly from dataframe values to index and column names. Thanks for the labels.

@mroeschke
Copy link
Member

xref: #10607

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@mroeschke
Copy link
Member

The linked issue has been closes as this case intentionally raises now so closing

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

Successfully merging a pull request may close this issue.

3 participants