Skip to content

Commit 582da12

Browse files
committed
test for 8669
1 parent 2dcdabb commit 582da12

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ Other Enhancements
407407
- Updated ``to_gbq`` and ``read_gbq`` signature and documentation to reflect changes from
408408
the Pandas-GBQ library version 0.4.0. Adds intersphinx mapping to Pandas-GBQ
409409
library. (:issue:`20564`)
410-
410+
411411
.. _whatsnew_0230.api_breaking:
412412

413413
Backwards incompatible API changes
@@ -487,7 +487,7 @@ If you wish to retain the old behavior while using Python >= 3.6, you can use
487487
Categorical Grouping no longer expands to all possible groupers
488488
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
489489

490-
In previous versions, grouping by 1 or more categorical columns would result in an index that was the cartesian product of all of the categories for each grouper, not just the observed values. This is inconsistent with output for other dtypes, can potentially cast to different dtypes (as missing values are introduced), and could cause a huge frame to be generated. Pandas will now return only the observed values, regardless if grouping on a categorical column; note that the categorical dtype is *still* preserved. You will still have a categorical columns (:issue:`14942`). This reverts issues (:issue:`10132`, :issue:`8138`, :issue:`14942`, :issue:`15217`, :issue:`17594`)
490+
In previous versions, grouping by 1 or more categorical columns would result in an index that was the cartesian product of all of the categories for each grouper, not just the observed values. This is inconsistent with output for other dtypes, can potentially cast to different dtypes (as missing values are introduced), and could cause a huge frame to be generated. Pandas will now return only the observed values, regardless if grouping on a categorical column; note that the categorical dtype is *still* preserved. You will still have a categorical columns (:issue:`14942`, :issue:`8138`, :issue:`15217`, :issue:`17594`, :issue:`8669`)
491491

492492

493493

pandas/tests/groupby/test_categorical.py

+15
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def test_with_multiple_groupers_no_expand():
226226
# gh-14942 (implement)
227227
# gh-10132 (revert)
228228
# gh-8138 (revert)
229+
# gh-8869
229230

230231
cat1 = Categorical(["a", "a", "b", "b"],
231232
categories=["a", "b", "z"], ordered=True)
@@ -316,6 +317,20 @@ def test_with_multiple_groupers_no_expand():
316317
"C3": [10, 100, 200, 34]}, index=idx)
317318
tm.assert_frame_equal(res, exp)
318319

320+
# gh-8869
321+
# with as_index
322+
d = {'foo': [10, 8, 4, 8, 4, 1, 1], 'bar': [10, 20, 30, 40, 50, 60, 70],
323+
'baz': ['d', 'c', 'e', 'a', 'a', 'd', 'c']}
324+
df = pd.DataFrame(d)
325+
cat = pd.cut(df['foo'], np.linspace(0, 10, 3))
326+
df['range'] = cat
327+
groups = df.groupby(['range', 'baz'], as_index=False)
328+
result = groups.agg('mean')
329+
330+
groups2 = df.groupby(['range', 'baz'], as_index=True)
331+
expected = groups2.agg('mean').reset_index()
332+
tm.assert_frame_equal(result, expected)
333+
319334

320335
def test_datetime():
321336
# GH9049: ensure backward compatibility

0 commit comments

Comments
 (0)