Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
43 changes: 30 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,43 @@ DropMissing(cols::T...) where {T<:ColSelector} =

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

_ftrans(::DropMissing{Colon}, table) =
# ftrans
_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)
colnames = Tables.columnnames(table)
selnames = _filter(transform.colspec, colnames)
ftrans = _ftrans(transform, selnames)
newtable, fcache = apply(ftrans, table)
newtable, (ftrans, fcache)

# post-processing
coltable = Tables.columntable(newtable)
pcolumns = [nm ∈ selnames ? _nonmissing(x) : x for (nm, x) in pairs(coltable)]
𝒯 = (; zip(colnames, pcolumns)...)
ptable = 𝒯 |> Tables.materializer(newtable)

types = Tables.schema(table).types
ptable, (ftrans, fcache, types)
end

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

# pre-processing
colnames = Tables.columnnames(newtable)
coltable = Tables.columntable(newtable)
pcolumns = [collect(T, x) for (T, x) in zip(types, coltable)]
𝒯 = (; zip(colnames, pcolumns)...)
ptable = 𝒯 |> Tables.materializer(newtable)

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

# column eltype
ttypes = Tables.schema(t).types

T = DropMissing()
n, c = apply(T, t)
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
tₒ = revert(T, n, c)
@test ttypes == Tables.schema(tₒ).types

T = DropMissing([:a, :c, :d])
n, c = apply(T, t)
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}
tₒ = revert(T, n, c)
@test ttypes == Tables.schema(tₒ).types

T = DropMissing([:b, :e, :f])
n, c = apply(T, t)
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
tₒ = revert(T, n, c)
@test ttypes == Tables.schema(tₒ).types

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