Skip to content

get_dummies to select string dtype in addition to object and categorical #45516

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 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def get_dummies(
columns : list-like, default None
Column names in the DataFrame to be encoded.
If `columns` is None then all the columns with
`object` or `category` dtype will be converted.
`object`, `string`, or `category` dtype will be converted.
sparse : bool, default False
Whether the dummy-encoded columns should be backed by
a :class:`SparseArray` (True) or a regular NumPy array (False).
Expand Down Expand Up @@ -915,7 +915,7 @@ def get_dummies(
"""
from pandas.core.reshape.concat import concat

dtypes_to_encode = ["object", "category"]
dtypes_to_encode = ["object", "string", "category"]

if isinstance(data, DataFrame):
# determine columns being encoded
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/reshape/test_get_dummies.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ def test_dataframe_dummies_all_obj(self, df, sparse):

tm.assert_frame_equal(result, expected)

def test_dataframe_dummies_string_dtype(self, df):
# GH44965
df = df[["A", "B"]]
df = df.astype({"A": "object", "B": "string"})
result = get_dummies(df)
expected = DataFrame(
{
"A_a": [1, 0, 1],
"A_b": [0, 1, 0],
"B_b": [1, 1, 0],
"B_c": [0, 0, 1],
},
dtype=np.uint8,
)
tm.assert_frame_equal(result, expected)

def test_dataframe_dummies_mix_default(self, df, sparse, dtype):
result = get_dummies(df, sparse=sparse, dtype=dtype)
if sparse:
Expand Down