Skip to content

Commit

Permalink
ENH: groupby speed optimization for Int64Index
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Sep 30, 2011
1 parent 3222a2f commit 775c12b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pandas.core.frame import DataFrame
from pandas.core.generic import NDFrame, PandasObject
from pandas.core.index import Index, MultiIndex
from pandas.core.index import Index, Int64Index, MultiIndex
from pandas.core.internals import BlockManager
from pandas.core.series import Series
from pandas.core.panel import Panel
Expand Down Expand Up @@ -164,13 +164,22 @@ def curried(x):
def primary(self):
return self.groupings[0]

@cache_readonly
def use_take(self):
group_axis = self.obj._get_axis(self.axis)
return isinstance(group_axis, Int64Index) and len(self.groupings) == 1

def get_group(self, name, obj=None):
if obj is None:
obj = self.obj

labels = self.groups[name]
axis_name = obj._get_axis_name(self.axis)
return obj.reindex(**{axis_name : labels})
if self.use_take:
inds = self.primary.indices[name]
return obj.take(inds, axis=self.axis)
else:
labels = self.groups[name]
axis_name = obj._get_axis_name(self.axis)
return obj.reindex(**{axis_name : labels})

def __iter__(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ def reindex_like(self, other, method=None):
"""
return self.reindex(other.index, method=method)

def take(self, indices):
def take(self, indices, axis=0):
"""
Analogous to ndarray.take, return Series corresponding to requested
indices
Expand Down

0 comments on commit 775c12b

Please sign in to comment.