Skip to content

Commit 5724a2a

Browse files
committed
BUG: Bug in groupby .apply with a non-affecting mutation in the function (GH8467)
1 parent 9b024b5 commit 5724a2a

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ Bug Fixes
946946
- Bug in ``is_superperiod`` and ``is_subperiod`` cannot handle higher frequencies than ``S`` (:issue:`7760`, :issue:`7772`, :issue:`7803`)
947947
- Bug in 32-bit platforms with ``Series.shift`` (:issue:`8129`)
948948
- Bug in ``PeriodIndex.unique`` returns int64 ``np.ndarray`` (:issue:`7540`)
949-
949+
- Bug in groupby ``.apply`` with a non-affecting mutation in the function (:issue:`8467`)
950950
- Bug in ``DataFrame.reset_index`` which has ``MultiIndex`` contains ``PeriodIndex`` or ``DatetimeIndex`` with tz raises ``ValueError`` (:issue:`7746`, :issue:`7793`)
951951

952952

pandas/core/groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2851,8 +2851,9 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
28512851
return concat(values)
28522852

28532853
if not all_indexed_same:
2854+
# GH 8467
28542855
return self._concat_objects(
2855-
keys, values, not_indexed_same=not_indexed_same
2856+
keys, values, not_indexed_same=True,
28562857
)
28572858

28582859
try:

pandas/tests/test_groupby.py

+12
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,18 @@ def f_no_copy(x):
21122112
grpby_no_copy = mydf.groupby('cat1').apply(f_no_copy)
21132113
assert_series_equal(grpby_copy,grpby_no_copy)
21142114

2115+
def test_no_mutate_but_looks_like(self):
2116+
2117+
# GH 8467
2118+
# first show's mutation indicator
2119+
# second does not, but should yield the same results
2120+
df = DataFrame({'key': [1, 1, 1, 2, 2, 2, 3, 3, 3],
2121+
'value': range(9)})
2122+
2123+
result1 = df.groupby('key', group_keys=True).apply(lambda x: x[:].key)
2124+
result2 = df.groupby('key', group_keys=True).apply(lambda x: x.key)
2125+
assert_series_equal(result1, result2)
2126+
21152127
def test_apply_chunk_view(self):
21162128
# Low level tinkering could be unsafe, make sure not
21172129
df = DataFrame({'key': [1, 1, 1, 2, 2, 2, 3, 3, 3],

0 commit comments

Comments
 (0)