Skip to content

Commit 66458b6

Browse files
committed
BUG: groupby cython bug in _aggregate_group, unit test and raise up exceptions
1 parent 3fc528c commit 66458b6

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

pandas/src/groupby.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,10 @@ def _group_reorder(values, label_list):
340340
sorted_values = values.take(indexer)
341341
return sorted_values, sorted_labels
342342

343-
cdef void _aggregate_group(double_t *out, int32_t *counts, double_t *values,
343+
cdef int _aggregate_group(double_t *out, int32_t *counts, double_t *values,
344344
list labels, int start, int end, tuple shape,
345345
Py_ssize_t which, Py_ssize_t offset,
346-
agg_func func):
346+
agg_func func) except -1:
347347
cdef:
348348
ndarray[int32_t] axis
349349
cdef Py_ssize_t stride
@@ -352,7 +352,7 @@ cdef void _aggregate_group(double_t *out, int32_t *counts, double_t *values,
352352
if which == len(labels) - 1:
353353
axis = labels[which]
354354

355-
while axis[start] == -1 and start < end:
355+
while start < end and axis[start] == -1:
356356
start += 1
357357
func(out, counts, values, <int32_t*> axis.data, start, end, offset)
358358
else:

pandas/tests/test_groupby.py

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
assert_series_equal, assert_almost_equal)
1414
from pandas.core.panel import Panel
1515
from collections import defaultdict
16+
import pandas._tseries as lib
1617
import pandas.core.datetools as dt
1718
import numpy as np
1819

@@ -797,6 +798,14 @@ def f(x, q=None):
797798
assert_frame_equal(agg_result, expected)
798799
assert_frame_equal(apply_result, expected)
799800

801+
def test_cython_na_bug(self):
802+
values = np.random.randn(10)
803+
shape = (5, 5)
804+
label_list = [np.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2], dtype=np.int32),
805+
np.array([1, 2, 3, 4, 0, 1, 2, 3, 3, 4], dtype=np.int32)]
806+
807+
lib.group_aggregate(values, label_list, shape)
808+
800809
class TestPanelGroupBy(unittest.TestCase):
801810

802811
def setUp(self):

0 commit comments

Comments
 (0)