Skip to content

Commit 082068b

Browse files
committed
BUG: groupby with a Float like index misbehaving when the index is non-monotonic (related GH5375)
1 parent b481b92 commit 082068b

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/release.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ API Changes
345345
indexing and slicing work exactly the same. Indexing on other index types
346346
are preserved (and positional fallback for ``[],ix``), with the exception,
347347
that floating point slicing on indexes on non ``Float64Index`` will raise a
348-
``TypeError``, e.g. ``Series(range(5))[3.5:4.5]`` (:issue:`263`)
348+
``TypeError``, e.g. ``Series(range(5))[3.5:4.5]`` (:issue:`263`,:issue:`5375`)
349349
- Make Categorical repr nicer (:issue:`4368`)
350350
- Remove deprecated ``Factor`` (:issue:`3650`)
351351
- Remove deprecated ``set_printoptions/reset_printoptions`` (:issue:``3046``)

pandas/core/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2435,7 +2435,7 @@ def _get_sorted_data(self):
24352435
return self.data.take(self.sort_idx, axis=self.axis, convert=False)
24362436

24372437
def _chop(self, sdata, slice_obj):
2438-
return sdata[slice_obj]
2438+
return sdata.iloc[slice_obj]
24392439

24402440
def apply(self, f):
24412441
raise NotImplementedError
@@ -2470,7 +2470,7 @@ def fast_apply(self, f, names):
24702470

24712471
def _chop(self, sdata, slice_obj):
24722472
if self.axis == 0:
2473-
return sdata[slice_obj]
2473+
return sdata.iloc[slice_obj]
24742474
else:
24752475
return sdata._slice(slice_obj, axis=1) # ix[:, slice_obj]
24762476

pandas/tests/test_groupby.py

+14
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,20 @@ def test_first_last_nth_dtypes(self):
202202
f = s.groupby(level=0).first()
203203
self.assert_(f.dtype == 'int64')
204204

205+
def test_grouper_index_types(self):
206+
# related GH5375
207+
# groupby misbehaving when using a Floatlike index
208+
df = DataFrame(np.arange(10).reshape(5,2),columns=list('AB'))
209+
for index in [ tm.makeFloatIndex, tm.makeStringIndex,
210+
tm.makeUnicodeIndex, tm.makeIntIndex,
211+
tm.makeDateIndex, tm.makePeriodIndex ]:
212+
213+
df.index = index(len(df))
214+
df.groupby(list('abcde')).apply(lambda x: x)
215+
216+
df.index = list(reversed(df.index.tolist()))
217+
df.groupby(list('abcde')).apply(lambda x: x)
218+
205219
def test_grouper_iter(self):
206220
self.assertEqual(sorted(self.df.groupby('A').grouper), ['bar', 'foo'])
207221

0 commit comments

Comments
 (0)