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

Switch renamecol to rename and insertcol to insertcols, add multiple column version #241

Merged
merged 5 commits into from
Apr 16, 2019
Merged
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
6 changes: 3 additions & 3 deletions src/IndexedTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export
# functions
aggregate!, antijoin, asofjoin, collect_columns, colnames,
column, columns, convertdim, dimlabels, flatten, flush!, groupby, groupjoin,
groupreduce, innerjoin, insertafter!, insertbefore!, insertcol, insertcolafter,
insertcolbefore, leftgroupjoin, leftjoin, map_rows, naturalgroupjoin, naturaljoin,
groupreduce, innerjoin, insertafter!, insertbefore!, insertcols, insertcolsafter,
insertcolsbefore, leftgroupjoin, leftjoin, map_rows, naturalgroupjoin, naturaljoin,
ncols, ndsparse, outergroupjoin, outerjoin, pkeynames, pkeys,
reducedim_vec, reindex, renamecol, rows, select, selectkeys, selectvalues,
reducedim_vec, reindex, rename, rows, select, selectkeys, selectvalues,
stack, summarize, table, transform, unstack, update!, where, dropmissing, dropna

const Tup = Union{Tuple,NamedTuple}
Expand Down
67 changes: 42 additions & 25 deletions src/columns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function Base.haskey(d::ColDict, key)
_colindex(d.names, key, 0) != 0
end

function Base.insert!(d::ColDict, index, key, col)
function Base.insert!(d::ColDict, index::Integer, (key, col)::Pair)
if haskey(d, key)
error("Key $key already exists. Use dict[key] = col instead of inserting.")
else
Expand All @@ -438,20 +438,29 @@ function Base.insert!(d::ColDict, index, key, col)
end
end

function insertafter!(d::ColDict, i, key, col)
function Base.insert!(d::ColDict, index::Integer, newcols)
for new::Pair in newcols
insert!(d, index, new)
index += 1
end
end

Base.insert!(d::ColDict, index::Integer, newcols::Pair...) = insert!(d, index, newcols)

function insertafter!(d::ColDict, i, args...)
k = _colindex(d.names, i, 0)
if k == 0
error("$i not found. Cannot insert column after $i")
end
insert!(d, k+1, key, col)
insert!(d, k+1, args...)
end

function insertbefore!(d::ColDict, i, key, col)
function insertbefore!(d::ColDict, i, args...)
k = _colindex(d.names, i, 0)
if k == 0
error("$i not found. Cannot insert column after $i")
end
insert!(d, k, key, col)
insert!(d, k, args...)
end

function rename!(d::ColDict, (col, newname)::Pair)
Expand Down Expand Up @@ -533,56 +542,64 @@ transform(t, args...) = @cols transform!(t, args...)
@deprecate popcol(t) select(t, Not(ncols(t)))

"""
insertcol(t, position::Integer, name, x)
insertcols(t, position::Integer, map::Pair...)

Insert a column `x` named `name` at `position`. Returns a new table.
For each pair `name => col` in `map`, insert a column `col` named `name` starting at `position`.
Returns a new table.

# Example

t = table([0.01, 0.05], [2,1], [3,4], names=[:t, :x, :y], pkey=:t)
insertcol(t, 2, :w, [0,1])
insertcol(t, 2, :w => [0,1])
"""
insertcol(t, i::Integer, name, x) = @cols insert!(t, i, name, x)
insertcols(t, i::Integer, args...) = @cols insert!(t, i, args...)

@deprecate insertcol(t, i, name, x) insertcols(t, i, name => x)

"""
insertcolafter(t, after, name, col)
insertcolsafter(t, after, map::Pair...)

Insert a column `col` named `name` after `after`. Returns a new table.
For each pair `name => col` in `map`, insert a column `col` named `name` after `after`.
Returns a new table.

# Example

t = table([0.01, 0.05], [2,1], [3,4], names=[:t, :x, :y], pkey=:t)
insertcolafter(t, :t, :w, [0,1])
insertcolsafter(t, :t, :w => [0,1])
"""
insertcolafter(t, after, name, x) = @cols insertafter!(t, after, name, x)
insertcolsafter(t, after, args...) = @cols insertafter!(t, after, args...)

@deprecate insertcolafter(t, i, name, x) insertcolsafter(t, i, name => x)

"""
insertcolbefore(t, before, name, col)
insertcolsbefore(t, before, map::Pair...)

Insert a column `col` named `name` before `before`. Returns a new table.
For each pair `name => col` in `map`, insert a column `col` named `name` before `before`.
Returns a new table.

# Example

t = table([0.01, 0.05], [2,1], [3,4], names=[:t, :x, :y], pkey=:t)
insertcolbefore(t, :x, :w, [0,1])
"""
insertcolbefore(t, before, name, x) = @cols insertbefore!(t, before, name, x)

insertcolsbefore(t, :x, :w => [0,1])
"""
renamecol(t, col, newname)
insertcolsbefore(t, before, args...) = @cols insertbefore!(t, before, args...)

Set `newname` as the new name for column `col` in `t`. Returns a new table.
@deprecate insertcolbefore(t, i, name, x) insertcolsbefore(t, i, name => x)

renamecol(t, map::Pair...)
"""
rename(t, map::Pair...)

Rename multiple columns at a time.
For each pair `col => newname` in `map`, set `newname` as the new name for column `col` in `t`.
Returns a new table.

# Example

t = table([0.01, 0.05], [2,1], names=[:t, :x])
renamecol(t, :t, :time)
rename(t, :t => :time)
"""
renamecol(t, args...) = @cols rename!(t, args...)
rename(t, args...) = @cols rename!(t, args...)

@deprecate renamecol(t, args...) rename(t, args...)

## Utilities for mapping and reduction with many functions / OnlineStats

Expand Down
4 changes: 2 additions & 2 deletions src/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ the data.
`name` optionally specifies a new name for the translated dimension.
"""
function convertdim(x::NDSparse, d::DimName, xlat; agg=nothing, vecagg=nothing, name=nothing, select=valuenames(x))
ks = setcol(pkeys(x), d, d=>xlat)
ks = transform(pkeys(x), d => d => xlat)
if name !== nothing
ks = renamecol(ks, d, name)
ks = rename(ks, d => name)
end

if vecagg !== nothing
Expand Down
34 changes: 24 additions & 10 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -596,18 +596,32 @@ end
@test t2 == table([4,5,6], names=[:y])

t = table([0.01, 0.05], [2, 1], [3, 4], names=[:t, :x, :y], pkey=:t)
@test insertcol(t, 2, :w, [0, 1]) == table([0.01, 0.05], [0, 1], [2, 1], [3, 4], names=Symbol[:t, :w, :x, :y])
@test insertcols(t, 2, :w => [0, 1]) == table([0.01, 0.05], [0, 1], [2, 1], [3, 4], names=Symbol[:t, :w, :x, :y])
t = table([0.01, 0.05], [2, 1], [3, 4], names=[:t, :x, :y], pkey=:t)
@test insertcolafter(t, :t, :w, [0, 1]) == table([0.01, 0.05], [0, 1], [2, 1], [3, 4], names=Symbol[:t, :w, :x, :y])
@test insertcolsafter(t, :t, :w => [0, 1]) == table([0.01, 0.05], [0, 1], [2, 1], [3, 4], names=Symbol[:t, :w, :x, :y])
t = table([0.01, 0.05], [2, 1], [3, 4], names=[:t, :x, :y], pkey=:t)
@test insertcolbefore(t, :x, :w, [0, 1]) == table([0.01, 0.05], [0, 1], [2, 1], [3, 4], names=Symbol[:t, :w, :x, :y])
@test insertcolsbefore(t, :x, :w => [0, 1]) == table([0.01, 0.05], [0, 1], [2, 1], [3, 4], names=Symbol[:t, :w, :x, :y])

t = table([0.01, 0.05], [2, 1], [3, 4], names=[:t, :x, :y], pkey=:t)
@test insertcols(t, 2, :w => [0, 1], :z => [2, 3]) ==
table([0.01, 0.05], [0, 1], [2, 3], [2, 1], [3, 4], names=Symbol[:t, :w, :z, :x, :y]) ==
insertcols(t, 2, :w => [0, 1], :z => [2, 3])
t = table([0.01, 0.05], [2, 1], [3, 4], names=[:t, :x, :y], pkey=:t)
@test insertcolsafter(t, :t, :w => [0, 1], :z => [2, 3]) ==
table([0.01, 0.05], [0, 1], [2, 3], [2, 1], [3, 4], names=Symbol[:t, :w, :z, :x, :y]) ==
insertcolsafter(t, :t, (:w => [0, 1], :z => [2, 3]))
t = table([0.01, 0.05], [2, 1], [3, 4], names=[:t, :x, :y], pkey=:t)
@test insertcolsbefore(t, :x, :w => [0, 1], :z => [2, 3]) ==
table([0.01, 0.05], [0, 1], [2, 3], [2, 1], [3, 4], names=Symbol[:t, :w, :z, :x, :y]) ==
insertcolsbefore(t, :x, (:w => [0, 1], :z => [2, 3]))

t = table([0.01, 0.05], [2, 1], names=[:t, :x])
@test renamecol(t, :t => :time) == table([0.01, 0.05], [2, 1], names=Symbol[:time, :x])
@test_throws ErrorException renamecol(t, :tt => :time)
@test renamecol(t, :t => :time) == renamecol(t, :t => :time)
@test renamecol(t, :t => :time, :x => :position) ==
@test rename(t, :t => :time) == table([0.01, 0.05], [2, 1], names=Symbol[:time, :x])
@test_throws ErrorException rename(t, :tt => :time)
@test rename(t, :t => :time) == rename(t, :t => :time)
@test rename(t, :t => :time, :x => :position) ==
table([0.01, 0.05], [2, 1], names=Symbol[:time, :position]) ==
renamecol(t, (:t => :time, :x => :position))
rename(t, (:t => :time, :x => :position))
end

@testset "map" begin
Expand Down Expand Up @@ -931,7 +945,7 @@ using OnlineStats
b = table(Columns(a=[1, 1, 2], b=[3, 2, 2], c=[4, 5, 2]), pkey=(1,2))

@test groupreduce(min, a, select=3) == a
@test groupreduce(min, b, select=3) == renamecol(b, :c => :min)
@test groupreduce(min, b, select=3) == rename(b, :c => :min)
@test_throws ArgumentError groupreduce(+, b, [:x, :y]) # issue JuliaDB.jl#100
t = table([1, 1, 1, 2, 2, 2], [1, 1, 2, 2, 1, 1], [1, 2, 3, 4, 5, 6], names=[:x, :y, :z], pkey=(:x, :y))
@test groupreduce(+, t, :x, select=:z) == table([1, 2], [6, 15], names=Symbol[:x, :+])
Expand Down Expand Up @@ -1178,7 +1192,7 @@ end
@test groupby((:normy => x->Iterators.repeated(mean(x), length(x)),),
t, :x, select=:y, flatten=true) == table([1,1,2,2], [3.5,3.5,5.5,5.5], names=[:x, :normy])
t=table([1,1,1,2,2,2], [1,1,2,1,1,2], [1,2,3,4,5,6], names=[:x,:y,:z], pkey=[1,2]);
@test groupby(identity, t, (:x, :y), select=:z, flatten = true) == renamecol(t, :z => :identity)
@test groupby(identity, t, (:x, :y), select=:z, flatten = true) == rename(t, :z => :identity)
@test groupby(identity, t, (:x, :y), select=:z, flatten = true).pkey == [1,2]
# If return type is non iterable, return the same as non flattened
@test groupby(i -> (y = :y,), t, :x, flatten=true) == groupby(i -> (y = :y,), t, :x, flatten=false)
Expand Down