@@ -18,15 +18,13 @@ cimport pandas._libs.util as util
18
18
from pandas._libs.lib import maybe_convert_objects
19
19
20
20
21
- cdef _get_result_array (object obj, Py_ssize_t size , Py_ssize_t cnt):
21
+ cdef _check_result_array (object obj, 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
26
raise ValueError (' Function does not reduce' )
27
27
28
- return np.empty(size, dtype = ' O' )
29
-
30
28
31
29
cdef bint _is_sparse_array(object obj):
32
30
# TODO can be removed one SparseArray.values is removed (GH26421)
@@ -116,6 +114,9 @@ cdef class Reducer:
116
114
has_index = self .index is not None
117
115
incr = self .increment
118
116
117
+ result = np.empty(self .nresults, dtype = ' O' )
118
+ it = < flatiter> PyArray_IterNew(result)
119
+
119
120
try :
120
121
for i in range (self .nresults):
121
122
@@ -158,10 +159,9 @@ cdef class Reducer:
158
159
and util.is_array(res.values)):
159
160
res = res.values
160
161
if i == 0 :
161
- result = _get_result_array(res,
162
- self .nresults,
163
- len (self .dummy))
164
- it = < flatiter> PyArray_IterNew(result)
162
+ # On the first pass, we check the output shape to see
163
+ # if this looks like a reduction.
164
+ _check_result_array(res, len (self .dummy))
165
165
166
166
PyArray_SETITEM(result, PyArray_ITER_DATA(it), res)
167
167
chunk.data = chunk.data + self .increment
@@ -170,9 +170,7 @@ cdef class Reducer:
170
170
# so we don't free the wrong memory
171
171
chunk.data = dummy_buf
172
172
173
- if result.dtype == np.object_:
174
- result = maybe_convert_objects(result)
175
-
173
+ result = maybe_convert_objects(result)
176
174
return result
177
175
178
176
@@ -275,6 +273,8 @@ cdef class SeriesBinGrouper(_BaseGrouper):
275
273
vslider = Slider(self .arr, self .dummy_arr)
276
274
islider = Slider(self .index, self .dummy_index)
277
275
276
+ result = np.empty(self .ngroups, dtype = ' O' )
277
+
278
278
try :
279
279
for i in range (self .ngroups):
280
280
group_size = counts[i]
@@ -289,10 +289,11 @@ cdef class SeriesBinGrouper(_BaseGrouper):
289
289
res = self .f(cached_typ)
290
290
res = _extract_result(res)
291
291
if not initialized:
292
+ # On the first pass, we check the output shape to see
293
+ # if this looks like a reduction.
292
294
initialized = 1
293
- result = _get_result_array(res,
294
- self .ngroups,
295
- len (self .dummy_arr))
295
+ _check_result_array(res, len (self .dummy_arr))
296
+
296
297
result[i] = res
297
298
298
299
islider.advance(group_size)
@@ -303,9 +304,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
303
304
islider.reset()
304
305
vslider.reset()
305
306
306
- if result.dtype == np.object_:
307
- result = maybe_convert_objects(result)
308
-
307
+ result = maybe_convert_objects(result)
309
308
return result, counts
310
309
311
310
@@ -368,6 +367,8 @@ cdef class SeriesGrouper(_BaseGrouper):
368
367
vslider = Slider(self .arr, self .dummy_arr)
369
368
islider = Slider(self .index, self .dummy_index)
370
369
370
+ result = np.empty(self .ngroups, dtype = ' O' )
371
+
371
372
try :
372
373
for i in range (n):
373
374
group_size += 1
@@ -391,10 +392,10 @@ cdef class SeriesGrouper(_BaseGrouper):
391
392
res = self .f(cached_typ)
392
393
res = _extract_result(res)
393
394
if not initialized:
395
+ # On the first pass, we check the output shape to see
396
+ # if this looks like a reduction.
394
397
initialized = 1
395
- result = _get_result_array(res,
396
- self .ngroups,
397
- len (self .dummy_arr))
398
+ _check_result_array(res, len (self .dummy_arr))
398
399
399
400
result[lab] = res
400
401
counts[lab] = group_size
@@ -410,10 +411,9 @@ cdef class SeriesGrouper(_BaseGrouper):
410
411
411
412
# We check for empty series in the constructor, so should always
412
413
# have result initialized by this point.
413
- assert result is not None , " `result` has not been assigned ."
414
+ assert initialized , " `result` has not been initialized ."
414
415
415
- if result.dtype == np.object_:
416
- result = maybe_convert_objects(result)
416
+ result = maybe_convert_objects(result)
417
417
418
418
return result, counts
419
419
0 commit comments