I would advise against using
DataFrame!(CSV.File(file; kw...))
if you plan on doing any mutating of your data frame. It’s safer to use
DataFrame(CSV.File(file; kw...))
Otherwise you’ll run into problems like this:
“df.csv” file:
x,y
a,1
b,2
Code
julia> using DataFrames, CSV
julia> df = DataFrame!(CSV.File("df.csv"))
2×2 DataFrame
│ Row │ x │ y │
│ │ String │ Int64 │
├─────┼────────┼───────┤
│ 1 │ a │ 1 │
│ 2 │ b │ 2 │
julia> filter!(r -> r.x == "b", df)
ERROR: MethodError: no method matching deleteat!(::CSV.Column{String,String}, ::Array{Int64,1})
Closest candidates are:
deleteat!(::Array{T,1} where T, ::AbstractArray{T,1} where T) at array.jl:1275
deleteat!(::Array{T,1} where T, ::Any) at array.jl:1274
deleteat!(::BitArray{1}, ::Any) at bitarray.jl:962
...
Stacktrace:
[1] (::DataFrames.var"#161#162"{Array{Int64,1}})(::CSV.Column{String,String}) at /Users/bieganek/.julia/packages/DataFrames/htZzm/src/dataframe/dataframe.jl:873
[2] foreach(::DataFrames.var"#161#162"{Array{Int64,1}}, ::Array{AbstractArray{T,1} where T,1}) at ./abstractarray.jl:1919
[3] delete!(::DataFrame, ::Array{Int64,1}) at /Users/bieganek/.julia/packages/DataFrames/htZzm/src/dataframe/dataframe.jl:873
[4] _filter!_helper(::DataFrame, ::Function, ::DataFrames.DataFrameRows{DataFrame,DataFrames.Index}) at /Users/bieganek/.julia/packages/DataFrames/htZzm/src/abstractdataframe/abstractdataframe.jl:1075
[5] filter!(::Function, ::DataFrame) at /Users/bieganek/.julia/packages/DataFrames/htZzm/src/abstractdataframe/abstractdataframe.jl:1056
[6] top-level scope at REPL[3]:1
My actual code was more complicated, and I was getting an inscrutable UndefRefError
:
ERROR: UndefRefError: access to undefined reference
so I wasted hours trying to figure out what the real problem was.
The UndefRefError
that I got with my real code appears to be related to a SentinelArray
column that was in the data frame that I got from DataFrame!(CSV.File(file))
.