BUG: Bug in groupby .apply with a non-affecting mutation in the function (GH8467)

closes #8467