@@ -16,12 +16,12 @@ from pandas._libs cimport util
1616from pandas._libs.lib import is_scalar, maybe_convert_objects
1717
1818
19- cdef _check_result_array (object obj, Py_ssize_t cnt):
19+ cpdef check_result_array (object obj, Py_ssize_t cnt):
2020
2121 if (util.is_array(obj) or
2222 (isinstance (obj, list ) and len (obj) == cnt) or
2323 getattr (obj, ' shape' , None ) == (cnt,)):
24- raise ValueError (' Function does not reduce ' )
24+ raise ValueError (' Must produce aggregated value ' )
2525
2626
2727cdef class _BaseGrouper:
@@ -74,12 +74,14 @@ cdef class _BaseGrouper:
7474 cached_ityp._engine.clear_mapping()
7575 cached_ityp._cache.clear() # e.g. inferred_freq must go
7676 res = self .f(cached_typ)
77- res = _extract_result (res)
77+ res = extract_result (res)
7878 if not initialized:
7979 # On the first pass, we check the output shape to see
8080 # if this looks like a reduction.
8181 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))
8385
8486 return res, initialized
8587
@@ -278,9 +280,14 @@ cdef class SeriesGrouper(_BaseGrouper):
278280 return result, counts
279281
280282
281- cdef inline _extract_result (object res, bint squeeze = True ):
283+ cpdef inline extract_result (object res, bint squeeze = True ):
282284 """ extract the result object, it might be a 0-dim ndarray
283285 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 ]
284291 if hasattr (res, ' values' ) and util.is_array(res.values):
285292 res = res.values
286293 if util.is_array(res):
0 commit comments