How to get type of a column using Tables.Schema from Tables.jl?

Hello,

I have a TSFrame like so

julia> table
50×5 TSFrame with Date Index
 Index       Open     High     Low      Close    Volume
 Date        Float64  Float64  Float64  Float64  Float64
─────────────────────────────────────────────────────────
 2020-01-01    10.81    11.02     9.9     10.5     55.03
 2020-01-02    10.58    10.74     9.78     9.78   117.86
 2020-01-03    10.07    10.65     9.5     10.46   301.04
 2020-01-04    10.58    11.05    10.47    10.51   157.94
 2020-01-05    10.56    10.7     10.26    10.55    39.96
 2020-01-06    10.4     10.73    10.4     10.72    42.87
 2020-01-07    10.74    11.16    10.12    10.16   191.95
 2020-01-08    10.16    10.86     9.91    10.25    55.09
 2020-01-09    10.29    10.29     9.4      9.4    131.58
 2020-01-10     9.4     10.8      9.11     9.5    249.69
 2020-01-11     9.62     9.62     9.12     9.23    77.75
 2020-01-12     9.35     9.35     8.5      8.5    197.33
 2020-01-13     8.64     9.43     8.55     8.8    107.93
 2020-01-14     8.8      8.91     8.21     8.33    35.86
 2020-01-15     8.31     8.84     7.34     7.53   269.05
 2020-01-16     7.56     7.82     7.53     7.61    34.18
 2020-01-17     7.61     7.61     6.5      6.78   209.1
 2020-01-18     7.04     8.84     7.04     8.6    241.95
 2020-01-19     8.56     9.42     8.15     9.21   162.86
 2020-01-20     9.26     9.5      8.72     8.95   112.99
 2020-01-21     8.95     9.29     8.6      9.22    66.53
 2020-01-22     9.31     9.4      8.89     9.1     87.5
 2020-01-23     9.1      9.1      8.14     8.31   349.14
 2020-01-24     8.51     8.51     8.24     8.37    44.38
 2020-01-25     8.42     8.95     8.06     8.3     45.79
 2020-01-26     8.3      8.7      7.7      7.78   139.4
 2020-01-27     7.87     8.95     7.87     8.05    46.49
 2020-01-28     7.94     8.75     7.94     8.1     27.45
 2020-01-29     8.1      8.39     8.0      8.08    16.44
 2020-01-30     8.08     8.28     7.37     7.49    83.54
 2020-01-31     7.49     7.58     7.49     7.58    15.08
 2020-02-01     7.4      8.17     7.38     8.17    60.72
 2020-02-02     8.09     8.83     8.05     8.83   140.22
 2020-02-03     8.86     9.2      8.79     8.91   171.6
 2020-02-04     8.81     9.25     8.67     9.2    209.26
 2020-02-05     9.16    10.1      9.16     9.76   199.2
 2020-02-06     9.69     9.88     8.9      9.42   165.77
 2020-02-07     9.45     9.65     9.17     9.3     61.71
 2020-02-08     9.18     9.32     8.6      9.32    29.73
 2020-02-09     9.4      9.4      8.92     9.04    12.93
 2020-02-10     9.0      9.01     8.99     9.0      4.14
 2020-02-11     9.11     9.36     9.11     9.33    12.45
 2020-02-12     9.23     9.46     9.11     9.34    42.23
 2020-02-13     9.34     9.34     8.43     8.49   133.29
 2020-02-14     8.49     9.4      8.42     9.21   120.02
 2020-02-15     9.3     10.5      9.26    10.15   255.3
 2020-02-16    10.23    10.3     10.0     10.3    111.55
 2020-02-17    10.29    10.86    10.19    10.59   108.27
 2020-02-18    10.77    10.77    10.15    10.23    48.29
 2020-02-19    10.28    10.39     9.62    10.0     81.66

I get the Tables.Schema using

julia> sch = Tables.schema(table)
Tables.Schema:
 :Index   Date
 :Open    Float64
 :High    Float64
 :Low     Float64
 :Close   Float64
 :Volume  Float64

julia> sch.
names
types
julia> sch.names
(:Index, :Open, :High, :Low, :Close, :Volume)

julia> sch.types
(Date, Float64, Float64, Float64, Float64, Float64)

julia> fieldnames(typeof(sch))
(:storednames, :storedtypes)

julia> methodswith(typeof(sch))
0-element Vector{Method}

I wonder what is the correct manner to get type of column :Close (if this column exists).

Kind regards

Just as you do. Quoting the documentation:

To access the names, one can simply call sch.names to return a collection of Symbols (Tuple or Vector). To access column element types, one can similarly call sch.types, which will return a collection of
types (like (Int64, Float64, String)).

sch[sch.names.==:close,:types]

not tested

or

eltype(table.close)
sch[sch.names.==:Close,:types]

doesn’t work. It raises

ERROR: MethodError: no method matching getindex(::Tables.Schema{(:Index, :Open, :High, :Low, :Close, :Volume), Tuple{…}}, ::NTuple{6, Bool}, ::Symbol)
Stacktrace:
 [1] top-level scope
   @ REPL[28]:1
Some type information was truncated. Use `show(err)` to see complete types.
eltype(table.Close)

works only is table is a TSFrame or DataFrame. Not if it’s an other sort of table compatible with Tables.jl such as RT_OHLCV or CT_OHLCV with

const OPEN_TMPL = [
    10.81,
    10.58,
    10.07,
    10.58,
    10.56,
    10.4,
    10.74,
    10.16,
    10.29,
    9.4,
    9.62,
    9.35,
    8.64,
    8.8,
    8.31,
    7.56,
    7.61,
    7.04,
    8.56,
    9.26,
    8.95,
    9.31,
    9.1,
    8.51,
    8.42,
    8.3,
    7.87,
    7.94,
    8.1,
    8.08,
    7.49,
    7.4,
    8.09,
    8.86,
    8.81,
    9.16,
    9.69,
    9.45,
    9.18,
    9.4,
    9.0,
    9.11,
    9.23,
    9.34,
    8.49,
    9.3,
    10.23,
    10.29,
    10.77,
    10.28,
]
const HIGH_TMPL = [
    11.02,
    10.74,
    10.65,
    11.05,
    10.7,
    10.73,
    11.16,
    10.86,
    10.29,
    10.8,
    9.62,
    9.35,
    9.43,
    8.91,
    8.84,
    7.82,
    7.61,
    8.84,
    9.42,
    9.5,
    9.29,
    9.4,
    9.1,
    8.51,
    8.95,
    8.7,
    8.95,
    8.75,
    8.39,
    8.28,
    7.58,
    8.17,
    8.83,
    9.2,
    9.25,
    10.1,
    9.88,
    9.65,
    9.32,
    9.4,
    9.01,
    9.36,
    9.46,
    9.34,
    9.4,
    10.5,
    10.3,
    10.86,
    10.77,
    10.39,
]
const LOW_TMPL = [
    9.9,
    9.78,
    9.5,
    10.47,
    10.26,
    10.4,
    10.12,
    9.91,
    9.4,
    9.11,
    9.12,
    8.5,
    8.55,
    8.21,
    7.34,
    7.53,
    6.5,
    7.04,
    8.15,
    8.72,
    8.6,
    8.89,
    8.14,
    8.24,
    8.06,
    7.7,
    7.87,
    7.94,
    8.0,
    7.37,
    7.49,
    7.38,
    8.05,
    8.79,
    8.67,
    9.16,
    8.9,
    9.17,
    8.6,
    8.92,
    8.99,
    9.11,
    9.11,
    8.43,
    8.42,
    9.26,
    10.0,
    10.19,
    10.15,
    9.62,
]
const CLOSE_TMPL = [
    10.5,
    9.78,
    10.46,
    10.51,
    10.55,
    10.72,
    10.16,
    10.25,
    9.4,
    9.5,
    9.23,
    8.5,
    8.8,
    8.33,
    7.53,
    7.61,
    6.78,
    8.6,
    9.21,
    8.95,
    9.22,
    9.1,
    8.31,
    8.37,
    8.3,
    7.78,
    8.05,
    8.1,
    8.08,
    7.49,
    7.58,
    8.17,
    8.83,
    8.91,
    9.2,
    9.76,
    9.42,
    9.3,
    9.32,
    9.04,
    9.0,
    9.33,
    9.34,
    8.49,
    9.21,
    10.15,
    10.3,
    10.59,
    10.23,
    10.0,
]

const VOLUME_TMPL = [
    55.03,
    117.86,
    301.04,
    157.94,
    39.96,
    42.87,
    191.95,
    55.09,
    131.58,
    249.69,
    77.75,
    197.33,
    107.93,
    35.86,
    269.05,
    34.18,
    209.1,
    241.95,
    162.86,
    112.99,
    66.53,
    87.5,
    349.14,
    44.38,
    45.79,
    139.4,
    46.49,
    27.45,
    16.44,
    83.54,
    15.08,
    60.72,
    140.22,
    171.6,
    209.26,
    199.2,
    165.77,
    61.71,
    29.73,
    12.93,
    4.14,
    12.45,
    42.23,
    133.29,
    120.02,
    255.3,
    111.55,
    108.27,
    48.29,
    81.66,
]

const CLOSE_EQUAL_VALUES_TMPL = repeat([10.46], length(CLOSE_TMPL))

using Dates
const DATE_TMPL =
    collect(range(Dates.Date(2020, 1, 1), step = Dates.Day(1), length = length(CLOSE_TMPL)))

const CT_OHLCV = (Index=DATE_TMPL, Open=OPEN_TMPL, High=HIGH_TMPL, Low=LOW_TMPL, Close=CLOSE_TMPL, Volume=VOLUME_TMPL)

const RT_OHLCV = [(Index=DATE_TMPL[i], Open=OPEN_TMPL[i], High=HIGH_TMPL[i], Low=LOW_TMPL[i], Close=CLOSE_TMPL[i], Volume=VOLUME_TMPL[i]) for i in 1:length(CLOSE_TMPL)]

I think I have a solution using…

julia> sch.types[argmax((sch.names.==:Close))]
Float64

But if you have a better one please tell me.

I think this is the way you are looking for:

Tables.columntype(sch, :Close)

If used a lot, then:

import Tables: columntype

can remove the need for a Module prefix.

Thanks @Dan and all

sch.types[findfirst(sch.names.==:Close)]
Dict(Pair.(sch.names,sch.types))[:Close]

to make it a little more generic

gp(sch)=getproperty.(Ref(sch), propertynames(sch))
Dict(zip(gp(sch)...))[:Close]
1 Like