Skip to content

Commit b3aff68

Browse files
jbrockmendeljreback
authored andcommitted
REF/TST: directories for CategoricalIndex, RangeIndex tests (pandas-dev#30476)
1 parent 977eae3 commit b3aff68

File tree

6 files changed

+304
-289
lines changed

6 files changed

+304
-289
lines changed

pandas/tests/indexes/categorical/__init__.py

Whitespace-only changes.

pandas/tests/indexes/test_category.py renamed to pandas/tests/indexes/categorical/test_category.py

+1-142
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pandas.core.indexes.api import CategoricalIndex, Index
1313
import pandas.util.testing as tm
1414

15-
from .common import Base
15+
from ..common import Base
1616

1717

1818
class TestCategoricalIndex(Base):
@@ -32,147 +32,6 @@ def test_can_hold_identifiers(self):
3232
key = idx[0]
3333
assert idx._can_hold_identifiers_and_holds_name(key) is True
3434

35-
def test_construction(self):
36-
37-
ci = self.create_index(categories=list("abcd"))
38-
categories = ci.categories
39-
40-
result = Index(ci)
41-
tm.assert_index_equal(result, ci, exact=True)
42-
assert not result.ordered
43-
44-
result = Index(ci.values)
45-
tm.assert_index_equal(result, ci, exact=True)
46-
assert not result.ordered
47-
48-
# empty
49-
result = CategoricalIndex(categories=categories)
50-
tm.assert_index_equal(result.categories, Index(categories))
51-
tm.assert_numpy_array_equal(result.codes, np.array([], dtype="int8"))
52-
assert not result.ordered
53-
54-
# passing categories
55-
result = CategoricalIndex(list("aabbca"), categories=categories)
56-
tm.assert_index_equal(result.categories, Index(categories))
57-
tm.assert_numpy_array_equal(
58-
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
59-
)
60-
61-
c = pd.Categorical(list("aabbca"))
62-
result = CategoricalIndex(c)
63-
tm.assert_index_equal(result.categories, Index(list("abc")))
64-
tm.assert_numpy_array_equal(
65-
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
66-
)
67-
assert not result.ordered
68-
69-
result = CategoricalIndex(c, categories=categories)
70-
tm.assert_index_equal(result.categories, Index(categories))
71-
tm.assert_numpy_array_equal(
72-
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
73-
)
74-
assert not result.ordered
75-
76-
ci = CategoricalIndex(c, categories=list("abcd"))
77-
result = CategoricalIndex(ci)
78-
tm.assert_index_equal(result.categories, Index(categories))
79-
tm.assert_numpy_array_equal(
80-
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
81-
)
82-
assert not result.ordered
83-
84-
result = CategoricalIndex(ci, categories=list("ab"))
85-
tm.assert_index_equal(result.categories, Index(list("ab")))
86-
tm.assert_numpy_array_equal(
87-
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
88-
)
89-
assert not result.ordered
90-
91-
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
92-
tm.assert_index_equal(result.categories, Index(list("ab")))
93-
tm.assert_numpy_array_equal(
94-
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
95-
)
96-
assert result.ordered
97-
98-
result = pd.CategoricalIndex(ci, categories=list("ab"), ordered=True)
99-
expected = pd.CategoricalIndex(
100-
ci, categories=list("ab"), ordered=True, dtype="category"
101-
)
102-
tm.assert_index_equal(result, expected, exact=True)
103-
104-
# turn me to an Index
105-
result = Index(np.array(ci))
106-
assert isinstance(result, Index)
107-
assert not isinstance(result, CategoricalIndex)
108-
109-
def test_construction_with_dtype(self):
110-
111-
# specify dtype
112-
ci = self.create_index(categories=list("abc"))
113-
114-
result = Index(np.array(ci), dtype="category")
115-
tm.assert_index_equal(result, ci, exact=True)
116-
117-
result = Index(np.array(ci).tolist(), dtype="category")
118-
tm.assert_index_equal(result, ci, exact=True)
119-
120-
# these are generally only equal when the categories are reordered
121-
ci = self.create_index()
122-
123-
result = Index(np.array(ci), dtype="category").reorder_categories(ci.categories)
124-
tm.assert_index_equal(result, ci, exact=True)
125-
126-
# make sure indexes are handled
127-
expected = CategoricalIndex([0, 1, 2], categories=[0, 1, 2], ordered=True)
128-
idx = Index(range(3))
129-
result = CategoricalIndex(idx, categories=idx, ordered=True)
130-
tm.assert_index_equal(result, expected, exact=True)
131-
132-
def test_construction_empty_with_bool_categories(self):
133-
# see gh-22702
134-
cat = pd.CategoricalIndex([], categories=[True, False])
135-
categories = sorted(cat.categories.tolist())
136-
assert categories == [False, True]
137-
138-
def test_construction_with_categorical_dtype(self):
139-
# construction with CategoricalDtype
140-
# GH18109
141-
data, cats, ordered = "a a b b".split(), "c b a".split(), True
142-
dtype = CategoricalDtype(categories=cats, ordered=ordered)
143-
144-
result = CategoricalIndex(data, dtype=dtype)
145-
expected = CategoricalIndex(data, categories=cats, ordered=ordered)
146-
tm.assert_index_equal(result, expected, exact=True)
147-
148-
# GH 19032
149-
result = Index(data, dtype=dtype)
150-
tm.assert_index_equal(result, expected, exact=True)
151-
152-
# error when combining categories/ordered and dtype kwargs
153-
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
154-
with pytest.raises(ValueError, match=msg):
155-
CategoricalIndex(data, categories=cats, dtype=dtype)
156-
157-
with pytest.raises(ValueError, match=msg):
158-
Index(data, categories=cats, dtype=dtype)
159-
160-
with pytest.raises(ValueError, match=msg):
161-
CategoricalIndex(data, ordered=ordered, dtype=dtype)
162-
163-
with pytest.raises(ValueError, match=msg):
164-
Index(data, ordered=ordered, dtype=dtype)
165-
166-
def test_create_categorical(self):
167-
# https://github.com/pandas-dev/pandas/pull/17513
168-
# The public CI constructor doesn't hit this code path with
169-
# instances of CategoricalIndex, but we still want to test the code
170-
ci = CategoricalIndex(["a", "b", "c"])
171-
# First ci is self, second ci is data.
172-
result = CategoricalIndex._create_categorical(ci, ci)
173-
expected = Categorical(["a", "b", "c"])
174-
tm.assert_categorical_equal(result, expected)
175-
17635
@pytest.mark.parametrize(
17736
"func,op_name",
17837
[
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import Categorical, CategoricalDtype, CategoricalIndex, Index
5+
import pandas.util.testing as tm
6+
7+
8+
class TestCategoricalIndexConstructors:
9+
def test_construction(self):
10+
11+
ci = CategoricalIndex(list("aabbca"), categories=list("abcd"), ordered=False)
12+
categories = ci.categories
13+
14+
result = Index(ci)
15+
tm.assert_index_equal(result, ci, exact=True)
16+
assert not result.ordered
17+
18+
result = Index(ci.values)
19+
tm.assert_index_equal(result, ci, exact=True)
20+
assert not result.ordered
21+
22+
# empty
23+
result = CategoricalIndex(categories=categories)
24+
tm.assert_index_equal(result.categories, Index(categories))
25+
tm.assert_numpy_array_equal(result.codes, np.array([], dtype="int8"))
26+
assert not result.ordered
27+
28+
# passing categories
29+
result = CategoricalIndex(list("aabbca"), categories=categories)
30+
tm.assert_index_equal(result.categories, Index(categories))
31+
tm.assert_numpy_array_equal(
32+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
33+
)
34+
35+
c = Categorical(list("aabbca"))
36+
result = CategoricalIndex(c)
37+
tm.assert_index_equal(result.categories, Index(list("abc")))
38+
tm.assert_numpy_array_equal(
39+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
40+
)
41+
assert not result.ordered
42+
43+
result = CategoricalIndex(c, categories=categories)
44+
tm.assert_index_equal(result.categories, Index(categories))
45+
tm.assert_numpy_array_equal(
46+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
47+
)
48+
assert not result.ordered
49+
50+
ci = CategoricalIndex(c, categories=list("abcd"))
51+
result = CategoricalIndex(ci)
52+
tm.assert_index_equal(result.categories, Index(categories))
53+
tm.assert_numpy_array_equal(
54+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
55+
)
56+
assert not result.ordered
57+
58+
result = CategoricalIndex(ci, categories=list("ab"))
59+
tm.assert_index_equal(result.categories, Index(list("ab")))
60+
tm.assert_numpy_array_equal(
61+
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
62+
)
63+
assert not result.ordered
64+
65+
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
66+
tm.assert_index_equal(result.categories, Index(list("ab")))
67+
tm.assert_numpy_array_equal(
68+
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
69+
)
70+
assert result.ordered
71+
72+
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
73+
expected = CategoricalIndex(
74+
ci, categories=list("ab"), ordered=True, dtype="category"
75+
)
76+
tm.assert_index_equal(result, expected, exact=True)
77+
78+
# turn me to an Index
79+
result = Index(np.array(ci))
80+
assert isinstance(result, Index)
81+
assert not isinstance(result, CategoricalIndex)
82+
83+
def test_construction_with_dtype(self):
84+
85+
# specify dtype
86+
ci = CategoricalIndex(list("aabbca"), categories=list("abc"), ordered=False)
87+
88+
result = Index(np.array(ci), dtype="category")
89+
tm.assert_index_equal(result, ci, exact=True)
90+
91+
result = Index(np.array(ci).tolist(), dtype="category")
92+
tm.assert_index_equal(result, ci, exact=True)
93+
94+
# these are generally only equal when the categories are reordered
95+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
96+
97+
result = Index(np.array(ci), dtype="category").reorder_categories(ci.categories)
98+
tm.assert_index_equal(result, ci, exact=True)
99+
100+
# make sure indexes are handled
101+
expected = CategoricalIndex([0, 1, 2], categories=[0, 1, 2], ordered=True)
102+
idx = Index(range(3))
103+
result = CategoricalIndex(idx, categories=idx, ordered=True)
104+
tm.assert_index_equal(result, expected, exact=True)
105+
106+
def test_construction_empty_with_bool_categories(self):
107+
# see GH#22702
108+
cat = CategoricalIndex([], categories=[True, False])
109+
categories = sorted(cat.categories.tolist())
110+
assert categories == [False, True]
111+
112+
def test_construction_with_categorical_dtype(self):
113+
# construction with CategoricalDtype
114+
# GH#18109
115+
data, cats, ordered = "a a b b".split(), "c b a".split(), True
116+
dtype = CategoricalDtype(categories=cats, ordered=ordered)
117+
118+
result = CategoricalIndex(data, dtype=dtype)
119+
expected = CategoricalIndex(data, categories=cats, ordered=ordered)
120+
tm.assert_index_equal(result, expected, exact=True)
121+
122+
# GH#19032
123+
result = Index(data, dtype=dtype)
124+
tm.assert_index_equal(result, expected, exact=True)
125+
126+
# error when combining categories/ordered and dtype kwargs
127+
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
128+
with pytest.raises(ValueError, match=msg):
129+
CategoricalIndex(data, categories=cats, dtype=dtype)
130+
131+
with pytest.raises(ValueError, match=msg):
132+
Index(data, categories=cats, dtype=dtype)
133+
134+
with pytest.raises(ValueError, match=msg):
135+
CategoricalIndex(data, ordered=ordered, dtype=dtype)
136+
137+
with pytest.raises(ValueError, match=msg):
138+
Index(data, ordered=ordered, dtype=dtype)
139+
140+
def test_create_categorical(self):
141+
# GH#17513 The public CI constructor doesn't hit this code path with
142+
# instances of CategoricalIndex, but we still want to test the code
143+
ci = CategoricalIndex(["a", "b", "c"])
144+
# First ci is self, second ci is data.
145+
result = CategoricalIndex._create_categorical(ci, ci)
146+
expected = Categorical(["a", "b", "c"])
147+
tm.assert_categorical_equal(result, expected)

pandas/tests/indexes/ranges/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)