Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataFrame.sort_index does not use ascending when then value is a list with a single element #4839

Closed
jburroni opened this issue Sep 14, 2013 · 13 comments · Fixed by #4846

Comments

@jburroni
Copy link

commented Sep 14, 2013

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
@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 14, 2013

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

@ghost ghost assigned jtratner Sep 14, 2013

@jburroni

This comment has been minimized.

Copy link
Author

commented Sep 14, 2013

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

@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 14, 2013

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.

@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 14, 2013

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,])?

@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 14, 2013

For example,this isn't supported:

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

@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 14, 2013

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

@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 15, 2013

@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).

@jburroni

This comment has been minimized.

Copy link
Author

commented Sep 15, 2013

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

@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 15, 2013

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

@jburroni

This comment has been minimized.

Copy link
Author

commented Sep 15, 2013

@jtratner thank you

@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 15, 2013

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

@jburroni

This comment has been minimized.

Copy link
Author

commented Sep 15, 2013

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])
@jtratner

This comment has been minimized.

Copy link
Contributor

commented Sep 15, 2013

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.