-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG/PERF: Sparse get_dummies uses concat #24372
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,8 @@ | |
|
||
from pandas.core.dtypes.cast import maybe_promote | ||
from pandas.core.dtypes.common import ( | ||
ensure_platform_int, is_bool_dtype, is_extension_array_dtype, is_list_like, | ||
is_object_dtype, needs_i8_conversion) | ||
ensure_platform_int, is_bool_dtype, is_extension_array_dtype, | ||
is_integer_dtype, is_list_like, is_object_dtype, needs_i8_conversion) | ||
from pandas.core.dtypes.missing import notna | ||
|
||
from pandas import compat | ||
|
@@ -853,6 +853,7 @@ def check_len(item, name): | |
|
||
def _get_dummies_1d(data, prefix, prefix_sep='_', dummy_na=False, | ||
sparse=False, drop_first=False, dtype=None): | ||
from pandas.core.reshape.concat import concat | ||
# Series avoids inconsistent NaN handling | ||
codes, levels = _factorize_from_iterable(Series(data)) | ||
|
||
|
@@ -909,7 +910,15 @@ def _make_col_name(prefix, prefix_sep, level): | |
index = None | ||
|
||
if sparse: | ||
sparse_series = {} | ||
|
||
if is_integer_dtype(dtype): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have a routine in pandas.core.dtypes.missing for this already There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have that too let’s try to not reinvent the wheel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't a function like this in any of the dtypes modules. |
||
fill_value = 0 | ||
elif dtype == bool: | ||
fill_value = False | ||
else: | ||
fill_value = 0.0 | ||
|
||
sparse_series = [] | ||
N = len(data) | ||
sp_indices = [[] for _ in range(len(dummy_cols))] | ||
mask = codes != -1 | ||
|
@@ -926,12 +935,12 @@ def _make_col_name(prefix, prefix_sep, level): | |
dummy_cols = dummy_cols[1:] | ||
for col, ixs in zip(dummy_cols, sp_indices): | ||
sarr = SparseArray(np.ones(len(ixs), dtype=dtype), | ||
sparse_index=IntIndex(N, ixs), fill_value=0, | ||
sparse_index=IntIndex(N, ixs), | ||
fill_value=fill_value, | ||
dtype=dtype) | ||
sparse_series[col] = Series(data=sarr, index=index) | ||
sparse_series.append(Series(data=sarr, index=index, name=col)) | ||
|
||
out = DataFrame(sparse_series, index=index, columns=dummy_cols, | ||
dtype=dtype) | ||
out = concat(sparse_series, axis=1, copy=False) | ||
return out | ||
|
||
else: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the input to
concat
is aList[Series[Sparse]]
, we now return a DataFrame with sparse values. Previously this was a dense DataFrame (probably a bug), so it isn't API breaking.