# Why can't I access a GroupedDataFrame (via get function) through a GroupKey?

**URL:** <https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106>\
**Category:** New to Julia\
**Tags:** question, dataframes\
**Created:** [August 27, 2021, 3:26pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106 "2021-08-27T15:26:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![zepphit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zepphit/32/28629_2.png) [@zepphit](https://discourse.julialang.org/u/zepphit)\
**Post date:** [August 27, 2021, 3:26pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/1 "2021-08-27T15:26:09Z")

</div>

I’m sorry, this should be a question easily answered I guess, but why does the following not work.

grouped\_df = groupby(original\_df,[:field\_to\_group])  
get(grouped\_df, keys(grouped\_df)[1])

LoadError: MethodError: no method matching get(::GroupedDataFrame{DataFrame}, ::DataFrames.GroupKey{GroupedDataFrame{DataFrame}})

The documentation for the get function clearly states that :

```julia
get(gd::GroupedDataFrame, key, default)
[...] key may be a GroupKey [...]

```

When I run typeof(keys(grouped\_df)[1]), I get DataFrames.GroupKey{GroupedDataFrame{DataFrame}}

What am I missing? Thank you for the time!

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [August 27, 2021, 3:37pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/2 "2021-08-27T15:37:50Z")

</div>

I think you want `getindex` instead of `get` here. A more idiomatic way of writing this is

```julia
grouped_df[first(keys(grouped_df))]

```

or

```julia
first(grouped_df)

```

---

<div class="post-metadata">

**Author:** ![zepphit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zepphit/32/28629_2.png) [@zepphit](https://discourse.julialang.org/u/zepphit)\
**Post date:** [August 27, 2021, 4:03pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/3 "2021-08-27T16:03:21Z")

</div>

Thank you for the answer. But what if i want to access a specific subgroup? (not by index, but rather, by name). And what if I want to loop on this GroupedDataFrame and access each one of its individual groups?

---

<div class="post-metadata">

**Author:** ![zepphit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zepphit/32/28629_2.png) [@zepphit](https://discourse.julialang.org/u/zepphit)\
**Post date:** [August 27, 2021, 4:04pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/4 "2021-08-27T16:04:28Z")

</div>

And i still don’t understand why get does not work with my GroupKey as stated on the documentation…

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 27, 2021, 4:09pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/5 "2021-08-27T16:09:25Z")

</div>

the docs have example right?

```julia
julia> df
3×3 DataFrame
 Row │ x1 x2 x3    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 10 9 8
   2 │ 10 7 6
   3 │ 7 7 4

julia> gd = groupby(df, :x2)
GroupedDataFrame with 2 groups based on key: x2
First Group (2 rows): x2 = 7
 Row │ x1 x2 x3    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 10 7 6
   2 │ 7 7 4
⋮
Last Group (1 row): x2 = 9
 Row │ x1 x2 x3    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 10 9 8

julia> get(gd, (x2=9,), 0)
1×3 SubDataFrame
 Row │ x1 x2 x3    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 10 9 8

julia> get(gd, keys(gd)[1], 0)
2×3 SubDataFrame
 Row │ x1 x2 x3    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 10 7 6
   2 │ 7 7 4

```

---

<div class="post-metadata">

**Author:** ![zepphit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zepphit/32/28629_2.png) [@zepphit](https://discourse.julialang.org/u/zepphit)\
**Post date:** [August 27, 2021, 4:10pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/6 "2021-08-27T16:10:28Z")

</div>

yes, yet still I don’t understand, if you could clarify. I could not reproduce what I asked

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 27, 2021, 4:11pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/7 "2021-08-27T16:11:25Z")

</div>

> [@zepphit](#):
>
> get(grouped\_df, keys(grouped\_df)[1])

you need a third argument that is `default`

---

<div class="post-metadata">

**Author:** ![zepphit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zepphit/32/28629_2.png) [@zepphit](https://discourse.julialang.org/u/zepphit)\
**Post date:** [August 27, 2021, 4:17pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/8 "2021-08-27T16:17:51Z")

</div>

Ok, now it works! It was really only the missing argument. The error message was a little bit misleading IMO… thanks @jling! Have a nice day 🙂

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 27, 2021, 4:18pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/9 "2021-08-27T16:18:37Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/a/3/a3449ae4330e40a4e210b0ef7d92135c4c8ee0ab.png)

the error suggests this too, highlighting the potentially missed argument

---

<div class="post-metadata">

**Author:** ![zepphit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zepphit/32/28629_2.png) [@zepphit](https://discourse.julialang.org/u/zepphit)\
**Post date:** [August 27, 2021, 4:22pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/10 "2021-08-27T16:22:02Z")

</div>

Interesting, because to me ::Any does not imply that I need to pass this argument, but rather that it would choose something as a default. But I will see it this way going forward. Thanks!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 27, 2021, 4:26pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/11 "2021-08-27T16:26:07Z")

</div>

A method taking an `::Any` argument requires that argument but has no type constraint on that argument. It might do additional checking within the method on the argument though, but the method will be called with any value but there must be a value.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 27, 2021, 4:27pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/12 "2021-08-27T16:27:51Z")

</div>

basically:

```julia
julia> f(x) = 3
f (generic function with 1 method)

julia> g() = 3
g (generic function with 1 method)

julia> f()
ERROR: MethodError: no method matching f()
Closest candidates are:
  f(::Any) at REPL[15]:1
Stacktrace:
 [1] top-level scope
   @ REPL[17]:1

julia> g()
3

```

---

<div class="post-metadata">

**Author:** ![zepphit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zepphit/32/28629_2.png) [@zepphit](https://discourse.julialang.org/u/zepphit)\
**Post date:** [August 27, 2021, 4:30pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/13 "2021-08-27T16:30:03Z")

</div>

Thank you for the clarification!

---

<div class="post-metadata">

**Author:** ![zepphit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zepphit/32/28629_2.png) [@zepphit](https://discourse.julialang.org/u/zepphit)\
**Post date:** [August 27, 2021, 4:30pm UTC](https://discourse.julialang.org/t/why-cant-i-access-a-groupeddataframe-via-get-function-through-a-groupkey/67106/14 "2021-08-27T16:30:45Z")

</div>

Thank you for taking the time with all these examples!
