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

make renaming perform copy in transform and transform! #2721

Merged
merged 10 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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 NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* `mapcols!` makes sure not to create columns being `AbstractRange` consistently
with other methods that add columns to a `DataFrame`
([#2594](https://github.com/JuliaData/DataFrames.jl/pull/2594))
* `transform` and `transform!` always copy columns when column renaming transformation
is passed
([#2721](https://github.com/JuliaData/DataFrames.jl/pull/2721))

## New functionalities

Expand Down
28 changes: 24 additions & 4 deletions src/abstractdataframe/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ const TRANSFORMATION_COMMON_RULES =
transformations that return the same object without copying may create
column aliases even if `copycols=true`. An example of such a situation is
`select!(df, :a, :a => :b, :a => identity => :c)`.
As a special case in `transform` and `transform!` column renaming always
copies columns to avoid storing aliased columns in the target data frame.

If `df` is a `SubDataFrame` and `copycols=true` then a `DataFrame` is
returned and the same copying rules apply as for a `DataFrame` input: this
Expand Down Expand Up @@ -656,8 +658,17 @@ $TRANSFORMATION_COMMON_RULES

See [`select`](@ref) for examples.
"""
transform!(df::DataFrame, @nospecialize(args...); renamecols::Bool=true) =
select!(df, :, args..., renamecols=renamecols)
function transform!(df::DataFrame, @nospecialize(args...); renamecols::Bool=true)
idx = index(df)
newargs = Any[if sel isa Pair{<:ColumnIndex, Symbol}
bkamins marked this conversation as resolved.
Show resolved Hide resolved
idx[first(sel)] => copy => last(sel)
elseif sel isa Pair{<:ColumnIndex, <:AbstractString}
idx[first(sel)] => copy => Symbol(last(sel))
else
sel
end for sel in args]
return select!(df, :, newargs..., renamecols=renamecols)
end

function transform!(@nospecialize(arg::Base.Callable), df::AbstractDataFrame; renamecols::Bool=true)
if arg isa Colon
Expand Down Expand Up @@ -978,8 +989,17 @@ ERROR: ArgumentError: column :x in returned data frame is not equal to grouping

See [`select`](@ref) for more examples.
"""
transform(df::AbstractDataFrame, @nospecialize(args...); copycols::Bool=true, renamecols::Bool=true) =
select(df, :, args..., copycols=copycols, renamecols=renamecols)
function transform(df::AbstractDataFrame, @nospecialize(args...); copycols::Bool=true, renamecols::Bool=true)
idx = index(df)
newargs = Any[if sel isa Pair{<:ColumnIndex, Symbol}
idx[first(sel)] => copy => last(sel)
elseif sel isa Pair{<:ColumnIndex, <:AbstractString}
idx[first(sel)] => copy => Symbol(last(sel))
else
sel
end for sel in args]
bkamins marked this conversation as resolved.
Show resolved Hide resolved
return select(df, :, newargs..., copycols=copycols, renamecols=renamecols)
end

function transform(@nospecialize(arg::Base.Callable), df::AbstractDataFrame; renamecols::Bool=true)
if arg isa Colon
Expand Down
22 changes: 22 additions & 0 deletions test/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1620,4 +1620,26 @@ end
[:a] => sum => ["new"], false) == (1 => (sum => [:new]))
end

@testset "copying in transform when renaming" begin
df = DataFrame(a=1)
df2 = transform(df, :a => :b)
@test df2.b == df2.a == df.a
@test df2.b !== df2.a
@test df2.b !== df.a
@test df2.a !== df.a

df2 = transform(df, :a => :b, copycols=false)
@test df2.b == df2.a == df.a
@test df2.b !== df2.a
@test df2.b !== df.a
@test df2.a === df.a

a = df.a
transform!(df, :a => :b)
@test df.b == df.a == a
@test df.b !== df.a
@test df.b !== a
@test df.a === a
bkamins marked this conversation as resolved.
Show resolved Hide resolved
end

end # module