1
1
import numpy as np
2
+
import pytest
2
3
3
4
from pandas import (
4
5
Categorical,
12
13
class TestReindex:
13
14
def test_reindex_list_non_unique(self):
14
15
# GH#11586
16
+
msg = "cannot reindex on an axis with duplicate labels"
15
17
ci = CategoricalIndex(["a", "b", "c", "a"])
16
-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
17
-
res, indexer = ci.reindex(["a", "c"])
18
-
19
-
tm.assert_index_equal(res, Index(["a", "a", "c"]), exact=True)
20
-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp))
18
+
with pytest.raises(ValueError, match=msg):
19
+
ci.reindex(["a", "c"])
21
20
22
21
def test_reindex_categorical_non_unique(self):
22
+
msg = "cannot reindex on an axis with duplicate labels"
23
23
ci = CategoricalIndex(["a", "b", "c", "a"])
24
-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
25
-
res, indexer = ci.reindex(Categorical(["a", "c"]))
26
-
27
-
exp = CategoricalIndex(["a", "a", "c"], categories=["a", "c"])
28
-
tm.assert_index_equal(res, exp, exact=True)
29
-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp))
24
+
with pytest.raises(ValueError, match=msg):
25
+
ci.reindex(Categorical(["a", "c"]))
30
26
31
27
def test_reindex_list_non_unique_unused_category(self):
28
+
msg = "cannot reindex on an axis with duplicate labels"
32
29
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
33
-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
34
-
res, indexer = ci.reindex(["a", "c"])
35
-
exp = Index(["a", "a", "c"], dtype="object")
36
-
tm.assert_index_equal(res, exp, exact=True)
37
-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp))
30
+
with pytest.raises(ValueError, match=msg):
31
+
ci.reindex(["a", "c"])
38
32
39
33
def test_reindex_categorical_non_unique_unused_category(self):
34
+
msg = "cannot reindex on an axis with duplicate labels"
40
35
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
41
-
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
42
-
res, indexer = ci.reindex(Categorical(["a", "c"]))
43
-
exp = CategoricalIndex(["a", "a", "c"], categories=["a", "c"])
44
-
tm.assert_index_equal(res, exp, exact=True)
45
-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2], dtype=np.intp))
36
+
with pytest.raises(ValueError, match=msg):
37
+
ci.reindex(Categorical(["a", "c"]))
46
38
47
39
def test_reindex_duplicate_target(self):
48
40
# See GH25459
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4