forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backport 2.3.x] TST (string dtype): duplicate pandas/tests/indexes/o…
…bject tests specifically for string dtypes (pandas-dev#60117) (pandas-dev#60131) TST (string dtype): duplicate pandas/tests/indexes/object tests specifically for string dtypes (pandas-dev#60117) (cherry picked from commit d8905e4)
- Loading branch information
1 parent
4079314
commit ba3e933
Showing
5 changed files
with
149 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from pandas import ( | ||
Index, | ||
Series, | ||
) | ||
import pandas._testing as tm | ||
|
||
|
||
def test_astype_str_from_bytes(): | ||
# https://github.com/pandas-dev/pandas/issues/38607 | ||
# GH#49658 pre-2.0 Index called .values.astype(str) here, which effectively | ||
# did a .decode() on the bytes object. In 2.0 we go through | ||
# ensure_string_array which does f"{val}" | ||
idx = Index(["あ", b"a"], dtype="object") | ||
result = idx.astype(str) | ||
expected = Index(["あ", "a"], dtype="str") | ||
tm.assert_index_equal(result, expected) | ||
|
||
# while we're here, check that Series.astype behaves the same | ||
result = Series(idx).astype(str) | ||
expected = Series(expected, dtype="str") | ||
tm.assert_series_equal(result, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas import Index | ||
import pandas._testing as tm | ||
|
||
|
||
class TestGetIndexer: | ||
@pytest.mark.parametrize( | ||
"method,expected", | ||
[ | ||
("pad", [-1, 0, 1, 1]), | ||
("backfill", [0, 0, 1, -1]), | ||
], | ||
) | ||
def test_get_indexer_strings(self, any_string_dtype, method, expected): | ||
expected = np.array(expected, dtype=np.intp) | ||
index = Index(["b", "c"], dtype=any_string_dtype) | ||
actual = index.get_indexer(["a", "b", "c", "d"], method=method) | ||
|
||
tm.assert_numpy_array_equal(actual, expected) | ||
|
||
def test_get_indexer_strings_raises(self, any_string_dtype): | ||
index = Index(["b", "c"], dtype=any_string_dtype) | ||
|
||
msg = "|".join( | ||
[ | ||
"operation 'sub' not supported for dtype 'str", | ||
r"unsupported operand type\(s\) for -: 'str' and 'str'", | ||
] | ||
) | ||
with pytest.raises(TypeError, match=msg): | ||
index.get_indexer(["a", "b", "c", "d"], method="nearest") | ||
|
||
with pytest.raises(TypeError, match=msg): | ||
index.get_indexer(["a", "b", "c", "d"], method="pad", tolerance=2) | ||
|
||
with pytest.raises(TypeError, match=msg): | ||
index.get_indexer( | ||
["a", "b", "c", "d"], method="pad", tolerance=[2, 2, 2, 2] | ||
) | ||
|
||
|
||
class TestGetIndexerNonUnique: | ||
@pytest.mark.xfail(reason="TODO(infer_string)", strict=False) | ||
def test_get_indexer_non_unique_nas(self, any_string_dtype, nulls_fixture): | ||
index = Index(["a", "b", None], dtype=any_string_dtype) | ||
indexer, missing = index.get_indexer_non_unique([nulls_fixture]) | ||
|
||
expected_indexer = np.array([2], dtype=np.intp) | ||
expected_missing = np.array([], dtype=np.intp) | ||
tm.assert_numpy_array_equal(indexer, expected_indexer) | ||
tm.assert_numpy_array_equal(missing, expected_missing) | ||
|
||
# actually non-unique | ||
index = Index(["a", None, "b", None], dtype=any_string_dtype) | ||
indexer, missing = index.get_indexer_non_unique([nulls_fixture]) | ||
|
||
expected_indexer = np.array([1, 3], dtype=np.intp) | ||
tm.assert_numpy_array_equal(indexer, expected_indexer) | ||
tm.assert_numpy_array_equal(missing, expected_missing) | ||
|
||
|
||
class TestSliceLocs: | ||
@pytest.mark.parametrize( | ||
"in_slice,expected", | ||
[ | ||
# error: Slice index must be an integer or None | ||
(pd.IndexSlice[::-1], "yxdcb"), | ||
(pd.IndexSlice["b":"y":-1], ""), # type: ignore[misc] | ||
(pd.IndexSlice["b"::-1], "b"), # type: ignore[misc] | ||
(pd.IndexSlice[:"b":-1], "yxdcb"), # type: ignore[misc] | ||
(pd.IndexSlice[:"y":-1], "y"), # type: ignore[misc] | ||
(pd.IndexSlice["y"::-1], "yxdcb"), # type: ignore[misc] | ||
(pd.IndexSlice["y"::-4], "yb"), # type: ignore[misc] | ||
# absent labels | ||
(pd.IndexSlice[:"a":-1], "yxdcb"), # type: ignore[misc] | ||
(pd.IndexSlice[:"a":-2], "ydb"), # type: ignore[misc] | ||
(pd.IndexSlice["z"::-1], "yxdcb"), # type: ignore[misc] | ||
(pd.IndexSlice["z"::-3], "yc"), # type: ignore[misc] | ||
(pd.IndexSlice["m"::-1], "dcb"), # type: ignore[misc] | ||
(pd.IndexSlice[:"m":-1], "yx"), # type: ignore[misc] | ||
(pd.IndexSlice["a":"a":-1], ""), # type: ignore[misc] | ||
(pd.IndexSlice["z":"z":-1], ""), # type: ignore[misc] | ||
(pd.IndexSlice["m":"m":-1], ""), # type: ignore[misc] | ||
], | ||
) | ||
def test_slice_locs_negative_step(self, in_slice, expected, any_string_dtype): | ||
index = Index(list("bcdxy"), dtype=any_string_dtype) | ||
|
||
s_start, s_stop = index.slice_locs(in_slice.start, in_slice.stop, in_slice.step) | ||
result = index[s_start : s_stop : in_slice.step] | ||
expected = Index(list(expected), dtype=any_string_dtype) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_slice_locs_negative_step_oob(self, any_string_dtype): | ||
index = Index(list("bcdxy"), dtype=any_string_dtype) | ||
|
||
result = index[-10:5:1] | ||
tm.assert_index_equal(result, index) | ||
|
||
result = index[4:-10:-1] | ||
expected = Index(list("yxdcb"), dtype=any_string_dtype) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_slice_locs_dup(self, any_string_dtype): | ||
index = Index(["a", "a", "b", "c", "d", "d"], dtype=any_string_dtype) | ||
assert index.slice_locs("a", "d") == (0, 6) | ||
assert index.slice_locs(end="d") == (0, 6) | ||
assert index.slice_locs("a", "c") == (0, 4) | ||
assert index.slice_locs("b", "d") == (2, 6) | ||
|
||
index2 = index[::-1] | ||
assert index2.slice_locs("d", "a") == (0, 6) | ||
assert index2.slice_locs(end="a") == (0, 6) | ||
assert index2.slice_locs("d", "b") == (0, 4) | ||
assert index2.slice_locs("c", "a") == (2, 6) |