Skip to content

PERF: Improve drop_duplicates for bool columns (#12963) #15738

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
5 changes: 5 additions & 0 deletions asv_bench/benchmarks/reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ def setup(self):
self.K = 10000
self.key1 = np.random.randint(0, self.K, size=self.N)
self.df_int = DataFrame({'key1': self.key1})
self.df_bool = DataFrame({i: np.random.randint(0, 2, size=self.K,
dtype=bool)
for i in range(10)})

def time_frame_drop_dups(self):
self.df.drop_duplicates(['key1', 'key2'])
Expand All @@ -154,6 +157,8 @@ def time_series_drop_dups_string(self):
def time_frame_drop_dups_int(self):
self.df_int.drop_duplicates()

def time_frame_drop_dups_bool(self):
self.df_bool.drop_duplicates()

#----------------------------------------------------------------------
# blog "pandas escaped the zoo"
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ Performance Improvements
- Improved performance of ``.rank()`` for categorical data (:issue:`15498`)
- Improved performance when using ``.unstack()`` (:issue:`15503`)
- Improved performance of merge/join on ``category`` columns (:issue:`10409`)
- Improved performance of ``drop_duplicates()`` on ``bool`` columns (:issue:`12963`)


.. _whatsnew_0200.bug_fixes:
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
is_period_dtype,
is_period_arraylike,
is_float_dtype,
is_bool_dtype,
needs_i8_conversion,
is_categorical,
is_datetime64_dtype,
Expand Down Expand Up @@ -341,6 +342,10 @@ def factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None):
# numpy dtype
dtype = values.dtype
vals = values.view(np.int64)
elif is_bool_dtype(values):
dtype = bool
# transform to int dtype to avoid object path
vals = np.asarray(values).view('uint8')
else:
vals = np.asarray(values)

Expand Down