From 1b4a8d3e932b82e74f92987b9e16fafb6c136028 Mon Sep 17 00:00:00 2001 From: lizz Date: Sat, 5 Oct 2019 20:21:07 +0800 Subject: [PATCH] fix Signed-off-by: lizz --- src/abstractdataframe/abstractdataframe.jl | 6 +++--- src/other/index.jl | 19 ++++++++++++++----- test/dataframe.jl | 6 +++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/abstractdataframe/abstractdataframe.jl b/src/abstractdataframe/abstractdataframe.jl index f684eaa703..4ec8db7fb9 100644 --- a/src/abstractdataframe/abstractdataframe.jl +++ b/src/abstractdataframe/abstractdataframe.jl @@ -150,21 +150,21 @@ rename(f::Function, df::AbstractDataFrame) * `::AbstractDataFrame` : the updated result -New names are processed sequentially. A new name must not already exist in the `DataFrame` -at the moment an attempt to rename a column is performed. +Each name is changed at most once. Permutation of names is allowed. **Examples** ```julia df = DataFrame(i = 1:10, x = rand(10), y = rand(["a", "b", "c"], 10)) rename(df, :i => :A, :x => :X) +rename(df, :x => :y, :y => :x) rename(df, [:i => :A, :x => :X]) rename(df, Dict(:i => :A, :x => :X)) rename(x -> Symbol(uppercase(string(x))), df) rename(df) do x Symbol(uppercase(string(x))) end -rename!(df, Dict(:i =>: A, :x => :X)) +rename!(df, Dict(:i => :A, :x => :X)) ``` """ diff --git a/src/other/index.jl b/src/other/index.jl index c167aa1492..64a9129dbf 100644 --- a/src/other/index.jl +++ b/src/other/index.jl @@ -46,14 +46,23 @@ function names!(x::Index, nms::Vector{Symbol}; makeunique::Bool=false) end function rename!(x::Index, nms) - n = length(nms) - nfrom = length(unique(getindex.(nms, 1))) - nto = length(unique(getindex.(nms, 2))) - n > nfrom && throw(ArgumentError("Tried renaming a column multiple times.")) - n > nto && throw(ArgumentError("Tried renaming to a column multiple times.")) xbackup = copy(x) + processedfrom = Set(Symbol[]) + processedto = Set(Symbol[]) toholder = Dict{Symbol,Int}() for (from, to) in nms + if from ∈ processedfrom + copy!(x.lookup, xbackup.lookup) + x.names .= xbackup.names + throw(ArgumentError("Tried renaming a name multiple times.")) + end + if to ∈ processedto + copy!(x.lookup, xbackup.lookup) + x.names .= xbackup.names + throw(ArgumentError("Tried renaming to a name multiple times.")) + end + push!(processedfrom, from) + push!(processedto, to) from == to && continue # No change, nothing to do if !haskey(xbackup, from) copy!(x.lookup, xbackup.lookup) diff --git a/test/dataframe.jl b/test/dataframe.jl index fb8acc7557..764c04c9fb 100644 --- a/test/dataframe.jl +++ b/test/dataframe.jl @@ -1146,12 +1146,12 @@ end @test names(df) == [:A, :B, :C] @test_throws ArgumentError rename!(df, :A => :B) @test names(df) == [:A, :B, :C] - @test_throws ArgumentError rename!(df, :A => :B, :B => :A, :C => :B) - @test names(df) == [:A, :B, :C] - @test_throws ArgumentError rename!(df, :A => :X, :A => :Y) + @test_throws ArgumentError rename!(df, :A => :X, :A => :X) @test names(df) == [:A, :B, :C] @test_throws ArgumentError rename!(df, :A => :X, :B => :X) @test names(df) == [:A, :B, :C] + @test_throws ArgumentError rename!(df, :A => :B, :B => :A, :C => :B) + @test names(df) == [:A, :B, :C] @test_throws ArgumentError rename!(df, :A => :B, :B => :A, :A => :X) @test names(df) == [:A, :B, :C] end