question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent output in GroupBy.apply returning a DataFrame

See original GitHub issue

This is a continuation of https://github.com/pandas-dev/pandas/issues/13056, https://github.com/pandas-dev/pandas/issues/14927, and https://github.com/pandas-dev/pandas/issues/13056, which were closed by https://github.com/pandas-dev/pandas/pull/31613. I think that PR ensured that we consistently take one of two code paths. This issue is to verify that we actually want the behavior on master.

Focusing on a specific pair of examples that differ only in whether the returned index is the same or not:

# master

In [10]: def f(x):
    ...:     return x.copy()  # same index


In [11]: def g(x):
    ...:     return x.copy().rename(lambda x: x + 1)  # different index

In [12]: df = pd.DataFrame({"A": ['a', 'b'], "B": [1, 2]})

In [13]: df.groupby("A").apply(f)
Out[13]:
   A  B
0  a  1
1  b  2

In [14]: df.groupby("A").apply(g)
Out[14]:
     A  B
A
a 1  a  1
b 2  b  2
# 1.0.4
In [8]: df.groupby("A").apply(f)
Out[8]:
     A  B
A
a 0  a  1
b 1  b  2

In [9]: df.groupby("A").apply(g)
Out[9]:
     A  B
A
a 1  a  1
b 2  b  2

So the 1.0.4 behavior is to always prepend the group keys to the result as an index level. In pandas 1.1.0, whether the group keys are prepended depends on whether the udf returns a dataframe with an identical index. Do we want that kind of value-dependent behavior?

@jorisvandenbossche’s notebook from https://github.com/pandas-dev/pandas/issues/13056#issuecomment-403300216 might be helpful, though it might be out of date.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
TomAugspurgercommented, Jun 26, 2020

Thanks. It looks like this is essentially inconsistent handling of the existing group_keys argument, which https://github.com/pandas-dev/pandas/pull/34998 is trying to clean up. Hopefully this doesn’t make things too much more complicated for cudf.

0reactions
simonjayhawkinscommented, Jun 11, 2021

removing the milestone and blocker label

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - pandas groupby-apply behavior, returning a Series ...
So a groupby() operation can downcast to a Series, or if given a Series as input, can upcast to dataframe. For your first...
Read more >
[Code]-pandas groupby-apply behavior, returning a Series ...
When the series are of different lengths, it returns a multi-indexed series. In [1]: import pandas as pd In [2]: df1=pd.DataFrame({'state':list("AABBB") ...
Read more >
What's new in 1.4.0 (January 22, 2022) - Pandas
DataFrame.to_sql() now returns an int of the number of written rows (GH23998) ... GroupBy.apply() is designed to be flexible, allowing users to perform ......
Read more >
What's new in 0.25.0 (July 18, 2019) - Pandas
When passed DataFrames whose values are sparse, concat() will now return a Series or DataFrame with sparse values, rather than a SparseDataFrame (GH25702)....
Read more >
pandas.core.groupby.GroupBy.apply
Apply function func group-wise and combine the results together. The function passed to apply must take a dataframe as its first argument and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found