Skip to content

Commit 8b26e8c

Browse files
restrict to numeric or boolean
1 parent 171d381 commit 8b26e8c

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

pandas/core/strings/accessor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
is_extension_array_dtype,
3030
is_integer,
3131
is_list_like,
32+
is_numeric_dtype,
3233
is_object_dtype,
3334
is_re,
34-
is_string_dtype,
3535
)
3636
from pandas.core.dtypes.dtypes import (
3737
ArrowDtype,
@@ -2525,8 +2525,8 @@ def get_dummies(
25252525
"""
25262526
from pandas.core.frame import DataFrame
25272527

2528-
if is_string_dtype(dtype):
2529-
raise ValueError("string dtype not supported, please use a numeric dtype")
2528+
if dtype is not None and not (is_numeric_dtype(dtype) or is_bool_dtype(dtype)):
2529+
raise ValueError("Only numeric or boolean dtypes are supported for 'dtype'")
25302530
# we need to cast to Series of strings as only that has all
25312531
# methods available for making the dummies...
25322532
result, name = self._data.array._str_get_dummies(sep, dtype)

pandas/tests/strings/test_get_dummies.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def test_get_dummies_with_pyarrow_dtype(any_string_dtype, dtype):
9393
# GH#47872
9494
def test_get_dummies_with_str_dtype(any_string_dtype):
9595
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
96-
with pytest.raises(
97-
ValueError, match="string dtype not supported, please use a numeric dtype"
98-
):
96+
97+
msg = "Only numeric or boolean dtypes are supported for 'dtype'"
98+
with pytest.raises(ValueError, match=msg):
9999
s.str.get_dummies("|", dtype=str)
100+
101+
with pytest.raises(ValueError, match=msg):
102+
s.str.get_dummies("|", dtype="datetime64[ns]")

0 commit comments

Comments
 (0)