BUG: fix get_indexer_non_unique with CategoricalIndex key (#21457) · pandas-dev/pandas@576d5c6 (original) (raw)

`@@ -5,7 +5,7 @@

`

5

5

`import numpy as np

`

6

6

``

7

7

`import pandas.util.testing as tm

`

8

``

`-

from pandas import Categorical, Index, PeriodIndex

`

``

8

`+

from pandas import Categorical, Index, CategoricalIndex, PeriodIndex

`

9

9

`from pandas.tests.categorical.common import TestCategorical

`

10

10

``

11

11

``

`@@ -103,3 +103,21 @@ def f():

`

103

103

`s.categories = [1, 2]

`

104

104

``

105

105

`pytest.raises(ValueError, f)

`

``

106

+

``

107

`+

Combinations of sorted/unique:

`

``

108

`+

@pytest.mark.parametrize("idx_values", [[1, 2, 3, 4], [1, 3, 2, 4],

`

``

109

`+

[1, 3, 3, 4], [1, 2, 2, 4]])

`

``

110

`+

Combinations of missing/unique

`

``

111

`+

@pytest.mark.parametrize("key_values", [[1, 2], [1, 5], [1, 1], [5, 5]])

`

``

112

`+

@pytest.mark.parametrize("key_class", [Categorical, CategoricalIndex])

`

``

113

`+

def test_get_indexer_non_unique(self, idx_values, key_values, key_class):

`

``

114

`+

GH 21448

`

``

115

`+

key = key_class(key_values, categories=range(1, 5))

`

``

116

`+

Test for flat index and CategoricalIndex with same/different cats:

`

``

117

`+

for dtype in None, 'category', key.dtype:

`

``

118

`+

idx = Index(idx_values, dtype=dtype)

`

``

119

`+

expected, exp_miss = idx.get_indexer_non_unique(key_values)

`

``

120

`+

result, res_miss = idx.get_indexer_non_unique(key)

`

``

121

+

``

122

`+

tm.assert_numpy_array_equal(expected, result)

`

``

123

`+

tm.assert_numpy_array_equal(exp_miss, res_miss)

`