# Buggy code
def _try_cast(self, result, obj, numeric_only=False):
    """
    Try to cast the result to our obj original type,
    we may have roundtripped through object in the mean-time.

    If numeric_only is True, then only try to cast numerics
    and not datetimelikes.

    """
    if obj.ndim > 1:
        dtype = obj._values.dtype
    else:
        dtype = obj.dtype

    if not is_scalar(result):
        if is_extension_array_dtype(dtype):
            # The function can return something of any type, so check
            # if the type is compatible with the calling EA.
            try:
                result = obj._values._from_sequence(result, dtype=dtype)
            except Exception:
                # https://github.com/pandas-dev/pandas/issues/22850
                # pandas has no control over what 3rd-party ExtensionArrays
                # do in _values_from_sequence. We still want ops to work
                # though, so we catch any regular Exception.
                pass
        elif numeric_only and is_numeric_dtype(dtype) or not numeric_only:
            result = maybe_downcast_to_dtype(result, dtype)

    return result


# Traceback (ValueError)
Traceback (most recent call last):
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 2670, in agg_series
return self._aggregate_series_fast(obj, func)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 2689, in _aggregate_series_fast
dummy)
File "pandas/_libs/reduction.pyx", line 334, in pandas._libs.reduction.SeriesGrouper.__init__
File "pandas/_libs/reduction.pyx", line 347, in pandas._libs.reduction.SeriesGrouper._check_dummy
ValueError: Dummy array must be same dtype

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 3495, in aggregate
return self._python_agg_general(func_or_funcs, *args, **kwargs)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 1068, in _python_agg_general
result, counts = self.grouper.agg_series(obj, f)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 2672, in agg_series
return self._aggregate_series_pure_python(obj, func)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 2706, in _aggregate_series_pure_python
raise ValueError('Function does not reduce')
ValueError: Function does not reduce

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 4656, in aggregate
return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 4087, in aggregate
result, how = self._aggregate(arg, _level=_level, *args, **kwargs)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/base.py", line 490, in _aggregate
result = _agg(arg, _agg_1dim)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/base.py", line 441, in _agg
result[fname] = func(fname, agg_how)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/base.py", line 424, in _agg_1dim
return colg.aggregate(how, _level=(_level or 0) + 1)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 3497, in aggregate
result = self._aggregate_named(func_or_funcs, *args, **kwargs)
File "~/.pyenv/versions/anaconda3-5.3.0/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 3627, in _aggregate_named
raise Exception('Must produce aggregated value')
Exception: Must produce aggregated value

# Fixed code
def _try_cast(self, result, obj, numeric_only=False):
    """
    Try to cast the result to our obj original type,
    we may have roundtripped through object in the mean-time.

    If numeric_only is True, then only try to cast numerics
    and not datetimelikes.

    """
    if obj.ndim > 1:
        dtype = obj._values.dtype
    else:
        dtype = obj.dtype

    if not is_scalar(result):
        if is_datetime64tz_dtype(dtype):
            # GH 23683
            # Prior results _may_ have been generated in UTC.
            # Ensure we localize to UTC first before converting
            # to the target timezone
            try:
                result = obj._values._from_sequence(result, dtype="datetime64[ns, UTC]")
                result = result.astype(dtype)
            except TypeError:
                # _try_cast was called at a point where the result
                # was already tz-aware
                pass
        elif is_extension_array_dtype(dtype):
            # The function can return something of any type, so check
            # if the type is compatible with the calling EA.
            try:
                result = obj._values._from_sequence(result, dtype=dtype)
            except Exception:
                # https://github.com/pandas-dev/pandas/issues/22850
                # pandas has no control over what 3rd-party ExtensionArrays
                # do in _values_from_sequence. We still want ops to work
                # though, so we catch any regular Exception.
                pass
        elif numeric_only and is_numeric_dtype(dtype) or not numeric_only:
            result = maybe_downcast_to_dtype(result, dtype)

    return result