-
Notifications
You must be signed in to change notification settings - Fork 53
Change getrows -> subset #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -569,15 +569,15 @@ struct Partitioner{T} | |
end | ||
|
||
""" | ||
Tables.getrows(x, inds; view=nothing) | ||
Tables.subset(x, inds; view=nothing) | ||
|
||
Return one or more rows from table `x` according to the position(s) specified by `inds`: | ||
|
||
- If `inds` is a single non-boolean integer return a row object. | ||
- If `inds` is a vector of non-boolean integers, a vector of booleans, or a `:`, return an indexable object of rows. | ||
- If `inds` is a vector of non-boolean integers, a vector of booleans, or a `:`, return a subset of the original table according to the indices. | ||
In this case, the returned type is not necessarily the same as the original table type. | ||
|
||
If other type of `inds` is passed than specified above the behavior is undefined. | ||
If other types of `inds` are passed than specified above the behavior is undefined. | ||
|
||
The `view` argument influences whether the returned object is a view of the original table | ||
or an independent copy: | ||
|
@@ -587,11 +587,37 @@ or an independent copy: | |
- If `view=true` then a view is returned and if `view=false` a copy is returned. | ||
This applies both to returning a row or a table. | ||
|
||
Any specialized implementation of `getrows` must support the `view=nothing` argument. | ||
Any specialized implementation of `subset` must support the `view=nothing` argument. | ||
Support for `view=true` or `view=false` is optional | ||
(i.e. implementations might error on them if they are not supported). | ||
""" | ||
function getrows end | ||
function subset(x::T, inds; view::Union{Bool, Nothing}=nothing) where {T} | ||
# because this method is being called, we know `x` didn't define it's own Tables.subset | ||
# first check if it supports column access, and if so, apply inds and wrap columns in a DictColumnTable | ||
if columnaccess(x) | ||
cols = columns(x) | ||
if inds isa Integer | ||
return ColumnsRow(cols, inds) | ||
else | ||
ret = view === true ? _map(c -> Base.view(c, inds), cols) : _map(c -> c[inds], cols) | ||
return DictColumnTable(schema(cols), ret) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a special case someone can pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like our existing |
||
end | ||
end | ||
# otherwise, let's get the rows and see if we can apply inds to them | ||
r = rows(x) | ||
if r isa AbstractVector | ||
inds isa Integer && return r[inds] | ||
ret = view === true ? Base.view(x, inds) : x[inds] | ||
(ret isa AbstractVector) || throw(ArgumentError("`Tables.subset`: invalid `inds` argument, expected `AbstractVector` output, got $(typeof(ret))")) | ||
return ret | ||
end | ||
throw(ArgumentError("no default `Tables.subset` implementation for type: $T")) | ||
end | ||
|
||
vectorcheck(x::AbstractVector) = x | ||
vectorcheck(x) = throw(ArgumentError("`Tables.subset`: invalid `inds` argument, expected `AbstractVector` output, got $(typeof(x))")) | ||
_map(f, cols) = OrderedDict(nm => vectorcheck(f(getcolumn(cols, nm))) for nm in columnnames(cols)) | ||
|
||
|
||
""" | ||
Tables.partitioner(f, itr) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Dict of Vectors as table | ||
struct DictColumnTable <: AbstractColumns | ||
schema::Schema | ||
values::OrderedDict{Symbol, AbstractVector} | ||
|
@@ -94,6 +95,7 @@ columnnames(x::DictColumnTable) = getfield(x, :schema).names | |
getcolumn(x::DictColumnTable, i::Int) = getfield(x, :values)[columnnames(x)[i]] | ||
getcolumn(x::DictColumnTable, nm::Symbol) = getfield(x, :values)[nm] | ||
|
||
# Vector of Dicts as table | ||
struct DictRowTable | ||
names::Vector{Symbol} | ||
types::Dict{Symbol, Type} | ||
|
@@ -122,6 +124,16 @@ function Base.iterate(x::DictRowTable, st=1) | |
return DictRow(x.names, x.values[st]), st + 1 | ||
end | ||
|
||
function subset(x::DictRowTable, inds; view::Union{Bool,Nothing} = nothing) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar comments as above to |
||
values = view === true ? Base.view(getfield(x, :values), inds) : getfield(x, :values)[inds] | ||
if inds isa Integer | ||
return DictRow(getfield(x, :names), values) | ||
else | ||
values isa AbstractVector || throw(ArgumentError("`Tables.subset`: invalid `inds` argument, expected `RowTable` output, got $(typeof(ret))")) | ||
return DictRowTable(getfield(x, :names), getfield(x, :types), values) | ||
end | ||
end | ||
|
||
""" | ||
Tables.dictrowtable(x) => Tables.DictRowTable | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.