Skip to content

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

@princekf

Description

@princekf

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()

Details

[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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions