{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/pandas/issues/12369","repository_url":"https://api.github.com/repos/pydata/pandas","labels_url":"https://api.github.com/repos/pydata/pandas/issues/12369/labels{/name}","comments_url":"https://api.github.com/repos/pydata/pandas/issues/12369/comments","events_url":"https://api.github.com/repos/pydata/pandas/issues/12369/events","html_url":"https://github.com/pydata/pandas/issues/12369","id":134337798,"number":12369,"title":"KeyError when retrieving columns with lists of incomplete tuples from MultiIndex","user":{"login":"toobaz","id":1224492,"avatar_url":"https://avatars.githubusercontent.com/u/1224492?v=3","gravatar_id":"","url":"https://api.github.com/users/toobaz","html_url":"https://github.com/toobaz","followers_url":"https://api.github.com/users/toobaz/followers","following_url":"https://api.github.com/users/toobaz/following{/other_user}","gists_url":"https://api.github.com/users/toobaz/gists{/gist_id}","starred_url":"https://api.github.com/users/toobaz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/toobaz/subscriptions","organizations_url":"https://api.github.com/users/toobaz/orgs","repos_url":"https://api.github.com/users/toobaz/repos","events_url":"https://api.github.com/users/toobaz/events{/privacy}","received_events_url":"https://api.github.com/users/toobaz/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/pydata/pandas/labels/MultiIndex","name":"MultiIndex","color":"207de5"},{"url":"https://api.github.com/repos/pydata/pandas/labels/Usage%20Question","name":"Usage Question","color":"0052cc"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-02-17T16:58:21Z","updated_at":"2016-02-22T08:14:03Z","closed_at":"2016-02-17T17:03:01Z","body":"Sorry in advance if this was already reported - I searched a bit but didn't know exactly what to search.\r\n\r\n    In [2]: df = pd.DataFrame(0, index=range(1), columns=pd.MultiIndex.from_product([['a', 'b'], ['c']]))\r\n\r\n    In [3]: cols = [('a',), ('b',)]\r\n\r\n    In [4]: df[cols[0]]\r\n    Out[4]: \r\n       c\r\n    0  0\r\n\r\n    In [5]: df[cols[1]]\r\n    Out[5]: \r\n       c\r\n    0  0\r\n\r\n    In [6]: df[cols]\r\n    ---------------------------------------------------------------------------\r\n    KeyError                                  Traceback (most recent call last)\r\n    <ipython-input-6-957cf8a677a9> in <module>()\r\n    ----> 1 df[cols]\r\n\r\n    /home/pietro/nobackup/repo/pandas/pandas/core/frame.pyc in __getitem__(self, key)\r\n       1975         if isinstance(key, (Series, np.ndarray, Index, list)):\r\n       1976             # either boolean or fancy integer index\r\n    -> 1977             return self._getitem_array(key)\r\n       1978         elif isinstance(key, DataFrame):\r\n       1979             return self._getitem_frame(key)\r\n\r\n    /home/pietro/nobackup/repo/pandas/pandas/core/frame.pyc in _getitem_array(self, key)\r\n       2019             return self.take(indexer, axis=0, convert=False)\r\n       2020         else:\r\n    -> 2021             indexer = self.ix._convert_to_indexer(key, axis=1)\r\n       2022             return self.take(indexer, axis=1, convert=True)\r\n       2023 \r\n\r\n    /home/pietro/nobackup/repo/pandas/pandas/core/indexing.pyc in _convert_to_indexer(self, obj, axis, is_setter)\r\n       1203                 mask = check == -1\r\n       1204                 if mask.any():\r\n    -> 1205                     raise KeyError('%s not in index' % objarr[mask])\r\n       1206 \r\n       1207                 return _values_from_object(indexer)\r\n\r\n    KeyError: \"[('a',) ('b',)] not in index\"\r\n\r\nNotice that ``df[[c[0] for c in cols]]`` works just fine... but still we probably want to consider as valid a list of valid keys."},"comment":{"url":"https://api.github.com/repos/pydata/pandas/issues/comments/187063995","html_url":"https://github.com/pydata/pandas/issues/12369#issuecomment-187063995","issue_url":"https://api.github.com/repos/pydata/pandas/issues/12369","id":187063995,"user":{"login":"toobaz","id":1224492,"avatar_url":"https://avatars.githubusercontent.com/u/1224492?v=3","gravatar_id":"","url":"https://api.github.com/users/toobaz","html_url":"https://github.com/toobaz","followers_url":"https://api.github.com/users/toobaz/followers","following_url":"https://api.github.com/users/toobaz/following{/other_user}","gists_url":"https://api.github.com/users/toobaz/gists{/gist_id}","starred_url":"https://api.github.com/users/toobaz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/toobaz/subscriptions","organizations_url":"https://api.github.com/users/toobaz/orgs","repos_url":"https://api.github.com/users/toobaz/repos","events_url":"https://api.github.com/users/toobaz/events{/privacy}","received_events_url":"https://api.github.com/users/toobaz/received_events","type":"User","site_admin":false},"created_at":"2016-02-22T08:14:03Z","updated_at":"2016-02-22T08:14:03Z","body":"So this neither is a bug, right?\r\n\r\n    In [2]: df = pd.DataFrame(index=range(2), columns=pd.MultiIndex.from_product([[10,20], ['a', 'b']]))\r\n\r\n    In [3]: df[[20]]\r\n    ---------------------------------------------------------------------------\r\n    IndexError                                Traceback (most recent call last)\r\n    <ipython-input-3-8422ebb3f356> in <module>()\r\n    ----> 1 df[[20]]\r\n\r\n    /home/pietro/nobackup/repo/pandas/pandas/core/frame.pyc in __getitem__(self, key)\r\n       1975         if isinstance(key, (Series, np.ndarray, Index, list)):\r\n       1976             # either boolean or fancy integer index\r\n    -> 1977             return self._getitem_array(key)\r\n       1978         elif isinstance(key, DataFrame):\r\n       1979             return self._getitem_frame(key)\r\n\r\n    /home/pietro/nobackup/repo/pandas/pandas/core/frame.pyc in _getitem_array(self, key)\r\n       2020         else:\r\n       2021             indexer = self.ix._convert_to_indexer(key, axis=1)\r\n    -> 2022             return self.take(indexer, axis=1, convert=True)\r\n       2023 \r\n       2024     def _getitem_multilevel(self, key):\r\n\r\n    /home/pietro/nobackup/repo/pandas/pandas/core/generic.pyc in take(self, indices, axis, convert, is_copy)\r\n       1590         new_data = self._data.take(indices,\r\n       1591                                    axis=self._get_block_manager_axis(axis),\r\n    -> 1592                                    convert=True, verify=True)\r\n       1593         result = self._constructor(new_data).__finalize__(self)\r\n       1594 \r\n\r\n    /home/pietro/nobackup/repo/pandas/pandas/core/internals.pyc in take(self, indexer, axis, verify, convert)\r\n       3617         n = self.shape[axis]\r\n       3618         if convert:\r\n    -> 3619             indexer = maybe_convert_indices(indexer, n)\r\n       3620 \r\n       3621         if verify:\r\n\r\n    /home/pietro/nobackup/repo/pandas/pandas/core/indexing.pyc in maybe_convert_indices(indices, n)\r\n       1803     mask = (indices >= n) | (indices < 0)\r\n       1804     if mask.any():\r\n    -> 1805         raise IndexError(\"indices are out-of-bounds\")\r\n       1806     return indices\r\n       1807 \r\n\r\n    IndexError: indices are out-of-bounds\r\n\r\n(it is interpreting the \"20\" as index rather than label)\r\nFor instance the following \"works\" instead:\r\n\r\n    In [4]: df[[1]]\r\n    Out[4]: \r\n        10\r\n         b\r\n    0  NaN\r\n    1  NaN\r\n"}}