Repository: pandas-dev/pandas

Issue:
DataFrame.sort_index does not use ascending when then value is a list with a single element
```
In [7]: d
Out[7]: {'one': [1.0, 2.0, 3.0, 4.0], 'two': [4.0, 3.0, 2.0, 1.0]}



In [11]: pd.DataFrame(d).sort_index(by=['two'], ascending=[0,])
Out[11]: 
   one  two
3    4    1
2    3    2
1    2    3
0    1    4

In [12]: pd.DataFrame(d).sort_index(by=['two'], ascending=0)
Out[12]: 
   one  two
0    1    4
1    2    3
2    3    2
3    4    1
```



Discussion:
Take it back looking at the wrong column. Main issue is that ascending is interpreted wrong. Trivial to fix though.

thank you for the replay.
I knew how to workaround the issue, but if you do compute the ascending elements programmatically, you have to add  a special case for the single column case, which is not good

Well, the main problem is that you're not actually passing what you think. WHen pass `ascending=0` with a single column, it gets interpreted into `ascending=False` (because 0 is falsey).  It works the same way with multiple columns too.

Are you thinking that passing:
`pd.DataFrame(d).sort_index(by=['two'], ascending=0)`
Should be equivalent to
`pd.DataFrame(d).sort_index(by=['two'], ascending=[0,])`?

For example,this isn't supported:

`pd.DataFrame(d).sort_index(by=['one', 'two'], ascending=0)`

basically, the other option for this would be to raise an error because it's not iterable or a bool...

@jburroni please close if this answers your concern. If not, I'll take another look. I think it might just be okay (otherwise have to be less Pythonic when testing for truthiness).

Here is the thing

using this:

```
In [19]: df.sort_index(by=['two', 'one'], ascending=[0,0])
Out[19]: 
   one  two
0    1    4
1    2    3
2    3    2
3    4    1
```

but, using only 'two'

```
In [20]: df.sort_index(by=['two'], ascending=[0])
Out[20]:     
   one  two
3    4    1
2    3    2
1    2    3
0    1    4
```

And this is not consistent

That is the issue I'm most concerned as this issue arise when you programmatically define a sort_index columns and ascending orders

@jburroni yes, that is a bug. I'll fix it.

@jtratner thank you 

@jburroni btw - why do you use [0, 0] as opposed to [False, False]?

I've just copied the example frmo:
http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.sort_index.html

```
>>> result = df.sort_index(by=['A', 'B'], ascending=[1, 0])
```

ah okay. I'll change that too for clarity...can only be True or False anyways (but True and False are actually equivalent to 1 and 0 respectively)


Resolving patch:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -2856,7 +2856,7 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
 
         Examples
         --------
-        >>> result = df.sort_index(by=['A', 'B'], ascending=[1, 0])
+        >>> result = df.sort_index(by=['A', 'B'], ascending=[True, False])
 
         Returns
         -------
@@ -2875,6 +2875,9 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
                 raise ValueError('When sorting by column, axis must be 0 (rows)')
             if not isinstance(by, (tuple, list)):
                 by = [by]
+            if com._is_sequence(ascending) and len(by) != len(ascending):
+                raise ValueError('Length of ascending (%d) != length of by'
+                                 ' (%d)' % (len(ascending), len(by)))
 
             if len(by) > 1:
                 keys = []
@@ -2900,6 +2903,8 @@ def trans(v):
                     raise ValueError('Cannot sort by duplicate column %s'
                                      % str(by))
                 indexer = k.argsort(kind=kind)
+                if isinstance(ascending, (tuple, list)):
+                    ascending = ascending[0]
                 if not ascending:
                     indexer = indexer[::-1]
         elif isinstance(labels, MultiIndex):


Test patch:
diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py
--- a/pandas/tests/test_frame.py
+++ b/pandas/tests/test_frame.py
@@ -8796,24 +8796,37 @@ def test_sort_index(self):
         expected = frame.ix[frame.index[indexer]]
         assert_frame_equal(sorted_df, expected)
 
+        sorted_df = frame.sort(columns='A', ascending=False)
+        assert_frame_equal(sorted_df, expected)
+
+        # GH4839
+        sorted_df = frame.sort(columns=['A'], ascending=[False])
+        assert_frame_equal(sorted_df, expected)
+
         # check for now
         sorted_df = frame.sort(columns='A')
+        assert_frame_equal(sorted_df, expected[::-1])
         expected = frame.sort_index(by='A')
         assert_frame_equal(sorted_df, expected)
 
-        sorted_df = frame.sort(columns='A', ascending=False)
-        expected = frame.sort_index(by='A', ascending=False)
-        assert_frame_equal(sorted_df, expected)
 
         sorted_df = frame.sort(columns=['A', 'B'], ascending=False)
         expected = frame.sort_index(by=['A', 'B'], ascending=False)
         assert_frame_equal(sorted_df, expected)
 
+        sorted_df = frame.sort(columns=['A', 'B'])
+        assert_frame_equal(sorted_df, expected[::-1])
+
         self.assertRaises(ValueError, frame.sort_index, axis=2, inplace=True)
+
         msg = 'When sorting by column, axis must be 0'
         with assertRaisesRegexp(ValueError, msg):
             frame.sort_index(by='A', axis=1)
 
+        msg = r'Length of ascending \(5\) != length of by \(2\)'
+        with assertRaisesRegexp(ValueError, msg):
+            frame.sort_index(by=['A', 'B'], axis=0, ascending=[True] * 5)
+
     def test_sort_index_multicolumn(self):
         import random
         A = np.arange(5).repeat(20)