Skip to content
New issue

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

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

Already on GitHub? # to your account

Disallow combine without any transformations #2633

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/abstractdataframe/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,9 @@ julia> combine(gd, :, AsTable(Not(:a)) => sum, renamecols=false)
combine(df::AbstractDataFrame, @nospecialize(args...); renamecols::Bool=true) =
manipulate(df, args..., copycols=true, keeprows=false, renamecols=renamecols)

combine(df::AbstractDataFrame; renamecols::Bool=true) =
throw(ArgumentError("At least one transformation must be specified"))

function combine(arg::Base.Callable, df::AbstractDataFrame; renamecols::Bool=true)
if arg isa Colon
throw(ArgumentError("First argument to select! must be a transformation if the second argument is a data frame"))
Expand Down
3 changes: 3 additions & 0 deletions src/groupeddataframe/splitapplycombine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,9 @@ combine(gd::GroupedDataFrame,
_combine_prepare(gd, cs..., keepkeys=keepkeys, ungroup=ungroup,
copycols=true, keeprows=false, renamecols=renamecols)

combine(df::GroupedDataFrame; renamecols::Bool=true) =
throw(ArgumentError("At least one transformation must be specified"))

function select(f::Base.Callable, gd::GroupedDataFrame; copycols::Bool=true,
keepkeys::Bool=true, ungroup::Bool=true, renamecols::Bool=true)
if f isa Colon
Expand Down
21 changes: 4 additions & 17 deletions test/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ end
combine(gd, :c => (x -> [Dict(:c_sum => sum(x))]) => AsTable)
@test_throws ArgumentError combine(:c => sum, gd)
@test_throws ArgumentError combine(:, gd)
@test_throws ArgumentError combine(gd)

@test combine(gd, :c => vexp) ==
combine(gd, :c => vexp => :c_function) ==
Expand Down Expand Up @@ -1658,11 +1659,6 @@ end
1 │ 1 1 1
2 │ 2 1 2
3 │ 2 2 3"""

df = DataFrame(a=[1, 1, 2, 2, 2], b=1:5)
gd = groupby_checked(df, :a)
@test size(combine(gd)) == (0, 1)
@test names(combine(gd)) == ["a"]
end

@testset "GroupedDataFrame dictionary interface" begin
Expand Down Expand Up @@ -2635,10 +2631,7 @@ end
df = DataFrame(x=[], g=[])
gdf = groupby_checked(df, :g)

@test size(combine(gdf)) == (0, 1)
@test names(combine(gdf)) == ["g"]
@test combine(gdf, keepkeys=false) == DataFrame()
@test combine(gdf, ungroup=false) == groupby(DataFrame(g=[]), :g)
@test_throws ArgumentError combine(gdf)
@test size(select(gdf)) == (0, 1)
@test names(select(gdf)) == ["g"]
@test groupcols(validate_gdf(select(gdf, ungroup=false))) == [:g]
Expand Down Expand Up @@ -2680,12 +2673,7 @@ end
df = DataFrame(x=[1], g=[1])
gdf = groupby_checked(df, :g)

@test size(combine(gdf)) == (0, 1)
@test names(combine(gdf)) == ["g"]
@test combine(gdf, ungroup=false) isa GroupedDataFrame
@test length(combine(gdf, ungroup=false)) == 0
@test parent(combine(gdf, ungroup=false)) == DataFrame(g=[])
@test combine(gdf, keepkeys=false) == DataFrame()
@test_throws ArgumentError combine(gdf)
@test size(select(gdf)) == (1, 1)
@test names(select(gdf)) == ["g"]
@test groupcols(validate_gdf(select(gdf, ungroup=false))) == [:g]
Expand Down Expand Up @@ -2714,8 +2702,7 @@ end
@test select(gdf, :x => (x -> 10x) => :g, keepkeys=false) == DataFrame(g=10)

gdf = gdf[1:0]
@test size(combine(gdf)) == (0, 1)
@test names(combine(gdf)) == ["g"]
@test_throws ArgumentError combine(gdf)
@test size(combine(x -> DataFrame(z=1), gdf)) == (0, 2)
@test names(combine(x -> DataFrame(z=1), gdf)) == ["g", "z"]
@test combine(x -> DataFrame(z=1), gdf, keepkeys=false) == DataFrame(z=[])
Expand Down
4 changes: 4 additions & 0 deletions test/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,8 @@ end
@testset "combine AbstractDataFrame" begin
df = DataFrame(x=1:3, y=4:6)

@test_throws ArgumentError combine(df)

@test combine(x -> Matrix(x), df) == rename(df, [:x1, :x2])
@test combine(x -> Ref(1:3), df) == DataFrame(x1=[1:3])
@test combine(df, x -> Ref(1:3)) == DataFrame(x1=[1:3])
Expand All @@ -1215,6 +1217,8 @@ end

dfv = view(df, [2, 1], [2, 1])

@test_throws ArgumentError combine(dfv)

@test combine(x -> Matrix(x), dfv) == rename(dfv, [:x1, :x2])

@test_throws ArgumentError combine(dfv, AsTable(:) => identity)
Expand Down