From 47a5e3a62bcca4d4e3e8080a2635df12e554722a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 12 Nov 2019 17:03:20 -0800 Subject: [PATCH 1/4] check instead of try/except --- pandas/core/groupby/groupby.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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. From a13f2e623520ac17d44f32e6731841b392dfcba8 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 12 Nov 2019 17:04:55 -0800 Subject: [PATCH 2/4] comment --- pandas/core/apply.py | 2 ++ 1 file changed, 2 insertions(+) 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 From dc758e3be7b9d709a1b13e2ba68b303a8b1ea98d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 12 Nov 2019 17:11:02 -0800 Subject: [PATCH 3/4] revert --- pandas/core/apply.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 037c9c604161c..9c5806a3fe945 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -218,8 +218,6 @@ 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 From 21b14cf058712d31b80bf45120dba05812d2e527 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 12 Nov 2019 20:37:39 -0800 Subject: [PATCH 4/4] comment --- pandas/core/apply.py | 2 ++ 1 file changed, 2 insertions(+) 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