41
41
ArrayLike ,
42
42
FrameOrSeries ,
43
43
FrameOrSeriesUnion ,
44
+ Manager ,
44
45
)
45
46
from pandas .util ._decorators import (
46
47
Appender ,
107
108
all_indexes_same ,
108
109
)
109
110
import pandas .core .indexes .base as ibase
110
- from pandas .core .internals import BlockManager
111
+ from pandas .core .internals import (
112
+ ArrayManager ,
113
+ BlockManager ,
114
+ )
111
115
from pandas .core .series import Series
112
116
from pandas .core .util .numba_ import maybe_use_numba
113
117
@@ -1074,20 +1078,22 @@ def _iterate_slices(self) -> Iterable[Series]:
1074
1078
def _cython_agg_general (
1075
1079
self , how : str , alt = None , numeric_only : bool = True , min_count : int = - 1
1076
1080
) -> DataFrame :
1077
- agg_mgr = self ._cython_agg_blocks (
1081
+ agg_mgr = self ._cython_agg_manager (
1078
1082
how , alt = alt , numeric_only = numeric_only , min_count = min_count
1079
1083
)
1080
1084
return self ._wrap_agged_manager (agg_mgr )
1081
1085
1082
- def _cython_agg_blocks (
1086
+ def _cython_agg_manager (
1083
1087
self , how : str , alt = None , numeric_only : bool = True , min_count : int = - 1
1084
- ) -> BlockManager :
1088
+ ) -> Manager :
1085
1089
1086
- data : BlockManager = self ._get_data_to_aggregate ()
1090
+ data : Manager = self ._get_data_to_aggregate ()
1087
1091
1088
1092
if numeric_only :
1089
1093
data = data .get_numeric_data (copy = False )
1090
1094
1095
+ using_array_manager = isinstance (data , ArrayManager )
1096
+
1091
1097
def cast_agg_result (result , values : ArrayLike , how : str ) -> ArrayLike :
1092
1098
# see if we can cast the values to the desired dtype
1093
1099
# this may not be the original dtype
@@ -1101,7 +1107,11 @@ def cast_agg_result(result, values: ArrayLike, how: str) -> ArrayLike:
1101
1107
result = type (values )._from_sequence (result .ravel (), dtype = values .dtype )
1102
1108
# Note this will have result.dtype == dtype from above
1103
1109
1104
- elif isinstance (result , np .ndarray ) and result .ndim == 1 :
1110
+ elif (
1111
+ not using_array_manager
1112
+ and isinstance (result , np .ndarray )
1113
+ and result .ndim == 1
1114
+ ):
1105
1115
# We went through a SeriesGroupByPath and need to reshape
1106
1116
# GH#32223 includes case with IntegerArray values
1107
1117
result = result .reshape (1 , - 1 )
@@ -1153,11 +1163,11 @@ def py_fallback(bvalues: ArrayLike) -> ArrayLike:
1153
1163
result = mgr .blocks [0 ].values
1154
1164
return result
1155
1165
1156
- def blk_func ( bvalues : ArrayLike ) -> ArrayLike :
1166
+ def array_func ( values : ArrayLike ) -> ArrayLike :
1157
1167
1158
1168
try :
1159
1169
result = self .grouper ._cython_operation (
1160
- "aggregate" , bvalues , how , axis = 1 , min_count = min_count
1170
+ "aggregate" , values , how , axis = 1 , min_count = min_count
1161
1171
)
1162
1172
except NotImplementedError :
1163
1173
# generally if we have numeric_only=False
@@ -1170,14 +1180,14 @@ def blk_func(bvalues: ArrayLike) -> ArrayLike:
1170
1180
assert how == "ohlc"
1171
1181
raise
1172
1182
1173
- result = py_fallback (bvalues )
1183
+ result = py_fallback (values )
1174
1184
1175
- return cast_agg_result (result , bvalues , how )
1185
+ return cast_agg_result (result , values , how )
1176
1186
1177
1187
# TypeError -> we may have an exception in trying to aggregate
1178
1188
# continue and exclude the block
1179
1189
# NotImplementedError -> "ohlc" with wrong dtype
1180
- new_mgr = data .grouped_reduce (blk_func , ignore_failures = True )
1190
+ new_mgr = data .grouped_reduce (array_func , ignore_failures = True )
1181
1191
1182
1192
if not len (new_mgr ):
1183
1193
raise DataError ("No numeric types to aggregate" )
@@ -1670,7 +1680,7 @@ def _wrap_frame_output(self, result, obj: DataFrame) -> DataFrame:
1670
1680
else :
1671
1681
return self .obj ._constructor (result , index = obj .index , columns = result_index )
1672
1682
1673
- def _get_data_to_aggregate (self ) -> BlockManager :
1683
+ def _get_data_to_aggregate (self ) -> Manager :
1674
1684
obj = self ._obj_with_exclusions
1675
1685
if self .axis == 1 :
1676
1686
return obj .T ._mgr
@@ -1755,17 +1765,17 @@ def _wrap_transformed_output(
1755
1765
1756
1766
return result
1757
1767
1758
- def _wrap_agged_manager (self , mgr : BlockManager ) -> DataFrame :
1768
+ def _wrap_agged_manager (self , mgr : Manager ) -> DataFrame :
1759
1769
if not self .as_index :
1760
1770
index = np .arange (mgr .shape [1 ])
1761
- mgr .axes [ 1 ] = ibase .Index (index )
1771
+ mgr .set_axis ( 1 , ibase .Index (index ), verify_integrity = False )
1762
1772
result = self .obj ._constructor (mgr )
1763
1773
1764
1774
self ._insert_inaxis_grouper_inplace (result )
1765
1775
result = result ._consolidate ()
1766
1776
else :
1767
1777
index = self .grouper .result_index
1768
- mgr .axes [ 1 ] = index
1778
+ mgr .set_axis ( 1 , index , verify_integrity = False )
1769
1779
result = self .obj ._constructor (mgr )
1770
1780
1771
1781
if self .axis == 1 :
0 commit comments