Skip to content

Commit

Permalink
fix bug in select/transform when GroupedDataFrame is reordered
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Oct 12, 2020
1 parent 71843b1 commit a13db50
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DataFrames"
uuid = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
version = "0.21.7"
version = "0.21.8"

This comment has been minimized.

Copy link
@bkamins

bkamins Oct 12, 2020

Author Member

@nalimilan + @pdeffebach -> could you please check this commit (it is a bug fix). I will include it in 0.22 branch independently in the PR I am currently preparing for multiple-column return values from transforms.

Thank you!

This comment has been minimized.

Copy link
@nalimilan

nalimilan Oct 12, 2020

Member

Looks good!


[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
9 changes: 7 additions & 2 deletions src/groupeddataframe/splitapplycombine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,14 @@ function _combine(f::AbstractVector{<:Pair},
newcol = similar(col)
# we can probably make it more efficient, but I leave it as an optimization for the future
gd_idx = gd.idx
for j in eachindex(gd.idx, col)
newcol[gd_idx[j]] = col[j]
k = 0
for (s, e) in zip(gd.starts, gd.ends)

This comment has been minimized.

Copy link
@nalimilan

nalimilan Oct 12, 2020

Member

Would it make sense to use @inbounds?

for j in s:e
k += 1
newcol[gd_idx[j]] = col[k]
end
end
@assert k == length(gd_idx)
res[i] = (col_idx, newcol)
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2483,4 +2483,30 @@ end
@test eltype(df2.a) === eltype(df2.b) === Union{UInt, Missing}
end

@testset "aggregation of reordered groups" begin
df = DataFrame(id = [1, 2, 3, 1, 3, 2], x=1:6)
gdf = groupby(df, :id)
@test select(df, :id, :x => x -> 2x) == select(gdf, :x => x -> 2x)
@test transform(df, :x => x -> 2x) == transform(gdf, :x => x -> 2x)
@test combine(gdf, :x => x -> 2x) ==
DataFrame(id=[1, 1, 2, 2, 3, 3], x_function=[2, 8, 4, 12, 6, 10])
gdf = groupby(df, :id)[[3, 1, 2]]
@test select(df, :id, :x => x -> 2x) == select(gdf, :x => x -> 2x)
@test transform(df, :x => x -> 2x) == transform(gdf, :x => x -> 2x)
@test combine(gdf, :x => x -> 2x) ==
DataFrame(id=[3, 3, 1, 1, 2, 2], x_function=[6, 10, 2, 8, 4, 12])

df = DataFrame(id = [3, 2, 1, 3, 1, 2], x=1:6)
gdf = groupby(df, :id, sort=true)
@test select(df, :id, :x => x -> 2x) == select(gdf, :x => x -> 2x)
@test transform(df, :x => x -> 2x) == transform(gdf, :x => x -> 2x)
@test combine(gdf, :x => x -> 2x) ==
DataFrame(id=[1, 1, 2, 2, 3, 3], x_function=[6, 10, 4, 12, 2, 8])
gdf = groupby(df, :id)[[3, 1, 2]]
@test select(df, :id, :x => x -> 2x) == select(gdf, :x => x -> 2x)
@test transform(df, :x => x -> 2x) == transform(gdf, :x => x -> 2x)
@test combine(gdf, :x => x -> 2x) ==
DataFrame(id=[1, 1, 3, 3, 2, 2], x_function=[6, 10, 2, 8, 4, 12])
end

end # module

2 comments on commit a13db50

@bkamins
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register branch=v0.21_patches

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/22840

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.21.8 -m "<description of version>" a13db50a0cd8115691fe92e67b1301422c664401
git push origin v0.21.8

Please sign in to comment.