# Dataframe map function drops empty results

**URL:** <https://discourse.julialang.org/t/dataframe-map-function-drops-empty-results/27505>\
**Category:** General Usage\
**Created:** [August 13, 2019, 5:48pm UTC](https://discourse.julialang.org/t/dataframe-map-function-drops-empty-results/27505 "2019-08-13T17:48:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [August 13, 2019, 5:48pm UTC](https://discourse.julialang.org/t/dataframe-map-function-drops-empty-results/27505/1 "2019-08-13T17:48:50Z")

</div>

Which is the result i wanted, but not the result i expected.

The result I expect in my example below is a group with an empty dataframe for each of the odd values from column a.

p.s. documentation does not indicate which will happen.

```julia
using DataFrames

function test()
    m = 3
    n = 4
    df = DataFrame([repeat(collect(1:n), inner=m),
                    [rand(1:10) for _ in 1:n*m],
                    [rand(1:10) for _ in 1:n*m]], [:a, :b, :c])

    g0 = groupby(df, :a)
    println(g0)

    g2 = map(sdf->filter(r->r[:a] % 2 == 0, sdf), g0)

    println(g2)
    
end
test()

```

```julia
GroupedDataFrame with 4 groups based on key: a
Group 1 (3 rows): a = 1
│ Row │ a │ b │ c │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 1 │ 2 │ 2 │
│ 2 │ 1 │ 3 │ 6 │
│ 3 │ 1 │ 9 │ 7 │
Group 2 (3 rows): a = 2
│ Row │ a │ b │ c │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 2 │ 10 │ 9 │
│ 2 │ 2 │ 8 │ 8 │
│ 3 │ 2 │ 8 │ 2 │
Group 3 (3 rows): a = 3
│ Row │ a │ b │ c │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 3 │ 10 │ 4 │
│ 2 │ 3 │ 8 │ 7 │
│ 3 │ 3 │ 3 │ 3 │
Group 4 (3 rows): a = 4
│ Row │ a │ b │ c │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 4 │ 1 │ 7 │
│ 2 │ 4 │ 1 │ 4 │
│ 3 │ 4 │ 10 │ 2 │
GroupedDataFrame with 2 groups based on key: a
Group 1 (3 rows): a = 2
│ Row │ a │ a_1 │ b │ c │
│ │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┼───────┤
│ 1 │ 2 │ 2 │ 10 │ 9 │
│ 2 │ 2 │ 2 │ 8 │ 8 │
│ 3 │ 2 │ 2 │ 8 │ 2 │
Group 2 (3 rows): a = 4
│ Row │ a │ a_1 │ b │ c │
│ │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┼───────┤
│ 1 │ 4 │ 4 │ 1 │ 7 │
│ 2 │ 4 │ 4 │ 1 │ 4 │
│ 3 │ 4 │ 4 │ 10 │ 2 │

```

---

<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:** [August 13, 2019, 6:02pm UTC](https://discourse.julialang.org/t/dataframe-map-function-drops-empty-results/27505/2 "2019-08-13T18:02:02Z")

</div>

I have not designed it (@nalimilan probably can comment more), but looking at the source code this is intended.

The underlying reason is that `GroupedDataFrame` does not store values of grouping variables for each group but only the groups.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [August 14, 2019, 4:03pm UTC](https://discourse.julialang.org/t/dataframe-map-function-drops-empty-results/27505/3 "2019-08-14T16:03:59Z")

</div>

Yes, `GroupedDataFrame` never stores empty groups. We would need to change the internal design to allow that. I guess the docs could be more explicit about that.
