diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 6b2d2b6aad490..9b3f79836141b 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -946,7 +946,7 @@ Bug Fixes - Bug in ``is_superperiod`` and ``is_subperiod`` cannot handle higher frequencies than ``S`` (:issue:`7760`, :issue:`7772`, :issue:`7803`) - Bug in 32-bit platforms with ``Series.shift`` (:issue:`8129`) - Bug in ``PeriodIndex.unique`` returns int64 ``np.ndarray`` (:issue:`7540`) - +- Bug in groupby ``.apply`` with a non-affecting mutation in the function (:issue:`8467`) - Bug in ``DataFrame.reset_index`` which has ``MultiIndex`` contains ``PeriodIndex`` or ``DatetimeIndex`` with tz raises ``ValueError`` (:issue:`7746`, :issue:`7793`) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 2e107e0b0e935..d63bc554c771a 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -2851,8 +2851,9 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False): return concat(values) if not all_indexed_same: + # GH 8467 return self._concat_objects( - keys, values, not_indexed_same=not_indexed_same + keys, values, not_indexed_same=True, ) try: diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index 09763d53c017d..0e8b5d68e3fd7 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -2112,6 +2112,18 @@ def f_no_copy(x): grpby_no_copy = mydf.groupby('cat1').apply(f_no_copy) assert_series_equal(grpby_copy,grpby_no_copy) + def test_no_mutate_but_looks_like(self): + + # GH 8467 + # first show's mutation indicator + # second does not, but should yield the same results + df = DataFrame({'key': [1, 1, 1, 2, 2, 2, 3, 3, 3], + 'value': range(9)}) + + result1 = df.groupby('key', group_keys=True).apply(lambda x: x[:].key) + result2 = df.groupby('key', group_keys=True).apply(lambda x: x.key) + assert_series_equal(result1, result2) + def test_apply_chunk_view(self): # Low level tinkering could be unsafe, make sure not df = DataFrame({'key': [1, 1, 1, 2, 2, 2, 3, 3, 3],