Groupby search not working as expected

It’s useful to consult the documentation in these situations:

https://dataframes.juliadata.org/stable/lib/indexing/#Indexing-GroupedDataFrames

In particular:

A GroupedDataFrame can behave as either an AbstractVector or AbstractDict depending on the type of index used. Integers (or arrays of them) trigger vector-like indexing while Tupless and NamedTuples trigger dictionary-like indexing.

And:

  • gd[i::Integer] → Get the ith group.
  • gd[key::NamedTuple] → Get the group corresponding to the given values of the grouping columns. >The fields of the NamedTuple must match the grouping columns columns passed to [groupby]>(Functions · DataFrames.jl) (including order).
  • gd[key::Tuple] → Same as previous, but omitting the names on key.

So:

julia> search_test_col[(test_value,)]
1×1 SubDataFrame
 Row │ test_col
     │ String
─────┼──────────
   1 │ B

Note the comma after test_value. Writing (test_value) doesn’t do anything:

julia> (test_value)
"B"

i.e. it’s the same as writing test_value without parens. If you’re confused about constructing tuples that’s more of an issue for the base Julia docs rather than DataFrames though.

1 Like