# Buggy code
def _concat_objects(self, keys, values, not_indexed_same: bool = False):
    from pandas.core.reshape.concat import concat

    def reset_identity(values):
        # reset the identities of the components
        # of the values to prevent aliasing
        for v in com.not_none(*values):
            ax = v._get_axis(self.axis)
            ax._reset_identity()
        return values

    if not not_indexed_same:
        result = concat(values, axis=self.axis)
        ax = self._selected_obj._get_axis(self.axis)

        if isinstance(result, Series):
            result = result.reindex(ax)
        else:
            # this is a very unfortunate situation
            # we have a multi-index that is NOT lexsorted
            # and we have a result which is duplicated
            # we can't reindex, so we resort to this
            # GH 14776
            if isinstance(ax, MultiIndex) and not ax.is_unique:
                indexer = algorithms.unique1d(result.index.get_indexer_for(ax.values))
                result = result.take(indexer, axis=self.axis)
            else:
                result = result.reindex(ax, axis=self.axis)

    elif self.group_keys:
        values = reset_identity(values)
        if self.as_index:
            # possible MI return case
            group_keys = keys
            group_levels = self.grouper.levels
            group_names = self.grouper.names

            result = concat(
                values,
                axis=self.axis,
                keys=group_keys,
                levels=group_levels,
                names=group_names,
                sort=False,
            )
        else:
            # GH5610, returns a MI, with the first level being a
            # range index
            keys = list(range(len(values)))
            result = concat(values, axis=self.axis, keys=keys)
    else:
        values = reset_identity(values)
        result = concat(values, axis=self.axis)

    if isinstance(result, Series) and self._selection_name is not None:
        result.name = self._selection_name

    return result


# Traceback (ValueError)
Traceback (most recent call last):
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 725, in apply
result = self._python_apply_general(f)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 745, in _python_apply_general
keys, values, not_indexed_same=mutated or self.mutated
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/groupby/generic.py", line 372, in _wrap_applied_output
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 955, in _concat_objects
result = result.reindex(ax, axis=self.axis)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/util/_decorators.py", line 221, in wrapper
return func(*args, **kwargs)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/frame.py", line 3976, in reindex
return super().reindex(**kwargs)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/generic.py", line 4514, in reindex
axes, level, limit, tolerance, method, fill_value, copy
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/frame.py", line 3864, in _reindex_axes
index, method, copy, level, fill_value, limit, tolerance
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/frame.py", line 3886, in _reindex_index
allow_dups=False,
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/generic.py", line 4577, in _reindex_with_indexers
copy=copy,
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 1251, in reindex_indexer
self.axes[axis]._can_reindex(indexer)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3362, in _can_reindex
raise ValueError("cannot reindex from a duplicate axis")
ValueError: cannot reindex from a duplicate axis

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/hm106930/ipv/web/groupby_bug.py", line 16, in <module>
df=df.groupby(['Y']).apply(lambda x: x)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 737, in apply
return self._python_apply_general(f)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 745, in _python_apply_general
keys, values, not_indexed_same=mutated or self.mutated
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/groupby/generic.py", line 372, in _wrap_applied_output
return self._concat_objects(keys, values, not_indexed_same=not_indexed_same)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/groupby/groupby.py", line 955, in _concat_objects
result = result.reindex(ax, axis=self.axis)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/util/_decorators.py", line 221, in wrapper
return func(*args, **kwargs)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/frame.py", line 3976, in reindex
return super().reindex(**kwargs)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/generic.py", line 4514, in reindex
axes, level, limit, tolerance, method, fill_value, copy
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/frame.py", line 3864, in _reindex_axes
index, method, copy, level, fill_value, limit, tolerance
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/frame.py", line 3886, in _reindex_index
allow_dups=False,
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/generic.py", line 4577, in _reindex_with_indexers
copy=copy,
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 1251, in reindex_indexer
self.axes[axis]._can_reindex(indexer)
File "/home/hm106930/.local/share/virtualenvs/ipv-work-62kMEXht/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3362, in _can_reindex
raise ValueError("cannot reindex from a duplicate axis")
ValueError: cannot reindex from a duplicate axis

# Fixed code
def _concat_objects(self, keys, values, not_indexed_same: bool = False):
    from pandas.core.reshape.concat import concat

    def reset_identity(values):
        # reset the identities of the components
        # of the values to prevent aliasing
        for v in com.not_none(*values):
            ax = v._get_axis(self.axis)
            ax._reset_identity()
        return values

    if not not_indexed_same:
        result = concat(values, axis=self.axis)
        ax = self._selected_obj._get_axis(self.axis)

        # this is a very unfortunate situation
        # we can't use reindex to restore the original order
        # when the ax has duplicates
        # so we resort to this
        # GH 14776, 30667
        if ax.has_duplicates:
            indexer, _ = result.index.get_indexer_non_unique(ax.values)
            indexer = algorithms.unique1d(indexer)
            result = result.take(indexer, axis=self.axis)
        else:
            result = result.reindex(ax, axis=self.axis)

    elif self.group_keys:
        values = reset_identity(values)
        if self.as_index:
            # possible MI return case
            group_keys = keys
            group_levels = self.grouper.levels
            group_names = self.grouper.names

            result = concat(
                values,
                axis=self.axis,
                keys=group_keys,
                levels=group_levels,
                names=group_names,
                sort=False,
            )
        else:
            # GH5610, returns a MI, with the first level being a
            # range index
            keys = list(range(len(values)))
            result = concat(values, axis=self.axis, keys=keys)
    else:
        values = reset_identity(values)
        result = concat(values, axis=self.axis)

    if isinstance(result, Series) and self._selection_name is not None:
        result.name = self._selection_name

    return result