Repository: pandas-dev/pandas

Issue:
Sort broken using MultiIndex with mixed `ascending`
#### Code Sample, a copy-pastable example if possible

```python
# Imports
import pandas as pd

# Set up a Series with a three level MultiIndex
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
  ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],
  [4, 3, 2, 1, 4, 3, 2, 1]]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second', 'third'])
s = pd.Series(range(8), index=index)

# Sort descending works
s.sort_index(level=['third', 'first'], ascending=False)

# But sort descending on third level, ascending on first does not
s.sort_index(level=['third', 'first'], ascending=[False, True])
```
#### Problem description

Sorting using a list for the `ascending` argument appears to have been implemented in #10905 but doesn't work in the above scenario. I get the following output:

```
>>> s.sort_index(level=['third', 'first'], ascending=[False, True])

first  second  third
qux    one     2        6
       two     1        7
foo    one     4        4
       two     3        5
baz    one     2        2
       two     1        3
bar    one     4        0
       two     3        1
dtype: int64
```

#### Expected Output

DataFrame sorted by index `third` descending then index `first` ascending:

```
>>> s.sort_index(level=['third', 'first'], ascending=[False, True])

first  second  third
bar    one     4        0
foo    one     4        4
bar    two     3        1
foo    two     3        5
baz    one     2        2
qux    one     2        6
baz    two     1        3
qux    two     1        7
```

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Darwin
OS-release: 16.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.2
pytest: None
pip: 9.0.1
setuptools: 35.0.1
Cython: None
numpy: 1.13.0
scipy: 0.19.0
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.0
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: 0.9.6
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: 2.7.1 (dt dec pq3 ext lo64)
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None
</details>



Discussion:
@alubbock : Would you mind adding (with a hand-constructed `DataFrame`) what you expect the output to look like?  That would be useful in addition to your description.

Otherwise, this definitely looks like a bug.  Nice catch!  PR is welcome.

cc @roblevy 
@gfyoung I've edited the issue to add expected output and removed the `np.random` call to make the example deterministic. Thanks.
@alubbock : Thanks!  Feel free to submit a PR for this if you know where the issue is.

Resolving patch:
diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py
--- a/pandas/core/indexes/multi.py
+++ b/pandas/core/indexes/multi.py
@@ -1697,7 +1697,8 @@ def sortlevel(self, level=0, ascending=True, sort_remaining=True):
                 raise ValueError("level must have same length as ascending")
 
             from pandas.core.sorting import lexsort_indexer
-            indexer = lexsort_indexer(self.labels, orders=ascending)
+            indexer = lexsort_indexer([self.labels[lev] for lev in level],
+                                      orders=ascending)
 
         # level ordering
         else:


Test patch:
diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py
--- a/pandas/tests/test_multilevel.py
+++ b/pandas/tests/test_multilevel.py
@@ -2781,3 +2781,26 @@ def test_sort_index_nan(self):
         result = s.sort_index(na_position='first')
         expected = s.iloc[[1, 2, 3, 0]]
         tm.assert_series_equal(result, expected)
+
+    def test_sort_ascending_list(self):
+        # GH: 16934
+
+        # Set up a Series with a three level MultiIndex
+        arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
+                  ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],
+                  [4, 3, 2, 1, 4, 3, 2, 1]]
+        tuples = list(zip(*arrays))
+        index = pd.MultiIndex.from_tuples(tuples,
+                                          names=['first', 'second', 'third'])
+        s = pd.Series(range(8), index=index)
+
+        # Sort with boolean ascending
+        result = s.sort_index(level=['third', 'first'], ascending=False)
+        expected = s.iloc[[4, 0, 5, 1, 6, 2, 7, 3]]
+        tm.assert_series_equal(result, expected)
+
+        # Sort with list of boolean ascending
+        result = s.sort_index(level=['third', 'first'],
+                              ascending=[False, True])
+        expected = s.iloc[[0, 4, 1, 5, 2, 6, 3, 7]]
+        tm.assert_series_equal(result, expected)