-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
ENH: Change default dtype of str.get_dummies() to bool #60641
base: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
import pandas.util._test_decorators as td | ||
|
||
from pandas import ( | ||
NA, | ||
CategoricalDtype, | ||
DataFrame, | ||
Index, | ||
MultiIndex, | ||
|
@@ -22,19 +24,28 @@ | |
def test_get_dummies(any_string_dtype): | ||
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype) | ||
result = s.str.get_dummies("|") | ||
expected = DataFrame([[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("abc")) | ||
exp_dtype = ( | ||
"boolean" | ||
if any_string_dtype == "string" and any_string_dtype.na_value is NA | ||
else "bool" | ||
) | ||
expected = DataFrame( | ||
[[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("abc"), dtype=exp_dtype | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
s = Series(["a;b", "a", 7], dtype=any_string_dtype) | ||
result = s.str.get_dummies(";") | ||
expected = DataFrame([[0, 1, 1], [0, 1, 0], [1, 0, 0]], columns=list("7ab")) | ||
expected = DataFrame( | ||
[[0, 1, 1], [0, 1, 0], [1, 0, 0]], columns=list("7ab"), dtype=exp_dtype | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_get_dummies_index(): | ||
# GH9980, GH8028 | ||
idx = Index(["a|b", "a|c", "b|c"]) | ||
result = idx.str.get_dummies("|") | ||
result = idx.str.get_dummies("|", dtype=np.int64) | ||
Comment on lines
45
to
+48
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. The behavior where the output becomes a With this PR, the default behavior of |
||
|
||
expected = MultiIndex.from_tuples( | ||
[(1, 1, 0), (1, 0, 1), (0, 1, 1)], names=("a", "b", "c") | ||
|
@@ -125,3 +136,15 @@ def test_get_dummies_with_pa_str_dtype(any_string_dtype): | |
dtype="str[pyarrow]", | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("dtype_type", ["string", "category"]) | ||
def test_get_dummies_ea_dtype(dtype_type, string_dtype_no_object): | ||
dtype = string_dtype_no_object | ||
exp_dtype = "boolean" if dtype.na_value is NA else "bool" | ||
if dtype_type == "category": | ||
dtype = CategoricalDtype(Index(["a", "b"], dtype)) | ||
s = Series(["a", "b"], dtype=dtype) | ||
result = s.str.get_dummies() | ||
expected = DataFrame([[1, 0], [0, 1]], columns=list("ab"), dtype=exp_dtype) | ||
tm.assert_frame_equal(result, expected) |
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.
In the existing implementation, the following code would raise a
TypeError: Cannot interpret 'BooleanDtype' as a data type
due to the linereturn np.empty(shape=(0, 0), dtype=dtype)
:With this PR, the default dtype is changed to a boolean type, which makes similar issues more likely to occur. To address this, I modified the code to pass
dummies_dtype
tonp.empty()
instead of usingdtype
directly.Related test: https://github.com/pandas-dev/pandas/blob/main/pandas/tests/strings/test_strings.py#L136