Skip to content
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

implement firstindex, lastindex, axes, size, and ndims consistently #2573

Merged
merged 11 commits into from
Dec 22, 2020
4 changes: 3 additions & 1 deletion docs/src/lib/indexing.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ Additionally the above types `T` (i.e. `AbstractDataFrame`, `DataFrameRow`, `Dat
* `size(::T)` returning a `Tuple` of `Int`.
* `size(::T, ::Integer)` returning an `Int`.
* `axes(::T)` returning a `Tuple` of `Int` vectors.
* `axes(::T, ::Integer)` returning an `Int` vector.
* `axes(::T, ::Integer)` returning an `Int` vector for a valid dimension (except
`DataFrameRows` and `GroupKeys` for which `Base.OneTo(1)` is also returned for
a dimension higher than a valid one because they are `AbstractVector`)..
bkamins marked this conversation as resolved.
Show resolved Hide resolved
* `firstindex(::T)` returning `1` (except `AbstractDataFrame` for which it is undefined).
* `firstindex(::T, ::Integer)` returning `1` for a valid dimension (except `DataFrameRows`
and `GroupKeys` for which `1` is also returned for a dimension higher than a valid one
Expand Down
6 changes: 4 additions & 2 deletions src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,10 @@ end

Base.isempty(df::AbstractDataFrame) = size(df, 1) == 0 || size(df, 2) == 0

Base.firstindex(df::AbstractDataFrame, i::Integer) = first(axes(df, i))
Base.lastindex(df::AbstractDataFrame, i::Integer) = last(axes(df, i))
if VERSION < v"1.6"
Base.firstindex(df::AbstractDataFrame, i::Integer) = first(axes(df, i))
Base.lastindex(df::AbstractDataFrame, i::Integer) = last(axes(df, i))
end
Base.axes(df::AbstractDataFrame, i::Integer) = Base.OneTo(size(df, i))

"""
Expand Down
6 changes: 4 additions & 2 deletions src/abstractdataframe/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ Base.eltype(::Type{<:DataFrameColumns}) = AbstractVector
Base.firstindex(itr::DataFrameColumns) = 1
Base.lastindex(itr::DataFrameColumns) = length(itr)

Base.firstindex(itr::DataFrameColumns, i::Integer) = first(axes(itr, i))
Base.lastindex(itr::DataFrameColumns, i::Integer) = last(axes(itr, i))
if VERSION < v"1.6"
Base.firstindex(itr::DataFrameColumns, i::Integer) = first(axes(itr, i))
Base.lastindex(itr::DataFrameColumns, i::Integer) = last(axes(itr, i))
end
Base.axes(itr::DataFrameColumns, i::Integer) = Base.OneTo(size(itr, i))

Base.iterate(itr::DataFrameColumns, i::Integer=1) =
Expand Down
6 changes: 4 additions & 2 deletions src/dataframerow/dataframerow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,10 @@ Base.ndims(::Type{<:DataFrameRow}) = 1
Base.firstindex(r::DataFrameRow) = 1
Base.lastindex(r::DataFrameRow) = length(r)

Base.firstindex(r::DataFrameRow, i::Integer) = first(axes(r, i))
Base.lastindex(r::DataFrameRow, i::Integer) = last(axes(r, i))
if VERSION < v"1.6"
Base.firstindex(r::DataFrameRow, i::Integer) = first(axes(r, i))
Base.lastindex(r::DataFrameRow, i::Integer) = last(axes(r, i))
end
Base.axes(r::DataFrameRow, i::Integer) = Base.OneTo(size(r, i))

Base.iterate(r::DataFrameRow) = iterate(r, 1)
Expand Down
12 changes: 8 additions & 4 deletions src/groupeddataframe/groupeddataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,10 @@ Base.ndims(::Type{<:GroupedDataFrame}) = 1
Base.firstindex(gd::GroupedDataFrame) = 1
Base.lastindex(gd::GroupedDataFrame) = gd.ngroups

Base.firstindex(gd::GroupedDataFrame, i::Integer) = first(axes(gd, i))
Base.lastindex(gd::GroupedDataFrame, i::Integer) = last(axes(gd, i))
if VERSION < v"1.6"
Base.firstindex(gd::GroupedDataFrame, i::Integer) = first(axes(gd, i))
Base.lastindex(gd::GroupedDataFrame, i::Integer) = last(axes(gd, i))
end
Base.axes(gd::GroupedDataFrame, i::Integer) = Base.OneTo(size(gd, i))

Base.first(gd::GroupedDataFrame) = gd[1]
Expand Down Expand Up @@ -479,8 +481,10 @@ Base.ndims(::Type{<:GroupKey}) = 1
Base.firstindex(key::GroupKey) = 1
Base.lastindex(key::GroupKey) = length(key)

Base.firstindex(key::GroupKey, i::Integer) = first(axes(key, i))
Base.lastindex(key::GroupKey, i::Integer) = last(axes(key, i))
if VERSION < v"1.6"
Base.firstindex(key::GroupKey, i::Integer) = first(axes(key, i))
Base.lastindex(key::GroupKey, i::Integer) = last(axes(key, i))
end
Base.axes(key::GroupKey, i::Integer) = Base.OneTo(size(key, i))

Base.names(key::GroupKey) = string.(parent(key).cols)
Expand Down