Skip to content

Commit 55e0e25

Browse files
meeseeksmachinerhshadrachphofl
authored
Backport PR pandas-dev#49864 on branch 1.5.x (REGR: DataFrameGroupBy.transform with reducer and as_index=False returns null values) (pandas-dev#49868)
* Backport PR pandas-dev#49864: REGR: DataFrameGroupBy.transform with reducer and as_index=False returns null values * Add back request Co-authored-by: Richard Shadrach <[email protected]> Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 57f4186 commit 55e0e25

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
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
@@ -1838,7 +1838,10 @@ def _transform(self, func, *args, engine=None, engine_kwargs=None, **kwargs):
18381838
# and deal with possible broadcasting below.
18391839
# Temporarily set observed for dealing with categoricals.
18401840
with com.temp_setattr(self, "observed", True):
1841-
result = getattr(self, func)(*args, **kwargs)
1841+
with com.temp_setattr(self, "as_index", True):
1842+
# GH#49834 - result needs groups in the index for
1843+
# _wrap_transform_fast_result
1844+
result = getattr(self, func)(*args, **kwargs)
18421845

18431846
return self._wrap_transform_fast_result(result)
18441847

pandas/tests/groupby/transform/test_transform.py

+14
Original file line numberDiff line numberDiff line change
@@ -1549,3 +1549,17 @@ def test_transform_aligns_depr(func, series, expected_values, keys, keys_in_inde
15491549
if series:
15501550
expected = expected["b"]
15511551
tm.assert_equal(result, expected)
1552+
1553+
1554+
@pytest.mark.parametrize("keys", ["A", ["A", "B"]])
1555+
def test_as_index_no_change(keys, df, groupby_func):
1556+
# GH#49834 - as_index should have no impact on DataFrameGroupBy.transform
1557+
if keys == "A":
1558+
# Column B is string dtype; will fail on some ops
1559+
df = df.drop(columns="B")
1560+
args = get_groupby_method_args(groupby_func, df)
1561+
gb_as_index_true = df.groupby(keys, as_index=True)
1562+
gb_as_index_false = df.groupby(keys, as_index=False)
1563+
result = gb_as_index_true.transform(groupby_func, *args)
1564+
expected = gb_as_index_false.transform(groupby_func, *args)
1565+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)