Skip to content

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

Closed
@louispotok

Description

@louispotok

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions