-
Notifications
You must be signed in to change notification settings - Fork 368
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
Add sorting options to groupby #3253
Changes from all commits
870c0e6
806f878
4bee9dc
dfe9460
669e439
35e7f04
3b6ad89
aee91ec
ae9fc23
ecf1d1b
c8759e7
ca766d9
7bfaea0
4948ae8
cc9b8e8
7e72fc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4378,4 +4378,132 @@ end | |
@test_throws ArgumentError combine(gdf, :a => (x -> [Dict('x' => 1)]) => AsTable) | ||
end | ||
|
||
@testset "sorting API" begin | ||
# simple tests | ||
df = DataFrame(x=["b", "c", "b", "a", "c"]) | ||
@test getindex.(keys(groupby(df, :x)), 1) == ["b", "c", "a"] | ||
@test getindex.(keys(groupby(df, :x, sort=true)), 1) == ["a", "b", "c"] | ||
@test getindex.(keys(groupby(df, :x, sort=NamedTuple())), 1) == ["a", "b", "c"] | ||
@test getindex.(keys(groupby(df, :x, sort=false)), 1) == ["b", "c", "a"] | ||
@test getindex.(keys(groupby(df, order(:x))), 1) == ["a", "b", "c"] | ||
@test getindex.(keys(groupby(df, order(:x), sort=true)), 1) == ["a", "b", "c"] | ||
@test_throws ArgumentError groupby(df, order(:x), sort=false) | ||
@test getindex.(keys(groupby(df, order(:x), sort=NamedTuple())), 1) == ["a", "b", "c"] | ||
@test getindex.(keys(groupby(df, [order(:x)])), 1) == ["a", "b", "c"] | ||
@test getindex.(keys(groupby(df, [order(:x)], sort=true)), 1) == ["a", "b", "c"] | ||
@test_throws ArgumentError groupby(df, [order(:x)], sort=false) | ||
@test getindex.(keys(groupby(df, [order(:x)], sort=NamedTuple())), 1) == ["a", "b", "c"] | ||
@test getindex.(keys(groupby(df, order(:x, rev=true))), 1) == ["c", "b", "a"] | ||
@test getindex.(keys(groupby(df, order(:x, rev=true), sort=true)), 1) == ["c", "b", "a"] | ||
@test getindex.(keys(groupby(df, order(:x, rev=true), sort=NamedTuple())), 1) == ["c", "b", "a"] | ||
@test getindex.(keys(groupby(df, [order(:x, rev=true)])), 1) == ["c", "b", "a"] | ||
@test getindex.(keys(groupby(df, [order(:x, rev=true)], sort=true)), 1) == ["c", "b", "a"] | ||
@test getindex.(keys(groupby(df, [order(:x, rev=true)], sort=NamedTuple())), 1) == ["c", "b", "a"] | ||
@test getindex.(keys(groupby(df, :x, sort=(;rev=true))), 1) == ["c", "b", "a"] | ||
@test getindex.(keys(groupby(df, [:x], sort=(;rev=true))), 1) == ["c", "b", "a"] | ||
|
||
# by default sorting is not applied as range of values is wide | ||
df = DataFrame(x=[2, 100, 2, 1, 100]) | ||
@test getindex.(keys(groupby(df, :x)), 1) == [2, 100, 1] | ||
@test getindex.(keys(groupby(df, :x, sort=true)), 1) == [1, 2, 100] | ||
@test getindex.(keys(groupby(df, :x, sort=NamedTuple())), 1) == [1, 2, 100] | ||
@test getindex.(keys(groupby(df, :x, sort=false)), 1) == [2, 100, 1] | ||
@test getindex.(keys(groupby(df, order(:x))), 1) == [1, 2, 100] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having so many equivalent ways to specify sorting does seem a bit much? Not sure if it's worth doing anything about. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue is that
In general - this complexity is needed when one has several columns. |
||
@test getindex.(keys(groupby(df, [order(:x)])), 1) == [1, 2, 100] | ||
@test getindex.(keys(groupby(df, order(:x, rev=true))), 1) == [100, 2, 1] | ||
@test getindex.(keys(groupby(df, [order(:x, rev=true)])), 1) == [100, 2, 1] | ||
@test getindex.(keys(groupby(df, :x, sort=(;rev=true))), 1) == [100, 2, 1] | ||
@test getindex.(keys(groupby(df, [:x], sort=(;rev=true))), 1) == [100, 2, 1] | ||
|
||
# by default sorting is applied as range of values is narrow | ||
df = DataFrame(x=[2, 3, 2, 1, 3]) | ||
@test getindex.(keys(groupby(df, :x)), 1) == [1, 2, 3] | ||
@test getindex.(keys(groupby(df, :x, sort=true)), 1) == [1, 2, 3] | ||
@test getindex.(keys(groupby(df, :x, sort=NamedTuple())), 1) == [1, 2, 3] | ||
@test getindex.(keys(groupby(df, :x, sort=false)), 1) == [2, 3, 1] | ||
@test getindex.(keys(groupby(df, order(:x))), 1) == [1, 2, 3] | ||
@test getindex.(keys(groupby(df, [order(:x)])), 1) == [1, 2, 3] | ||
@test getindex.(keys(groupby(df, order(:x, rev=true))), 1) == [3, 2, 1] | ||
@test getindex.(keys(groupby(df, [order(:x, rev=true)])), 1) == [3, 2, 1] | ||
@test getindex.(keys(groupby(df, :x, sort=(;rev=true))), 1) == [3, 2, 1] | ||
@test getindex.(keys(groupby(df, [:x], sort=(;rev=true))), 1) == [3, 2, 1] | ||
|
||
# randomized tests | ||
Random.seed!(1234) | ||
df1 = DataFrame(a=rand(-10:10, 100), b=rand(-10:10, 100), c=1:100) | ||
df2 = string.(df1, pad=3) | ||
|
||
for df in (df1, df2) | ||
for col in (:a, "a", 1, :b, "b", 2, :c, "c", 3) | ||
gdf = groupby(df, order(col)) | ||
@test issorted(DataFrame(gdf)[:, col]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, col, sort=true) | ||
@test issorted(DataFrame(gdf)[:, col]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, order(col), sort=true) | ||
@test issorted(DataFrame(gdf)[:, col]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, col, sort=NamedTuple()) | ||
@test issorted(DataFrame(gdf)[:, col]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, order(col), sort=NamedTuple()) | ||
@test issorted(DataFrame(gdf)[:, col]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, col, sort=(rev=true,)) | ||
@test issorted(DataFrame(gdf)[:, col], rev=true) | ||
@test all(x -> issorted(x.c), gdf) | ||
if eltype(df[!, col]) === Int | ||
gdf = groupby(df, order(col, by=abs), sort=(rev=true,)) | ||
@test issorted(DataFrame(gdf)[:, col], rev=true, by=abs) | ||
else | ||
gdf = groupby(df, order(col, by=abs∘(x -> parse(Int, x))), sort=(rev=true,)) | ||
@test issorted(DataFrame(gdf)[:, col], rev=true, by=abs∘(x -> parse(Int, x))) | ||
end | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, col, sort=false) | ||
@test getindex.(keys(gdf), 1) == unique(df[!, col]) | ||
@test all(x -> issorted(x.c), gdf) | ||
end | ||
|
||
gdf = groupby(df, [:a, :b], sort=true) | ||
@test issorted(DataFrame(gdf), [:a, :b]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, [:a, order(:b)]) | ||
@test issorted(DataFrame(gdf), [:a, :b]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, [:a, order(:b)], sort=true) | ||
@test issorted(DataFrame(gdf), [:a, :b]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, [:a, :b], sort=NamedTuple()) | ||
@test issorted(DataFrame(gdf), [:a, :b]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, [:a, order(:b)], sort=NamedTuple()) | ||
@test issorted(DataFrame(gdf), [:a, :b]) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, [:a, :b], sort=(rev=true,)) | ||
@test issorted(DataFrame(gdf), [:a, :b], rev=true) | ||
@test all(x -> issorted(x.c), gdf) | ||
if eltype(df[!, :a]) === Int | ||
gdf = groupby(df, [order(:a, by=abs), :b], sort=(rev=true,)) | ||
@test issorted(DataFrame(gdf), [order(:a, by=abs), :b], rev=true) | ||
@test all(x -> issorted(x.c), gdf) | ||
else | ||
gdf = groupby(df, [order(:a, by=abs∘(x -> parse(Int, x))), :b], sort=(rev=true,)) | ||
@test issorted(DataFrame(gdf), [order(:a, by=abs∘(x -> parse(Int, x))), :b], rev=true) | ||
@test all(x -> issorted(x.c), gdf) | ||
end | ||
gdf = groupby(df, [:a, order(:b, rev=false)], sort=(rev=true,)) | ||
@test issorted(DataFrame(gdf), [:a, order(:b, rev=false)], rev=true) | ||
@test all(x -> issorted(x.c), gdf) | ||
gdf = groupby(df, [:a, :b], sort=false) | ||
@test Tuple.(keys(gdf)) == unique(Tuple.(eachrow(df[!, [:a, :b]]))) | ||
@test all(x -> issorted(x.c), gdf) | ||
|
||
@test_throws ArgumentError groupby(df, order(:a), sort=false) | ||
@test_throws ArgumentError groupby(df, [:b, order(:a)], sort=false) | ||
@test_throws MethodError groupby(df, :a, sort=(x=1,)) | ||
end | ||
end | ||
|
||
end # module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these can use
only
instead ofgetindex(_, 1)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed it could. I initially had this test implemented on groups not on keys and using
getindex
works on both.