zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import pytest
 
from pandas import (
    Categorical,
    CategoricalIndex,
    Index,
    Interval,
)
import pandas._testing as tm
 
 
class TestReindex:
    def test_reindex_list_non_unique(self):
        # GH#11586
        msg = "cannot reindex on an axis with duplicate labels"
        ci = CategoricalIndex(["a", "b", "c", "a"])
        with pytest.raises(ValueError, match=msg):
            ci.reindex(["a", "c"])
 
    def test_reindex_categorical_non_unique(self):
        msg = "cannot reindex on an axis with duplicate labels"
        ci = CategoricalIndex(["a", "b", "c", "a"])
        with pytest.raises(ValueError, match=msg):
            ci.reindex(Categorical(["a", "c"]))
 
    def test_reindex_list_non_unique_unused_category(self):
        msg = "cannot reindex on an axis with duplicate labels"
        ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
        with pytest.raises(ValueError, match=msg):
            ci.reindex(["a", "c"])
 
    def test_reindex_categorical_non_unique_unused_category(self):
        msg = "cannot reindex on an axis with duplicate labels"
        ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
        with pytest.raises(ValueError, match=msg):
            ci.reindex(Categorical(["a", "c"]))
 
    def test_reindex_duplicate_target(self):
        # See GH25459
        cat = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c", "d"])
        res, indexer = cat.reindex(["a", "c", "c"])
        exp = Index(["a", "c", "c"], dtype="object")
        tm.assert_index_equal(res, exp, exact=True)
        tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
 
        res, indexer = cat.reindex(
            CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
        )
        exp = CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
        tm.assert_index_equal(res, exp, exact=True)
        tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
 
    def test_reindex_empty_index(self):
        # See GH16770
        c = CategoricalIndex([])
        res, indexer = c.reindex(["a", "b"])
        tm.assert_index_equal(res, Index(["a", "b"]), exact=True)
        tm.assert_numpy_array_equal(indexer, np.array([-1, -1], dtype=np.intp))
 
    def test_reindex_categorical_added_category(self):
        # GH 42424
        ci = CategoricalIndex(
            [Interval(0, 1, closed="right"), Interval(1, 2, closed="right")],
            ordered=True,
        )
        ci_add = CategoricalIndex(
            [
                Interval(0, 1, closed="right"),
                Interval(1, 2, closed="right"),
                Interval(2, 3, closed="right"),
                Interval(3, 4, closed="right"),
            ],
            ordered=True,
        )
        result, _ = ci.reindex(ci_add)
        expected = ci_add
        tm.assert_index_equal(expected, result)