@@ -15,15 +15,15 @@ from numpy cimport (ndarray,
15
15
cnp.import_array()
16
16
17
17
cimport pandas._libs.util as util
18
- from pandas._libs.lib import maybe_convert_objects, values_from_object
18
+ from pandas._libs.lib import maybe_convert_objects
19
19
20
20
21
21
cdef _get_result_array(object obj, Py_ssize_t size, Py_ssize_t cnt):
22
22
23
23
if (util.is_array(obj) or
24
24
(isinstance (obj, list ) and len (obj) == cnt) or
25
25
getattr (obj, ' shape' , None ) == (cnt,)):
26
- raise ValueError (' function does not reduce' )
26
+ raise ValueError (' Function does not reduce' )
27
27
28
28
return np.empty(size, dtype = ' O' )
29
29
@@ -103,7 +103,7 @@ cdef class Reducer:
103
103
ndarray arr, result, chunk
104
104
Py_ssize_t i, incr
105
105
flatiter it
106
- bint has_labels
106
+ bint has_labels, has_ndarray_labels
107
107
object res, name, labels, index
108
108
object cached_typ= None
109
109
@@ -113,14 +113,18 @@ cdef class Reducer:
113
113
chunk.data = arr.data
114
114
labels = self .labels
115
115
has_labels = labels is not None
116
+ has_ndarray_labels = util.is_array(labels)
116
117
has_index = self .index is not None
117
118
incr = self .increment
118
119
119
120
try :
120
121
for i in range (self .nresults):
121
122
122
- if has_labels :
123
+ if has_ndarray_labels :
123
124
name = util.get_value_at(labels, i)
125
+ elif has_labels:
126
+ # labels is an ExtensionArray
127
+ name = labels[i]
124
128
else :
125
129
name = None
126
130
@@ -362,7 +366,8 @@ cdef class SeriesGrouper:
362
366
363
367
def get_result (self ):
364
368
cdef:
365
- ndarray arr, result
369
+ # Define result to avoid UnboundLocalError
370
+ ndarray arr, result = None
366
371
ndarray[int64_t] labels, counts
367
372
Py_ssize_t i, n, group_size, lab
368
373
object res
@@ -428,6 +433,9 @@ cdef class SeriesGrouper:
428
433
islider.reset()
429
434
vslider.reset()
430
435
436
+ if result is None :
437
+ raise ValueError (" No result." )
438
+
431
439
if result.dtype == np.object_:
432
440
result = maybe_convert_objects(result)
433
441
@@ -639,11 +647,11 @@ def compute_reduction(arr, f, axis=0, dummy=None, labels=None):
639
647
"""
640
648
641
649
if labels is not None :
642
- if labels._has_complex_internals:
643
- raise Exception ( ' Cannot use shortcut ' )
650
+ # Caller is responsible for ensuring we don't have MultiIndex
651
+ assert not labels._has_complex_internals
644
652
645
- # pass as an ndarray
646
- labels = values_from_object( labels)
653
+ # pass as an ndarray/ExtensionArray
654
+ labels = labels._values
647
655
648
656
reducer = Reducer(arr, f, axis = axis, dummy = dummy, labels = labels)
649
657
return reducer.get_result()
0 commit comments