Skip to content

improves groupby.get_group_index when shape is a long sequence #11180

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

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 9 additions & 1 deletion asv_bench/benchmarks/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ def setup(self):
def time_frame_duplicated(self):
self.df.duplicated()

class frame_duplicated_wide(object):
goal_time = 0.2

def setup(self):
self.df = DataFrame(np.random.randn(1000, 100).astype(str))

def time_frame_duplicated_wide(self):
self.df.T.duplicated()

class frame_fancy_lookup(object):
goal_time = 0.2
Expand Down Expand Up @@ -929,4 +937,4 @@ def setup(self):
self.s = Series((['abcdefg', np.nan] * 500000))

def time_series_string_vector_slice(self):
self.s.str[:5]
self.s.str[:5]
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@ Performance Improvements
- Performance improvements in ``Categorical.value_counts`` (:issue:`10804`)
- Performance improvements in ``SeriesGroupBy.nunique`` and ``SeriesGroupBy.value_counts`` and ``SeriesGroupby.transform`` (:issue:`10820`, :issue:`11077`)
- Performance improvements in ``DataFrame.drop_duplicates`` with integer dtypes (:issue:`10917`)
- Performance improvements in ``DataFrame.duplicated`` with wide frames. (:issue:`11180`)
- 4x improvement in ``timedelta`` string parsing (:issue:`6755`, :issue:`10426`)
- 8x improvement in ``timedelta64`` and ``datetime64`` ops (:issue:`6755`)
- Significantly improved performance of indexing ``MultiIndex`` with slicers (:issue:`10287`)
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3739,10 +3739,17 @@ def get_group_index(labels, shape, sort, xnull):
An array of type int64 where two elements are equal if their corresponding
labels are equal at all location.
"""
def _int64_cut_off(shape):
acc = long(1)
for i, mul in enumerate(shape):
acc *= long(mul)
if not acc < _INT64_MAX:
return i
return len(shape)

def loop(labels, shape):
# how many levels can be done without overflow:
pred = lambda i: not _int64_overflow_possible(shape[:i])
nlev = next(filter(pred, range(len(shape), 0, -1)))
nlev = _int64_cut_off(shape)

# compute flat ids for the first `nlev` levels
stride = np.prod(shape[1:nlev], dtype='i8')
Expand Down