diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 9c5806a3fe945..037c9c604161c 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -218,6 +218,8 @@ def apply_raw(self): if "Function does not reduce" not in str(err): # catch only ValueError raised intentionally in libreduction raise + # We expect np.apply_along_axis to give a two-dimensional result, or + # also raise. result = np.apply_along_axis(self.f, self.axis, self.values) # TODO: mixed type case diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 204346bb7b741..8674f0fd4985a 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -795,13 +795,13 @@ def _try_cast(self, result, obj, numeric_only: bool = False): # Ensure we localize to UTC first before converting # to the target timezone arr = extract_array(obj) - try: + if not is_datetime64tz_dtype(result.dtype): + # arr may have already been cast back to tzaware in _try_cast result = arr._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 + else: + result = arr + 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.