@@ -12,14 +12,14 @@ struct TableDataset{T} <: AbstractDataContainer
12
12
# TableDatasets must implement the Tables.jl interface
13
13
function TableDataset {T} (table:: T ) where {T}
14
14
Tables. istable (table) ||
15
- throw (ArgumentError (" TableDatasets must implement the Tabels .jl interface" ))
15
+ throw (ArgumentError (" The input must implement the Tables .jl interface" ))
16
16
17
17
new {T} (table)
18
18
end
19
19
end
20
20
21
21
TableDataset (table:: T ) where {T} = TableDataset {T} (table)
22
- # TableDataset(path::AbstractString) = TableDataset(DataFrame(CSV.File( path) ))
22
+ TableDataset (path:: AbstractString ) = TableDataset (read_csv ( path))
23
23
24
24
# slow accesses based on Tables.jl
25
25
_getobs_row (x, i) = first (Iterators. peel (Iterators. drop (x, i - 1 )))
@@ -29,26 +29,32 @@ function _getobs_column(x, i)
29
29
30
30
return NamedTuple {colnames} (rowvals)
31
31
end
32
- function Base. getindex (dataset:: TableDataset , i)
33
- if Tables. rowaccess (dataset. table)
34
- return _getobs_row (Tables. rows (dataset. table), i)
35
- elseif Tables. columnaccess (dataset. table)
36
- return _getobs_column (dataset. table, i)
32
+
33
+ function getobs_table (table, i)
34
+ if Tables. rowaccess (table)
35
+ return _getobs_row (Tables. rows (table), i)
36
+ elseif Tables. columnaccess (table)
37
+ return _getobs_column (table, i)
37
38
else
38
39
error (" The Tables.jl implementation used should have either rowaccess or columnaccess." )
39
40
end
40
41
end
41
- function Base. length (dataset:: TableDataset )
42
- if Tables. columnaccess (dataset. table)
43
- return length (Tables. getcolumn (dataset. table, 1 ))
44
- elseif Tables. rowaccess (dataset. table)
42
+
43
+ function numobs_table (table)
44
+ if Tables. columnaccess (table)
45
+ return length (Tables. getcolumn (table, 1 ))
46
+ elseif Tables. rowaccess (table)
45
47
# length might not be defined, but has to be for this to work.
46
- return length (Tables. rows (dataset . table))
48
+ return length (Tables. rows (table))
47
49
else
48
50
error (" The Tables.jl implementation used should have either rowaccess or columnaccess." )
49
51
end
50
52
end
51
53
54
+ Base. getindex (dataset:: TableDataset , i) = getobs_table (dataset. table, i)
55
+ Base. length (dataset:: TableDataset ) = numobs_table (dataset. table)
56
+
57
+
52
58
# fast access for DataFrame
53
59
# Base.getindex(dataset::TableDataset{<:DataFrame}, i) = dataset.table[i, :]
54
60
# Base.length(dataset::TableDataset{<:DataFrame}) = nrow(dataset.table)
60
66
# # Tables.jl interface
61
67
62
68
Tables. istable (:: TableDataset ) = true
69
+
63
70
for fn in (:rowaccess , :rows , :columnaccess , :columns , :schema , :materializer )
64
71
@eval Tables.$ fn (dataset:: TableDataset ) = Tables.$ fn (dataset. table)
65
72
end
0 commit comments