Skip to content

Commit 3fb208a

Browse files
rhshadrachmliu08
authored andcommitted
REGR: DataFrameGroupBy.transform with reducer and as_index=False returns null values (pandas-dev#49864)
* REGR: groupby.transform with reducer returns null values * whatsnew * Better test
1 parent 34f04ac commit 3fb208a

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v1.5.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed performance regression in :meth:`Series.isin` when ``values`` is empty (:issue:`49839`)
17+
- Fixed regression in :meth:`DataFrameGroupBy.transform` when used with ``as_index=False`` (:issue:`49834`)
1718
-
1819

1920
.. ---------------------------------------------------------------------------

pandas/core/groupby/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,10 @@ def _transform(self, func, *args, engine=None, engine_kwargs=None, **kwargs):
18101810
# and deal with possible broadcasting below.
18111811
# Temporarily set observed for dealing with categoricals.
18121812
with com.temp_setattr(self, "observed", True):
1813-
result = getattr(self, func)(*args, **kwargs)
1813+
with com.temp_setattr(self, "as_index", True):
1814+
# GH#49834 - result needs groups in the index for
1815+
# _wrap_transform_fast_result
1816+
result = getattr(self, func)(*args, **kwargs)
18141817

18151818
return self._wrap_transform_fast_result(result)
18161819

pandas/tests/groupby/transform/test_transform.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def test_dispatch_transform(tsframe):
360360
tm.assert_frame_equal(filled, expected)
361361

362362

363-
def test_transform_transformation_func(request, transformation_func):
363+
def test_transform_transformation_func(transformation_func):
364364
# GH 30918
365365
df = DataFrame(
366366
{
@@ -1505,3 +1505,17 @@ def test_transform_aligns_depr(func, series, expected_values, keys, keys_in_inde
15051505
if series:
15061506
expected = expected["b"]
15071507
tm.assert_equal(result, expected)
1508+
1509+
1510+
@pytest.mark.parametrize("keys", ["A", ["A", "B"]])
1511+
def test_as_index_no_change(keys, df, groupby_func):
1512+
# GH#49834 - as_index should have no impact on DataFrameGroupBy.transform
1513+
if keys == "A":
1514+
# Column B is string dtype; will fail on some ops
1515+
df = df.drop(columns="B")
1516+
args = get_groupby_method_args(groupby_func, df)
1517+
gb_as_index_true = df.groupby(keys, as_index=True)
1518+
gb_as_index_false = df.groupby(keys, as_index=False)
1519+
result = gb_as_index_true.transform(groupby_func, *args)
1520+
expected = gb_as_index_false.transform(groupby_func, *args)
1521+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)