Pandas groupby and make set of items


  • As late as pandas version 0.22, this is an issue.
  • As of pandas version 1.1.2, this is not an issue. Aggregating set, doesn’t result in TypeError: 'type' object is not iterable.
    • Not certain when the functionality was updated.

Original Answer

It’s because set is of type type whereas to_set is of type function:

<class 'type'>

def to_set(x):
    return set(x)


<class 'function'>

According to the docs, .agg() expects:

arg : function or dict

Function to use for aggregating groups.

  • If a function, must either work when passed a DataFrame or when passed to
  • If passed a dict, the keys must be DataFrame column

Accepted Combinations are:

  • string cythonized function name
  • function
  • list of functions
  • dict of columns -> functions
  • nested dict of names -> dicts of functions

Leave a Comment