Skip to content

Commit

Permalink
Add NarrowTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Jun 11, 2022
1 parent ce4437c commit 1ff0510
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/TableTransforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export
Coerce,
Levels,
OneHot,
NarrowTypes,
Identity,
Center,
Scale,
Expand Down
1 change: 1 addition & 0 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ include("transforms/coalesce.jl")
include("transforms/coerce.jl")
include("transforms/levels.jl")
include("transforms/onehot.jl")
include("transforms/narrowtypes.jl")
include("transforms/identity.jl")
include("transforms/center.jl")
include("transforms/scale.jl")
Expand Down
18 changes: 18 additions & 0 deletions src/transforms/narrowtypes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

"""
NarrowTypes()
Converts the element type of columns with generic types to more specific types.
"""
struct NarrowTypes <: Colwise end

isrevertible(::Type{NarrowTypes}) = true

colcache(::NarrowTypes, x) = eltype(x)

colapply(::NarrowTypes, x, c) = identity.(x)

colrevert(::NarrowTypes, y, c) = collect(c, y)
18 changes: 18 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,24 @@
@test_throws AssertionError apply(OneHot("c"), t)
end

@testset "NarrowTypes" begin
a = Integer[4, 6, 5, 9, 3, 1]
b = Number[1, 0, 1, true, false, true]
c = Any[5, 6, 7, 1.5, 1.6, 1.7]
t = Table(; a, b, c)

T = NarrowTypes()
n, c = apply(T, t)
@test eltype(n.a) == Int
@test eltype(n.b) == Integer
@test eltype(n.c) == Real
tₒ = revert(T, n, c)
@test eltype(tₒ.a) == Integer
@test eltype(tₒ.b) == Number
@test eltype(tₒ.c) == Any
@test t == tₒ
end

@testset "Identity" begin
x = rand(4000)
y = rand(4000)
Expand Down

0 comments on commit 1ff0510

Please sign in to comment.