Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
48 changes: 35 additions & 13 deletions src/transforms/filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ function revert(::Filter, newtable, cache)
rows |> Tables.materializer(newtable)
end

# DropMissing

const VecOrTuple{T} = Union{Vector{T}, NTuple{N, T}} where {T, N}

"""
DropMissing()
DropMissing(:)
Expand Down Expand Up @@ -69,22 +65,48 @@ DropMissing(cols::T...) where {T<:ColSelector} =

isrevertible(::Type{<:DropMissing}) = true

_ftrans(::DropMissing{Colon}, table) =
_ftrans(::DropMissing{Colon}, cols) =
Filter(row -> all(!ismissing, row))

function _ftrans(transform::DropMissing, table)
allcols = Tables.columnnames(table)
cols = _filter(transform.colspec, allcols)
_ftrans(::DropMissing, cols) =
Filter(row -> all(!ismissing, getindex.(Ref(row), cols)))
end

# nonmissing
_nonmissing(::Type{T}, x) where {T} = x
_nonmissing(::Type{Union{Missing,T}}, x) where {T} = collect(T, x)
_nonmissing(x) = _nonmissing(eltype(x), x)

function apply(transform::DropMissing, table)
ftrans = _ftrans(transform, table)
names = Tables.columnnames(table)
coltypes = Tables.schema(table).types
selnames = _filter(transform.colspec, names)
ftrans = _ftrans(transform, selnames)
newtable, fcache = apply(ftrans, table)
newtable, (ftrans, fcache)

# post-processing
cols = Tables.columns(newtable)
pcols = map(names) do n
x = Tables.getcolumn(cols, n)
n ∈ selnames ? _nonmissing(x) : x
end
𝒯 = (; zip(names, pcols)...)
ptable = 𝒯 |> Tables.materializer(newtable)

ptable, (ftrans, fcache, coltypes)
end

function revert(::DropMissing, newtable, cache)
ftrans, fcache = cache
revert(ftrans, newtable, fcache)
ftrans, fcache, coltypes = cache

# pre-processing
cols = Tables.columns(newtable)
names = Tables.columnnames(newtable)
pcols = map(zip(coltypes, names)) do (T, n)
x = Tables.getcolumn(cols, n)
collect(T, x)
end
𝒯 = (; zip(names, pcols)...)
ptable = 𝒯 |> Tables.materializer(newtable)

revert(ftrans, ptable, fcache)
end
38 changes: 38 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,44 @@
@test isequalmissing(n.e, [missing, 5, 6, 5])
@test isequalmissing(n.f, [4, missing, 4, 5])

# table schema after apply and revert
T = DropMissing()
n, c = apply(T, t)
tₒ = revert(T, n, c)
ttypes = Tables.schema(t).types
ntypes = Tables.schema(n).types
@test ntypes[1] == Int
@test ntypes[2] == Int
@test ntypes[3] == Int
@test ntypes[4] == Int
@test ntypes[5] == Int
@test ntypes[6] == Int
@test ttypes == Tables.schema(tₒ).types

T = DropMissing([:a, :c, :d])
n, c = apply(T, t)
tₒ = revert(T, n, c)
ntypes = Tables.schema(n).types
@test ntypes[1] == Int
@test ntypes[2] == Union{Missing,Int}
@test ntypes[3] == Int
@test ntypes[4] == Int
@test ntypes[5] == Union{Missing,Int}
@test ntypes[6] == Union{Missing,Int}
@test ttypes == Tables.schema(tₒ).types

T = DropMissing([:b, :e, :f])
n, c = apply(T, t)
tₒ = revert(T, n, c)
ntypes = Tables.schema(n).types
@test ntypes[1] == Union{Missing,Int}
@test ntypes[2] == Int
@test ntypes[3] == Union{Missing,Int}
@test ntypes[4] == Union{Missing,Int}
@test ntypes[5] == Int
@test ntypes[6] == Int
@test ttypes == Tables.schema(tₒ).types

# reapply test
T = DropMissing()
n1, c1 = apply(T, t)
Expand Down