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

How to order descending in @orderby? #81

Closed
ferris-wufei opened this issue Dec 22, 2017 · 5 comments
Closed

How to order descending in @orderby? #81

ferris-wufei opened this issue Dec 22, 2017 · 5 comments

Comments

@ferris-wufei
Copy link

Tried adding a minus sign before the column symbol, but got the error msg

MethodError: no method matching -(::String)
...

Is there any workaround? Thanks.

@nalimilan
Copy link
Member

Unfortunately that doesn't seem to be supported currently. However it shouldn't be hard to support, by adding a keyword argument and passing it to sortperm.

@tshort
Copy link
Contributor

tshort commented Dec 22, 2017

It works for me:

julia> using DataFrames, DataFramesMeta

julia> d = DataFrame(n = 1:20, x = [3, 3, 3, 3, 1, 1, 1, 2, 1, 1,
                                           2, 1, 1, 2, 2, 2, 3, 1, 1, 2]);

julia> @orderby(d, -:x)
20×2 DataFrames.DataFrame
│ Row │ n  │ x │
├─────┼────┼───┤
│ 113 │
│ 223 │
│ 333 │
│ 443 │
│ 5173 │
│ 682 │
│ 7112 │
│ 8142 │
│ 9152 │
│ 10162 │
│ 11202 │
│ 1251 │
│ 1361 │
│ 1471 │
│ 1591 │
│ 16101 │
│ 17121 │
│ 18131 │
│ 19181 │
│ 20191 │

julia> g = groupby(d, :x);

julia> @orderby(g, -mean(:n))
DataFrames.GroupedDataFrame  3 groups with keys: Symbol[:x]
First Group:
6×2 DataFrames.SubDataFrame{Array{Int64,1}}
│ Row │ n  │ x │
├─────┼────┼───┤
│ 182 │
│ 2112 │
│ 3142 │
│ 4152 │
│ 5162 │
│ 6202
Last Group:
5×2 DataFrames.SubDataFrame{Array{Int64,1}}
│ Row │ n  │ x │
├─────┼────┼───┤
│ 113 │
│ 223 │
│ 333 │
│ 443 │
│ 5173

@nalimilan
Copy link
Member

I guess that only works for numeric columns...

@tshort
Copy link
Contributor

tshort commented Dec 22, 2017

Correct on numeric. One way around that might be to encode with a CategoricalArray and control the sort order with that. (I didn't try that.)

Or, someone could PR the use of a KW arg to add this feature.

@pdeffebach
Copy link
Collaborator

Closed in favor of #166

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants