@@ -16,12 +16,12 @@ from pandas._libs cimport util
16
16
from pandas._libs.lib import is_scalar, maybe_convert_objects
17
17
18
18
19
- cdef _check_result_array (object obj, Py_ssize_t cnt):
19
+ cpdef check_result_array (object obj, Py_ssize_t cnt):
20
20
21
21
if (util.is_array(obj) or
22
22
(isinstance (obj, list ) and len (obj) == cnt) or
23
23
getattr (obj, ' shape' , None ) == (cnt,)):
24
- raise ValueError (' Function does not reduce ' )
24
+ raise ValueError (' Must produce aggregated value ' )
25
25
26
26
27
27
cdef class _BaseGrouper:
@@ -74,12 +74,14 @@ cdef class _BaseGrouper:
74
74
cached_ityp._engine.clear_mapping()
75
75
cached_ityp._cache.clear() # e.g. inferred_freq must go
76
76
res = self .f(cached_typ)
77
- res = _extract_result (res)
77
+ res = extract_result (res)
78
78
if not initialized:
79
79
# On the first pass, we check the output shape to see
80
80
# if this looks like a reduction.
81
81
initialized = True
82
- _check_result_array(res, len (self .dummy_arr))
82
+ # In all tests other than test_series_grouper and
83
+ # test_series_bin_grouper, we have len(self.dummy_arr) == 0
84
+ check_result_array(res, len (self .dummy_arr))
83
85
84
86
return res, initialized
85
87
@@ -278,9 +280,14 @@ cdef class SeriesGrouper(_BaseGrouper):
278
280
return result, counts
279
281
280
282
281
- cdef inline _extract_result (object res, bint squeeze = True ):
283
+ cpdef inline extract_result (object res, bint squeeze = True ):
282
284
""" extract the result object, it might be a 0-dim ndarray
283
285
or a len-1 0-dim, or a scalar """
286
+ if hasattr (res, " _values" ):
287
+ # Preserve EA
288
+ res = res._values
289
+ if squeeze and res.ndim == 1 and len (res) == 1 :
290
+ res = res[0 ]
284
291
if hasattr (res, ' values' ) and util.is_array(res.values):
285
292
res = res.values
286
293
if util.is_array(res):
0 commit comments