[FEA]Groupby Mode Aggregation
- Dominant language
- C++
- Stars
- 9.8k
- Forks
- 1.1k
- Avg merge
- 3d 6m
- Merged PRs (30d)
- 278
Description
**Is your feature request related to a problem? Please describe.**
I would like to do group by mode aggregation.
**Describe the solution you'd like**
```python
import pandas as pd
import numpy as np
df =cudf.DataFrame({'col_1':[0,0,0,0,1,1,1,1],
'col_2':[0,10,20,20,30,40,10,20]})
df.groupby(['col_1']).agg({'col_2':'mode')})
```
**Additional context**
In pandas we achieve this by doing below:
```python
import pandas as pd
import numpy as np
df = pd.DataFrame({'col_1':[0,0,0,0,1,1,1,1],
'col_2':[0,10,20,20,30,40,10,20]})
df.groupby(['col_1']).agg({'col_2':lambda s:s.mode().get(0)})
```
```python
col_2
col_1
0 20
1 10
```
If we do the same in cudf we get:
```python
import cudf
import numpy as np
df = cudf.DataFrame({'col_1':[0,0,0,0,1,1,1,1],
'col_2':[0,10,20,20,30,40,10,20]})
df.groupby(['col_1']).agg({'col_2':lambda s:s.mode().get(0)})
```
```
AttributeError Traceback (most recent call last)
in
3 df = cudf.DataFrame({'col_1':[0,0,0,0,1,1,1,1],
4 'col_2':[0,10,20,20,30,40,10,20]})
----> 5 df.groupby(['col_1']).agg({'col_2':lambda s:s.mode().get(0)})
/nvme/0/vjawa/conda/envs/cudf_dev_encoding_branch/lib/python3.7/contextlib.py in inner(*args, **kwds)
72 def inner(*args, **kwds):
73 with self._recreate_cm():
---> 74 return func(*args, **kwds)
75 return inner
76
/nvme/0/vjawa/vjawa_cudf/cudf/python/cudf/cudf/core/groupby/groupby.py in agg(self, func)
168 # a Float64Index, while Pandas returns an Int64Index
169 # (GH: 6945)
--> 170 result = self._groupby.aggregate(self.obj, normalized_aggs)
171
172 result = cudf.DataFrame._from_table(result)
/nvme/0/vjawa/vjawa_cudf/cudf/python/cudf/cudf/_lib/groupby.pyx in cudf._lib.groupby.GroupBy.aggregate()
/nvme/0/vjawa/vjawa_cudf/cudf/python/cudf/cudf/_lib/aggregation.pyx in cudf._lib.aggregation.make_aggregation()
in (s)
3 df = cudf.DataFrame({'col_1':[0,0,0,0,1,1,1,1],
4 'col_2':[0,10,20,20,30,40,10,20]})
----> 5 df.groupby(['col_1']).agg({'col_2':lambda s:s.mode().get(0)})
AttributeError: type object 'cudf._lib.aggregation._AggregationFactory' has no attribute 'mode'
```
Contributor guide
Assessment
This issue has not been assessed yet.