Skip to content

Commit

Permalink
ENH: Series.str.get_dummies() raise on string type (#59786)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronchucarroll authored Jan 22, 2025
1 parent 1d33e4c commit 4c3b968
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 38 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Other enhancements
- :meth:`DataFrameGroupBy.transform`, :meth:`SeriesGroupBy.transform`, :meth:`DataFrameGroupBy.agg`, :meth:`SeriesGroupBy.agg`, :meth:`RollingGroupby.apply`, :meth:`ExpandingGroupby.apply`, :meth:`Rolling.apply`, :meth:`Expanding.apply`, :meth:`DataFrame.apply` with ``engine="numba"`` now supports positional arguments passed as kwargs (:issue:`58995`)
- :meth:`Rolling.agg`, :meth:`Expanding.agg` and :meth:`ExponentialMovingWindow.agg` now accept :class:`NamedAgg` aggregations through ``**kwargs`` (:issue:`28333`)
- :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`)
- :meth:`Series.str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`)
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
- :meth:`str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`)
- Implemented :meth:`Series.str.isascii` and :meth:`Series.str.isascii` (:issue:`59091`)
- Multiplying two :class:`DateOffset` objects will now raise a ``TypeError`` instead of a ``RecursionError`` (:issue:`59442`)
- Restore support for reading Stata 104-format and enable reading 103-format dta files (:issue:`58554`)
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2531,8 +2531,6 @@ def _str_get_dummies(self, sep: str = "|", dtype: NpDtype | None = None):
else:
dummies_dtype = np.bool_
dummies = np.zeros(n_rows * n_cols, dtype=dummies_dtype)
if dtype == str:
dummies[:] = False
dummies[indices] = True
dummies = dummies.reshape((n_rows, n_cols))
result = type(self)(pa.array(list(dummies)))
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
is_extension_array_dtype,
is_integer,
is_list_like,
is_numeric_dtype,
is_object_dtype,
is_re,
)
Expand Down Expand Up @@ -2524,10 +2525,12 @@ def get_dummies(
"""
from pandas.core.frame import DataFrame

if dtype is not None and not (is_numeric_dtype(dtype) or is_bool_dtype(dtype)):
raise ValueError("Only numeric or boolean dtypes are supported for 'dtype'")
# 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, dtype)
if is_extension_array_dtype(dtype) or isinstance(dtype, ArrowDtype):
if is_extension_array_dtype(dtype):
return self._wrap_result(
DataFrame(result, columns=name, dtype=dtype),
name=name,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/strings/object_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def _str_get_dummies(self, sep: str = "|", dtype: NpDtype | None = None):
dummies_dtype = _dtype
else:
dummies_dtype = np.bool_
dummies = np.empty((len(arr), len(tags2)), dtype=dummies_dtype)
dummies = np.empty((len(arr), len(tags2)), dtype=dummies_dtype, order="F")

def _isin(test_elements: str, element: str) -> bool:
return element in test_elements
Expand Down
38 changes: 5 additions & 33 deletions pandas/tests/strings/test_get_dummies.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import numpy as np
import pytest

from pandas._config import using_string_dtype

import pandas.util._test_decorators as td

from pandas import (
ArrowDtype,
DataFrame,
Index,
MultiIndex,
Series,
_testing as tm,
)

try:
import pyarrow as pa
except ImportError:
pa = None


def test_get_dummies(any_string_dtype):
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
Expand Down Expand Up @@ -99,32 +91,12 @@ def test_get_dummies_with_pyarrow_dtype(any_string_dtype, dtype):


# GH#47872
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
def test_get_dummies_with_str_dtype(any_string_dtype):
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
result = s.str.get_dummies("|", dtype=str)
expected = DataFrame(
[["T", "T", "F"], ["T", "F", "T"], ["F", "F", "F"]],
columns=list("abc"),
dtype=str,
)
tm.assert_frame_equal(result, expected)


# GH#47872
@td.skip_if_no("pyarrow")
def test_get_dummies_with_pa_str_dtype(any_string_dtype):
import pyarrow as pa
msg = "Only numeric or boolean dtypes are supported for 'dtype'"
with pytest.raises(ValueError, match=msg):
s.str.get_dummies("|", dtype=str)

s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
result = s.str.get_dummies("|", dtype=ArrowDtype(pa.string()))
expected = DataFrame(
[
["true", "true", "false"],
["true", "false", "true"],
["false", "false", "false"],
],
columns=list("abc"),
dtype=ArrowDtype(pa.string()),
)
tm.assert_frame_equal(result, expected)
with pytest.raises(ValueError, match=msg):
s.str.get_dummies("|", dtype="datetime64[ns]")

0 comments on commit 4c3b968

Please sign in to comment.