Skip to content

ENH: Allow to group by an empty list #35366

Closed
@tomjaguarpaw

Description

@tomjaguarpaw

Is your feature request related to a problem?

Yes, I cannot group by an empty list.

Describe the solution you'd like

I would like to be able to group by an empty list of columns. At the moment I get an error.

>>> DataFrame([{"a":1, "b":2}, {"a":3, "b":4}]).groupby([]).agg(aagg=('a', 'sum'), bagg=('b', 'sum'))
Traceback (most recent call last):
...
ValueError: No group keys passed!

I would prefer to see

>>> DataFrame([{"a":1, "b":2}, {"a":3, "b":4}]).groupby([]).agg(aagg=('a', 'sum'), bagg=('b', 'sum'))
aagg  bagg
   4     6

which would generalise grouping by a non-empty list, which works fine.

>>> DataFrame([{"a":1, "b":2}, {"a":3, "b":4}]).groupby(["a", "b"]).agg(aagg=('a', 'sum'), bagg=('b', 'sum'))
     aagg  bagg
a b
1 2     1     2
3 4     3     4
>>> DataFrame([{"a":1, "b":2}, {"a":3, "b":4}]).groupby(["a"]).agg(aagg=('a', 'sum'), bagg=('b', 'sum'))
   aagg  bagg
a
1     1     2
3     3     4

It seems to me this would be reasonable behaviour. Is there a particular reason it is not supported?

API breaking implications

None that I know of.

Describe alternatives you've considered

A partial workaround is to cook up a grouping function for simulating the behaviour I want, but this lacks uniformity.

>>> DataFrame([{"a":1, "b":2}, {"a":3, "b":4}]).groupby(lambda _: "Only row").agg(aagg=('a', 'sum'), bagg=('b', 'sum'))
          aagg  bagg
Only row     4     6

But I don't really want "Only row" to be there. I just want

>>> DataFrame([{"a":1, "b":2}, {"a":3, "b":4}]).groupby([]).agg(aagg=('a', 'sum'), bagg=('b', 'sum'))
aagg  bagg
   4     6

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions