From 34637c5232e71a3041939db4b414261a608c360c Mon Sep 17 00:00:00 2001 From: tpapp Date: Tue, 16 Feb 2016 09:29:53 +0100 Subject: [PATCH 1/3] Fix deprecation warning about []. --- src/dataframerow/dataframerow.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index 94ba0890f0..7557fcb5bc 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -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) end function Base.getindex(r::DataFrameRow, idx::Any) From 2006db909a2317a6a96c5e8c2df6c87a57139283 Mon Sep 17 00:00:00 2001 From: tpapp Date: Tue, 16 Feb 2016 09:30:43 +0100 Subject: [PATCH 2/3] similar and setindex! methods for DataFrameRows. Useful for working with DataFrameRow objects, allowing, for example, a natural selection-partition-accumulation idiom when iterating over an eachrow(dataframe). --- src/dataframerow/dataframerow.jl | 14 ++++++++++++++ test/dataframerow.jl | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index 7557fcb5bc..2847de7c7a 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -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) @@ -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) diff --git a/test/dataframerow.jl b/test/dataframerow.jl index 4fcc2c66e0..272d34de1a 100644 --- a/test/dataframerow.jl +++ b/test/dataframerow.jl @@ -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 # @@ -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 From 052b38abc6e5c4d25a458c127c5e5c0fe3afc508 Mon Sep 17 00:00:00 2001 From: tpapp Date: Tue, 16 Feb 2016 13:19:05 +0100 Subject: [PATCH 3/3] Removed collect. Suggestion by @nalimilan. --- src/dataframerow/dataframerow.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index 2847de7c7a..fa4cade251 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -5,7 +5,7 @@ immutable DataFrameRow{T <: AbstractDataFrame} end function Base.getindex(r::DataFrameRow, idx::AbstractArray) - return DataFrameRow(r.df[collect(idx)], r.row) + return DataFrameRow(r.df[idx], r.row) end function Base.getindex(r::DataFrameRow, idx::Any)