@@ -31,7 +31,6 @@ class providing the base-class of operations.
31
31
from pandas .core .dtypes .common import (
32
32
ensure_float ,
33
33
is_datetime64_dtype ,
34
- is_datetime64tz_dtype ,
35
34
is_extension_array_dtype ,
36
35
is_integer_dtype ,
37
36
is_numeric_dtype ,
@@ -45,7 +44,6 @@ class providing the base-class of operations.
45
44
from pandas .core .arrays import Categorical , try_cast_to_ea
46
45
from pandas .core .base import DataError , PandasObject , SelectionMixin
47
46
import pandas .core .common as com
48
- from pandas .core .construction import extract_array
49
47
from pandas .core .frame import DataFrame
50
48
from pandas .core .generic import NDFrame
51
49
from pandas .core .groupby import base , ops
@@ -790,22 +788,11 @@ def _try_cast(self, result, obj, numeric_only: bool = False):
790
788
dtype = obj .dtype
791
789
792
790
if not is_scalar (result ):
793
- if is_datetime64tz_dtype (dtype ):
794
- # GH 23683
795
- # Prior results _may_ have been generated in UTC.
796
- # Ensure we localize to UTC first before converting
797
- # to the target timezone
798
- arr = extract_array (obj )
799
- try :
800
- result = arr ._from_sequence (result , dtype = "datetime64[ns, UTC]" )
801
- result = result .astype (dtype )
802
- except TypeError :
803
- # _try_cast was called at a point where the result
804
- # was already tz-aware
805
- pass
806
- elif is_extension_array_dtype (dtype ):
791
+ if is_extension_array_dtype (dtype ) and dtype .kind != "M" :
807
792
# The function can return something of any type, so check
808
- # if the type is compatible with the calling EA.
793
+ # if the type is compatible with the calling EA.
794
+ # datetime64tz is handled correctly in agg_series,
795
+ # so is excluded here.
809
796
810
797
# return the same type (Series) as our caller
811
798
cls = dtype .construct_array_type ()
@@ -872,7 +859,9 @@ def _cython_agg_general(
872
859
if numeric_only and not is_numeric :
873
860
continue
874
861
875
- result , names = self .grouper .aggregate (obj .values , how , min_count = min_count )
862
+ result , names = self .grouper .aggregate (
863
+ obj ._values , how , min_count = min_count
864
+ )
876
865
output [name ] = self ._try_cast (result , obj )
877
866
878
867
if len (output ) == 0 :
0 commit comments