# Grouped Data Frame -- Two different types

**URL:** <https://discourse.julialang.org/t/grouped-data-frame-two-different-types/85404>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [August 6, 2022, 4:51pm UTC](https://discourse.julialang.org/t/grouped-data-frame-two-different-types/85404 "2022-08-06T16:51:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snowy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snowy/32/36765_2.png) [@Snowy](https://discourse.julialang.org/u/Snowy)\
**Post date:** [August 6, 2022, 4:51pm UTC](https://discourse.julialang.org/t/grouped-data-frame-two-different-types/85404/1 "2022-08-06T16:51:21Z")

</div>

Hi!  
I have two different implementations of grouping dataframes, but I’m trying to determine why they are different types.  
Here is my code:

```julia
using CSV
using DataFrames

timeseries = CSV.read("link to data file (see below)",DataFrame)
#1
transform!(groupby(sort(timeseries, [:Name, :date]), :Name), :Name => eachindex => :rank)

#2
gdf = groupby(sort(timeseries,[:Name,:date]),:Name)
transform!(gdf,:Name => eachindex => :rank)

```

As far as I can tell, both these implementations should produce the same result, but when I look at the type…

```julia
julia> typeof(gdf)
GroupedDataFrame{DataFrame}

julia> typeof(timeseries)
DataFrame

```

Am I overlooking something super obvious here? Where are these different types?  
If you want to reproduce, here is the link to the csv I’m reading: [timeseries\_0.csv - Google Drive](https://drive.google.com/file/d/1-AB5a1nBmanaA_Zs2qpuJ8C_lDWW4ewq/view?usp=sharing)

Thanks!

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [August 6, 2022, 5:22pm UTC](https://discourse.julialang.org/t/grouped-data-frame-two-different-types/85404/2 "2022-08-06T17:22:00Z")

</div>

Operations like `transform`, `select`, etc. ungroup a `GroupedDataFrame` by default and return a normal `DataFrame`. If you want to retain the grouping, provide the keyword `ungroup=false`.

From the docstring of `transform`:

> • ungroup::Bool=true : whether the return value of the operation on gd should be a data frame or a  
> GroupedDataFrame.

---

<div class="post-metadata">

**Author:** ![Snowy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snowy/32/36765_2.png) [@Snowy](https://discourse.julialang.org/u/Snowy)\
**Post date:** [August 6, 2022, 6:04pm UTC](https://discourse.julialang.org/t/grouped-data-frame-two-different-types/85404/3 "2022-08-06T18:04:40Z")

</div>

And so if ungroup is the default behavior, why wouldn’t transform! ungroup gdf in implementation #2, as it does in #1?

---

<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 6, 2022, 8:47pm UTC](https://discourse.julialang.org/t/grouped-data-frame-two-different-types/85404/4 "2022-08-06T20:47:21Z")

</div>

> [@skleinbo](#):
>
> Operations like `transform`, `select`, etc. ungroup a `GroupedDataFrame` by default and return a normal `DataFrame`.

This is unrelated in this case. `timeseries` is a `DataFrame`, because you read it in as a `DataFrame` with `CSV.read`. The fact that you later run `transform!` on it does not affect the fact that `timeseries` is bound to a `DataFrame`. Running a function on some value does not change a binding of a name to a value that you made earlier.

The same case is with `gdf`. You bind this name to the return value of `groupby` which is `GroupedDataFrame`. The fact that you run `transform!` later does not matter here.

However the return value of `transform!` in both cases is `DataFrame` as @skleinbo noted (unless `ungroup` kwarg is passed).

> [@Snowy](#):
>
> And so if ungroup is the default behavior, why wouldn’t transform! ungroup gdf in implementation #2, as it does in #1?

It does ungroup in both cases as I have commented above. But the design of Julia is such that it does not affect the bindings of names to values that you do before running `transform!`.
