Skip to content

Commit 03e5858

Browse files
authored
BUG: groupby.apply on the NaN group drops values with original axes return (#38257)
1 parent ca3e351 commit 03e5858

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ Groupby/resample/rolling
772772
- Bug in :meth:`DataFrame.groupby` dropped ``nan`` groups from result with ``dropna=False`` when grouping over a single column (:issue:`35646`, :issue:`35542`)
773773
- Bug in :meth:`.DataFrameGroupBy.head`, :meth:`.DataFrameGroupBy.tail`, :meth:`SeriesGroupBy.head`, and :meth:`SeriesGroupBy.tail` would raise when used with ``axis=1`` (:issue:`9772`)
774774
- Bug in :meth:`.DataFrameGroupBy.transform` would raise when used with ``axis=1`` and a transformation kernel (e.g. "shift") (:issue:`36308`)
775+
- Bug in :meth:`.DataFrameGroupBy.apply` dropped values on ``nan`` group when returning the same axes with the original frame (:issue:`38227`)
775776
- Bug in :meth:`.DataFrameGroupBy.quantile` couldn't handle with arraylike ``q`` when grouping by columns (:issue:`33795`)
776777
- Bug in :meth:`DataFrameGroupBy.rank` with ``datetime64tz`` or period dtype incorrectly casting results to those dtypes instead of returning ``float64`` dtype (:issue:`38187`)
777778

pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ def reset_identity(values):
11841184

11851185
if not not_indexed_same:
11861186
result = concat(values, axis=self.axis)
1187-
ax = self._selected_obj._get_axis(self.axis)
1187+
ax = self.filter(lambda x: True).axes[self.axis]
11881188

11891189
# this is a very unfortunate situation
11901190
# we can't use reindex to restore the original order

pandas/tests/groupby/test_apply.py

+22
Original file line numberDiff line numberDiff line change
@@ -1087,3 +1087,25 @@ def test_apply_by_cols_equals_apply_by_rows_transposed():
10871087

10881088
tm.assert_frame_equal(by_cols, by_rows.T)
10891089
tm.assert_frame_equal(by_cols, df)
1090+
1091+
1092+
def test_apply_dropna_with_indexed_same():
1093+
# GH 38227
1094+
1095+
df = DataFrame(
1096+
{
1097+
"col": [1, 2, 3, 4, 5],
1098+
"group": ["a", np.nan, np.nan, "b", "b"],
1099+
},
1100+
index=list("xxyxz"),
1101+
)
1102+
result = df.groupby("group").apply(lambda x: x)
1103+
expected = DataFrame(
1104+
{
1105+
"col": [1, 4, 5],
1106+
"group": ["a", "b", "b"],
1107+
},
1108+
index=list("xxz"),
1109+
)
1110+
1111+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)