How to select dataframe rows in Julia 1.6?

Hello,
I have been selecting the rows of a dataframe with a column Hit matching a parameter stored in the variable h with qry = df[df[:Hit] .== h, :] but now I have switched to Hulia 1.6 and I am getting this error:

julia> qry = df[df[:Hit] .== h, :]
ERROR: ArgumentError: syntax df[column] is not supported use df[!, column] instead
Stacktrace:
 [1] getindex(#unused#::DataFrame, #unused#::Symbol)
   @ DataFrames ~/.julia/packages/DataFrames/pf2RS/src/abstractdataframe/abstractdataframe.jl:2157
 [2] top-level scope
   @ none:1

I tried with:

julia> qry = df[!, Hit .== h, :]
ERROR: UndefVarError: Hit not defined
Stacktrace:
 [1] top-level scope
   @ none:1

julia> qry = df[!, :Hit .== h, :]
ERROR: MethodError: no method matching getindex(::DataFrame, ::typeof(!), ::Bool, ::Colon)
Closest candidates are:
  getindex(::DataFrame, ::typeof(!), ::Union{Colon, Regex, AbstractVector{T} where T, All, Between, Cols, InvertedIndex}) at /home/gigiux/.julia/packages/DataFrames/pf2RS/src/dataframe/dataframe.jl:587
  getindex(::DataFrame, ::typeof(!), ::Union{AbstractString, Symbol}) at /home/gigiux/.julia/packages/DataFrames/pf2RS/src/dataframe/dataframe.jl:507
  getindex(::DataFrame, ::typeof(!), ::Union{Signed, Unsigned}) at /home/gigiux/.julia/packages/DataFrames/pf2RS/src/dataframe/dataframe.jl:499
  ...
Stacktrace:
 [1] top-level scope
   @ none:1
julia> qry = df[!, :Hit .== h]
ERROR: MethodError: no method matching getindex(::DataFrame, ::typeof(!), ::Bool)
Closest candidates are:
  getindex(::DataFrame, ::typeof(!), ::Union{Colon, Regex, AbstractVector{T} where T, All, Between, Cols, InvertedIndex}) at /home/gigiux/.julia/packages/DataFrames/pf2RS/src/dataframe/dataframe.jl:587
  getindex(::DataFrame, ::typeof(!), ::Union{AbstractString, Symbol}) at /home/gigiux/.julia/packages/DataFrames/pf2RS/src/dataframe/dataframe.jl:507
  getindex(::DataFrame, ::typeof(!), ::Union{Signed, Unsigned}) at /home/gigiux/.julia/packages/DataFrames/pf2RS/src/dataframe/dataframe.jl:499
  ...
Stacktrace:
 [1] top-level scope
   @ none:1

What is the new syntax for this task?
Thanks

You misread the error message:

ERROR: ArgumentError: syntax df[column] is not supported use df[!, column] instead

means you can’t do df[:Hit] anymore, you should do df[!, :Hit] (or alternatively df.Hit).

So you want:

df[df.Hit .== h, :]

Note that this has nothing to do with the Julia version, this is DataFrames behaviour and only depends on the version of the DataFrames package you use. The df[:col] syntax has been deprecated a long time ago, and the current DataFrames version is 1.1.0 so now is a good time to update any old cold you might have given that the API is now considered stable since the DataFrames 1.0 release.

Finally, you can also use filter and subset to select rows in a DataFrame.

2 Likes