-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Defer Series.str.get_dummies to pandas.get_dummies #26686
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 4 commits
d95a42b
7ccaf47
11e168f
70e077a
a98aa26
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 |
---|---|---|
|
@@ -999,23 +999,25 @@ def str_get_dummies(arr, sep='|'): | |
1 0 0 0 | ||
2 1 0 1 | ||
""" | ||
arr = arr.fillna('') | ||
try: | ||
arr = sep + arr + sep | ||
except TypeError: | ||
arr = sep + arr.astype(str) + sep | ||
from pandas.core.reshape.reshape import get_dummies | ||
from pandas import Series | ||
|
||
if len(arr) == 0: | ||
empty = np.empty(0, dtype='object') | ||
return empty, empty | ||
|
||
arr = arr.fillna('').astype('str') | ||
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. Why is this astype needed? 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.
One of the example given in the docstring is:
|
||
|
||
arr_split = arr.str.split(sep) | ||
|
||
tags = set() | ||
for ts in arr.str.split(sep): | ||
tags.update(ts) | ||
tags = sorted(tags - {""}) | ||
stacked = Series(np.concatenate(arr_split)) | ||
stacked[stacked == ''] = np.nan | ||
stacked_idx = np.repeat(np.arange(len(arr)), arr_split.str.len()) | ||
|
||
dummies = np.empty((len(arr), len(tags)), dtype=np.int64) | ||
dummies_stacked = get_dummies(stacked) | ||
dummies = dummies_stacked.groupby(by=stacked_idx).sum() | ||
|
||
for i, t in enumerate(tags): | ||
pat = sep + t + sep | ||
dummies[:, i] = lib.map_infer(arr.values, lambda x: pat in x) | ||
return dummies, tags | ||
return dummies.values, dummies.columns.values | ||
|
||
|
||
def str_join(arr, sep): | ||
|
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.
is this case not handled by get_dummies already?
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.
np.concatenate
fails with: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.
Is there a nicer way to detect an empty input and output empty results? Should this be handled by
pandas.core.strings.StringMethods.get_dummies
instead?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.
@jreback I have replaced
if len(arr) == 0: return empty
withSeries(np.concatenate(arr_split)) if len(arr) > 0 else Series()
which side stepsnp.concatenate
when the input is empty. Is this satisfactory?