Skip to content

Group by a categorical Series of unequal length #44180

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
merged 6 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ Groupby/resample/rolling
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` for centered datetimelike windows with uneven nanosecond (:issue:`43997`)
- Bug in :meth:`GroupBy.nth` failing on ``axis=1`` (:issue:`43926`)
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not respecting right bound on centered datetime-like windows, if the index contain duplicates (:issue:`#3944`)

- Enabled grouping by a :class:`Series` that has a categorical data type and length unequal to the axis of grouping (:issue:`44179`); also, with this fix, the ``ValueError`` raised when grouping by a :class:`Categorical` with unequal length now has the same message as when grouping by other sequences

Reshaping
^^^^^^^^^
Expand Down
8 changes: 1 addition & 7 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,12 +887,6 @@ def is_in_obj(gpr) -> bool:
else:
in_axis = False

if is_categorical_dtype(gpr) and len(gpr) != obj.shape[axis]:
raise ValueError(
f"Length of grouper ({len(gpr)}) and axis ({obj.shape[axis]}) "
"must be same length"
)

# create the Grouping
# allow us to passing the actual Grouping as the gpr
ping = (
Expand Down Expand Up @@ -938,7 +932,7 @@ def _convert_grouper(axis: Index, grouper):
return grouper.reindex(axis)._values
elif isinstance(grouper, MultiIndex):
return grouper._values
elif isinstance(grouper, (list, tuple, Series, Index, np.ndarray)):
elif isinstance(grouper, (list, tuple, Index, Categorical, np.ndarray)):
if len(grouper) != len(axis):
raise ValueError("Grouper and axis must be same length")

Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,16 @@ def test_bins_unequal_len():
bins = pd.cut(series.dropna().values, 4)

# len(bins) != len(series) here
msg = r"Length of grouper \(8\) and axis \(10\) must be same length"
with pytest.raises(ValueError, match=msg):
with pytest.raises(ValueError, match=r"Grouper and axis must be same length"):
series.groupby(bins).mean()


def test_categorical_series_unequal_len():
# GH44179
groupby = Series(range(7)).groupby(Series(list("ABBA"), dtype="category"))
tm.assert_dict_equal(groupby.groups, {"A": [0, 3], "B": [1, 2]})


def test_as_index():
# GH13204
df = DataFrame(
Expand Down