This is an explanation for MaxNoe's answer, as it was lengthy to include in the comments.
As he pointed out, df[0] is True evaluates to False , which is then forced to 0 , which matches the column name. What is interesting about this? what if you run
>>>df = pd.DataFrame([True, False, True]) >>>df[False] KeyError Traceback (most recent call last) <ipython-input-21-62b48754461f> in <module>() ----> 1 df[False] >>>df[0] 0 True 1 False 2 True Name: 0, dtype: bool >>>df[False] 0 True 1 False 2 True Name: 0, dtype: bool
This seems a little perplexing at first (at least for me), but has to do with how pandas uses caching. If you look at how df[False] resolved, it looks like
/home/matthew/anaconda/lib/python2.7/site-packages/pandas/core/frame.py(1975)__getitem__() -> return self._getitem_column(key) /home/matthew/anaconda/lib/python2.7/site-packages/pandas/core/frame.py(1999)_getitem_column() -> return self._get_item_cache(key) > /home/matthew/anaconda/lib/python2.7/site-packages/pandas/core/generic.py(1343)_get_item_cache() -> res = cache.get(item)
Since cache is just a regular dict python, after running df[0] cache looks like
>>>cache {0: 0 True 1 False 2 True Name: 0, dtype: bool}
so when searching for False , python forces this to 0 . If we don’t have already loaded the cache using df[0] , then res is None , which KeyError on line 1345 generic.py
def _get_item_cache(self, item): 1341 """Return the cached item, item represents a label indexer.""" 1342 cache = self._item_cache 1343 -> res = cache.get(item) 1344 if res is None: 1345 values = self._data.get(item)