Skip to content

Commit debed82

Browse files
committed
pandas-dev#34951 bug fixed and test added
1 parent 9827ce0 commit debed82

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,7 @@ Groupby/resample/rolling
10911091
- Bug in :meth:`Rolling.apply` where ``center=True`` was ignored when ``engine='numba'`` was specified (:issue:`34784`)
10921092
- Bug in :meth:`DataFrame.ewm.cov` was throwing ``AssertionError`` for :class:`MultiIndex` inputs (:issue:`34440`)
10931093
- Bug in :meth:`core.groupby.DataFrameGroupBy.transform` when ``func='nunique'`` and columns are of type ``datetime64``, the result would also be of type ``datetime64`` instead of ``int64`` (:issue:`35109`)
1094+
- Bug in :meth:'DataFrameGroupBy.first' and :meth:'DataFrameGroupBy.last' that would raise ``ValueError`` grouping on multiple ``Categoricals`` (:issue:`34951`)
10941095

10951096
Reshaping
10961097
^^^^^^^^^

pandas/core/groupby/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ def _cython_agg_blocks(
10581058
# reductions; see GH#28949
10591059
obj = obj.iloc[:, 0]
10601060

1061-
s = get_groupby(obj, self.grouper)
1061+
s = get_groupby(obj, self.grouper, observed=True)
10621062
try:
10631063
result = s.aggregate(lambda x: alt(x, axis=self.axis))
10641064
except TypeError:

pandas/tests/groupby/test_categorical.py

+23
Original file line numberDiff line numberDiff line change
@@ -1669,3 +1669,26 @@ def test_categorical_transform():
16691669
expected["status"] = expected["status"].astype(delivery_status_type)
16701670

16711671
tm.assert_frame_equal(result, expected)
1672+
1673+
1674+
@pytest.mark.parametrize("func", ["first", "last"])
1675+
def test_groupby_first_on_categorical_col_grouped_on_2_categoricals(func: str):
1676+
1677+
cat = pd.Categorical([0, 0, 1, 1])
1678+
val = [0, 1, 1, 0]
1679+
df = pd.DataFrame({"a": cat, "b": cat, "c": val})
1680+
1681+
idx = pd.Categorical([0, 1])
1682+
idx = pd.MultiIndex.from_product([idx, idx], names=["a", "b"])
1683+
expected = {
1684+
"first": pd.Series([0, np.NaN, np.NaN, 1], idx, name="c"),
1685+
"last": pd.Series([1, np.NaN, np.NaN, 0], idx, name="c"),
1686+
}
1687+
1688+
df_grp = df.groupby(["a", "b"])
1689+
df_res = getattr(df_grp, func)()
1690+
tm.assert_frame_equal(df_res, expected[func].to_frame())
1691+
1692+
srs_grp = df_grp["c"]
1693+
srs_res = getattr(srs_grp, func)()
1694+
tm.assert_series_equal(srs_res, expected[func])

0 commit comments

Comments
 (0)