Skip to content

Commit 9d5552c

Browse files
committed
Moved test, changed whatsnew to api breaking section
1 parent 680c3c6 commit 9d5552c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+28
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,34 @@ Assignment to multiple columns of a :class:`DataFrame` when some of the columns
552552
df[['a', 'c']] = 1
553553
df
554554
555+
.. _whatsnew_110.api_breaking.groupby_as_index_false:
556+
557+
Using groupby with ``as_index=False``
558+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
559+
560+
Using :meth:`DataFrame.groupby` with ``as_index=False`` and the function ``idxmax``, ``idxmin``, ``mad``, ``nunique``, or ``skew`` would modify the grouping column. Now, the grouping column remains unchanged. (:issue:`21090`)
561+
562+
.. ipython:: python
563+
564+
df = pd.DataFrame({"a": ["x", "x", "y", "y"], "b": [1, 1, 2, 3]})
565+
df
566+
567+
*Previous behavior*:
568+
569+
.. code-block:: ipython
570+
571+
In [3]: df.groupby("a", as_index=False).nunique()
572+
Out[4]:
573+
a b
574+
0 1 1
575+
1 1 2
576+
577+
*New behavior*:
578+
579+
.. ipython:: python
580+
581+
df.groupby("a", as_index=False).nunique()
582+
555583
.. _whatsnew_110.deprecations:
556584

557585
Deprecations

pandas/tests/groupby/test_groupby.py

+31
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,37 @@ def test_groupby_as_index_agg(df):
658658
tm.assert_frame_equal(left, right)
659659

660660

661+
def test_ops_not_as_index(reduction_func):
662+
# GH 21090
663+
# Using as_index=False should not modify grouped column
664+
665+
if reduction_func in ("corrwith",):
666+
pytest.skip("Test not applicable")
667+
668+
if reduction_func in ("nth", "ngroup", "size",):
669+
pytest.skip("Skip until behavior is determined (GH #5755)")
670+
671+
if reduction_func in ("sem", "std"):
672+
pytest.skip("Function incorrectly modifies keys (GH #10355)")
673+
674+
df = DataFrame(np.random.randint(0, 5, size=(100, 2)), columns=["a", "b"])
675+
expected = getattr(df.groupby("a"), reduction_func)().reset_index()
676+
677+
g = df.groupby("a", as_index=False)
678+
679+
result = getattr(g, reduction_func)()
680+
tm.assert_frame_equal(result, expected)
681+
682+
result = g.agg(reduction_func)
683+
tm.assert_frame_equal(result, expected)
684+
685+
result = getattr(g["b"], reduction_func)()
686+
tm.assert_frame_equal(result, expected)
687+
688+
result = g["b"].agg(reduction_func)
689+
tm.assert_frame_equal(result, expected)
690+
691+
661692
def test_as_index_series_return_frame(df):
662693
grouped = df.groupby("A", as_index=False)
663694
grouped2 = df.groupby(["A", "B"], as_index=False)

0 commit comments

Comments
 (0)