Skip to content

Commit

Permalink
Some general maintenance (#303)
Browse files Browse the repository at this point in the history
* Import stack from Base starting with Julia 1.9

* Switch from DataAPI.All to Cols and use values to extract from kwargs
  • Loading branch information
andreasnoack authored Oct 13, 2023
1 parent 340dafe commit f83f0c7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"

[compat]
DataAPI = "1.0.1"
DataAPI = "1.3.0"
DataValues = "0.4.6"
InvertedIndices = "1"
IteratorInterfaceExtensions = "0.1.1, 1"
Expand Down
8 changes: 5 additions & 3 deletions src/IndexedTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Tables, TableTraits, IteratorInterfaceExtensions, TableTraitsUtils

import DataValues: DataValue, DataValueArray, isna

import DataAPI: Between, All
import DataAPI: Between, Cols

using InvertedIndices: Not
import StatsBase: transform
Expand All @@ -24,14 +24,16 @@ import Base:
permutedims, sort, sort!, iterate, pairs, reduce, push!, size, permute!, issorted,
sortperm, summary, resize!, vcat, append!, copyto!, view, tail,
tuple_type_cons, tuple_type_head, tuple_type_tail, in, convert

if VERSION >= v"1.9"
import Base: stack
end

#-----------------------------------------------------------------------# exports
export
# macros
@cols,
# types
AbstractNDSparse, All, ApplyColwise, Between, ColDict, Columns, IndexedTable,
AbstractNDSparse, ApplyColwise, Between, ColDict, Cols, Columns, IndexedTable,
Keys, NDSparse, NextTable, Not,
# functions
aggregate!, antijoin, asofjoin, collect_columns, colnames,
Expand Down
10 changes: 5 additions & 5 deletions src/columns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Base.@pure colnames(t::Columns) = fieldnames(eltype(t))
Base.@pure colnames(t::Columns{<:Pair}) = colnames(t.first) => colnames(t.second)

"""
columns(itr, select::Selection = All())
columns(itr, select::Selection = Cols())
Select one or more columns from an iterable of rows as a tuple of vectors.
Expand Down Expand Up @@ -152,15 +152,15 @@ Select the primary keys.
"""
struct Keys; end

const SpecialSelector = Union{Not, All, Keys, Between, Function, Regex, Type}
const SpecialSelector = Union{Not, Cols, Keys, Between, Function, Regex, Type}

hascolumns(t, s) = true
hascolumns(t, s::Symbol) = s in colnames(t)
hascolumns(t, s::Int) = s in 1:length(columns(t))
hascolumns(t, s::Tuple) = all(hascolumns(t, x) for x in s)
hascolumns(t, s::Not) = hascolumns(t, s.skip)
hascolumns(t, s::Between) = hascolumns(t, s.first) && hascolumns(t, s.last)
hascolumns(t, s::All) = all(hascolumns(t, x) for x in s.cols)
hascolumns(t, s::Cols) = all(hascolumns(t, x) for x in s.cols)
hascolumns(t, s::Type) = any(x -> eltype(x) <: s, columns(t))

lowerselection(t, s) = s
Expand All @@ -173,7 +173,7 @@ lowerselection(t, s::Function) = colindex(t, Tuple(filter(s, collect(c
lowerselection(t, s::Regex) = lowerselection(t, x -> occursin(s, string(x)))
lowerselection(t, s::Type) = Tuple(findall(x -> eltype(x) <: s, columns(t)))

function lowerselection(t, s::All)
function lowerselection(t, s::Cols)
s.cols == () && return lowerselection(t, valuenames(t))
ls = (isa(i, Tuple) ? i : (i,) for i in lowerselection(t, s.cols))
ls |> Iterators.flatten |> union |> Tuple
Expand Down Expand Up @@ -275,7 +275,7 @@ function colname(c, col)
end

"""
rows(itr, select = All())
rows(itr, select = Cols())
Select one or more fields from an iterable of rows as a vector of their values. Refer to
the [`select`](@ref) function for selection options and syntax.
Expand Down
6 changes: 3 additions & 3 deletions src/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ can be:
"""
function reduce(f, t::IndexedTable; select=valuenames(t), kws...)
if haskey(kws, :init)
return _reduce_select_init(f, t, select, kws.data.init)
return _reduce_select_init(f, t, select, values(kws).init)
end
_reduce_select(f, t, select)
end
Expand Down Expand Up @@ -313,7 +313,7 @@ function Base.reduce(f, x::NDSparse; kws...)
if haskey(kws, :select) || haskey(kws, :init)
throw(ArgumentError("select and init keyword arguments cannot be used with dims"))
end
dims = kws.data.dims
dims = values(kws).dims
if dims isa Symbol
dims = [dims]
end
Expand All @@ -325,7 +325,7 @@ function Base.reduce(f, x::NDSparse; kws...)
else
select = get(kws, :select, valuenames(x))
if haskey(kws, :init)
return _reduce_select_init(f, x, select, kws.data.init)
return _reduce_select_init(f, x, select, values(kws).init)
end
return _reduce_select(f, x, select)
end
Expand Down
6 changes: 3 additions & 3 deletions src/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ non-missing types. For example:
dropmissing(t)
dropmissing(t, (:t, :x))
"""
function dropmissing(t::IndexedTable, sel = All())
function dropmissing(t::IndexedTable, sel = Cols())
selection = lowerselection(t, sel)
indxs = missing_indxs(rows(t, selection))
t2 = subtable(t, indxs)
Expand All @@ -170,11 +170,11 @@ function dropmissing(t::IndexedTable, sel = All())
T = eltype(d[s])
d[s] = convert(Vector{missingtype2type(T)}, d[s])
end

table(d[], copy=false, perms=t.perms, presorted=true)
end

dropmissing(t::NDSparse, sel=All()) = ndsparse(dropmissing(table(t), sel))
dropmissing(t::NDSparse, sel=Cols()) = ndsparse(dropmissing(table(t), sel))

Base.@deprecate_binding dropna dropmissing

Expand Down
32 changes: 16 additions & 16 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ let a = rand(5,5,5)
end

for a in (rand(2,2), rand(3,5))
nd = convert(NDSparse, a)
local nd = convert(NDSparse, a)
@test nd == convert(NDSparse, sparse(a))
for (I,d) in zip(nd.index, nd.data)
@test a[I...] == d
Expand Down Expand Up @@ -567,7 +567,7 @@ end
t = table([0.01, 0.05], [2, 1], [3, 4], names=[:t, :x, :y], pkey=(:t,:x))
@test select(t, Not(:t)) == table([1, 2], [4,3], names=Symbol[:x, :y])
@test select(t, Not(ncols(t))) == table([0.01, 0.05], [2, 1], names=Symbol[:t, :x])
@test select(t, Not(All(:x, :y))) == table([0.01, 0.05], names=Symbol[:t]) == select(t, Not((:x, :y)))
@test select(t, Not(Cols(:x, :y))) == table([0.01, 0.05], names=Symbol[:t]) == select(t, Not((:x, :y)))
@test transform(t, :z => [1 // 2, 3 // 4]) == table([0.01, 0.05], [2, 1], [3, 4], [1//2, 3//4], names=Symbol[:t, :x, :y, :z])

# 99
Expand Down Expand Up @@ -648,7 +648,7 @@ end

t3 = map(x->ntuple(identity, x.x), t)
@test isa(t3.data, Vector)
@test eltype(t3.data) == Tuple{Int,Int,Int,Int,Vararg{Int,N} where N}
@test eltype(t3.data) == Tuple{Int,Int,Int,Int,Vararg{Int}}

y = [1, 1//2, "x"]
function foo(x)
Expand Down Expand Up @@ -795,9 +795,9 @@ end
@test select(t, Keys()) == select(t, (:x,))
@test select(t, (Keys(), :y)) == select(t, ((:x,), :y))
@test select(t, Not(Keys())) == select(t, Not(:x)) == select(t, (:y, :z))
@test select(t, Not(All(Keys(), :y))) == select(t, Not(All(:x, :y))) == select(t, (:z,))
@test select(t, All(Keys(), :y)) == select(t, (:x, :y))
@test select(t, All()) == t
@test select(t, Not(Cols(Keys(), :y))) == select(t, Not(Cols(:x, :y))) == select(t, (:z,))
@test select(t, Cols(Keys(), :y)) == select(t, (:x, :y))
@test select(t, Cols()) == t
@test select(t, Between(:x, :z)) == select(t, (:x, :y, :z))
@test select(t, i -> i == :y) == select(t, (:y,))
@test select(t, r"x|z") == select(t, (:x, :z))
Expand All @@ -807,19 +807,19 @@ end
@test rows(t, Keys()) == rows(t, (:x,))
@test rows(t, (Keys(), :y)) == rows(t, ((:x,), :y))
@test rows(t, Not(Keys())) == rows(t, Not(:x)) == rows(t, (:y, :z))
@test rows(t, Not(All(Keys(), :y))) == rows(t, Not(All(:x, :y))) == rows(t, (:z,))
@test rows(t, All(Keys(), :y)) == rows(t, (:x, :y))
@test rows(t, All()) == rows(t)
@test rows(t, Not(Cols(Keys(), :y))) == rows(t, Not(Cols(:x, :y))) == rows(t, (:z,))
@test rows(t, Cols(Keys(), :y)) == rows(t, (:x, :y))
@test rows(t, Cols()) == rows(t)
@test rows(t, Between(:x, :z)) == rows(t, (:x, :y, :z))
@test rows(t, i -> i == :y) == rows(t, (:y,))
@test rows(t, r"x|z") == rows(t, (:x, :z))

@test columns(t, Keys()) == columns(t, (:x,))
@test columns(t, (Keys(), :y)) == columns(t, ((:x,), :y))
@test columns(t, Not(Keys())) == columns(t, Not(:x)) == columns(t, (:y, :z))
@test columns(t, Not(All(Keys(), :y))) == columns(t, Not(All(:x, :y))) == columns(t, (:z,))
@test columns(t, All(Keys(), :y)) == columns(t, (:x, :y))
@test columns(t, All()) == columns(t)
@test columns(t, Not(Cols(Keys(), :y))) == columns(t, Not(Cols(:x, :y))) == columns(t, (:z,))
@test columns(t, Cols(Keys(), :y)) == columns(t, (:x, :y))
@test columns(t, Cols()) == columns(t)
@test columns(t, Between(:x, :z)) == columns(t, (:x, :y, :z))
@test columns(t, i -> i == :y) == columns(t, (:y,))
@test columns(t, r"x|z") == columns(t, (:x, :z))
Expand All @@ -832,9 +832,9 @@ end
@test hascolumns(t, Keys())
@test hascolumns(t, (Keys(), :y))
@test hascolumns(t, Not(Keys()))
@test !hascolumns(t, Not(All(Keys(), :xx)))
@test hascolumns(t, All(Keys(), :y))
@test hascolumns(t, All())
@test !hascolumns(t, Not(Cols(Keys(), :xx)))
@test hascolumns(t, Cols(Keys(), :y))
@test hascolumns(t, Cols())
@test hascolumns(t, Between(:x, :z))
@test !hascolumns(t, Between(:x, :xx))
@test hascolumns(t, i -> i == :y)
Expand Down Expand Up @@ -959,7 +959,7 @@ using OnlineStats
@test groupreduce(+, t, :x, select=:z) == table([1, 2], [6, 15], names=Symbol[:x, :+])
@test groupreduce(+, t, (:x, :y), select=:z) == table([1, 1, 2, 2], [1, 2, 1, 2], [3, 3, 11, 4], names=Symbol[:x, :y, :+])
@test groupreduce((+, min, max), t, (:x, :y), select=:z) == table([1, 1, 2, 2], [1, 2, 1, 2], [3, 3, 11, 4], [1, 3, 5, 4], [2, 3, 6, 4], names=Symbol[:x, :y, :+, :min, :max])
@test groupreduce((+, min, max), t, All(:x, :y), select=:z) == groupreduce((+, min, max), t, (:x, :y), select=:z)
@test groupreduce((+, min, max), t, Cols(:x, :y), select=:z) == groupreduce((+, min, max), t, (:x, :y), select=:z)
@test groupreduce((zsum = (+), zmin = min, zmax = max), t, (:x, :y), select=:z) == table([1, 1, 2, 2], [1, 2, 1, 2], [3, 3, 11, 4], [1, 3, 5, 4], [2, 3, 6, 4], names=Symbol[:x, :y, :zsum, :zmin, :zmax])
@test groupreduce((xsum = :z => +, negysum = (:y => -) => +), t, :x) == table([1, 2], [6, 15], [-4, -4], names=Symbol[:x, :xsum, :negysum])
t = NDSparse([1, 1, 1, 1, 2, 2],
Expand Down

0 comments on commit f83f0c7

Please sign in to comment.