Skip to content

segmentation fault on groupby with categorical grouper of mismatched len #3017

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

Merged
2 commits merged into from Mar 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/v0.11.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ Bug Fixes
reporting (GH2807_)
- Fix pretty-printing of infinite data structures (closes GH2978_)
- str.contains ignored na argument (GH2806_)
- Substitute warning for segfault when grouping with categorical grouper
of mismatched length (GH3011_)


See the `full release notes
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
Expand All @@ -331,3 +334,4 @@ on GitHub for a complete list.
.. _GH2806: https://github.com/pydata/pandas/issues/2806
.. _GH2807: https://github.com/pydata/pandas/issues/2807
.. _GH2918: https://github.com/pydata/pandas/issues/2918
.. _GH3011: https://github.com/pydata/pandas/issues/3011
5 changes: 5 additions & 0 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,11 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True):
exclusions.append(gpr)
name = gpr
gpr = obj[gpr]

if (isinstance(gpr,Categorical) and len(gpr) != len(obj)):
errmsg = "Categorical grouper must have len(grouper) == len(data)"
raise AssertionError(errmsg)

ping = Grouping(group_axis, gpr, name=name, level=level, sort=sort)
groupings.append(ping)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,15 @@ def test_groupby_first_datetime64(self):
got_dt = result.dtype
self.assert_(issubclass(got_dt.type, np.datetime64))

def test_groupby_categorical_unequal_len(self):
import pandas as pd
#GH3011
series = Series([np.nan, np.nan, 1, 1, 2, 2, 3, 3, 4, 4])
bins = pd.cut(series.dropna(), 4)

# len(bins) != len(series) here
self.assertRaises(AssertionError,lambda : series.groupby(bins).mean())

def assert_fp_equal(a, b):
assert((np.abs(a - b) < 1e-12).all())

Expand Down