NVIDIA / NVIDIA/cudf

[BUG] groupby().agg() with list() expansion: TypeError: 'type' object is not iterable

Open
#12,518 0 comments 0 reactions 0 assignees View on GitHub
bug Python
Dominant language
C++
Stars
9.8k
Forks
1.1k
Avg merge
3d 6m
Merged PRs (30d)
278

Description

**Describe the bug**
rewriting code with `import cudf as pd`

**Steps/Code to reproduce bug**
```
In [1]: import cudf as pd

In [2]: pd.__version__
Out[2]: '22.12.0'

In [3]: df = pd.DataFrame({
...: "col1": [1, 2, 3, 4, 3],
...: "col2": ["a", "a", "b", "b", "c"],
...: "col3": ["d", "e", "f", "g", "h"]
...: })

In [4]: df.groupby(["col2"]).agg({"col1": "mean", "col3": lambda x: list(x)})
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [4], line 1
----> 1 df.groupby(["col2"]).agg({"col1": "mean", "col3": lambda x: list(x)})

File ~/.local/lib/python3.9/site-packages/nvtx/nvtx.py:101, in annotate.__call__..inner(*args, **kwargs)
98 @wraps(func)
99 def inner(*args, **kwargs):
100 libnvtx_push_range(self.attributes, self.domain.handle)
--> 101 result = func(*args, **kwargs)
102 libnvtx_pop_range(self.domain.handle)
103 return result

File ~/.local/lib/python3.9/site-packages/cudf/core/groupby/groupby.py:460, in GroupBy.agg(self, func)
451 column_names, columns, normalized_aggs = self._normalize_aggs(func)
453 # Note: When there are no key columns, the below produces
454 # a Float64Index, while Pandas returns an Int64Index
455 # (GH: 6945)
456 (
457 result_columns,
458 grouped_key_cols,
459 included_aggregations,
--> 460 ) = self._groupby.aggregate(columns, normalized_aggs)
462 result_index = self.grouping.keys._from_columns_like_self(
463 grouped_key_cols,
464 )
466 multilevel = _is_multi_agg(func)

File groupby.pyx:309, in cudf._lib.groupby.GroupBy.aggregate()

File groupby.pyx:184, in cudf._lib.groupby.GroupBy.aggregate_internal()

File aggregation.pyx:866, in cudf._lib.aggregation.make_groupby_aggregation()

Cell In [4], line 1, in (x)
----> 1 df.groupby(["col2"]).agg({"col1": "mean", "col3": lambda x: list(x)})

TypeError: 'type' object is not iterable

In [5]: df.to_pandas().groupby(["col2"]).agg({"col1": "mean", "col3": lambda x: list(x)})
Out[5]:
col1 col3
col2
a 1.5 [d, e]
b 3.5 [f, g]
c 3.0 [h]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.