# \`MethodError\` when trying to \`filter\` a \`GroupedDataframe\`

**URL:** <https://discourse.julialang.org/t/methoderror-when-trying-to-filter-a-groupeddataframe/133996>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [November 20, 2025, 12:38am UTC](https://discourse.julialang.org/t/methoderror-when-trying-to-filter-a-groupeddataframe/133996 "2025-11-20T00:38:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quantodeluz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quantodeluz/32/218696_2.png) [@Quantodeluz](https://discourse.julialang.org/u/Quantodeluz)\
**Post date:** [November 20, 2025, 12:38am UTC](https://discourse.julialang.org/t/methoderror-when-trying-to-filter-a-groupeddataframe/133996/1 "2025-11-20T00:38:19Z")

</div>

I’m trying to filter the SubDataFrames with less than a given length (`nrow`) out of a GroupedDataFrame, but `filter()` doesn’t seem to recognize the anonymus function i’m passing to it and throws a `MethodError`:

```ps
ERROR: LoadError: MethodError: no method matching filter!(::var"#2#3", ::GroupedDataFrame{DataFrame})
The function `filter!` exists, but no method is defined for this combination of argument types.

```

Here’s a MWE:

```julia-auto
using DataFrames;

sections = [
    1, 1, 1, 1,
    2, 2, 2, 2, 2, 2,
    3, 3, 3, 3, 3,
    4, 4]
df = DataFrame(A=sections, B=0)
grouped_df = groupby(df, :A)

filter!(sub_df->(nrow(sub_df) <= 5), grouped_df)

```

---

<div class="post-metadata">

**Author:** ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)\
**Post date:** [November 20, 2025, 1:15am UTC](https://discourse.julialang.org/t/methoderror-when-trying-to-filter-a-groupeddataframe/133996/2 "2025-11-20T01:15:32Z")

</div>

`filter` is disfavored in recent DataFrames versions. Use `subset`, instead.

```julia-auto
julia> subset!(grouped_df, :A => ByRow(x -> x <= 5))
17×2 DataFrame
 Row │ A B     
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 0
   2 │ 1 0
   3 │ 1 0
   4 │ 1 0
   5 │ 2 0
   6 │ 2 0
  ⋮ │ ⋮ ⋮
  13 │ 3 0
  14 │ 3 0
  15 │ 3 0
  16 │ 4 0
  17 │ 4 0
      6 rows omitted

```

---

<div class="post-metadata">

**Author:** ![Quantodeluz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quantodeluz/32/218696_2.png) [@Quantodeluz](https://discourse.julialang.org/u/Quantodeluz)\
**Post date:** [November 20, 2025, 1:59am UTC](https://discourse.julialang.org/t/methoderror-when-trying-to-filter-a-groupeddataframe/133996/3 "2025-11-20T01:59:44Z")

</div>

This code seems to filter by row, but I’m trying to filter out entire sub dataframes  
How could I do that?

---

<div class="post-metadata">

**Author:** ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)\
**Post date:** [November 20, 2025, 2:11am UTC](https://discourse.julialang.org/t/methoderror-when-trying-to-filter-a-groupeddataframe/133996/4 "2025-11-20T02:11:49Z")

</div>

Sorry. Misapprehended the MWE.

```julia-auto

julia> # Filter to keep only groups with 5 or fewer rows
       filtered_gdf = filter(subdf -> nrow(subdf) <= 5, grouped_df)
GroupedDataFrame with 3 groups based on key: A
First Group (4 rows): A = 1
 Row │ A B     
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 0
   2 │ 1 0
   3 │ 1 0
   4 │ 1 0
⋮
Last Group (2 rows): A = 4
 Row │ A B     
     │ Int64 Int64 
─────┼──────────────
   1 │ 4 0
   2 │ 4 0

julia> # Or if you want a regular DataFrame back
       filtered_df = filter(subdf -> nrow(subdf) <= 5, grouped_df, ungroup=true)
11×2 DataFrame
 Row │ A B     
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 0
   2 │ 1 0
   3 │ 1 0
   4 │ 1 0
   5 │ 3 0
   6 │ 3 0
   7 │ 3 0
   8 │ 3 0
   9 │ 3 0
  10 │ 4 0
  11 │ 4 0

```

---

<div class="post-metadata">

**Author:** ![Quantodeluz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quantodeluz/32/218696_2.png) [@Quantodeluz](https://discourse.julialang.org/u/Quantodeluz)\
**Post date:** [November 20, 2025, 2:28am UTC](https://discourse.julialang.org/t/methoderror-when-trying-to-filter-a-groupeddataframe/133996/5 "2025-11-20T02:28:12Z")

</div>

Wait, that’s literally the same i tried at first, except it doesn’t use the mutating version…  
I just tested it and yeah, `filter!()` throws an error while `filter()` works perfectly fine  
Do you know why could that be?

---

<div class="post-metadata">

**Author:** ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)\
**Post date:** [November 20, 2025, 2:52am UTC](https://discourse.julialang.org/t/methoderror-when-trying-to-filter-a-groupeddataframe/133996/6 "2025-11-20T02:52:24Z")

</div>

I guess that’s down to the nature of the object being mutated, which is a GroupedDataFrame and not its constituent SubDataFrames, so we’ll have to assign back.

```julia-auto

julia> grouped_df = [g for g in grouped_df if nrow(g) <= 5]
3-element Vector{SubDataFrame{DataFrame, DataFrames.Index, Vector{Int64}}}:
 4×2 SubDataFrame
 Row │ A B     
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 0
   2 │ 1 0
   3 │ 1 0
   4 │ 1 0
 5×2 SubDataFrame
 Row │ A B     
     │ Int64 Int64 
─────┼──────────────
   1 │ 3 0
   2 │ 3 0
   3 │ 3 0
   4 │ 3 0
   5 │ 3 0
 2×2 SubDataFrame
 Row │ A B     
     │ Int64 Int64 
─────┼──────────────
   1 │ 4 0
   2 │ 4 0

```
