Skip to content

Commit ac26c84

Browse files
committed
BUG: check for non-contiguous memory in SeriesGrouper, causing segfault
1 parent 81a4853 commit ac26c84

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2856,7 +2856,7 @@ def append(self, other, ignore_index=False, verify_integrity=True):
28562856
other = other.reindex(self.columns, copy=False)
28572857
other = DataFrame(other.values.reshape((1, len(other))),
28582858
index=index, columns=self.columns)
2859-
elif isinstance(other, list):
2859+
elif isinstance(other, list) and not isinstance(other[0], DataFrame):
28602860
other = DataFrame(other)
28612861
if (self.columns.get_indexer(other.columns) >= 0).all():
28622862
other = other.ix[:, self.columns]

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ def _aggregate_series_fast(obj, func, group_index, ngroups):
13311331
raise TypeError('Incompatible index for Cython grouper')
13321332

13331333
# avoids object / Series creation overhead
1334-
dummy = obj[:0]
1334+
dummy = obj[:0].copy()
13351335
indexer = lib.groupsort_indexer(group_index, ngroups)[0]
13361336
obj = obj.take(indexer)
13371337
group_index = group_index.take(indexer)

pandas/src/reduce.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ cdef class SeriesGrouper:
106106

107107
self.labels = labels
108108
self.f = f
109+
if not series.flags.c_contiguous:
110+
series = series.copy('C')
109111
self.arr = series
110112
self.index = series.index
111113

@@ -119,6 +121,9 @@ cdef class SeriesGrouper:
119121
else:
120122
if dummy.dtype != self.arr.dtype:
121123
raise ValueError('Dummy array must be same dtype')
124+
if not dummy.flags.contiguous:
125+
dummy = dummy.copy()
126+
122127
return dummy
123128

124129
def get_result(self):

pandas/tests/test_groupby.py

+9
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,15 @@ def test_groupby_nonstring_columns(self):
12301230
del expected[0]
12311231
assert_frame_equal(result, expected)
12321232

1233+
def test_cython_grouper_series_bug_noncontig(self):
1234+
arr = np.empty((100, 100))
1235+
arr.fill(np.nan)
1236+
obj = Series(arr[:, 0], index=range(100))
1237+
inds = np.tile(range(10), 10)
1238+
1239+
result = obj.groupby(inds).agg(Series.median)
1240+
self.assert_(result.isnull().all())
1241+
12331242
class TestPanelGroupBy(unittest.TestCase):
12341243

12351244
def setUp(self):

0 commit comments

Comments
 (0)