From c754fa1720b446328e76f69824c6192ecad26448 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 30 Jan 2021 08:28:21 -0800 Subject: [PATCH 1/4] TST/REF: collect index tests --- .../indexes/datetimelike_/test_indexing.py | 43 +++ pandas/tests/indexes/numeric/test_indexing.py | 231 +++++++++++- pandas/tests/indexes/object/test_indexing.py | 94 +++++ pandas/tests/indexes/test_base.py | 347 ------------------ 4 files changed, 367 insertions(+), 348 deletions(-) create mode 100644 pandas/tests/indexes/datetimelike_/test_indexing.py create mode 100644 pandas/tests/indexes/object/test_indexing.py diff --git a/pandas/tests/indexes/datetimelike_/test_indexing.py b/pandas/tests/indexes/datetimelike_/test_indexing.py new file mode 100644 index 0000000000000..51de446eea3e3 --- /dev/null +++ b/pandas/tests/indexes/datetimelike_/test_indexing.py @@ -0,0 +1,43 @@ +import numpy as np +import pytest + +import pandas as pd +from pandas import DatetimeIndex, Index +import pandas._testing as tm + +dtlike_dtypes = [ + np.dtype("timedelta64[ns]"), + np.dtype("datetime64[ns]"), + pd.DatetimeTZDtype("ns", "Asia/Tokyo"), + pd.PeriodDtype("ns"), +] + + +@pytest.mark.parametrize("ldtype", dtlike_dtypes) +@pytest.mark.parametrize("rdtype", dtlike_dtypes) +def test_get_indexer_non_unique_wrong_dtype(ldtype, rdtype): + + vals = np.tile(3600 * 10 ** 9 * np.arange(3), 2) + + def construct(dtype): + if dtype is dtlike_dtypes[-1]: + # PeriodArray will try to cast ints to strings + return DatetimeIndex(vals).astype(dtype) + return Index(vals, dtype=dtype) + + left = construct(ldtype) + right = construct(rdtype) + + result = left.get_indexer_non_unique(right) + + if ldtype is rdtype: + ex1 = np.array([0, 3, 1, 4, 2, 5] * 2, dtype=np.intp) + ex2 = np.array([], dtype=np.intp) + tm.assert_numpy_array_equal(result[0], ex1) + tm.assert_numpy_array_equal(result[1], ex2) + + else: + no_matches = np.array([-1] * 6, dtype=np.intp) + missing = np.arange(6, dtype=np.intp) + tm.assert_numpy_array_equal(result[0], no_matches) + tm.assert_numpy_array_equal(result[1], missing) diff --git a/pandas/tests/indexes/numeric/test_indexing.py b/pandas/tests/indexes/numeric/test_indexing.py index 7420cac2f9da4..33f927bdd7c04 100644 --- a/pandas/tests/indexes/numeric/test_indexing.py +++ b/pandas/tests/indexes/numeric/test_indexing.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from pandas import Float64Index, Index, Int64Index, Series, UInt64Index +from pandas import Float64Index, Index, Int64Index, RangeIndex, Series, UInt64Index import pandas._testing as tm @@ -13,6 +13,54 @@ def index_large(): class TestGetLoc: + @pytest.mark.parametrize("method", [None, "pad", "backfill", "nearest"]) + def test_get_loc(self, method): + index = Index([0, 1, 2]) + assert index.get_loc(1, method=method) == 1 + + if method: + assert index.get_loc(1, method=method, tolerance=0) == 1 + + @pytest.mark.parametrize("method", [None, "pad", "backfill", "nearest"]) + def test_get_loc_raises_bad_label(self, method): + index = Index([0, 1, 2]) + if method: + msg = "not supported between" + else: + msg = "invalid key" + + with pytest.raises(TypeError, match=msg): + index.get_loc([1, 2], method=method) + + @pytest.mark.parametrize( + "method,loc", [("pad", 1), ("backfill", 2), ("nearest", 1)] + ) + def test_get_loc_tolerance(self, method, loc): + index = Index([0, 1, 2]) + assert index.get_loc(1.1, method) == loc + assert index.get_loc(1.1, method, tolerance=1) == loc + + @pytest.mark.parametrize("method", ["pad", "backfill", "nearest"]) + def test_get_loc_outside_tolerance_raises(self, method): + index = Index([0, 1, 2]) + with pytest.raises(KeyError, match="1.1"): + index.get_loc(1.1, method, tolerance=0.05) + + def test_get_loc_bad_tolerance_raises(self): + index = Index([0, 1, 2]) + with pytest.raises(ValueError, match="must be numeric"): + index.get_loc(1.1, "nearest", tolerance="invalid") + + def test_get_loc_tolerance_no_method_raises(self): + index = Index([0, 1, 2]) + with pytest.raises(ValueError, match="tolerance .* valid if"): + index.get_loc(1.1, tolerance=1) + + def test_get_loc_raises_missized_tolerance(self): + index = Index([0, 1, 2]) + with pytest.raises(ValueError, match="tolerance size must match"): + index.get_loc(1.1, "nearest", tolerance=[1, 1]) + def test_get_loc_float64(self): idx = Float64Index([0.0, 1.0, 2.0]) for method in [None, "pad", "backfill", "nearest"]: @@ -82,6 +130,131 @@ def test_get_loc_missing_nan(self): class TestGetIndexer: + def test_get_indexer(self): + index1 = Index([1, 2, 3, 4, 5]) + index2 = Index([2, 4, 6]) + + r1 = index1.get_indexer(index2) + e1 = np.array([1, 3, -1], dtype=np.intp) + tm.assert_almost_equal(r1, e1) + + @pytest.mark.parametrize("reverse", [True, False]) + @pytest.mark.parametrize( + "expected,method", + [ + (np.array([-1, 0, 0, 1, 1], dtype=np.intp), "pad"), + (np.array([-1, 0, 0, 1, 1], dtype=np.intp), "ffill"), + (np.array([0, 0, 1, 1, 2], dtype=np.intp), "backfill"), + (np.array([0, 0, 1, 1, 2], dtype=np.intp), "bfill"), + ], + ) + def test_get_indexer_methods(self, reverse, expected, method): + index1 = Index([1, 2, 3, 4, 5]) + index2 = Index([2, 4, 6]) + + if reverse: + index1 = index1[::-1] + expected = expected[::-1] + + result = index2.get_indexer(index1, method=method) + tm.assert_almost_equal(result, expected) + + def test_get_indexer_invalid(self): + # GH10411 + index = Index(np.arange(10)) + + with pytest.raises(ValueError, match="tolerance argument"): + index.get_indexer([1, 0], tolerance=1) + + with pytest.raises(ValueError, match="limit argument"): + index.get_indexer([1, 0], limit=1) + + @pytest.mark.parametrize( + "method, tolerance, indexer, expected", + [ + ("pad", None, [0, 5, 9], [0, 5, 9]), + ("backfill", None, [0, 5, 9], [0, 5, 9]), + ("nearest", None, [0, 5, 9], [0, 5, 9]), + ("pad", 0, [0, 5, 9], [0, 5, 9]), + ("backfill", 0, [0, 5, 9], [0, 5, 9]), + ("nearest", 0, [0, 5, 9], [0, 5, 9]), + ("pad", None, [0.2, 1.8, 8.5], [0, 1, 8]), + ("backfill", None, [0.2, 1.8, 8.5], [1, 2, 9]), + ("nearest", None, [0.2, 1.8, 8.5], [0, 2, 9]), + ("pad", 1, [0.2, 1.8, 8.5], [0, 1, 8]), + ("backfill", 1, [0.2, 1.8, 8.5], [1, 2, 9]), + ("nearest", 1, [0.2, 1.8, 8.5], [0, 2, 9]), + ("pad", 0.2, [0.2, 1.8, 8.5], [0, -1, -1]), + ("backfill", 0.2, [0.2, 1.8, 8.5], [-1, 2, -1]), + ("nearest", 0.2, [0.2, 1.8, 8.5], [0, 2, -1]), + ], + ) + def test_get_indexer_nearest(self, method, tolerance, indexer, expected): + index = Index(np.arange(10)) + + actual = index.get_indexer(indexer, method=method, tolerance=tolerance) + tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp)) + + @pytest.mark.parametrize("listtype", [list, tuple, Series, np.array]) + @pytest.mark.parametrize( + "tolerance, expected", + list( + zip( + [[0.3, 0.3, 0.1], [0.2, 0.1, 0.1], [0.1, 0.5, 0.5]], + [[0, 2, -1], [0, -1, -1], [-1, 2, 9]], + ) + ), + ) + def test_get_indexer_nearest_listlike_tolerance( + self, tolerance, expected, listtype + ): + index = Index(np.arange(10)) + + actual = index.get_indexer( + [0.2, 1.8, 8.5], method="nearest", tolerance=listtype(tolerance) + ) + tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp)) + + def test_get_indexer_nearest_error(self): + index = Index(np.arange(10)) + with pytest.raises(ValueError, match="limit argument"): + index.get_indexer([1, 0], method="nearest", limit=1) + + with pytest.raises(ValueError, match="tolerance size must match"): + index.get_indexer([1, 0], method="nearest", tolerance=[1, 2, 3]) + + @pytest.mark.parametrize( + "method,expected", + [("pad", [8, 7, 0]), ("backfill", [9, 8, 1]), ("nearest", [9, 7, 0])], + ) + def test_get_indexer_nearest_decreasing(self, method, expected): + index = Index(np.arange(10))[::-1] + + actual = index.get_indexer([0, 5, 9], method=method) + tm.assert_numpy_array_equal(actual, np.array([9, 4, 0], dtype=np.intp)) + + actual = index.get_indexer([0.2, 1.8, 8.5], method=method) + tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp)) + + @pytest.mark.parametrize( + "idx_class", [Int64Index, RangeIndex, Float64Index, UInt64Index] + ) + @pytest.mark.parametrize("method", ["get_indexer", "get_indexer_non_unique"]) + def test_get_indexer_numeric_index_boolean_target(self, method, idx_class): + # GH 16877 + + numeric_index = idx_class(RangeIndex(4)) + other = Index([True, False, True]) + + result = getattr(numeric_index, method)(other) + expected = np.array([-1, -1, -1], dtype=np.intp) + if method == "get_indexer": + tm.assert_numpy_array_equal(result, expected) + else: + missing = np.arange(3, dtype=np.intp) + tm.assert_numpy_array_equal(result[0], expected) + tm.assert_numpy_array_equal(result[1], missing) + @pytest.mark.parametrize("method", ["pad", "backfill", "nearest"]) def test_get_indexer_with_method_numeric_vs_bool(self, method): left = Index([1, 2, 3]) @@ -274,6 +447,62 @@ def test_contains_float64_not_nans(self): assert 1.0 in index +class TestSliceLocs: + @pytest.mark.parametrize("dtype", [int, float]) + def test_slice_locs(self, dtype): + index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype)) + n = len(index) + + assert index.slice_locs(start=2) == (2, n) + assert index.slice_locs(start=3) == (3, n) + assert index.slice_locs(3, 8) == (3, 6) + assert index.slice_locs(5, 10) == (3, n) + assert index.slice_locs(end=8) == (0, 6) + assert index.slice_locs(end=9) == (0, 7) + + # reversed + index2 = index[::-1] + assert index2.slice_locs(8, 2) == (2, 6) + assert index2.slice_locs(7, 3) == (2, 5) + + @pytest.mark.parametrize("dtype", [int, float]) + def test_slice_locs_float_locs(self, dtype): + index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype)) + n = len(index) + assert index.slice_locs(5.0, 10.0) == (3, n) + assert index.slice_locs(4.5, 10.5) == (3, 8) + + index2 = index[::-1] + assert index2.slice_locs(8.5, 1.5) == (2, 6) + assert index2.slice_locs(10.5, -1) == (0, n) + + @pytest.mark.parametrize("dtype", [int, float]) + def test_slice_locs_dup_numeric(self, dtype): + index = Index(np.array([10, 12, 12, 14], dtype=dtype)) + assert index.slice_locs(12, 12) == (1, 3) + assert index.slice_locs(11, 13) == (1, 3) + + index2 = index[::-1] + assert index2.slice_locs(12, 12) == (1, 3) + assert index2.slice_locs(13, 11) == (1, 3) + + def test_slice_locs_na(self): + index = Index([np.nan, 1, 2]) + assert index.slice_locs(1) == (1, 3) + assert index.slice_locs(np.nan) == (0, 3) + + index = Index([0, np.nan, np.nan, 1, 2]) + assert index.slice_locs(np.nan) == (1, 5) + + def test_slice_locs_na_raises(self): + index = Index([np.nan, 1, 2]) + with pytest.raises(KeyError, match=""): + index.slice_locs(start=1.5) + + with pytest.raises(KeyError, match=""): + index.slice_locs(end=1.5) + + class TestGetSliceBounds: @pytest.mark.parametrize("kind", ["getitem", "loc", None]) @pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)]) diff --git a/pandas/tests/indexes/object/test_indexing.py b/pandas/tests/indexes/object/test_indexing.py new file mode 100644 index 0000000000000..76daa1779a584 --- /dev/null +++ b/pandas/tests/indexes/object/test_indexing.py @@ -0,0 +1,94 @@ +import numpy as np +import pytest + +import pandas as pd +from pandas import Index +import pandas._testing as tm + + +class TestGetLoc: + def test_get_loc_raises_object_nearest(self): + index = Index(["a", "c"]) + with pytest.raises(TypeError, match="unsupported operand type"): + index.get_loc("a", method="nearest") + + def test_get_loc_raises_object_tolerance(self): + index = Index(["a", "c"]) + with pytest.raises(TypeError, match="unsupported operand type"): + index.get_loc("a", method="pad", tolerance="invalid") + + +class TestGetIndexer: + @pytest.mark.parametrize( + "method,expected", + [ + ("pad", np.array([-1, 0, 1, 1], dtype=np.intp)), + ("backfill", np.array([0, 0, 1, -1], dtype=np.intp)), + ], + ) + def test_get_indexer_strings(self, method, expected): + index = Index(["b", "c"]) + actual = index.get_indexer(["a", "b", "c", "d"], method=method) + + tm.assert_numpy_array_equal(actual, expected) + + def test_get_indexer_strings_raises(self): + index = Index(["b", "c"]) + + msg = 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 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): + index = Index(list("bcdxy")) + + 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)) + tm.assert_index_equal(result, expected) + + def test_slice_locs_dup(self): + index = Index(["a", "a", "b", "c", "d", "d"]) + 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) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 05bc577d159dc..bfa6af313b16a 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1119,159 +1119,6 @@ def test_logical_compat(self, op): def _check_method_works(self, method, index): method(index) - def test_get_indexer(self): - index1 = Index([1, 2, 3, 4, 5]) - index2 = Index([2, 4, 6]) - - r1 = index1.get_indexer(index2) - e1 = np.array([1, 3, -1], dtype=np.intp) - tm.assert_almost_equal(r1, e1) - - @pytest.mark.parametrize("reverse", [True, False]) - @pytest.mark.parametrize( - "expected,method", - [ - (np.array([-1, 0, 0, 1, 1], dtype=np.intp), "pad"), - (np.array([-1, 0, 0, 1, 1], dtype=np.intp), "ffill"), - (np.array([0, 0, 1, 1, 2], dtype=np.intp), "backfill"), - (np.array([0, 0, 1, 1, 2], dtype=np.intp), "bfill"), - ], - ) - def test_get_indexer_methods(self, reverse, expected, method): - index1 = Index([1, 2, 3, 4, 5]) - index2 = Index([2, 4, 6]) - - if reverse: - index1 = index1[::-1] - expected = expected[::-1] - - result = index2.get_indexer(index1, method=method) - tm.assert_almost_equal(result, expected) - - def test_get_indexer_invalid(self): - # GH10411 - index = Index(np.arange(10)) - - with pytest.raises(ValueError, match="tolerance argument"): - index.get_indexer([1, 0], tolerance=1) - - with pytest.raises(ValueError, match="limit argument"): - index.get_indexer([1, 0], limit=1) - - @pytest.mark.parametrize( - "method, tolerance, indexer, expected", - [ - ("pad", None, [0, 5, 9], [0, 5, 9]), - ("backfill", None, [0, 5, 9], [0, 5, 9]), - ("nearest", None, [0, 5, 9], [0, 5, 9]), - ("pad", 0, [0, 5, 9], [0, 5, 9]), - ("backfill", 0, [0, 5, 9], [0, 5, 9]), - ("nearest", 0, [0, 5, 9], [0, 5, 9]), - ("pad", None, [0.2, 1.8, 8.5], [0, 1, 8]), - ("backfill", None, [0.2, 1.8, 8.5], [1, 2, 9]), - ("nearest", None, [0.2, 1.8, 8.5], [0, 2, 9]), - ("pad", 1, [0.2, 1.8, 8.5], [0, 1, 8]), - ("backfill", 1, [0.2, 1.8, 8.5], [1, 2, 9]), - ("nearest", 1, [0.2, 1.8, 8.5], [0, 2, 9]), - ("pad", 0.2, [0.2, 1.8, 8.5], [0, -1, -1]), - ("backfill", 0.2, [0.2, 1.8, 8.5], [-1, 2, -1]), - ("nearest", 0.2, [0.2, 1.8, 8.5], [0, 2, -1]), - ], - ) - def test_get_indexer_nearest(self, method, tolerance, indexer, expected): - index = Index(np.arange(10)) - - actual = index.get_indexer(indexer, method=method, tolerance=tolerance) - tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp)) - - @pytest.mark.parametrize("listtype", [list, tuple, Series, np.array]) - @pytest.mark.parametrize( - "tolerance, expected", - list( - zip( - [[0.3, 0.3, 0.1], [0.2, 0.1, 0.1], [0.1, 0.5, 0.5]], - [[0, 2, -1], [0, -1, -1], [-1, 2, 9]], - ) - ), - ) - def test_get_indexer_nearest_listlike_tolerance( - self, tolerance, expected, listtype - ): - index = Index(np.arange(10)) - - actual = index.get_indexer( - [0.2, 1.8, 8.5], method="nearest", tolerance=listtype(tolerance) - ) - tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp)) - - def test_get_indexer_nearest_error(self): - index = Index(np.arange(10)) - with pytest.raises(ValueError, match="limit argument"): - index.get_indexer([1, 0], method="nearest", limit=1) - - with pytest.raises(ValueError, match="tolerance size must match"): - index.get_indexer([1, 0], method="nearest", tolerance=[1, 2, 3]) - - @pytest.mark.parametrize( - "method,expected", - [("pad", [8, 7, 0]), ("backfill", [9, 8, 1]), ("nearest", [9, 7, 0])], - ) - def test_get_indexer_nearest_decreasing(self, method, expected): - index = Index(np.arange(10))[::-1] - - actual = index.get_indexer([0, 5, 9], method=method) - tm.assert_numpy_array_equal(actual, np.array([9, 4, 0], dtype=np.intp)) - - actual = index.get_indexer([0.2, 1.8, 8.5], method=method) - tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp)) - - @pytest.mark.parametrize( - "method,expected", - [ - ("pad", np.array([-1, 0, 1, 1], dtype=np.intp)), - ("backfill", np.array([0, 0, 1, -1], dtype=np.intp)), - ], - ) - def test_get_indexer_strings(self, method, expected): - index = Index(["b", "c"]) - actual = index.get_indexer(["a", "b", "c", "d"], method=method) - - tm.assert_numpy_array_equal(actual, expected) - - def test_get_indexer_strings_raises(self): - index = Index(["b", "c"]) - - msg = 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] - ) - - @pytest.mark.parametrize( - "idx_class", [Int64Index, RangeIndex, Float64Index, UInt64Index] - ) - @pytest.mark.parametrize("method", ["get_indexer", "get_indexer_non_unique"]) - def test_get_indexer_numeric_index_boolean_target(self, method, idx_class): - # GH 16877 - - numeric_index = idx_class(RangeIndex(4)) - other = Index([True, False, True]) - - result = getattr(numeric_index, method)(other) - expected = np.array([-1, -1, -1], dtype=np.intp) - if method == "get_indexer": - tm.assert_numpy_array_equal(result, expected) - else: - missing = np.arange(3, dtype=np.intp) - tm.assert_numpy_array_equal(result[0], expected) - tm.assert_numpy_array_equal(result[1], missing) - def test_get_indexer_with_NA_values( self, unique_nulls_fixture, unique_nulls_fixture2 ): @@ -1288,162 +1135,6 @@ def test_get_indexer_with_NA_values( expected = np.array([0, 1, -1], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) - @pytest.mark.parametrize("method", [None, "pad", "backfill", "nearest"]) - def test_get_loc(self, method): - index = Index([0, 1, 2]) - assert index.get_loc(1, method=method) == 1 - - if method: - assert index.get_loc(1, method=method, tolerance=0) == 1 - - @pytest.mark.parametrize("method", [None, "pad", "backfill", "nearest"]) - def test_get_loc_raises_bad_label(self, method): - index = Index([0, 1, 2]) - if method: - msg = "not supported between" - else: - msg = "invalid key" - - with pytest.raises(TypeError, match=msg): - index.get_loc([1, 2], method=method) - - @pytest.mark.parametrize( - "method,loc", [("pad", 1), ("backfill", 2), ("nearest", 1)] - ) - def test_get_loc_tolerance(self, method, loc): - index = Index([0, 1, 2]) - assert index.get_loc(1.1, method) == loc - assert index.get_loc(1.1, method, tolerance=1) == loc - - @pytest.mark.parametrize("method", ["pad", "backfill", "nearest"]) - def test_get_loc_outside_tolerance_raises(self, method): - index = Index([0, 1, 2]) - with pytest.raises(KeyError, match="1.1"): - index.get_loc(1.1, method, tolerance=0.05) - - def test_get_loc_bad_tolerance_raises(self): - index = Index([0, 1, 2]) - with pytest.raises(ValueError, match="must be numeric"): - index.get_loc(1.1, "nearest", tolerance="invalid") - - def test_get_loc_tolerance_no_method_raises(self): - index = Index([0, 1, 2]) - with pytest.raises(ValueError, match="tolerance .* valid if"): - index.get_loc(1.1, tolerance=1) - - def test_get_loc_raises_missized_tolerance(self): - index = Index([0, 1, 2]) - with pytest.raises(ValueError, match="tolerance size must match"): - index.get_loc(1.1, "nearest", tolerance=[1, 1]) - - def test_get_loc_raises_object_nearest(self): - index = Index(["a", "c"]) - with pytest.raises(TypeError, match="unsupported operand type"): - index.get_loc("a", method="nearest") - - def test_get_loc_raises_object_tolerance(self): - index = Index(["a", "c"]) - with pytest.raises(TypeError, match="unsupported operand type"): - index.get_loc("a", method="pad", tolerance="invalid") - - @pytest.mark.parametrize("dtype", [int, float]) - def test_slice_locs(self, dtype): - index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype)) - n = len(index) - - assert index.slice_locs(start=2) == (2, n) - assert index.slice_locs(start=3) == (3, n) - assert index.slice_locs(3, 8) == (3, 6) - assert index.slice_locs(5, 10) == (3, n) - assert index.slice_locs(end=8) == (0, 6) - assert index.slice_locs(end=9) == (0, 7) - - # reversed - index2 = index[::-1] - assert index2.slice_locs(8, 2) == (2, 6) - assert index2.slice_locs(7, 3) == (2, 5) - - @pytest.mark.parametrize("dtype", [int, float]) - def test_slice_float_locs(self, dtype): - index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype)) - n = len(index) - assert index.slice_locs(5.0, 10.0) == (3, n) - assert index.slice_locs(4.5, 10.5) == (3, 8) - - index2 = index[::-1] - assert index2.slice_locs(8.5, 1.5) == (2, 6) - assert index2.slice_locs(10.5, -1) == (0, n) - - def test_slice_locs_dup(self): - index = Index(["a", "a", "b", "c", "d", "d"]) - 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) - - @pytest.mark.parametrize("dtype", [int, float]) - def test_slice_locs_dup_numeric(self, dtype): - index = Index(np.array([10, 12, 12, 14], dtype=dtype)) - assert index.slice_locs(12, 12) == (1, 3) - assert index.slice_locs(11, 13) == (1, 3) - - index2 = index[::-1] - assert index2.slice_locs(12, 12) == (1, 3) - assert index2.slice_locs(13, 11) == (1, 3) - - def test_slice_locs_na(self): - index = Index([np.nan, 1, 2]) - assert index.slice_locs(1) == (1, 3) - assert index.slice_locs(np.nan) == (0, 3) - - index = Index([0, np.nan, np.nan, 1, 2]) - assert index.slice_locs(np.nan) == (1, 5) - - def test_slice_locs_na_raises(self): - index = Index([np.nan, 1, 2]) - with pytest.raises(KeyError, match=""): - index.slice_locs(start=1.5) - - with pytest.raises(KeyError, match=""): - index.slice_locs(end=1.5) - - @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): - index = Index(list("bcdxy")) - - 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)) - tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) def test_drop_by_str_label(self, index): n = len(index) @@ -2343,41 +2034,3 @@ def test_convert_almost_null_slice(index): msg = "'>=' not supported between instances of 'str' and 'int'" with pytest.raises(TypeError, match=msg): index._convert_slice_indexer(key, "loc") - - -dtlike_dtypes = [ - np.dtype("timedelta64[ns]"), - np.dtype("datetime64[ns]"), - pd.DatetimeTZDtype("ns", "Asia/Tokyo"), - pd.PeriodDtype("ns"), -] - - -@pytest.mark.parametrize("ldtype", dtlike_dtypes) -@pytest.mark.parametrize("rdtype", dtlike_dtypes) -def test_get_indexer_non_unique_wrong_dtype(ldtype, rdtype): - - vals = np.tile(3600 * 10 ** 9 * np.arange(3), 2) - - def construct(dtype): - if dtype is dtlike_dtypes[-1]: - # PeriodArray will try to cast ints to strings - return DatetimeIndex(vals).astype(dtype) - return Index(vals, dtype=dtype) - - left = construct(ldtype) - right = construct(rdtype) - - result = left.get_indexer_non_unique(right) - - if ldtype is rdtype: - ex1 = np.array([0, 3, 1, 4, 2, 5] * 2, dtype=np.intp) - ex2 = np.array([], dtype=np.intp) - tm.assert_numpy_array_equal(result[0], ex1) - tm.assert_numpy_array_equal(result[1], ex2) - - else: - no_matches = np.array([-1] * 6, dtype=np.intp) - missing = np.arange(6, dtype=np.intp) - tm.assert_numpy_array_equal(result[0], no_matches) - tm.assert_numpy_array_equal(result[1], missing) From d294dbfbb34230dc4d21a8577f1cfd54631800eb Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 30 Jan 2021 10:13:28 -0800 Subject: [PATCH 2/4] collct tests --- pandas/tests/indexes/numeric/test_setops.py | 18 +++++++ pandas/tests/indexes/object/test_indexing.py | 16 ++++++ pandas/tests/indexes/test_base.py | 51 -------------------- pandas/tests/indexes/test_indexing.py | 20 ++++++++ 4 files changed, 54 insertions(+), 51 deletions(-) diff --git a/pandas/tests/indexes/numeric/test_setops.py b/pandas/tests/indexes/numeric/test_setops.py index 6cde3e2366062..27e19468dddd2 100644 --- a/pandas/tests/indexes/numeric/test_setops.py +++ b/pandas/tests/indexes/numeric/test_setops.py @@ -110,6 +110,24 @@ def test_intersection_monotonic(self, index2, keeps_name, sort): expected = expected.sort_values() tm.assert_index_equal(result, expected) + def test_symmetric_difference(self, sort): + # smoke + index1 = Index([5, 2, 3, 4], name="index1") + index2 = Index([2, 3, 4, 1]) + result = index1.symmetric_difference(index2, sort=sort) + expected = Index([5, 1]) + assert tm.equalContents(result, expected) + assert result.name is None + if sort is None: + expected = expected.sort_values() + tm.assert_index_equal(result, expected) + + # __xor__ syntax + with tm.assert_produces_warning(FutureWarning): + expected = index1 ^ index2 + assert tm.equalContents(result, expected) + assert result.name is None + class TestSetOpsSort: @pytest.mark.parametrize("slice_", [slice(None), slice(0)]) diff --git a/pandas/tests/indexes/object/test_indexing.py b/pandas/tests/indexes/object/test_indexing.py index 76daa1779a584..a683e9faed1f2 100644 --- a/pandas/tests/indexes/object/test_indexing.py +++ b/pandas/tests/indexes/object/test_indexing.py @@ -47,6 +47,22 @@ def test_get_indexer_strings_raises(self): ["a", "b", "c", "d"], method="pad", tolerance=[2, 2, 2, 2] ) + def test_get_indexer_with_NA_values( + self, unique_nulls_fixture, unique_nulls_fixture2 + ): + # GH#22332 + # check pairwise, that no pair of na values + # is mangled + if unique_nulls_fixture is unique_nulls_fixture2: + return # skip it, values are not unique + arr = np.array([unique_nulls_fixture, unique_nulls_fixture2], dtype=object) + index = Index(arr, dtype=object) + result = index.get_indexer( + [unique_nulls_fixture, unique_nulls_fixture2, "Unknown"] + ) + expected = np.array([0, 1, -1], dtype=np.intp) + tm.assert_numpy_array_equal(result, expected) + class TestSliceLocs: @pytest.mark.parametrize( diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index bfa6af313b16a..d9f56bf8dd301 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -912,24 +912,6 @@ def test_difference_sort(self, index, sort): tm.assert_index_equal(result, expected) - def test_symmetric_difference(self, sort): - # smoke - index1 = Index([5, 2, 3, 4], name="index1") - index2 = Index([2, 3, 4, 1]) - result = index1.symmetric_difference(index2, sort=sort) - expected = Index([5, 1]) - assert tm.equalContents(result, expected) - assert result.name is None - if sort is None: - expected = expected.sort_values() - tm.assert_index_equal(result, expected) - - # __xor__ syntax - with tm.assert_produces_warning(FutureWarning): - expected = index1 ^ index2 - assert tm.equalContents(result, expected) - assert result.name is None - @pytest.mark.parametrize("opname", ["difference", "symmetric_difference"]) def test_difference_incomparable(self, opname): a = Index([3, Timestamp("2000"), 1]) @@ -1119,22 +1101,6 @@ def test_logical_compat(self, op): def _check_method_works(self, method, index): method(index) - def test_get_indexer_with_NA_values( - self, unique_nulls_fixture, unique_nulls_fixture2 - ): - # GH 22332 - # check pairwise, that no pair of na values - # is mangled - if unique_nulls_fixture is unique_nulls_fixture2: - return # skip it, values are not unique - arr = np.array([unique_nulls_fixture, unique_nulls_fixture2], dtype=object) - index = Index(arr, dtype=object) - result = index.get_indexer( - [unique_nulls_fixture, unique_nulls_fixture2, "Unknown"] - ) - expected = np.array([0, 1, -1], dtype=np.intp) - tm.assert_numpy_array_equal(result, expected) - @pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True) def test_drop_by_str_label(self, index): n = len(index) @@ -1246,23 +1212,6 @@ def test_set_value_deprecated(self): idx.set_value(arr, idx[1], 80) assert arr[1] == 80 - @pytest.mark.parametrize( - "index", ["string", "int", "datetime", "timedelta"], indirect=True - ) - def test_get_value(self, index): - # TODO: Remove function? GH 19728 - values = np.random.randn(100) - value = index[67] - - with pytest.raises(AttributeError, match="has no attribute '_values'"): - # Index.get_value requires a Series, not an ndarray - with tm.assert_produces_warning(FutureWarning): - index.get_value(values, value) - - with tm.assert_produces_warning(FutureWarning): - result = index.get_value(Series(values, index=values), value) - tm.assert_almost_equal(result, values[67]) - @pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}]) @pytest.mark.parametrize( "index,expected", diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 538575781b4b2..4bb5af0324138 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -22,6 +22,7 @@ Index, Int64Index, PeriodIndex, + Series, TimedeltaIndex, UInt64Index, ) @@ -137,6 +138,25 @@ def test_contains_with_float_index(self): assert 1 not in float_index +class TestGetValue: + @pytest.mark.parametrize( + "index", ["string", "int", "datetime", "timedelta"], indirect=True + ) + def test_get_value(self, index): + # TODO: Remove function? GH#19728 + values = np.random.randn(100) + value = index[67] + + with pytest.raises(AttributeError, match="has no attribute '_values'"): + # Index.get_value requires a Series, not an ndarray + with tm.assert_produces_warning(FutureWarning): + index.get_value(values, value) + + with tm.assert_produces_warning(FutureWarning): + result = index.get_value(Series(values, index=values), value) + tm.assert_almost_equal(result, values[67]) + + @pytest.mark.parametrize( "idx", [Index([1, 2, 3]), Index([0.1, 0.2, 0.3]), Index(["a", "b", "c"])] ) From 1a475cb6da5961d4ad47db09484c84ebc798acd0 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 30 Jan 2021 10:25:27 -0800 Subject: [PATCH 3/4] collect tests --- pandas/tests/indexes/common.py | 20 ------------- pandas/tests/indexes/test_any_index.py | 13 +++++++++ pandas/tests/indexes/test_base.py | 15 ---------- pandas/tests/indexes/test_common.py | 11 ------- pandas/tests/indexes/test_indexing.py | 40 ++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 46 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 6874db66a8597..96fc85fcf4ae6 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -5,7 +5,6 @@ import pytest from pandas._libs import iNaT -from pandas.errors import InvalidIndexError from pandas.core.dtypes.common import is_datetime64tz_dtype from pandas.core.dtypes.dtypes import CategoricalDtype @@ -202,25 +201,6 @@ def test_reindex_base(self): with pytest.raises(ValueError, match="Invalid fill method"): idx.get_indexer(idx, method="invalid") - def test_get_indexer_consistency(self, index): - # See GH 16819 - if isinstance(index, IntervalIndex): - # requires index.is_non_overlapping - return - - if index.is_unique: - indexer = index.get_indexer(index[0:2]) - assert isinstance(indexer, np.ndarray) - assert indexer.dtype == np.intp - else: - e = "Reindexing only valid with uniquely valued Index objects" - with pytest.raises(InvalidIndexError, match=e): - index.get_indexer(index[0:2]) - - indexer, _ = index.get_indexer_non_unique(index[0:2]) - assert isinstance(indexer, np.ndarray) - assert indexer.dtype == np.intp - def test_ndarray_compat_properties(self): idx = self.create_index() assert idx.T.equals(idx) diff --git a/pandas/tests/indexes/test_any_index.py b/pandas/tests/indexes/test_any_index.py index afeeb63217489..df5c631791c87 100644 --- a/pandas/tests/indexes/test_any_index.py +++ b/pandas/tests/indexes/test_any_index.py @@ -3,6 +3,8 @@ TODO: consider using hypothesis for these. """ +import re + import pytest import pandas._testing as tm @@ -82,6 +84,17 @@ class TestIndexing: def test_slice_keeps_name(self, index): assert index.name == index[1:].name + @pytest.mark.parametrize("item", [101, "no_int"]) + # FutureWarning from non-tuple sequence of nd indexing + @pytest.mark.filterwarnings("ignore::FutureWarning") + def test_getitem_error(self, index, item): + msg = r"index 101 is out of bounds for axis 0 with size [\d]+|" + re.escape( + "only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) " + "and integer or boolean arrays are valid indices" + ) + with pytest.raises(IndexError, match=msg): + index[item] + class TestRendering: def test_str(self, index): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index d9f56bf8dd301..cc7e15c705edb 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1968,18 +1968,3 @@ def test_validate_1d_input(): ser = Series(0, range(4)) with pytest.raises(ValueError, match=msg): ser.index = np.array([[2, 3]] * 4) - - -def test_convert_almost_null_slice(index): - # slice with None at both ends, but not step - - key = slice(None, None, "foo") - - if isinstance(index, pd.IntervalIndex): - msg = "label-based slicing with step!=1 is not supported for IntervalIndex" - with pytest.raises(ValueError, match=msg): - index._convert_slice_indexer(key, "loc") - else: - msg = "'>=' not supported between instances of 'str' and 'int'" - with pytest.raises(TypeError, match=msg): - index._convert_slice_indexer(key, "loc") diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 4ee1ba24df1d4..a41f39bfd328a 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -75,17 +75,6 @@ def test_constructor_unwraps_index(self, index): b = type(a)(a) tm.assert_equal(a._data, b._data) - @pytest.mark.parametrize("itm", [101, "no_int"]) - # FutureWarning from non-tuple sequence of nd indexing - @pytest.mark.filterwarnings("ignore::FutureWarning") - def test_getitem_error(self, index, itm): - msg = r"index 101 is out of bounds for axis 0 with size [\d]+|" + re.escape( - "only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) " - "and integer or boolean arrays are valid indices" - ) - with pytest.raises(IndexError, match=msg): - index[itm] - def test_to_flat_index(self, index): # 22866 if isinstance(index, MultiIndex): diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 4bb5af0324138..04f7a65bd5c56 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -16,11 +16,14 @@ import numpy as np import pytest +from pandas.errors import InvalidIndexError + from pandas import ( DatetimeIndex, Float64Index, Index, Int64Index, + IntervalIndex, PeriodIndex, Series, TimedeltaIndex, @@ -157,6 +160,43 @@ def test_get_value(self, index): tm.assert_almost_equal(result, values[67]) +class TestGetIndexer: + def test_get_indexer_consistency(self, index): + # See GH#16819 + if isinstance(index, IntervalIndex): + # requires index.is_non_overlapping + return + + if index.is_unique: + indexer = index.get_indexer(index[0:2]) + assert isinstance(indexer, np.ndarray) + assert indexer.dtype == np.intp + else: + e = "Reindexing only valid with uniquely valued Index objects" + with pytest.raises(InvalidIndexError, match=e): + index.get_indexer(index[0:2]) + + indexer, _ = index.get_indexer_non_unique(index[0:2]) + assert isinstance(indexer, np.ndarray) + assert indexer.dtype == np.intp + + +class TestConvertSliceIndexer: + def test_convert_almost_null_slice(self, index): + # slice with None at both ends, but not step + + key = slice(None, None, "foo") + + if isinstance(index, IntervalIndex): + msg = "label-based slicing with step!=1 is not supported for IntervalIndex" + with pytest.raises(ValueError, match=msg): + index._convert_slice_indexer(key, "loc") + else: + msg = "'>=' not supported between instances of 'str' and 'int'" + with pytest.raises(TypeError, match=msg): + index._convert_slice_indexer(key, "loc") + + @pytest.mark.parametrize( "idx", [Index([1, 2, 3]), Index([0.1, 0.2, 0.3]), Index(["a", "b", "c"])] ) From 130d47b7006c55eb68b7610eed830687147b5f17 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 30 Jan 2021 10:44:29 -0800 Subject: [PATCH 4/4] collect tests --- pandas/tests/indexes/test_any_index.py | 21 +++++++++++++++++++++ pandas/tests/indexes/test_base.py | 7 ++----- pandas/tests/indexes/test_common.py | 19 ++----------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/pandas/tests/indexes/test_any_index.py b/pandas/tests/indexes/test_any_index.py index df5c631791c87..c8629fdf1e3a6 100644 --- a/pandas/tests/indexes/test_any_index.py +++ b/pandas/tests/indexes/test_any_index.py @@ -35,12 +35,27 @@ def test_mutability(index): index[0] = index[0] +def test_map_identity_mapping(index): + # GH#12766 + tm.assert_index_equal(index, index.map(lambda x: x)) + + def test_wrong_number_names(index): names = index.nlevels * ["apple", "banana", "carrot"] with pytest.raises(ValueError, match="^Length"): index.names = names +def test_view_preserves_name(index): + assert index.view().name == index.name + + +def test_ravel_deprecation(index): + # GH#19956 ravel returning ndarray is deprecated + with tm.assert_produces_warning(FutureWarning): + index.ravel() + + class TestConversion: def test_to_series(self, index): # assert that we are creating a copy of the index @@ -79,6 +94,12 @@ def test_pickle_roundtrip(self, index): # GH#8367 round-trip with timezone assert index.equal_levels(result) + def test_pickle_preserves_name(self, index): + original_name, index.name = index.name, "foo" + unpickled = tm.round_trip_pickle(index) + assert index.equals(unpickled) + index.name = original_name + class TestIndexing: def test_slice_keeps_name(self, index): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index cc7e15c705edb..092b1c447eb0d 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -748,10 +748,6 @@ def test_union_dt_as_obj(self, sort): tm.assert_contains_all(index, second_cat) tm.assert_contains_all(date_index, first_cat) - def test_map_identity_mapping(self, index): - # GH 12766 - tm.assert_index_equal(index, index.map(lambda x: x)) - def test_map_with_tuples(self): # GH 12766 @@ -881,8 +877,9 @@ def test_difference_name_preservation(self, index, second_name, expected, sort): else: assert result.name == expected - @pytest.mark.parametrize("index", ["string"], indirect=True) def test_difference_empty_arg(self, index, sort): + if isinstance(index, MultiIndex): + pytest.skip("Not applicable") first = index[5:20] first.name = "name" result = first.difference([], sort) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index a41f39bfd328a..2b49ea00d3322 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -128,9 +128,8 @@ def test_copy_and_deepcopy(self, index): def test_unique(self, index): # don't test a MultiIndex here (as its tested separated) - # don't test a CategoricalIndex because categories change (GH 18291) - if isinstance(index, (MultiIndex, CategoricalIndex)): - pytest.skip("Skip check for MultiIndex/CategoricalIndex") + if isinstance(index, MultiIndex): + pytest.skip("Skip check for MultiIndex") # GH 17896 expected = index.drop_duplicates() @@ -200,9 +199,6 @@ def test_get_unique_index(self, index): result = i._get_unique_index(dropna=dropna) tm.assert_index_equal(result, expected) - def test_view(self, index): - assert index.view().name == index.name - def test_searchsorted_monotonic(self, index): # GH17271 # not implemented for tuple searches in MultiIndex @@ -248,12 +244,6 @@ def test_searchsorted_monotonic(self, index): with pytest.raises(ValueError, match=msg): index._searchsorted_monotonic(value, side="left") - def test_pickle(self, index): - original_name, index.name = index.name, "foo" - unpickled = tm.round_trip_pickle(index) - assert index.equals(unpickled) - index.name = original_name - def test_drop_duplicates(self, index, keep): if isinstance(index, MultiIndex): pytest.skip("MultiIndex is tested separately") @@ -360,11 +350,6 @@ def test_astype_preserves_name(self, index, dtype): else: assert result.name == index.name - def test_ravel_deprecation(self, index): - # GH#19956 ravel returning ndarray is deprecated - with tm.assert_produces_warning(FutureWarning): - index.ravel() - def test_asi8_deprecation(self, index): # GH#37877 if isinstance(index, (DatetimeIndex, TimedeltaIndex, PeriodIndex)):