{"action":"opened","issue":{"url":"https://api.github.com/repos/pandas-dev/pandas/issues/14750","repository_url":"https://api.github.com/repos/pandas-dev/pandas","labels_url":"https://api.github.com/repos/pandas-dev/pandas/issues/14750/labels{/name}","comments_url":"https://api.github.com/repos/pandas-dev/pandas/issues/14750/comments","events_url":"https://api.github.com/repos/pandas-dev/pandas/issues/14750/events","html_url":"https://github.com/pandas-dev/pandas/issues/14750","id":191788979,"number":14750,"title":"Boolean slicing on MultiIndex only works if bool index is same length as MultiIndex","user":{"login":"lightcatcher","id":287200,"avatar_url":"https://avatars.githubusercontent.com/u/287200?v=3","gravatar_id":"","url":"https://api.github.com/users/lightcatcher","html_url":"https://github.com/lightcatcher","followers_url":"https://api.github.com/users/lightcatcher/followers","following_url":"https://api.github.com/users/lightcatcher/following{/other_user}","gists_url":"https://api.github.com/users/lightcatcher/gists{/gist_id}","starred_url":"https://api.github.com/users/lightcatcher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lightcatcher/subscriptions","organizations_url":"https://api.github.com/users/lightcatcher/orgs","repos_url":"https://api.github.com/users/lightcatcher/repos","events_url":"https://api.github.com/users/lightcatcher/events{/privacy}","received_events_url":"https://api.github.com/users/lightcatcher/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-11-26T05:56:53Z","updated_at":"2016-11-26T05:56:53Z","closed_at":null,"body":"#### Code Sample, a copy-pastable example if possible\r\n\r\n```python\r\nfrom __future__ import print_function\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\nidx = pd.MultiIndex.from_product([['foo', 'bar'], ['a', 'b']],\r\n                                 names=['first', 'second'])\r\ndf = pd.DataFrame(np.random.randn(4, 2), index=idx).sort_index()\r\n\r\nbool_idx = pd.Series([True, False], index=['foo', 'bar'])\r\n\r\nprint ('dataframe')\r\nprint(df)\r\n\r\nprint('\\nbool idx')\r\nprint(bool_idx)\r\n\r\ntry:\r\n    df.loc[(bool_idx, slice(None)), :]\r\nexcept ValueError as e:\r\n    import traceback\r\n    print('\\nbool index in MultiIndex failed')\r\n    traceback.print_exc()\r\n\r\nno_multi_idx = df.reset_index(level=1)\r\nprint('\\nno multi index')\r\nprint(no_multi_idx)\r\n\r\nprint('successful indexing')\r\nprint(no_multi_idx.loc[bool_idx])\r\n```\r\n\r\n#### Problem description\r\n\r\nA level of a MultiIndex cannot be sliced by a boolean array shorter than the level. A single level of a MultiIndex can contain repeated values (as one level isn't the entire index), so slicing by a smaller array can be necessary.\r\nA single Index with repeated values can currently be sliced by a boolean array without repeated Index values. It would be a smoother user experience if the same feature existed for MultiIndex, especially since this is less of a corner case in with MultiIndex (because it doesn't require repeating index values). I would guess that the same or very similar code to the single Index case could be used to handle the MultiIndex case.\r\n\r\n#### Expected Output\r\n```\r\n                     0         1\r\nfirst second                    \r\nfoo   a      -0.569935 -1.146863\r\n      b      -2.087799 -0.506962\r\n```\r\n\r\nThis can currently be computed by calling ``reset_index``, slicing, and then calling ``set_index`` with append=True.\r\n\r\n#### Output of ``pd.show_versions()``\r\n\r\n<details>\r\n\r\nINSTALLED VERSIONS\r\n------------------\r\ncommit: None\r\npython: 2.7.9.final.0\r\npython-bits: 64\r\nOS: Linux\r\nOS-release: 3.19.0-15-generic\r\nmachine: x86_64\r\nprocessor: x86_64\r\nbyteorder: little\r\nLC_ALL: None\r\nLANG: en_US.UTF-8\r\nLOCALE: None.None\r\n\r\npandas: 0.19.1\r\nnose: 1.3.4\r\npip: 9.0.1\r\nsetuptools: 18.0.1\r\nCython: 0.21.1\r\nnumpy: 1.11.2\r\nscipy: 0.14.1\r\nstatsmodels: None\r\nxarray: None\r\nIPython: 3.2.0\r\nsphinx: None\r\npatsy: None\r\ndateutil: 2.6.0\r\npytz: 2016.7\r\nblosc: None\r\nbottleneck: None\r\ntables: None\r\nnumexpr: None\r\nmatplotlib: 1.4.3\r\nopenpyxl: None\r\nxlrd: None\r\nxlwt: None\r\nxlsxwriter: None\r\nlxml: None\r\nbs4: 4.3.2\r\nhtml5lib: 0.999\r\nhttplib2: 0.9.2\r\napiclient: None\r\nsqlalchemy: 1.0.8\r\npymysql: None\r\npsycopg2: 2.6.2 (dt dec pq3 ext lo64)\r\njinja2: 2.8\r\nboto: None\r\npandas_datareader: None\r\n</details>\r\n"}}
{"actor":{"display_login":"lightcatcher"}}