{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/pandas/issues/13431","repository_url":"https://api.github.com/repos/pydata/pandas","labels_url":"https://api.github.com/repos/pydata/pandas/issues/13431/labels{/name}","comments_url":"https://api.github.com/repos/pydata/pandas/issues/13431/comments","events_url":"https://api.github.com/repos/pydata/pandas/issues/13431/events","html_url":"https://github.com/pydata/pandas/issues/13431","id":159829532,"number":13431,"title":"different behaviors of `sort_index()` and `sort_index(level=0)`","user":{"login":"zym1010","id":4273204,"avatar_url":"https://avatars.githubusercontent.com/u/4273204?v=3","gravatar_id":"","url":"https://api.github.com/users/zym1010","html_url":"https://github.com/zym1010","followers_url":"https://api.github.com/users/zym1010/followers","following_url":"https://api.github.com/users/zym1010/following{/other_user}","gists_url":"https://api.github.com/users/zym1010/gists{/gist_id}","starred_url":"https://api.github.com/users/zym1010/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zym1010/subscriptions","organizations_url":"https://api.github.com/users/zym1010/orgs","repos_url":"https://api.github.com/users/zym1010/repos","events_url":"https://api.github.com/users/zym1010/events{/privacy}","received_events_url":"https://api.github.com/users/zym1010/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/pydata/pandas/labels/MultiIndex","name":"MultiIndex","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-06-12T14:13:48Z","updated_at":"2016-06-12T16:49:38Z","closed_at":null,"body":"Inspired by some bug reports around multiindex sortedness (<http://stackoverflow.com/questions/31427466/ensuring-lexicographical-sort-in-pandas-multiindex>, #10651, #9212), I found that `sort_index()` sometimes can't make a multiindex ready for slicing, but `sort_index(level=0)` (so does `sortlevel()`) can.\r\n\r\n~~~python\r\nIn [119]: pd.__version__\r\nOut[119]: u'0.18.1'\r\n\r\nIn [120]: df = pd.DataFrame({'col1': ['b','d','b','a'], 'col2': [3,1,1,2], 'data':['one','two','three','four']})\r\n\r\nIn [121]: df2 = df.set_index(['col1','col2'])\r\n\r\nIn [122]: df2.index.set_levels(['b','d','a'], level='col1', inplace=True)\r\n\r\nIn [123]: df2.index.set_labels([0,1,0,2], level='col1', inplace=True)\r\n\r\nIn [124]: df2.sortlevel()\r\nOut[124]:\r\n            data\r\ncol1 col2\r\nb    1     three\r\n     3       one\r\nd    1       two\r\na    2      four\r\n\r\nIn [125]: df2.sort_index()\r\nOut[125]:\r\n            data\r\ncol1 col2\r\na    2      four\r\nb    1     three\r\n     3       one\r\nd    1       two\r\n\r\nIn [126]: df2.sort_index(level=0)\r\nOut[126]:\r\n            data\r\ncol1 col2\r\nb    1     three\r\n     3       one\r\nd    1       two\r\na    2      four\r\n~~~\r\n\r\nWhile `df2.sort_index()` does give a visually lexicographically sorted output, it DOES NOT support slicing.\r\n\r\n~~~python\r\ndf2.sort_index().loc['b':'d']\r\n---------------------------------------------------------------------------\r\nKeyError                                  Traceback (most recent call last)\r\n\r\n# a lot of lines omitted here.\r\n\r\n/Users/yimengzh/miniconda/envs/cafferc3/lib/python2.7/site-packages/pandas/indexes/multi.py in _partial_tup_index(self, tup, side)\r\n   1488             raise KeyError('Key length (%d) was greater than MultiIndex'\r\n   1489                            ' lexsort depth (%d)' %\r\n-> 1490                            (len(tup), self.lexsort_depth))\r\n   1491\r\n   1492         n = len(tup)\r\n\r\nKeyError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'\r\n~~~\r\n\r\nSo I have two questions.\r\n1. Is this the intended behavior? I thought `level=0` and `level=None` are synonyms to me, but they are not. Looking at the code, <https://github.com/pydata/pandas/blob/4de83d25d751d8ca102867b2d46a5547c01d7248/pandas/core/frame.py#L3245-L3247> indeed there's a special processing when `level` is not `None`.\r\n2. What does \"lexicographically sorted\" mean? I think it should mean sorted in terms of levels, not labels. Is this what \"lexicographically sorted\" means in the doc for Advanced Indexing? If this is true, then I think `make_index(level=0)` is correct, yet `make_index()` is not.\r\n\r\nThanks.\r\n\r\n#### output of ``pd.show_versions()``\r\n\r\n~~~python\r\nIn [130]: pd.show_versions()\r\n\r\nINSTALLED VERSIONS\r\n------------------\r\ncommit: None\r\npython: 2.7.11.final.0\r\npython-bits: 64\r\nOS: Darwin\r\nOS-release: 14.5.0\r\nmachine: x86_64\r\nprocessor: i386\r\nbyteorder: little\r\nLC_ALL: None\r\nLANG: en_US.UTF-8\r\n\r\npandas: 0.18.1\r\nnose: 1.3.7\r\npip: 8.1.2\r\nsetuptools: 23.0.0\r\nCython: 0.23.4\r\nnumpy: 1.11.0\r\nscipy: 0.17.0\r\nstatsmodels: None\r\nxarray: None\r\nIPython: 4.2.0\r\nsphinx: None\r\npatsy: None\r\ndateutil: 2.5.3\r\npytz: 2016.4\r\nblosc: None\r\nbottleneck: None\r\ntables: None\r\nnumexpr: None\r\nmatplotlib: 1.5.1\r\nopenpyxl: None\r\nxlrd: None\r\nxlwt: None\r\nxlsxwriter: None\r\nlxml: None\r\nbs4: None\r\nhtml5lib: None\r\nhttplib2: None\r\napiclient: None\r\nsqlalchemy: None\r\npymysql: None\r\npsycopg2: None\r\njinja2: 2.8\r\nboto: None\r\npandas_datareader: None\r\n~~~~\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/pydata/pandas/issues/comments/225447489","html_url":"https://github.com/pydata/pandas/issues/13431#issuecomment-225447489","issue_url":"https://api.github.com/repos/pydata/pandas/issues/13431","id":225447489,"user":{"login":"jreback","id":953992,"avatar_url":"https://avatars.githubusercontent.com/u/953992?v=3","gravatar_id":"","url":"https://api.github.com/users/jreback","html_url":"https://github.com/jreback","followers_url":"https://api.github.com/users/jreback/followers","following_url":"https://api.github.com/users/jreback/following{/other_user}","gists_url":"https://api.github.com/users/jreback/gists{/gist_id}","starred_url":"https://api.github.com/users/jreback/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jreback/subscriptions","organizations_url":"https://api.github.com/users/jreback/orgs","repos_url":"https://api.github.com/users/jreback/repos","events_url":"https://api.github.com/users/jreback/events{/privacy}","received_events_url":"https://api.github.com/users/jreback/received_events","type":"User","site_admin":false},"created_at":"2016-06-12T16:49:38Z","updated_at":"2016-06-12T16:49:38Z","body":"``.sort_index()`` and ``.sort_index(level=0)`` should yield the same index. The factors are the same (and don't change) in all cases.\r\n\r\nSee [here for the contributing docs](http://pandas.pydata.org/pandas-docs/stable/contributing.html)"}}
{"actor":{"display_login":"jreback"}}