-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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 1 commit
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 |
---|---|---|
|
@@ -2489,7 +2489,7 @@ def get_dummies( | |
---------- | ||
sep : str, default "|" | ||
String to split on. | ||
dtype : dtype, default np.int64 | ||
dtype : dtype, default bool | ||
Data type for new columns. Only a single dtype is allowed. | ||
|
||
Returns | ||
|
@@ -2505,27 +2505,48 @@ def get_dummies( | |
Examples | ||
-------- | ||
>>> pd.Series(["a|b", "a", "a|c"]).str.get_dummies() | ||
a b c | ||
0 1 1 0 | ||
1 1 0 0 | ||
2 1 0 1 | ||
a b c | ||
0 True True False | ||
1 True False False | ||
2 True False True | ||
|
||
>>> pd.Series(["a|b", np.nan, "a|c"]).str.get_dummies() | ||
a b c | ||
0 True True False | ||
1 False False False | ||
2 True False True | ||
|
||
>>> pd.Series(["a|b", np.nan, "a|c"]).str.get_dummies(dtype=np.int64) | ||
a b c | ||
0 1 1 0 | ||
1 0 0 0 | ||
2 1 0 1 | ||
|
||
>>> pd.Series(["a|b", np.nan, "a|c"]).str.get_dummies(dtype=bool) | ||
a b c | ||
0 True True False | ||
1 False False False | ||
2 True False True | ||
""" | ||
from pandas.core.frame import DataFrame | ||
|
||
# we need to cast to Series of strings as only that has all | ||
# methods available for making the dummies... | ||
input_dtype = self._data.dtype | ||
if dtype is None and not isinstance(input_dtype, ArrowDtype): | ||
from pandas.core.arrays.string_ import StringDtype | ||
|
||
if isinstance(input_dtype, CategoricalDtype): | ||
input_dtype = input_dtype.categories.dtype | ||
|
||
if isinstance(input_dtype, ArrowDtype): | ||
import pyarrow as pa | ||
|
||
dtype = ArrowDtype(pa.bool_()) | ||
elif ( | ||
isinstance(input_dtype, StringDtype) | ||
and input_dtype.na_value is not np.nan | ||
): | ||
from pandas.core.dtypes.common import pandas_dtype | ||
|
||
dtype = pandas_dtype("boolean") | ||
else: | ||
dtype = np.bool_ | ||
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 based this logic on the existing implementation of I added the condition sr = pd.Series(["A", "B", "A"], dtype=pd.ArrowDtype(pa.string()))
sr.str.get_dummies(dtype=pd.ArrowDtype(pa.bool_())) Output (this issue also exists in the implementation before this PR):
With this PR, the default dtype is changed to a boolean type, which makes similar issues more likely to occur. |
||
|
||
result, name = self._data.array._str_get_dummies(sep, dtype) | ||
if is_extension_array_dtype(dtype) or isinstance(dtype, ArrowDtype): | ||
return self._wrap_result( | ||
|
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