Skip to content

TST (string dtype): clean-up assorted xfails #60354

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
12 changes: 11 additions & 1 deletion pandas/core/reshape/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from pandas._libs import missing as libmissing
from pandas._libs.sparse import IntIndex

from pandas.core.dtypes.cast import (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this commit has code changes but have pushed to this PR while we discuss

find_common_type,
infer_dtype_from_scalar,
)
from pandas.core.dtypes.common import (
is_integer_dtype,
is_list_like,
Expand Down Expand Up @@ -567,7 +571,13 @@ def from_dummies(
)
else:
data_slice = data_to_decode.loc[:, prefix_slice]
cats_array = data._constructor_sliced(cats, dtype=data.columns.dtype)
dtype = data.columns.dtype
if default_category:
default_category_dtype = infer_dtype_from_scalar(default_category[prefix])[
0
]
dtype = find_common_type([dtype, default_category_dtype])
cats_array = data._constructor_sliced(cats, dtype=dtype)
# get indices of True entries along axis=1
true_values = data_slice.idxmax(axis=1)
indexer = data_slice.columns.get_indexer_for(true_values)
Expand Down
11 changes: 2 additions & 9 deletions pandas/tests/reshape/test_from_dummies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import numpy as np
import pytest

from pandas._config import using_string_dtype

from pandas import (
DataFrame,
Series,
Expand Down Expand Up @@ -330,14 +328,10 @@ def test_no_prefix_string_cats_contains_get_dummies_NaN_column():
),
],
)
def test_no_prefix_string_cats_default_category(
default_category, expected, using_infer_string
):
def test_no_prefix_string_cats_default_category(default_category, expected):
dummies = DataFrame({"a": [1, 0, 0], "b": [0, 1, 0]})
result = from_dummies(dummies, default_category=default_category)
expected = DataFrame(expected)
if using_infer_string:
expected[""] = expected[""].astype("str")
tm.assert_frame_equal(result, expected)


Expand All @@ -364,7 +358,6 @@ def test_with_prefix_contains_get_dummies_NaN_column():
tm.assert_frame_equal(result, expected)


@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)", strict=False)
@pytest.mark.parametrize(
"default_category, expected",
[
Expand All @@ -390,7 +383,7 @@ def test_with_prefix_contains_get_dummies_NaN_column():
),
pytest.param(
{"col2": None, "col1": False},
{"col1": ["a", "b", False], "col2": [None, "a", "c"]},
{"col1": ["a", "b", False], "col2": Series([None, "a", "c"], dtype=object)},
id="default_category is a dict with bool and None values",
),
pytest.param(
Expand Down
23 changes: 6 additions & 17 deletions pandas/tests/strings/test_find_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,23 +293,12 @@ def test_startswith_endswith_validate_na(any_string_dtype):
dtype=any_string_dtype,
)

dtype = ser.dtype
if (isinstance(dtype, pd.StringDtype)) or dtype == np.dtype("object"):
msg = "Allowing a non-bool 'na' in obj.str.startswith is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
ser.str.startswith("kapow", na="baz")
msg = "Allowing a non-bool 'na' in obj.str.endswith is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
ser.str.endswith("bar", na="baz")
else:
# TODO(infer_string): don't surface pyarrow errors
import pyarrow as pa

msg = "Could not convert 'baz' with type str: tried to convert to boolean"
with pytest.raises(pa.lib.ArrowInvalid, match=msg):
ser.str.startswith("kapow", na="baz")
with pytest.raises(pa.lib.ArrowInvalid, match=msg):
ser.str.endswith("kapow", na="baz")
msg = "Allowing a non-bool 'na' in obj.str.startswith is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
ser.str.startswith("kapow", na="baz")
msg = "Allowing a non-bool 'na' in obj.str.endswith is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
ser.str.endswith("bar", na="baz")


@pytest.mark.parametrize("pat", ["foo", ("foo", "baz")])
Expand Down