Skip to content

Gropuby using pd.Series.mode throws error when by column contains values with same starting value #29635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
princekf opened this issue Nov 15, 2019 · 4 comments · Fixed by #46721
Assignees
Labels
good first issue Groupby Needs Tests Unit test(s) needed to prevent regressions

Comments

@princekf
Copy link

Groupby with pd.Series.mode throws error, if by values starts with a value from previous row.

df2 = pd.DataFrame({
    "Name" : ['Thomas', 'Thomas', 'Thomas John'],
    "Credit" : [1200, 1300, 900],
    "Mood" : ['sad', 'happy', 'happy']
})
aggrFDColumnDetails = {
   'Mood':pd.Series.mode,
   'Credit':'sum'
}
df2.groupby(['Name']).agg(aggrFDColumnDetails)

Problem description

When I execute the above code I got error

Exception: Must produce aggregated value
.....
....
and a lot of stack traces..

If I change the third name to John instead of Thomas John, it works as expected. ie The following code works.

df2 = pd.DataFrame({
    "Name" : ['Thomas', 'Thomas', 'John'],
    "Credit" : [1200, 1300, 900],
    "Mood" : ['sad', 'happy', 'happy']
})
aggrFDColumnDetails = {
   'Mood':pd.Series.mode,
   'Credit':'sum'
}
df2.groupby(['Name']).agg(aggrFDColumnDetails)

Note: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

Note: Many problems can be resolved by simply upgrading pandas to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if master addresses this issue, but that is not necessary.

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

Groupby should work, even if first letters of column values are same

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 2.7.16.final.0
python-bits: 64
OS: Darwin
OS-release: 19.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.23.3
pytest: None
pip: 10.0.1
setuptools: 40.0.0
Cython: None
numpy: 1.14.5
scipy: 0.13.0b1
pyarrow: None
xarray: None
IPython: 5.7.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@wesbarnett
Copy link

wesbarnett commented Nov 15, 2019

This bug also occurs even in this situation where the names have no overlap:

df2 = pd.DataFrame({
    "Name" : ['A', 'A', 'B'],
    "Credit" : [1200, 1300, 900],
    "Mood" : ['sad', 'happy', 'happy']
})
aggrFDColumnDetails = {
   'Mood':pd.Series.mode,
   'Credit':'sum'
}
df2.groupby(['Name']).agg(aggrFDColumnDetails)

But this works:

df2 = pd.DataFrame({
    "Name" : ['B', 'A', 'B'],
    "Credit" : [1200, 1300, 900],
    "Mood" : ['sad', 'happy', 'happy']
})
aggrFDColumnDetails = {
   'Mood':pd.Series.mode,
   'Credit':'sum'
}
df2.groupby(['Name']).agg(aggrFDColumnDetails)

And this works:

df2 = pd.DataFrame({
    "Name" : ['B', 'B', 'A'],
    "Credit" : [1200, 1300, 900],
    "Mood" : ['sad', 'happy', 'happy']
})
aggrFDColumnDetails = {
   'Mood':pd.Series.mode,
   'Credit':'sum'
}
df2.groupby(['Name']).agg(aggrFDColumnDetails)

I actually tried several combinations of what labels are where and some work and some don't.

Installed versions ``` INSTALLED VERSIONS ------------------ commit : None python : 3.7.4.final.0 python-bits : 64 OS : Darwin OS-release : 18.7.0 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.17.4
pytz : 2019.2
dateutil : 2.8.0
pip : 19.3
setuptools : 41.2.0
Cython : 0.29.13
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.8.3 (dt dec pq3 ext lo64)
jinja2 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : 4.8.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.1
sqlalchemy : 1.3.8
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

</details>

@mroeschke
Copy link
Member

I suppose this works on master now. Could use a test

In [6]: df2 = pd.DataFrame({
   ...:     "Name" : ['Thomas', 'Thomas', 'Thomas John'],
   ...:     "Credit" : [1200, 1300, 900],
   ...:     "Mood" : ['sad', 'happy', 'happy']
   ...: })
   ...: aggrFDColumnDetails = {
   ...:    'Mood':pd.Series.mode,
   ...:    'Credit':'sum'
   ...: }
   ...: df2.groupby(['Name']).agg(aggrFDColumnDetails)
Out[6]:
                     Mood  Credit
Name
Thomas       [happy, sad]    2500
Thomas John         happy     900

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions labels Jul 23, 2021
@jxb4892
Copy link
Contributor

jxb4892 commented Aug 12, 2021

take
+) Does this issue only need more tests?

@FactorizeD
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Groupby Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants