1
module DataTables
2

3
import TableShowUtils, TableTraitsUtils, ReadOnlyArrays
4

5
using DataValues
6

7
export DataTable, NA, isna
8

9
struct DataTable{T,TCOLS} <: AbstractVector{T}
10 25
    columns::TCOLS
11
end
12

13
function fromNT(nt)
14 25
    nt = map(i->i isa ReadOnlyArrays.ReadOnlyArray ? i : ReadOnlyArrays.ReadOnlyArray(i), nt)
15
    tx = typeof(nt)
16 25
    et = NamedTuple{propertynames(nt),Tuple{(eltype(fieldtype(tx, i)) for i in 1:fieldcount(typeof(nt)))...}}
17 25
    return DataTable{et,typeof(nt)}(nt)
18
end
19

20 0
swap_dva_in(A) = A
21 0
swap_dva_in(A::Array{<:DataValue}) = DataValueArray(A)
22

23
function DataTable(;cols...)
24 25
    return fromNT(map(col->swap_dva_in(col), values(cols)))
25
end
26

27
function DataTable(table)
28 25
    cols, colnames = TableTraitsUtils.create_columns_from_iterabletable(table)
29

30 25
    return fromNT(NamedTuple{tuple(colnames...)}(tuple(cols...)))
31
end
32

33 25
columns(dt::DataTable) = getfield(dt, :columns)
34

35 25
@inline Base.getproperty(dt::DataTable, name::Symbol) = getproperty(columns(dt), name)
36

37 20
Base.size(dt::DataTable) = size(columns(dt)[1])
38

39 25
Base.IndexStyle(::Type{T}) where {T <: DataTable} = Base.IndexLinear()
40

41
function Base.checkbounds(::Type{Bool}, dt::DataTable, i)
42 20
    cols = columns(dt)
43 0
    if length(cols) == 0
44 0
        return true
45
    else
46 20
        return checkbounds(Bool, cols[1], i)
47
    end
48
end
49

50
@inline function Base.getindex(dt::DataTable{T}, i::Int) where {T}
51 25
    @boundscheck checkbounds(dt, i)
52 25
    return map(col->@inbounds(getindex(col, i)), columns(dt))
53
end
54

55
function Base.show(io::IO, dt::DataTable)
56 25
    TableShowUtils.printtable(io, dt, "DataTable")
57
end
58

59
function Base.show(io::IO, ::MIME"text/plain", dt::DataTable)
60 25
    TableShowUtils.printtable(io, dt, "DataTable")
61
end
62

63
function Base.show(io::IO, ::MIME"text/html", dt::DataTable)
64 25
    TableShowUtils.printHTMLtable(io, dt)
65
end
66

67 25
Base.showable(::MIME"text/html", dt::DataTable) = true
68

69
function Base.show(io::IO, ::MIME"application/vnd.dataresource+json", dt::DataTable)
70 25
    TableShowUtils.printdataresource(io, dt)
71
end
72

73 25
Base.showable(::MIME"application/vnd.dataresource+json", dt::DataTable) = true
74

75
end

Read our documentation on viewing source code .

Loading