# Groupby search not working as expected

**URL:** <https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [June 19, 2022, 11:23pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035 "2022-06-19T23:23:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [June 19, 2022, 11:23pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/1 "2022-06-19T23:23:03Z")

</div>

slowly working my way through RElearning dataframes ( from pandas ) and wanted to search a dataframe for a value. This is a search I will be doing many times so I thought the approach below might speed things up.

```julia
using DataFrames

df = DataFrame( test_col = ["a","B","a","D"])

search_test_col = groupby(df,:test_col)

test_value = "B"

 search_test_col[(test_value)]

```

when I run it I get

```julia
ulia> using DataFrames

julia> df = DataFrame( test_col = ["a","B","a","D"])
4×1 DataFrame
 Row │ test_col 
     │ String   
─────┼──────────
   1 │ a
   2 │ B
   3 │ a
   4 │ D

julia> search_test_col = groupby(df,:test_col)
GroupedDataFrame with 3 groups based on key: test_col
First Group (2 rows): test_col = "a"
 Row │ test_col 
     │ String   
─────┼──────────
   1 │ a
   2 │ a
⋮
Last Group (1 row): test_col = "D"
 Row │ test_col 
     │ String   
─────┼──────────
   1 │ D

julia> test_value = "B"
"B"

julia> search_test_col[(test_value)]
ERROR: ArgumentError: invalid index: "B" of type String
Stacktrace:
 [1] to_index(i::String)
   @ Base ./indices.jl:300
 [2] to_index(A::GroupedDataFrame{DataFrame}, i::String)
   @ Base ./indices.jl:277
 [3] to_indices
   @ ./indices.jl:333 [inlined]
 [4] to_indices
   @ ./indices.jl:325 [inlined]
 [5] getindex(gd::GroupedDataFrame{DataFrame}, idx::String)
   @ DataFrames ~/.julia/packages/DataFrames/zqFGs/src/groupeddataframe/groupeddataframe.jl:660
 [6] top-level scope
   @ REPL[4]:1

julia> typeof(df.test_col)
Vector{String} (alias for Array{String, 1})

julia> typeof(test_value)
String

julia> df.test_col == test_value
false

julia> df.test_col[2] == test_value
true

julia> df.test_col[2]
"B"

julia> test_value
"B"

```

what am I doing wrong please?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 20, 2022, 6:16am UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/2 "2022-06-20T06:16:49Z")

</div>

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

[https://dataframes.juliadata.org/stable/lib/indexing/#Indexing-GroupedDataFrames](https://dataframes.juliadata.org/stable/lib/indexing/#Indexing-GroupedDataFrames)

In particular:

> A [`GroupedDataFrame`](https://dataframes.juliadata.org/stable/lib/types/#DataFrames.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 `Tuples`s and `NamedTuple`s trigger dictionary-like indexing.

And:

> - `gd[i::Integer]` → Get the `i`th 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](https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.groupby)) (including order).
> - `gd[key::Tuple]` → Same as previous, but omitting the names on `key`.

So:

```julia
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
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.

---

<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 20, 2022, 6:40am UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/3 "2022-06-20T06:40:45Z")

</div>

Yes - this is a consequence of “dual indexing style”.

Related recent issues:

- [Make row lookup easier · Issue #3051 · JuliaData/DataFrames.jl · GitHub](https://github.com/JuliaData/DataFrames.jl/issues/3051)
- [Improve filter performance for single column source · Issue #3082 · JuliaData/DataFrames.jl · GitHub](https://github.com/JuliaData/DataFrames.jl/issues/3082)

In particular using [GitHub - andyferris/AcceleratedArrays.jl: Arrays with acceleration indices](https://github.com/andyferris/AcceleratedArrays.jl) seems a good general solution if you have a condition on a single column. The only problem is that AcceleratedArrays.jl does not seem to be actively maintained currently, so we need to decide what to do about it.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [June 20, 2022, 12:54pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/4 "2022-06-20T12:54:07Z")

</div>

First thank you, as always, for the excellent answer. I had read the docs on groupby but didn’t look at the dataframes groupby documentation.

```julia
help?> groupby
search: groupby groupcols groupindices GroupedDataFrame

```

that said I am still confused

```julia
A GroupedDataFrame 1 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.

```

I was using a Symbol “:test\_col” which would seem to be pretty apposite for this case. I suppose part of my issue is that I don’t know what a Tupless is.

thank you again

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 20, 2022, 2:32pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/5 "2022-06-20T14:32:37Z")

</div>

[https://docs.julialang.org/en/v1/manual/functions/#Tuples](https://docs.julialang.org/en/v1/manual/functions/#Tuples)

---

<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 20, 2022, 2:41pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/6 "2022-06-20T14:41:24Z")

</div>

Consider e.g.:

```julia
julia> df = DataFrame(a=[3,3,2,2,2], b=1:5)
5×2 DataFrame
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 3 1
   2 │ 3 2
   3 │ 2 3
   4 │ 2 4
   5 │ 2 5

julia> gdf = groupby(df, :a)
GroupedDataFrame with 2 groups based on key: a
First Group (3 rows): a = 2
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 2 3
   2 │ 2 4
   3 │ 2 5
⋮
Last Group (2 rows): a = 3
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 3 1
   2 │ 3 2

julia> gdf[2] # pick group number 2
2×2 SubDataFrame
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 3 1
   2 │ 3 2

julia> gdf[(2,)] # pick group with grouping variable value equal to 2
3×2 SubDataFrame
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 2 3
   2 │ 2 4
   3 │ 2 5

```

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [June 20, 2022, 2:44pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/7 "2022-06-20T14:44:02Z")

</div>

> [@nilshg](#):
>
> `search_test_col[(test_value,)]`

I know what a tuple is but NOT what a tupless is.

```julia
 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 

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 20, 2022, 2:45pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/8 "2022-06-20T14:45:47Z")

</div>

Oh sorry I didn’t clock that this was just a joke about the typo in the docstring!

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [June 20, 2022, 2:50pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/9 "2022-06-20T14:50:06Z")

</div>

thank you professor,

as always a worked example is really helpful. I don’t have a lot of time to learn julia and so having a reference point that I can store away that gives me context to rtfm. They also allow people like me, example driven, to stick with learning the language properly.

I look forward to your new book.  
theakson

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [June 20, 2022, 2:53pm UTC](https://discourse.julialang.org/t/groupby-search-not-working-as-expected/83035/10 "2022-06-20T14:53:04Z")

</div>

thanks for the apology but your help is invaluable AND I have a pretty thick skin, and head 🙂 I learn from examples MORE than rtfm. Examples, like yours, raise questions which lead to me rtfm.  
thanks again for everything you do.  
theakson
