# Expressiveness for queries

**URL:** <https://discourse.julialang.org/t/expressiveness-for-queries/82318>\
**Category:** Performance\
**Tags:** dataframes\
**Created:** [June 6, 2022, 2:57pm UTC](https://discourse.julialang.org/t/expressiveness-for-queries/82318 "2022-06-06T14:57:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [June 6, 2022, 2:57pm UTC](https://discourse.julialang.org/t/expressiveness-for-queries/82318/1 "2022-06-06T14:57:52Z")

</div>

I have a table, with some missing values and I wanted to do a data retrieval from the table. It so happened that a comparison of the DataFrames implementation I came up with is much more wordy than the equivalent Pandas. So, in Julia, I have:

```julia
nrb_df = DataFrame(CSV.File("5GNR_BW_NRB.csv"));
nrb_select = @from i in nrb_df[:, [:FR, :SCS_kHz, Symbol(bw)]] begin
    @where i.FR==fr
    @where i.SCS_kHz == scs_kHz
    @select i
    @collect DataFrame
end;
nrb = nrb_select[:, Symbol(bw)][1];
if nrb === missing
    error(["Missing number of RBs for selected Frequency ", 
    "Range $(fr), subcarrier spacing $(scs_kHz), and ", 
    "bandwidth $(bw)."]);
end

```

while the equivalent Pandas implementation is

```julia
nrb_df = pd.read_csv('5GNR_BW_NRB.csv')
nrb_df.fillna(-9999, inplace=True)
nrb_df = nrb_df.astype(int)
nrb = nrb_df.query("FR == {} and SCS_kHz == {}".format(fr,scs_kHz))[bw][0]

assert(nrb > 0)

```

The way pandas handles NaN is by forcing all values to Float64 – this is annoying but ok.

My observation is with the way DataFramesMeta handles queries, using an SQL-compatible syntax. I think it is unfortunate that Julia does not simplify this to a more Matlab compatible syntax or some other pattern such as:

```julia
qq=@query :FR == $(fr), :SCS_kHz == $(scs), Symbol(bw) == bw
nrb_select = nrb_df[:, qq] 

```

I think there are some quirks with the way DataFrames is designed that make it look like a two-language solution when embedded in Julia code. Is there a plan to make the package more Julia/MATLAB-like in syntax?  
/K

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 6, 2022, 3:21pm UTC](https://discourse.julialang.org/t/expressiveness-for-queries/82318/2 "2022-06-06T15:21:54Z")

</div>

> [@kumarbalachandran](#):
>
> DataFramesMeta

Note, you are not using DataFramesMeta.jl here, but rather Query.jl. DataFramesMeta.jl works more similar to how you describe

```julia
julia> using DataFramesMeta

julia> df = DataFrame(FR = [1, 2, 3], SCS_kHz = [4, 5, 6], bw = [7, 8, 9]);

julia> fr = 1; scs = 4; bw = 7;

julia> @rsubset(df, :FR == fr, :SCS_kHz == scs, :bw == bw)
1×3 DataFrame
 Row │ FR SCS_kHz bw    
     │ Int64 Int64 Int64 
─────┼───────────────────────
   1 │ 1 4 7

```

But I think you are saying you want to _store_ the query. This is a good feature request and is tracked in DataFramesMeta.jl [here](https://github.com/JuliaData/DataFramesMeta.jl/issues/300).

It would be great to get a PR together to work on this.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 6, 2022, 3:25pm UTC](https://discourse.julialang.org/t/expressiveness-for-queries/82318/3 "2022-06-06T15:25:32Z")

</div>

The query can easily be stored with DataFrames.jl, but indeed it would be convenient to allow storing it in DataFramesMeta.jl as the syntax is simpler.

The “query” part in DataFrames.jl is a function:

```julia
query(fr, scs, bw) = [:FR => ByRow(isequal(fr)), :SCS_kHz => ByRow(isequal(scs)), :bw => ByRow(isequal(bw))]

```

and you can run it using:

```julia
select(df, query(some_fr, some_scs, some_bw))

```

---

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [June 6, 2022, 6:58pm UTC](https://discourse.julialang.org/t/expressiveness-for-queries/82318/4 "2022-06-06T18:58:04Z")

</div>

Thanks. I guess I got confused. I believe this solves my gripe very well, although the feature improvement to query would indeed be very good.

I was trying to do something very quickly and obviously did not bother to try to read up on all the functionality.

Kumar.
