Skip to content

[backport 2.3.x] TST (string dtype): add explicit object vs str dtype to index fixture (#60116) #60136

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

Merged
3 changes: 2 additions & 1 deletion pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ def _create_mi_with_dt64tz_level():


indices_dict = {
"string": Index([f"pandas_{i}" for i in range(100)]),
"object": Index([f"pandas_{i}" for i in range(100)], dtype=object),
"string": Index([f"pandas_{i}" for i in range(100)], dtype="str"),
"datetime": date_range("2020-01-01", periods=100),
"datetime-tz": date_range("2020-01-01", periods=100, tz="US/Pacific"),
"period": period_range("2020-01-01", periods=100, freq="D"),
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ def value_counts_internal(
# For backwards compatibility, we let Index do its normal type
# inference, _except_ for if if infers from object to bool.
idx = Index(keys)
if idx.dtype == bool and keys.dtype == object:
if idx.dtype in [bool, "string"] and keys.dtype == object:
idx = idx.astype(object)
Comment on lines 931 to 935
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 is needed for 2.3.x in addition to the changes on main, because here the Index(..) is not yet updated to idx = Index(keys, dtype=keys.dtype) after the deprecation enforcement

elif (
idx.dtype != keys.dtype # noqa: PLR1714 # # pylint: disable=R1714
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/base/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def test_searchsorted(request, index_or_series_obj):
assert 0 <= index <= len(obj)


@pytest.mark.filterwarnings(r"ignore:Dtype inference:FutureWarning")
def test_access_by_position(index_flat):
index = index_flat

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_map_identity_mapping(index, request):
# GH#12766

result = index.map(lambda x: x)
if index.dtype == object and result.dtype == bool:
if index.dtype == object and result.dtype in [bool, "string"]:
assert (index == result).all()
# TODO: could work that into the 'exact="equiv"'?
return # FIXME: doesn't belong in this file anymore!
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def test_copy_and_deepcopy(self, index_flat):
new_copy = index.copy(deep=True, name="banana")
assert new_copy.name == "banana"

@pytest.mark.filterwarnings(r"ignore:Dtype inference:FutureWarning")
def test_copy_name(self, index_flat):
# GH#12309: Check that the "name" argument
# passed at initialization is honored.
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_old_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def test_ensure_copied_data(self, index):
"RangeIndex cannot be initialized from data, "
"MultiIndex and CategoricalIndex are tested separately"
)
elif index.dtype == object and index.inferred_type == "boolean":
elif index.dtype == object and index.inferred_type in ["boolean", "string"]:
init_kwargs["dtype"] = index.dtype

index_type = type(index)
Expand Down Expand Up @@ -485,6 +485,7 @@ def test_delete_base(self, index):
with pytest.raises(IndexError, match=msg):
index.delete(length)

@pytest.mark.filterwarnings(r"ignore:Dtype inference:FutureWarning")
@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
def test_equals(self, index):
if isinstance(index, IntervalIndex):
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,13 @@ def test_difference_base(self, sort, index):
first.difference([1, 2, 3], sort)

@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
def test_symmetric_difference(self, index):
def test_symmetric_difference(self, index, using_infer_string, request):
if (
using_infer_string
and index.dtype == "object"
and index.inferred_type == "string"
):
request.applymarker(pytest.mark.xfail(reason="TODO: infer_string"))
if isinstance(index, CategoricalIndex):
pytest.skip(f"Not relevant for {type(index).__name__}")
if len(index) < 2:
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/series/methods/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def test_map_category_string():
tm.assert_series_equal(a.map(c), exp)


@pytest.mark.filterwarnings(r"ignore:Dtype inference:FutureWarning")
def test_map_empty(request, index):
if isinstance(index, MultiIndex):
request.applymarker(
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def test_factorize_complex(self):
expected_uniques = np.array([(1 + 0j), (2 + 0j), (2 + 1j)], dtype=object)
tm.assert_numpy_array_equal(uniques, expected_uniques)

@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)", strict=False)
@pytest.mark.parametrize("sort", [True, False])
def test_factorize(self, index_or_series_obj, sort):
obj = index_or_series_obj
Expand Down
Loading