DataFramesMeta.jl icon indicating copy to clipboard operation
DataFramesMeta.jl copied to clipboard

Add convenience function to look up a single value in a `DataFrame`

Open nathanrboyer opened this issue 1 year ago • 5 comments

It would work similarly to @rsubset but it would automatically call only and extract the desired value.

For example:

julia> df = DataFrame(x = 1:5, y = ["A","A","B","B","B"], z = (1:5)./10)
5×3 DataFrame
 Row │ x      y       z
     │ Int64  String  Float64
─────┼────────────────────────
   1 │     1  A           0.1
   2 │     2  A           0.2
   3 │     3  B           0.3
   4 │     4  B           0.4
   5 │     5  B           0.5

julia> zvalue = @rsubset(df, :x > 1, :y == "A").z |> only
0.2

julia> zvalue = @lookup(df, :z, :x > 1, :y == "A")
0.2

It could maybe also work without the second argument to return a DataFrameRow.

It was mentioned that this may need to be called rlookup to match rsubset, but I think that is not necessary if the single value method is implemented. (This discussion started in the DataFrames.jl repository: https://github.com/JuliaData/DataFrames.jl/issues/3051#issuecomment-2059978408)

Would this function make DataFrames lookups more accessible to newcomers, or is the existing @rsubset functionality good enough?

nathanrboyer avatar Apr 17 '24 20:04 nathanrboyer

Since this is a meta package, it may not need to look like a function call at all. The syntax could be something more exotic?

zvalue = df[@lookup, :z]  :x > 1, :y == "A"

nathanrboyer avatar Apr 18 '24 13:04 nathanrboyer

That particular syntax isn't actually possible, since @lookup would only apply to the expression inside the brackets.

Moreover, I would definitely want to avoid that kind of exotic syntax. DataFrames.jl doesn't have any data.table style indexing, and I think the initial proposed syntax is good.

pdeffebach avatar Apr 18 '24 15:04 pdeffebach

Any chance of you looking into implementing this in the near future?

I started looking at it myself this morning, but I have not made much progress ...

##############################################################################
##
## @lookup - select unique rows and values
##
##############################################################################

function lookup_helper(x, args...)
    @rsubset(x, args...) |> only
end

macro lookup(x, args...)
    esc(lookup_helper(x, args...))
end

macro lookup(x, y::Union{Symbol,AbstractString}, args...)
    @lookup(x, args...)[:, y] |> only
end
julia> using DataFramesMeta
Precompiling DataFramesMeta...
Info Given DataFramesMeta was explicitly requested, output will be shown live
ERROR: LoadError: syntax: "..." expression outside call

I would need to learn a lot about macros to get something functional.

nathanrboyer avatar Jan 03 '25 16:01 nathanrboyer

I still think this is a good idea.

pdeffebach avatar Oct 23 '25 20:10 pdeffebach

Me too, but it's not something I'm going to be able to implement myself.

nathanrboyer avatar Oct 24 '25 12:10 nathanrboyer