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

correctly use dotview instead of maybeview in broadcasting contexts #1960

Merged
merged 8 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1532,8 +1532,13 @@ function Base.setindex!(df::DataFrame, v, ::Colon, col_inds)
df
end

function Base.dotview(df::AbstractDataFrame, col::ColumnIndex)
Base.depwarn("in broadcasted assignment use `df[:, col]` instead of `df[col]`", :dotview)
@view df[:, col]
end

import Base: setproperty!
@deprecate setproperty!(df::DataFrame, col_ind::Symbol, v) (df[!, col_ind] .= v)
@deprecate setproperty!(df::SubDataFrame, col_ind::Symbol, v) (df[:, col_ind] .= v)

@deprecate eltypes(df::AbstractDataFrame) eltype.(eachcol(df))
@deprecate eltypes(df::AbstractDataFrame) eltype.(eachcol(df))
4 changes: 2 additions & 2 deletions src/other/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Base.maybeview(df::AbstractDataFrame, idx::CartesianIndex{2}) = df[idx]
Base.maybeview(df::AbstractDataFrame, row::Integer, col::ColumnIndex) = df[row, col]
Base.maybeview(df::AbstractDataFrame, rows, cols) = view(df, rows, cols)

function Base.maybeview(df::DataFrame, ::typeof(!), cols)
function Base.dotview(df::DataFrame, ::typeof(!), cols)
if !(cols isa ColumnIndex)
return ColReplaceDataFrame(df, index(df)[cols])
end
Expand All @@ -106,7 +106,7 @@ function Base.maybeview(df::DataFrame, ::typeof(!), cols)
LazyNewColDataFrame(df, cols)
end

Base.maybeview(df::SubDataFrame, ::typeof(!), idxs) =
Base.dotview(df::SubDataFrame, ::typeof(!), idxs) =
throw(ArgumentError("broadcasting with ! row selector is not allowed for SubDataFrame"))

function Base.copyto!(lazydf::LazyNewColDataFrame, bc::Base.Broadcast.Broadcasted{T}) where T
Expand Down
11 changes: 11 additions & 0 deletions test/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1479,4 +1479,15 @@ end
@test df == f_identity.(df)
end

@testset "@views on df[!, col]" begin
df = DataFrame(ones(3, 4))
@views df[!, 1] .+= 1
@test df[!, 1] == [2.0, 2.0, 2.0]
@views df[:, 2] .= df[!, 4] .+ df[!, 3]
@test df[!, 2] == [2.0, 2.0, 2.0]

# make sure we do not mess with maybeview
@test @views typeof(df[!, 1:2]) <: SubDataFrame
end

end # module