Skip to content

Sparse get dummies perf #21997

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 4 commits into from
Jul 20, 2018
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
18 changes: 18 additions & 0 deletions asv_bench/benchmarks/reshape.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import string
from itertools import product

import numpy as np
from pandas import DataFrame, MultiIndex, date_range, melt, wide_to_long
import pandas as pd

from .pandas_vb_common import setup # noqa

Expand Down Expand Up @@ -132,3 +134,19 @@ def setup(self):

def time_pivot_table(self):
self.df.pivot_table(index='key1', columns=['key2', 'key3'])


class GetDummies(object):
goal_time = 0.2

def setup(self):
categories = list(string.ascii_letters[:12])
s = pd.Series(np.random.choice(categories, size=1_000_000),
dtype=pd.api.types.CategoricalDtype(categories))
self.s = s

def time_get_dummies_1d(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: you can param over sparce=False/True

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight preference for leaving them separate, since they're such distinct code paths and it's a tad easier to run just sparse with this layout. Happy to change if you feel strongly about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, no strong preference to use params then.

pd.get_dummies(self.s, sparse=False)

def time_get_dummies_1d_sparse(self):
pd.get_dummies(self.s, sparse=True)
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ Performance Improvements
- Improved performance of :meth:`HDFStore.groups` (and dependent functions like
:meth:`~HDFStore.keys`. (i.e. ``x in store`` checks are much faster)
(:issue:`21372`)
-
- Improved the performance of :func:`pandas.get_dummies` with ``sparse=True`` (:issue:`21997`)

.. _whatsnew_0240.docs:

Expand Down
9 changes: 5 additions & 4 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,10 +940,11 @@ def get_empty_Frame(data, sparse):
sparse_series = {}
N = len(data)
sp_indices = [[] for _ in range(len(dummy_cols))]
for ndx, code in enumerate(codes):
if code == -1:
# Blank entries if not dummy_na and code == -1, #GH4446
continue
mask = codes != -1
codes = codes[mask]
n_idx = np.arange(N)[mask]

for ndx, code in zip(n_idx, codes):
sp_indices[code].append(ndx)

if drop_first:
Expand Down