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

add rownumber #2356

Merged
merged 7 commits into from
Aug 17, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/dataframerow/dataframerow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ julia> Vector(df[1, :])
```
"""
struct DataFrameRow{D<:AbstractDataFrame,S<:AbstractIndex}
# although we allow D to be AbstractDataFrame to support extensions
# in DataFrames.jl it will always be a DataFrame unless an inner constructor
# is used. In this way we have a fast access to the data frame that
# actually stores the data that DataFrameRow refers to
df::D
colindex::S
row::Int # row number in true parent DataFrame
rownumber::Int # row number in the direct source
dfrow::Int # row number in df
rownumber::Int # row number in the direct source AbstractDataFrame from which DataFrameRow was created

@inline DataFrameRow(df::D, colindex::S, row::Union{Signed, Unsigned},
rownumber::Union{Signed, Unsigned}) where
Expand Down Expand Up @@ -112,7 +116,7 @@ Base.@propagate_inbounds DataFrameRow(df::SubDataFrame, row::Bool, cols) =
Base.@propagate_inbounds DataFrameRow(df::AbstractDataFrame, row::Integer) =
DataFrameRow(df, row, :)

row(r::DataFrameRow) = getfield(r, :row)
row(r::DataFrameRow) = getfield(r, :dfrow)

"""
rownumber(dfr::DataFrameRow)
Expand Down Expand Up @@ -209,6 +213,11 @@ Base.@propagate_inbounds Base.getindex(r::DataFrameRow, idx::ColumnIndex) =
parent(r)[row(r), parentcols(index(r), idx)]

Base.@propagate_inbounds function Base.getindex(r::DataFrameRow, idxs::MultiColumnIndex)
# we create a temporaty DataFrameRow object to compute the SubIndex
bkamins marked this conversation as resolved.
Show resolved Hide resolved
# in the parent(r), but this object has an incorrect rownumber
# so we later copy rownumber from r
# the Julia compiler should be able to optimize out this indirection
# and in this way we avoid duplicating the code that computes the correct SubIndex
dfr_tmp = DataFrameRow(parent(r), row(r), parentcols(index(r), idxs))
bkamins marked this conversation as resolved.
Show resolved Hide resolved
return DataFrameRow(parent(dfr_tmp), index(dfr_tmp), row(r), rownumber(r))
end
Expand Down Expand Up @@ -297,6 +306,11 @@ Base.view(r::DataFrameRow, col::ColumnIndex) =
view(parent(r)[!, parentcols(index(r), col)], row(r))

function Base.view(r::DataFrameRow, cols::MultiColumnIndex)
# we create a temporaty DataFrameRow object to compute the SubIndex
bkamins marked this conversation as resolved.
Show resolved Hide resolved
# in the parent(r), but this object has an incorrect rownumber
# so we later copy rownumber from r
# the Julia compiler should be able to optimize out this indirection
# and in this way we avoid duplicating the code that computes the correct SubIndex
dfr_tmp = DataFrameRow(parent(r), row(r), parentcols(index(r), cols))
return DataFrameRow(parent(dfr_tmp), index(dfr_tmp), row(r), rownumber(r))
end
Expand Down