|
21 | 21 | TableDataset(table::T) where {T} = TableDataset{T}(table)
|
22 | 22 | TableDataset(path::AbstractString) = TableDataset(read_csv(path))
|
23 | 23 |
|
24 |
| -# see https://github.com/JuliaML/MLUtils.jl/issues/67 |
25 |
| -# Assume the table provides a size and indexing interface (DataFrame does) |
26 |
| -# otherwise have to resort to very slow fallbacks |
27 |
| -numobs_table(x) = size(x, 1) |
28 |
| -getobs_table(x, i) = x[i, :] |
29 |
| - |
30 |
| -# # slow accesses based on Tables.jl |
31 |
| -# _getobs_row(x, i) = first(Iterators.peel(Iterators.drop(x, i - 1))) |
32 |
| -# function _getobs_column(x, i) |
33 |
| -# colnames = Tuple(Tables.columnnames(x)) |
34 |
| -# rowvals = ntuple(j -> Tables.getcolumn(x, j)[i], length(colnames)) |
35 |
| - |
36 |
| -# return NamedTuple{colnames}(rowvals) |
37 |
| -# end |
38 |
| - |
39 |
| -# function getobs_table(table, i) |
40 |
| -# if Tables.rowaccess(table) |
41 |
| -# return _getobs_row(Tables.rows(table), i) |
42 |
| -# elseif Tables.columnaccess(table) |
43 |
| -# return _getobs_column(table, i) |
44 |
| -# else |
45 |
| -# error("The Tables.jl implementation used should have either rowaccess or columnaccess.") |
46 |
| -# end |
47 |
| -# end |
48 |
| - |
49 |
| -# function numobs_table(table) |
50 |
| -# if Tables.columnaccess(table) |
51 |
| -# return length(Tables.getcolumn(table, 1)) |
52 |
| -# elseif Tables.rowaccess(table) |
53 |
| -# # length might not be defined, but has to be for this to work. |
54 |
| -# return length(Tables.rows(table)) |
55 |
| -# else |
56 |
| -# error("The Tables.jl implementation used should have either rowaccess or columnaccess.") |
57 |
| -# end |
58 |
| -# end |
| 24 | + |
| 25 | +# slow accesses based on Tables.jl |
| 26 | +_getobs_row(x, i) = first(Iterators.peel(Iterators.drop(x, i - 1))) |
| 27 | +function _getobs_column(x, i) |
| 28 | + colnames = Tuple(Tables.columnnames(x)) |
| 29 | + rowvals = ntuple(j -> Tables.getcolumn(x, j)[i], length(colnames)) |
| 30 | + |
| 31 | + return NamedTuple{colnames}(rowvals) |
| 32 | +end |
| 33 | + |
| 34 | +function getobs_table(table, i) |
| 35 | + if Tables.rowaccess(table) |
| 36 | + return _getobs_row(Tables.rows(table), i) |
| 37 | + elseif Tables.columnaccess(table) |
| 38 | + return _getobs_column(table, i) |
| 39 | + else |
| 40 | + error("The Tables.jl implementation used should have either rowaccess or columnaccess.") |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +function numobs_table(table) |
| 45 | + if Tables.columnaccess(table) |
| 46 | + return length(Tables.getcolumn(table, 1)) |
| 47 | + elseif Tables.rowaccess(table) |
| 48 | + # length might not be defined, but has to be for this to work. |
| 49 | + return length(Tables.rows(table)) |
| 50 | + else |
| 51 | + error("The Tables.jl implementation used should have either rowaccess or columnaccess.") |
| 52 | + end |
| 53 | +end |
59 | 54 |
|
60 | 55 | Base.getindex(dataset::TableDataset, i) = getobs_table(dataset.table, i)
|
61 | 56 | Base.length(dataset::TableDataset) = numobs_table(dataset.table)
|
|
0 commit comments