The DataFrames.jl
documentation says
Since
df[!, :col]
does not make a copy, changing the elements of the column vector returned by this syntax will affect the values stored in the originaldf
. To get a copy of the column usedf[:, :col]
: changing the vector returned by this syntax does not changedf
.
Here, df
is an instance of DataFrame
. I thought the above excerpt of the documentation meant the following. df[!, :col]
returns a view of the column named col
of df
and therefore changing its contents changes the contents of df
itself. On the other hand, df[:, :col]
returns a copy of the same column and thus changing its contents does not alter the contents of df
.
However, df[:, :col]
also seems to change the contents of df
:
(@v1.7) pkg> st DataFrames
Status `~/.julia/environments/v1.7/Project.toml`
[a93c6f00] DataFrames v1.3.2
julia> using DataFrames
julia> df = DataFrame(A=1:3, B=4:6)
3Γ2 DataFrame
Row β A B
β Int64 Int64
ββββββΌββββββββββββββ
1 β 1 4
2 β 2 5
3 β 3 6
julia> df[:, :A] .= 0 # intend to change copy of column A by using : instead of !
3-element view(::Vector{Int64}, :) with eltype Int64:
0
0
0
julia> df # column A of df has changed!
3Γ2 DataFrame
Row β A B
β Int64 Int64
ββββββΌββββββββββββββ
1 β 0 4
2 β 0 5
3 β 0 6
What did I wrong here? Is my interpretation of the documentation incorrect?