Skip to content

Commit

Permalink
implement getrows for RowTable and ColumnTable
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloLucibello committed Jun 28, 2022
1 parent 9bc355c commit b41146a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,15 @@ end
"""
getrows(x, inds; view=nothing)
Return one or more rows from table `x` according to the position(s) specified by `inds`.
Return one or more rows from table `x` according to the position(s) specified by `inds`:
- If `inds` is a single integer return a row object.
- If `inds` is a collection of integers, return a table object.
In this case,t he returned type is not necessarily the same as the original table type.
In this case, the returned type is not necessarily the same as the original table type.
The `view` argument influences whether the returned object is a view of the original table
or an independent copy:
- If `view=nothing` (the default) then the implementation for a specific table type
is free to decide whether to return a copy or a view.
- If `view=true` then a view is returned and if `view=false` a copy is returned.
Expand Down
16 changes: 16 additions & 0 deletions src/namedtuples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ function rowtable(itr::T) where {T}
return collect(namedtupleiterator(eltype(r), r))
end

function getrows(x::RowTable, inds; view::Union{Bool,Nothing} = nothing)
if view === true
return view(x, inds)
else
return x[inds]
end
end

# NamedTuple of arrays of matching dimensionality
const ColumnTable = NamedTuple{names, T} where {names, T <: NTuple{N, AbstractArray{S, D} where S}} where {N, D}
rowcount(c::ColumnTable) = length(c) == 0 ? 0 : length(c[1])
Expand Down Expand Up @@ -173,3 +181,11 @@ function columntable(itr::T) where {T}
return columntable(schema(cols), cols)
end
columntable(x::ColumnTable) = x

function getrows(x::ColumnTable, inds; view::Union{Bool,Nothing} = nothing)
if view === true
return map(c -> view(c, inds), x)
else
return map(c -> c[inds], x)
end
end

0 comments on commit b41146a

Please sign in to comment.