Skip to content

ENH: Add dtype param for pandas.Series.str.get_dummies #47958

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
6 changes: 4 additions & 2 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2136,7 +2136,7 @@ def wrap(self, width, **kwargs):
return self._wrap_result(result)

@forbid_nonstring_types(["bytes"])
def get_dummies(self, sep="|"):
def get_dummies(self, sep="|", dtype=np.int64):
"""
Return DataFrame of dummy/indicator variables for Series.

Expand All @@ -2147,6 +2147,8 @@ def get_dummies(self, sep="|"):
----------
sep : str, default "|"
String to split on.
dtype : numpy.dtype, default np.int64
The numpy dtype to use for the result ndarray.

Returns
-------
Expand Down Expand Up @@ -2174,7 +2176,7 @@ def get_dummies(self, sep="|"):
"""
# we need to cast to Series of strings as only that has all
# methods available for making the dummies...
result, name = self._data.array._str_get_dummies(sep)
result, name = self._data.array._str_get_dummies(sep, dtype=dtype)
return self._wrap_result(
result,
name=name,
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/strings/object_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _str_wrap(self, width, **kwargs):
tw = textwrap.TextWrapper(**kwargs)
return self._str_map(lambda s: "\n".join(tw.wrap(s)))

def _str_get_dummies(self, sep="|"):
def _str_get_dummies(self, sep="|", dtype=np.int64):
from pandas import Series

arr = Series(self).fillna("")
Expand All @@ -368,7 +368,7 @@ def _str_get_dummies(self, sep="|"):
tags.update(ts)
tags2 = sorted(tags - {""})

dummies = np.empty((len(arr), len(tags2)), dtype=np.int64)
dummies = np.empty((len(arr), len(tags2)), dtype=dtype)

for i, t in enumerate(tags2):
pat = sep + t + sep
Expand Down