Skip to content

BUG: groupby with a Float like index misbehaving when the index is non-monotonic (related GH5375) #5393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ API Changes
indexing and slicing work exactly the same. Indexing on other index types
are preserved (and positional fallback for ``[],ix``), with the exception,
that floating point slicing on indexes on non ``Float64Index`` will raise a
``TypeError``, e.g. ``Series(range(5))[3.5:4.5]`` (:issue:`263`)
``TypeError``, e.g. ``Series(range(5))[3.5:4.5]`` (:issue:`263`,:issue:`5375`)
- Make Categorical repr nicer (:issue:`4368`)
- Remove deprecated ``Factor`` (:issue:`3650`)
- Remove deprecated ``set_printoptions/reset_printoptions`` (:issue:``3046``)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2435,7 +2435,7 @@ def _get_sorted_data(self):
return self.data.take(self.sort_idx, axis=self.axis, convert=False)

def _chop(self, sdata, slice_obj):
return sdata[slice_obj]
return sdata.iloc[slice_obj]

def apply(self, f):
raise NotImplementedError
Expand Down Expand Up @@ -2470,7 +2470,7 @@ def fast_apply(self, f, names):

def _chop(self, sdata, slice_obj):
if self.axis == 0:
return sdata[slice_obj]
return sdata.iloc[slice_obj]
else:
return sdata._slice(slice_obj, axis=1) # ix[:, slice_obj]

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ def test_first_last_nth_dtypes(self):
f = s.groupby(level=0).first()
self.assert_(f.dtype == 'int64')

def test_grouper_index_types(self):
# related GH5375
# groupby misbehaving when using a Floatlike index
df = DataFrame(np.arange(10).reshape(5,2),columns=list('AB'))
for index in [ tm.makeFloatIndex, tm.makeStringIndex,
tm.makeUnicodeIndex, tm.makeIntIndex,
tm.makeDateIndex, tm.makePeriodIndex ]:

df.index = index(len(df))
df.groupby(list('abcde')).apply(lambda x: x)

df.index = list(reversed(df.index.tolist()))
df.groupby(list('abcde')).apply(lambda x: x)

def test_grouper_iter(self):
self.assertEqual(sorted(self.df.groupby('A').grouper), ['bar', 'foo'])

Expand Down