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

minor fixes #916

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
16 changes: 15 additions & 1 deletion src/dataframerow/dataframerow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ immutable DataFrameRow{T <: AbstractDataFrame}
end

function Base.getindex(r::DataFrameRow, idx::AbstractArray)
return DataFrameRow(r.df[[idx]], r.row)
return DataFrameRow(r.df[collect(idx)], r.row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you simply remove the innner [ ] here? getindex(::DataFrame, idx) should take care of the rest.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to make a minimal modification originally, but I see your point. I removed collect and updated the PR. Tests run fine, so if it had a role it is not immediately apparent.

end

function Base.getindex(r::DataFrameRow, idx::Any)
Expand All @@ -16,6 +16,16 @@ function Base.setindex!(r::DataFrameRow, value::Any, idx::Any)
return setindex!(r.df, value, r.row, idx)
end

function Base.setindex!{T <: AbstractDataFrame}(df::DataFrame, r::DataFrameRow{T},
::Colon, ::Colon)
return setindex!(df, r.df[r.row,:], :, :)
end

function Base.setindex!{T <: AbstractDataFrame}(df::DataFrame, r::DataFrameRow{T},
row_inds, ::Colon)
return setindex!(df, r.df[r.row,:], row_inds, :)
end

Base.names(r::DataFrameRow) = names(r.df)
_names(r::DataFrameRow) = _names(r.df)

Expand All @@ -37,6 +47,10 @@ Base.done(r::DataFrameRow, s) = s > length(r)

Base.convert(::Type{Array}, r::DataFrameRow) = convert(Array, r.df[r.row,:])

Base.similar(r::DataFrameRow, dims::Int) = similar(r.df, dims)

Base.similar(r::DataFrameRow) = DataFrameRow(similar(r, 1), 1)

# hash of DataFrame rows based on its values
# so that duplicate rows would have the same hash
function Base.hash(r::DataFrameRow, h::UInt)
Expand Down
24 changes: 24 additions & 0 deletions test/dataframerow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module TestDataFrameRow
@data([:A, NA, :C, :A, NA, :C])))
df2 = DataFrame(a = @data([1, 2, 3]))

df3 = DataFrame(a = @data([1,1,1]), b = @data([2,2,2]))

#
# Equality
#
Expand All @@ -25,4 +27,26 @@ module TestDataFrameRow
@test isequal(hash(DataFrameRow(df, 1)), hash(DataFrameRow(df, 4)))
@test isequal(hash(DataFrameRow(df, 2)), hash(DataFrameRow(df, 5)))
@test !isequal(hash(DataFrameRow(df, 2)), hash(DataFrameRow(df, 6)))

# similar
let dfsim1=similar(DataFrameRow(df3,1))
@test isa(dfsim1, DataFrameRow)
@test length(dfsim1)==size(df3,2)
end
let dfsim2=similar(DataFrameRow(df3,1),4)
@test isa(dfsim2, DataFrame)
@test size(dfsim2)==(4,size(df3,2))
end

# setindex!
let df4 = DataFrame(a = @data([1,3,5]), b = @data([2,4,6]))
df4[2,:] = DataFrameRow(df3,1)
df4[3,:] = DataFrameRow(df3,1)
@test isequal(df4,df3)
end
let df5 = DataFrame(a = @data(rand(1:100,3)), b = @data(rand(1:100,3)))
df5[:,:] = DataFrameRow(df3,1)
@test isequal(df5,df3[1,:])
end

end