Skip to content

[PERF] Vectorize select_dtypes #28447

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 16 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
11 changes: 11 additions & 0 deletions asv_bench/benchmarks/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,15 @@ def time_dataframe_describe(self):
self.df.describe()


class SelectDtypes:
params = [10, 100, 1000, 10000, 100000]
param_names = ["n"]

def setup(self, n):
self.df = DataFrame(np.random.randn(10, n))

def time_select_dtypes(self, n):
self.df.select_dtypes(include="int")


from .pandas_vb_common import setup # noqa: F401 isort:skip
7 changes: 7 additions & 0 deletions doc/source/whatsnew/v0.25.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ What's new in 0.25.2 (October XX, 2019)
These are the changes in pandas 0.25.2. See :ref:`release` for a full changelog
including other versions of pandas.

.. _whatsnew_0252.performance:

Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~

- Improves :meth:`DataFrame.select_dtypes` performance by using vectorization instead of iterating over a loop. With this improvement :meth:`DataFrame.select_dtypes` becomes feasible for dataframes with lots of columns. (:issue:`28317`)

.. _whatsnew_0252.bug_fixes:

Bug fixes
Expand Down
50 changes: 23 additions & 27 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""
import collections
from collections import OrderedDict, abc
import functools
from io import StringIO
import itertools
import sys
Expand Down Expand Up @@ -3489,33 +3488,30 @@ def _get_info_slice(obj, indexer):
)
)

# empty include/exclude -> defaults to True
# three cases (we've already raised if both are empty)
# case 1: empty include, nonempty exclude
# we have True, True, ... True for include, same for exclude
# in the loop below we get the excluded
# and when we call '&' below we get only the excluded
# case 2: nonempty include, empty exclude
# same as case 1, but with include
# case 3: both nonempty
# the "union" of the logic of case 1 and case 2:
# we get the included and excluded, and return their logical and
include_these = Series(not bool(include), index=self.columns)
exclude_these = Series(not bool(exclude), index=self.columns)

def is_dtype_instance_mapper(idx, dtype):
return idx, functools.partial(issubclass, dtype.type)

for idx, f in itertools.starmap(
is_dtype_instance_mapper, enumerate(self.dtypes)
):
if include: # checks for the case of empty include or exclude
include_these.iloc[idx] = any(map(f, include))
if exclude:
exclude_these.iloc[idx] = not any(map(f, exclude))
# We raise when both include and exclude are empty
# Hence, we can just shrink the columns we want to keep
keep_these = Series(True, index=self.columns)
unique_dtypes = self.dtypes.unique()

def filter_unique_dtypes_on(selection, unqiue_dtypes):
result = [
dtype
for dtype in unique_dtypes
if any(
issubclass(dtype.type, selected_type) for selected_type in selection
)
]
return result

if include:
included_dtypes = filter_unique_dtypes_on(include, unique_dtypes)
keep_these &= self.dtypes.isin(included_dtypes)

if exclude:
excluded_dtypes = filter_unique_dtypes_on(exclude, unique_dtypes)
keep_these &= ~self.dtypes.isin(excluded_dtypes)

dtype_indexer = include_these & exclude_these
return self.loc[_get_info_slice(self, dtype_indexer)]
return self.loc[_get_info_slice(self, keep_these)]

def insert(self, loc, column, value, allow_duplicates=False):
"""
Expand Down