Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- ckpts/universal/global_step80/zero/14.mlp.dense_h_to_4h.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/14.mlp.dense_h_to_4h.weight/fp32.pt +3 -0
- ckpts/universal/global_step80/zero/9.attention.dense.weight/fp32.pt +3 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/conftest.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_any_index.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_base.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_common.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_datetimelike.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_engines.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_frozen.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_index_new.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_indexing.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_numpy_compat.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_old_base.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_setops.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_subclass.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__init__.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_append.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_astype.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_category.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_constructors.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_equals.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_fillna.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_formats.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_indexing.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_map.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_reindex.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_setops.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_append.py +62 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_astype.py +90 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_category.py +394 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_constructors.py +142 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_equals.py +96 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_fillna.py +54 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_formats.py +120 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_indexing.py +420 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_map.py +144 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_reindex.py +78 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_setops.py +18 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/conftest.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_analytics.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_compat.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_drop.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_duplicates.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_equivalence.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_formats.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_get_set.cpython-310.pyc +0 -0
ckpts/universal/global_step80/zero/14.mlp.dense_h_to_4h.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d6101e0d4b6e9dbcf4502c22f6913f97780d8582dcf6d92a6b6fc812938b1745
|
3 |
+
size 33555612
|
ckpts/universal/global_step80/zero/14.mlp.dense_h_to_4h.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2fc87ed0a360ddf81ffb1b2c0bc718db0ccbefc3fb97aba525779364579ee0f8
|
3 |
+
size 33555533
|
ckpts/universal/global_step80/zero/9.attention.dense.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7d7b915fb8268f1ce18c079d5df8054171ae4bcb7f353381df39f7f21bf9bab9
|
3 |
+
size 16778317
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (188 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/conftest.cpython-310.pyc
ADDED
Binary file (1.27 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_any_index.cpython-310.pyc
ADDED
Binary file (6.77 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_base.cpython-310.pyc
ADDED
Binary file (53.3 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_common.cpython-310.pyc
ADDED
Binary file (13 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_datetimelike.cpython-310.pyc
ADDED
Binary file (5.87 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_engines.cpython-310.pyc
ADDED
Binary file (5.91 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_frozen.cpython-310.pyc
ADDED
Binary file (4.52 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_index_new.cpython-310.pyc
ADDED
Binary file (15.1 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (11.6 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_numpy_compat.cpython-310.pyc
ADDED
Binary file (4.47 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_old_base.cpython-310.pyc
ADDED
Binary file (30.1 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_setops.cpython-310.pyc
ADDED
Binary file (23.3 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_subclass.cpython-310.pyc
ADDED
Binary file (1.53 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (200 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_append.cpython-310.pyc
ADDED
Binary file (2.76 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (2.36 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_category.cpython-310.pyc
ADDED
Binary file (11.5 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (3.91 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_equals.cpython-310.pyc
ADDED
Binary file (3.07 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_fillna.cpython-310.pyc
ADDED
Binary file (1.83 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_formats.cpython-310.pyc
ADDED
Binary file (5.39 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (13 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_map.cpython-310.pyc
ADDED
Binary file (3.74 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_reindex.cpython-310.pyc
ADDED
Binary file (3.2 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_setops.cpython-310.pyc
ADDED
Binary file (731 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_append.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
CategoricalIndex,
|
5 |
+
Index,
|
6 |
+
)
|
7 |
+
import pandas._testing as tm
|
8 |
+
|
9 |
+
|
10 |
+
class TestAppend:
|
11 |
+
@pytest.fixture
|
12 |
+
def ci(self):
|
13 |
+
categories = list("cab")
|
14 |
+
return CategoricalIndex(list("aabbca"), categories=categories, ordered=False)
|
15 |
+
|
16 |
+
def test_append(self, ci):
|
17 |
+
# append cats with the same categories
|
18 |
+
result = ci[:3].append(ci[3:])
|
19 |
+
tm.assert_index_equal(result, ci, exact=True)
|
20 |
+
|
21 |
+
foos = [ci[:1], ci[1:3], ci[3:]]
|
22 |
+
result = foos[0].append(foos[1:])
|
23 |
+
tm.assert_index_equal(result, ci, exact=True)
|
24 |
+
|
25 |
+
def test_append_empty(self, ci):
|
26 |
+
# empty
|
27 |
+
result = ci.append([])
|
28 |
+
tm.assert_index_equal(result, ci, exact=True)
|
29 |
+
|
30 |
+
def test_append_mismatched_categories(self, ci):
|
31 |
+
# appending with different categories or reordered is not ok
|
32 |
+
msg = "all inputs must be Index"
|
33 |
+
with pytest.raises(TypeError, match=msg):
|
34 |
+
ci.append(ci.values.set_categories(list("abcd")))
|
35 |
+
with pytest.raises(TypeError, match=msg):
|
36 |
+
ci.append(ci.values.reorder_categories(list("abc")))
|
37 |
+
|
38 |
+
def test_append_category_objects(self, ci):
|
39 |
+
# with objects
|
40 |
+
result = ci.append(Index(["c", "a"]))
|
41 |
+
expected = CategoricalIndex(list("aabbcaca"), categories=ci.categories)
|
42 |
+
tm.assert_index_equal(result, expected, exact=True)
|
43 |
+
|
44 |
+
def test_append_non_categories(self, ci):
|
45 |
+
# invalid objects -> cast to object via concat_compat
|
46 |
+
result = ci.append(Index(["a", "d"]))
|
47 |
+
expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"])
|
48 |
+
tm.assert_index_equal(result, expected, exact=True)
|
49 |
+
|
50 |
+
def test_append_object(self, ci):
|
51 |
+
# GH#14298 - if base object is not categorical -> coerce to object
|
52 |
+
result = Index(["c", "a"]).append(ci)
|
53 |
+
expected = Index(list("caaabbca"))
|
54 |
+
tm.assert_index_equal(result, expected, exact=True)
|
55 |
+
|
56 |
+
def test_append_to_another(self):
|
57 |
+
# hits Index._concat
|
58 |
+
fst = Index(["a", "b"])
|
59 |
+
snd = CategoricalIndex(["d", "e"])
|
60 |
+
result = fst.append(snd)
|
61 |
+
expected = Index(["a", "b", "d", "e"])
|
62 |
+
tm.assert_index_equal(result, expected)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_astype.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import date
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
from pandas import (
|
7 |
+
Categorical,
|
8 |
+
CategoricalDtype,
|
9 |
+
CategoricalIndex,
|
10 |
+
Index,
|
11 |
+
IntervalIndex,
|
12 |
+
)
|
13 |
+
import pandas._testing as tm
|
14 |
+
|
15 |
+
|
16 |
+
class TestAstype:
|
17 |
+
def test_astype(self):
|
18 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
19 |
+
|
20 |
+
result = ci.astype(object)
|
21 |
+
tm.assert_index_equal(result, Index(np.array(ci), dtype=object))
|
22 |
+
|
23 |
+
# this IS equal, but not the same class
|
24 |
+
assert result.equals(ci)
|
25 |
+
assert isinstance(result, Index)
|
26 |
+
assert not isinstance(result, CategoricalIndex)
|
27 |
+
|
28 |
+
# interval
|
29 |
+
ii = IntervalIndex.from_arrays(left=[-0.001, 2.0], right=[2, 4], closed="right")
|
30 |
+
|
31 |
+
ci = CategoricalIndex(
|
32 |
+
Categorical.from_codes([0, 1, -1], categories=ii, ordered=True)
|
33 |
+
)
|
34 |
+
|
35 |
+
result = ci.astype("interval")
|
36 |
+
expected = ii.take([0, 1, -1], allow_fill=True, fill_value=np.nan)
|
37 |
+
tm.assert_index_equal(result, expected)
|
38 |
+
|
39 |
+
result = IntervalIndex(result.values)
|
40 |
+
tm.assert_index_equal(result, expected)
|
41 |
+
|
42 |
+
@pytest.mark.parametrize("name", [None, "foo"])
|
43 |
+
@pytest.mark.parametrize("dtype_ordered", [True, False])
|
44 |
+
@pytest.mark.parametrize("index_ordered", [True, False])
|
45 |
+
def test_astype_category(self, name, dtype_ordered, index_ordered):
|
46 |
+
# GH#18630
|
47 |
+
index = CategoricalIndex(
|
48 |
+
list("aabbca"), categories=list("cab"), ordered=index_ordered
|
49 |
+
)
|
50 |
+
if name:
|
51 |
+
index = index.rename(name)
|
52 |
+
|
53 |
+
# standard categories
|
54 |
+
dtype = CategoricalDtype(ordered=dtype_ordered)
|
55 |
+
result = index.astype(dtype)
|
56 |
+
expected = CategoricalIndex(
|
57 |
+
index.tolist(),
|
58 |
+
name=name,
|
59 |
+
categories=index.categories,
|
60 |
+
ordered=dtype_ordered,
|
61 |
+
)
|
62 |
+
tm.assert_index_equal(result, expected)
|
63 |
+
|
64 |
+
# non-standard categories
|
65 |
+
dtype = CategoricalDtype(index.unique().tolist()[:-1], dtype_ordered)
|
66 |
+
result = index.astype(dtype)
|
67 |
+
expected = CategoricalIndex(index.tolist(), name=name, dtype=dtype)
|
68 |
+
tm.assert_index_equal(result, expected)
|
69 |
+
|
70 |
+
if dtype_ordered is False:
|
71 |
+
# dtype='category' can't specify ordered, so only test once
|
72 |
+
result = index.astype("category")
|
73 |
+
expected = index
|
74 |
+
tm.assert_index_equal(result, expected)
|
75 |
+
|
76 |
+
@pytest.mark.parametrize("box", [True, False])
|
77 |
+
def test_categorical_date_roundtrip(self, box):
|
78 |
+
# astype to categorical and back should preserve date objects
|
79 |
+
v = date.today()
|
80 |
+
|
81 |
+
obj = Index([v, v])
|
82 |
+
assert obj.dtype == object
|
83 |
+
if box:
|
84 |
+
obj = obj.array
|
85 |
+
|
86 |
+
cat = obj.astype("category")
|
87 |
+
|
88 |
+
rtrip = cat.astype(object)
|
89 |
+
assert rtrip.dtype == object
|
90 |
+
assert type(rtrip[0]) is date
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_category.py
ADDED
@@ -0,0 +1,394 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas._config import using_pyarrow_string_dtype
|
5 |
+
|
6 |
+
from pandas._libs import index as libindex
|
7 |
+
from pandas._libs.arrays import NDArrayBacked
|
8 |
+
|
9 |
+
import pandas as pd
|
10 |
+
from pandas import (
|
11 |
+
Categorical,
|
12 |
+
CategoricalDtype,
|
13 |
+
)
|
14 |
+
import pandas._testing as tm
|
15 |
+
from pandas.core.indexes.api import (
|
16 |
+
CategoricalIndex,
|
17 |
+
Index,
|
18 |
+
)
|
19 |
+
|
20 |
+
|
21 |
+
class TestCategoricalIndex:
|
22 |
+
@pytest.fixture
|
23 |
+
def simple_index(self) -> CategoricalIndex:
|
24 |
+
return CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
25 |
+
|
26 |
+
def test_can_hold_identifiers(self):
|
27 |
+
idx = CategoricalIndex(list("aabbca"), categories=None, ordered=False)
|
28 |
+
key = idx[0]
|
29 |
+
assert idx._can_hold_identifiers_and_holds_name(key) is True
|
30 |
+
|
31 |
+
def test_insert(self, simple_index):
|
32 |
+
ci = simple_index
|
33 |
+
categories = ci.categories
|
34 |
+
|
35 |
+
# test 0th element
|
36 |
+
result = ci.insert(0, "a")
|
37 |
+
expected = CategoricalIndex(list("aaabbca"), categories=categories)
|
38 |
+
tm.assert_index_equal(result, expected, exact=True)
|
39 |
+
|
40 |
+
# test Nth element that follows Python list behavior
|
41 |
+
result = ci.insert(-1, "a")
|
42 |
+
expected = CategoricalIndex(list("aabbcaa"), categories=categories)
|
43 |
+
tm.assert_index_equal(result, expected, exact=True)
|
44 |
+
|
45 |
+
# test empty
|
46 |
+
result = CategoricalIndex([], categories=categories).insert(0, "a")
|
47 |
+
expected = CategoricalIndex(["a"], categories=categories)
|
48 |
+
tm.assert_index_equal(result, expected, exact=True)
|
49 |
+
|
50 |
+
# invalid -> cast to object
|
51 |
+
expected = ci.astype(object).insert(0, "d")
|
52 |
+
result = ci.insert(0, "d").astype(object)
|
53 |
+
tm.assert_index_equal(result, expected, exact=True)
|
54 |
+
|
55 |
+
# GH 18295 (test missing)
|
56 |
+
expected = CategoricalIndex(["a", np.nan, "a", "b", "c", "b"])
|
57 |
+
for na in (np.nan, pd.NaT, None):
|
58 |
+
result = CategoricalIndex(list("aabcb")).insert(1, na)
|
59 |
+
tm.assert_index_equal(result, expected)
|
60 |
+
|
61 |
+
def test_insert_na_mismatched_dtype(self):
|
62 |
+
ci = CategoricalIndex([0, 1, 1])
|
63 |
+
result = ci.insert(0, pd.NaT)
|
64 |
+
expected = Index([pd.NaT, 0, 1, 1], dtype=object)
|
65 |
+
tm.assert_index_equal(result, expected)
|
66 |
+
|
67 |
+
def test_delete(self, simple_index):
|
68 |
+
ci = simple_index
|
69 |
+
categories = ci.categories
|
70 |
+
|
71 |
+
result = ci.delete(0)
|
72 |
+
expected = CategoricalIndex(list("abbca"), categories=categories)
|
73 |
+
tm.assert_index_equal(result, expected, exact=True)
|
74 |
+
|
75 |
+
result = ci.delete(-1)
|
76 |
+
expected = CategoricalIndex(list("aabbc"), categories=categories)
|
77 |
+
tm.assert_index_equal(result, expected, exact=True)
|
78 |
+
|
79 |
+
with tm.external_error_raised((IndexError, ValueError)):
|
80 |
+
# Either depending on NumPy version
|
81 |
+
ci.delete(10)
|
82 |
+
|
83 |
+
@pytest.mark.parametrize(
|
84 |
+
"data, non_lexsorted_data",
|
85 |
+
[[[1, 2, 3], [9, 0, 1, 2, 3]], [list("abc"), list("fabcd")]],
|
86 |
+
)
|
87 |
+
def test_is_monotonic(self, data, non_lexsorted_data):
|
88 |
+
c = CategoricalIndex(data)
|
89 |
+
assert c.is_monotonic_increasing is True
|
90 |
+
assert c.is_monotonic_decreasing is False
|
91 |
+
|
92 |
+
c = CategoricalIndex(data, ordered=True)
|
93 |
+
assert c.is_monotonic_increasing is True
|
94 |
+
assert c.is_monotonic_decreasing is False
|
95 |
+
|
96 |
+
c = CategoricalIndex(data, categories=reversed(data))
|
97 |
+
assert c.is_monotonic_increasing is False
|
98 |
+
assert c.is_monotonic_decreasing is True
|
99 |
+
|
100 |
+
c = CategoricalIndex(data, categories=reversed(data), ordered=True)
|
101 |
+
assert c.is_monotonic_increasing is False
|
102 |
+
assert c.is_monotonic_decreasing is True
|
103 |
+
|
104 |
+
# test when data is neither monotonic increasing nor decreasing
|
105 |
+
reordered_data = [data[0], data[2], data[1]]
|
106 |
+
c = CategoricalIndex(reordered_data, categories=reversed(data))
|
107 |
+
assert c.is_monotonic_increasing is False
|
108 |
+
assert c.is_monotonic_decreasing is False
|
109 |
+
|
110 |
+
# non lexsorted categories
|
111 |
+
categories = non_lexsorted_data
|
112 |
+
|
113 |
+
c = CategoricalIndex(categories[:2], categories=categories)
|
114 |
+
assert c.is_monotonic_increasing is True
|
115 |
+
assert c.is_monotonic_decreasing is False
|
116 |
+
|
117 |
+
c = CategoricalIndex(categories[1:3], categories=categories)
|
118 |
+
assert c.is_monotonic_increasing is True
|
119 |
+
assert c.is_monotonic_decreasing is False
|
120 |
+
|
121 |
+
def test_has_duplicates(self):
|
122 |
+
idx = CategoricalIndex([0, 0, 0], name="foo")
|
123 |
+
assert idx.is_unique is False
|
124 |
+
assert idx.has_duplicates is True
|
125 |
+
|
126 |
+
idx = CategoricalIndex([0, 1], categories=[2, 3], name="foo")
|
127 |
+
assert idx.is_unique is False
|
128 |
+
assert idx.has_duplicates is True
|
129 |
+
|
130 |
+
idx = CategoricalIndex([0, 1, 2, 3], categories=[1, 2, 3], name="foo")
|
131 |
+
assert idx.is_unique is True
|
132 |
+
assert idx.has_duplicates is False
|
133 |
+
|
134 |
+
@pytest.mark.parametrize(
|
135 |
+
"data, categories, expected",
|
136 |
+
[
|
137 |
+
(
|
138 |
+
[1, 1, 1],
|
139 |
+
[1, 2, 3],
|
140 |
+
{
|
141 |
+
"first": np.array([False, True, True]),
|
142 |
+
"last": np.array([True, True, False]),
|
143 |
+
False: np.array([True, True, True]),
|
144 |
+
},
|
145 |
+
),
|
146 |
+
(
|
147 |
+
[1, 1, 1],
|
148 |
+
list("abc"),
|
149 |
+
{
|
150 |
+
"first": np.array([False, True, True]),
|
151 |
+
"last": np.array([True, True, False]),
|
152 |
+
False: np.array([True, True, True]),
|
153 |
+
},
|
154 |
+
),
|
155 |
+
(
|
156 |
+
[2, "a", "b"],
|
157 |
+
list("abc"),
|
158 |
+
{
|
159 |
+
"first": np.zeros(shape=(3), dtype=np.bool_),
|
160 |
+
"last": np.zeros(shape=(3), dtype=np.bool_),
|
161 |
+
False: np.zeros(shape=(3), dtype=np.bool_),
|
162 |
+
},
|
163 |
+
),
|
164 |
+
(
|
165 |
+
list("abb"),
|
166 |
+
list("abc"),
|
167 |
+
{
|
168 |
+
"first": np.array([False, False, True]),
|
169 |
+
"last": np.array([False, True, False]),
|
170 |
+
False: np.array([False, True, True]),
|
171 |
+
},
|
172 |
+
),
|
173 |
+
],
|
174 |
+
)
|
175 |
+
def test_drop_duplicates(self, data, categories, expected):
|
176 |
+
idx = CategoricalIndex(data, categories=categories, name="foo")
|
177 |
+
for keep, e in expected.items():
|
178 |
+
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), e)
|
179 |
+
e = idx[~e]
|
180 |
+
result = idx.drop_duplicates(keep=keep)
|
181 |
+
tm.assert_index_equal(result, e)
|
182 |
+
|
183 |
+
@pytest.mark.parametrize(
|
184 |
+
"data, categories, expected_data",
|
185 |
+
[
|
186 |
+
([1, 1, 1], [1, 2, 3], [1]),
|
187 |
+
([1, 1, 1], list("abc"), [np.nan]),
|
188 |
+
([1, 2, "a"], [1, 2, 3], [1, 2, np.nan]),
|
189 |
+
([2, "a", "b"], list("abc"), [np.nan, "a", "b"]),
|
190 |
+
],
|
191 |
+
)
|
192 |
+
def test_unique(self, data, categories, expected_data, ordered):
|
193 |
+
dtype = CategoricalDtype(categories, ordered=ordered)
|
194 |
+
|
195 |
+
idx = CategoricalIndex(data, dtype=dtype)
|
196 |
+
expected = CategoricalIndex(expected_data, dtype=dtype)
|
197 |
+
tm.assert_index_equal(idx.unique(), expected)
|
198 |
+
|
199 |
+
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="repr doesn't roundtrip")
|
200 |
+
def test_repr_roundtrip(self):
|
201 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
202 |
+
str(ci)
|
203 |
+
tm.assert_index_equal(eval(repr(ci)), ci, exact=True)
|
204 |
+
|
205 |
+
# formatting
|
206 |
+
str(ci)
|
207 |
+
|
208 |
+
# long format
|
209 |
+
# this is not reprable
|
210 |
+
ci = CategoricalIndex(np.random.default_rng(2).integers(0, 5, size=100))
|
211 |
+
str(ci)
|
212 |
+
|
213 |
+
def test_isin(self):
|
214 |
+
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
|
215 |
+
tm.assert_numpy_array_equal(
|
216 |
+
ci.isin(["c"]), np.array([False, False, False, True, False, False])
|
217 |
+
)
|
218 |
+
tm.assert_numpy_array_equal(
|
219 |
+
ci.isin(["c", "a", "b"]), np.array([True] * 5 + [False])
|
220 |
+
)
|
221 |
+
tm.assert_numpy_array_equal(
|
222 |
+
ci.isin(["c", "a", "b", np.nan]), np.array([True] * 6)
|
223 |
+
)
|
224 |
+
|
225 |
+
# mismatched categorical -> coerced to ndarray so doesn't matter
|
226 |
+
result = ci.isin(ci.set_categories(list("abcdefghi")))
|
227 |
+
expected = np.array([True] * 6)
|
228 |
+
tm.assert_numpy_array_equal(result, expected)
|
229 |
+
|
230 |
+
result = ci.isin(ci.set_categories(list("defghi")))
|
231 |
+
expected = np.array([False] * 5 + [True])
|
232 |
+
tm.assert_numpy_array_equal(result, expected)
|
233 |
+
|
234 |
+
def test_isin_overlapping_intervals(self):
|
235 |
+
# GH 34974
|
236 |
+
idx = pd.IntervalIndex([pd.Interval(0, 2), pd.Interval(0, 1)])
|
237 |
+
result = CategoricalIndex(idx).isin(idx)
|
238 |
+
expected = np.array([True, True])
|
239 |
+
tm.assert_numpy_array_equal(result, expected)
|
240 |
+
|
241 |
+
def test_identical(self):
|
242 |
+
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
243 |
+
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
|
244 |
+
assert ci1.identical(ci1)
|
245 |
+
assert ci1.identical(ci1.copy())
|
246 |
+
assert not ci1.identical(ci2)
|
247 |
+
|
248 |
+
def test_ensure_copied_data(self):
|
249 |
+
# gh-12309: Check the "copy" argument of each
|
250 |
+
# Index.__new__ is honored.
|
251 |
+
#
|
252 |
+
# Must be tested separately from other indexes because
|
253 |
+
# self.values is not an ndarray.
|
254 |
+
index = CategoricalIndex(list("ab") * 5)
|
255 |
+
|
256 |
+
result = CategoricalIndex(index.values, copy=True)
|
257 |
+
tm.assert_index_equal(index, result)
|
258 |
+
assert not np.shares_memory(result._data._codes, index._data._codes)
|
259 |
+
|
260 |
+
result = CategoricalIndex(index.values, copy=False)
|
261 |
+
assert result._data._codes is index._data._codes
|
262 |
+
|
263 |
+
|
264 |
+
class TestCategoricalIndex2:
|
265 |
+
def test_view_i8(self):
|
266 |
+
# GH#25464
|
267 |
+
ci = CategoricalIndex(list("ab") * 50)
|
268 |
+
msg = "When changing to a larger dtype, its size must be a divisor"
|
269 |
+
with pytest.raises(ValueError, match=msg):
|
270 |
+
ci.view("i8")
|
271 |
+
with pytest.raises(ValueError, match=msg):
|
272 |
+
ci._data.view("i8")
|
273 |
+
|
274 |
+
ci = ci[:-4] # length divisible by 8
|
275 |
+
|
276 |
+
res = ci.view("i8")
|
277 |
+
expected = ci._data.codes.view("i8")
|
278 |
+
tm.assert_numpy_array_equal(res, expected)
|
279 |
+
|
280 |
+
cat = ci._data
|
281 |
+
tm.assert_numpy_array_equal(cat.view("i8"), expected)
|
282 |
+
|
283 |
+
@pytest.mark.parametrize(
|
284 |
+
"dtype, engine_type",
|
285 |
+
[
|
286 |
+
(np.int8, libindex.Int8Engine),
|
287 |
+
(np.int16, libindex.Int16Engine),
|
288 |
+
(np.int32, libindex.Int32Engine),
|
289 |
+
(np.int64, libindex.Int64Engine),
|
290 |
+
],
|
291 |
+
)
|
292 |
+
def test_engine_type(self, dtype, engine_type):
|
293 |
+
if dtype != np.int64:
|
294 |
+
# num. of uniques required to push CategoricalIndex.codes to a
|
295 |
+
# dtype (128 categories required for .codes dtype to be int16 etc.)
|
296 |
+
num_uniques = {np.int8: 1, np.int16: 128, np.int32: 32768}[dtype]
|
297 |
+
ci = CategoricalIndex(range(num_uniques))
|
298 |
+
else:
|
299 |
+
# having 2**32 - 2**31 categories would be very memory-intensive,
|
300 |
+
# so we cheat a bit with the dtype
|
301 |
+
ci = CategoricalIndex(range(32768)) # == 2**16 - 2**(16 - 1)
|
302 |
+
arr = ci.values._ndarray.astype("int64")
|
303 |
+
NDArrayBacked.__init__(ci._data, arr, ci.dtype)
|
304 |
+
assert np.issubdtype(ci.codes.dtype, dtype)
|
305 |
+
assert isinstance(ci._engine, engine_type)
|
306 |
+
|
307 |
+
@pytest.mark.parametrize(
|
308 |
+
"func,op_name",
|
309 |
+
[
|
310 |
+
(lambda idx: idx - idx, "__sub__"),
|
311 |
+
(lambda idx: idx + idx, "__add__"),
|
312 |
+
(lambda idx: idx - ["a", "b"], "__sub__"),
|
313 |
+
(lambda idx: idx + ["a", "b"], "__add__"),
|
314 |
+
(lambda idx: ["a", "b"] - idx, "__rsub__"),
|
315 |
+
(lambda idx: ["a", "b"] + idx, "__radd__"),
|
316 |
+
],
|
317 |
+
)
|
318 |
+
def test_disallow_addsub_ops(self, func, op_name):
|
319 |
+
# GH 10039
|
320 |
+
# set ops (+/-) raise TypeError
|
321 |
+
idx = Index(Categorical(["a", "b"]))
|
322 |
+
cat_or_list = "'(Categorical|list)' and '(Categorical|list)'"
|
323 |
+
msg = "|".join(
|
324 |
+
[
|
325 |
+
f"cannot perform {op_name} with this index type: CategoricalIndex",
|
326 |
+
"can only concatenate list",
|
327 |
+
rf"unsupported operand type\(s\) for [\+-]: {cat_or_list}",
|
328 |
+
]
|
329 |
+
)
|
330 |
+
with pytest.raises(TypeError, match=msg):
|
331 |
+
func(idx)
|
332 |
+
|
333 |
+
def test_method_delegation(self):
|
334 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
335 |
+
result = ci.set_categories(list("cab"))
|
336 |
+
tm.assert_index_equal(
|
337 |
+
result, CategoricalIndex(list("aabbca"), categories=list("cab"))
|
338 |
+
)
|
339 |
+
|
340 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
341 |
+
result = ci.rename_categories(list("efg"))
|
342 |
+
tm.assert_index_equal(
|
343 |
+
result, CategoricalIndex(list("ffggef"), categories=list("efg"))
|
344 |
+
)
|
345 |
+
|
346 |
+
# GH18862 (let rename_categories take callables)
|
347 |
+
result = ci.rename_categories(lambda x: x.upper())
|
348 |
+
tm.assert_index_equal(
|
349 |
+
result, CategoricalIndex(list("AABBCA"), categories=list("CAB"))
|
350 |
+
)
|
351 |
+
|
352 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
353 |
+
result = ci.add_categories(["d"])
|
354 |
+
tm.assert_index_equal(
|
355 |
+
result, CategoricalIndex(list("aabbca"), categories=list("cabd"))
|
356 |
+
)
|
357 |
+
|
358 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
359 |
+
result = ci.remove_categories(["c"])
|
360 |
+
tm.assert_index_equal(
|
361 |
+
result,
|
362 |
+
CategoricalIndex(list("aabb") + [np.nan] + ["a"], categories=list("ab")),
|
363 |
+
)
|
364 |
+
|
365 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
366 |
+
result = ci.as_unordered()
|
367 |
+
tm.assert_index_equal(result, ci)
|
368 |
+
|
369 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
370 |
+
result = ci.as_ordered()
|
371 |
+
tm.assert_index_equal(
|
372 |
+
result,
|
373 |
+
CategoricalIndex(list("aabbca"), categories=list("cabdef"), ordered=True),
|
374 |
+
)
|
375 |
+
|
376 |
+
# invalid
|
377 |
+
msg = "cannot use inplace with CategoricalIndex"
|
378 |
+
with pytest.raises(ValueError, match=msg):
|
379 |
+
ci.set_categories(list("cab"), inplace=True)
|
380 |
+
|
381 |
+
def test_remove_maintains_order(self):
|
382 |
+
ci = CategoricalIndex(list("abcdda"), categories=list("abcd"))
|
383 |
+
result = ci.reorder_categories(["d", "c", "b", "a"], ordered=True)
|
384 |
+
tm.assert_index_equal(
|
385 |
+
result,
|
386 |
+
CategoricalIndex(list("abcdda"), categories=list("dcba"), ordered=True),
|
387 |
+
)
|
388 |
+
result = result.remove_categories(["c"])
|
389 |
+
tm.assert_index_equal(
|
390 |
+
result,
|
391 |
+
CategoricalIndex(
|
392 |
+
["a", "b", np.nan, "d", "d", "a"], categories=list("dba"), ordered=True
|
393 |
+
),
|
394 |
+
)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_constructors.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
Categorical,
|
6 |
+
CategoricalDtype,
|
7 |
+
CategoricalIndex,
|
8 |
+
Index,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
class TestCategoricalIndexConstructors:
|
14 |
+
def test_construction_disallows_scalar(self):
|
15 |
+
msg = "must be called with a collection of some kind"
|
16 |
+
with pytest.raises(TypeError, match=msg):
|
17 |
+
CategoricalIndex(data=1, categories=list("abcd"), ordered=False)
|
18 |
+
with pytest.raises(TypeError, match=msg):
|
19 |
+
CategoricalIndex(categories=list("abcd"), ordered=False)
|
20 |
+
|
21 |
+
def test_construction(self):
|
22 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("abcd"), ordered=False)
|
23 |
+
categories = ci.categories
|
24 |
+
|
25 |
+
result = Index(ci)
|
26 |
+
tm.assert_index_equal(result, ci, exact=True)
|
27 |
+
assert not result.ordered
|
28 |
+
|
29 |
+
result = Index(ci.values)
|
30 |
+
tm.assert_index_equal(result, ci, exact=True)
|
31 |
+
assert not result.ordered
|
32 |
+
|
33 |
+
# empty
|
34 |
+
result = CategoricalIndex([], categories=categories)
|
35 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
36 |
+
tm.assert_numpy_array_equal(result.codes, np.array([], dtype="int8"))
|
37 |
+
assert not result.ordered
|
38 |
+
|
39 |
+
# passing categories
|
40 |
+
result = CategoricalIndex(list("aabbca"), categories=categories)
|
41 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
42 |
+
tm.assert_numpy_array_equal(
|
43 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
44 |
+
)
|
45 |
+
|
46 |
+
c = Categorical(list("aabbca"))
|
47 |
+
result = CategoricalIndex(c)
|
48 |
+
tm.assert_index_equal(result.categories, Index(list("abc")))
|
49 |
+
tm.assert_numpy_array_equal(
|
50 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
51 |
+
)
|
52 |
+
assert not result.ordered
|
53 |
+
|
54 |
+
result = CategoricalIndex(c, categories=categories)
|
55 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
56 |
+
tm.assert_numpy_array_equal(
|
57 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
58 |
+
)
|
59 |
+
assert not result.ordered
|
60 |
+
|
61 |
+
ci = CategoricalIndex(c, categories=list("abcd"))
|
62 |
+
result = CategoricalIndex(ci)
|
63 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
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(ci, categories=list("ab"))
|
70 |
+
tm.assert_index_equal(result.categories, Index(list("ab")))
|
71 |
+
tm.assert_numpy_array_equal(
|
72 |
+
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
|
73 |
+
)
|
74 |
+
assert not result.ordered
|
75 |
+
|
76 |
+
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
|
77 |
+
tm.assert_index_equal(result.categories, Index(list("ab")))
|
78 |
+
tm.assert_numpy_array_equal(
|
79 |
+
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
|
80 |
+
)
|
81 |
+
assert result.ordered
|
82 |
+
|
83 |
+
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
|
84 |
+
expected = CategoricalIndex(
|
85 |
+
ci, categories=list("ab"), ordered=True, dtype="category"
|
86 |
+
)
|
87 |
+
tm.assert_index_equal(result, expected, exact=True)
|
88 |
+
|
89 |
+
# turn me to an Index
|
90 |
+
result = Index(np.array(ci))
|
91 |
+
assert isinstance(result, Index)
|
92 |
+
assert not isinstance(result, CategoricalIndex)
|
93 |
+
|
94 |
+
def test_construction_with_dtype(self):
|
95 |
+
# specify dtype
|
96 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("abc"), ordered=False)
|
97 |
+
|
98 |
+
result = Index(np.array(ci), dtype="category")
|
99 |
+
tm.assert_index_equal(result, ci, exact=True)
|
100 |
+
|
101 |
+
result = Index(np.array(ci).tolist(), dtype="category")
|
102 |
+
tm.assert_index_equal(result, ci, exact=True)
|
103 |
+
|
104 |
+
# these are generally only equal when the categories are reordered
|
105 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
106 |
+
|
107 |
+
result = Index(np.array(ci), dtype="category").reorder_categories(ci.categories)
|
108 |
+
tm.assert_index_equal(result, ci, exact=True)
|
109 |
+
|
110 |
+
# make sure indexes are handled
|
111 |
+
idx = Index(range(3))
|
112 |
+
expected = CategoricalIndex([0, 1, 2], categories=idx, ordered=True)
|
113 |
+
result = CategoricalIndex(idx, categories=idx, ordered=True)
|
114 |
+
tm.assert_index_equal(result, expected, exact=True)
|
115 |
+
|
116 |
+
def test_construction_empty_with_bool_categories(self):
|
117 |
+
# see GH#22702
|
118 |
+
cat = CategoricalIndex([], categories=[True, False])
|
119 |
+
categories = sorted(cat.categories.tolist())
|
120 |
+
assert categories == [False, True]
|
121 |
+
|
122 |
+
def test_construction_with_categorical_dtype(self):
|
123 |
+
# construction with CategoricalDtype
|
124 |
+
# GH#18109
|
125 |
+
data, cats, ordered = "a a b b".split(), "c b a".split(), True
|
126 |
+
dtype = CategoricalDtype(categories=cats, ordered=ordered)
|
127 |
+
|
128 |
+
result = CategoricalIndex(data, dtype=dtype)
|
129 |
+
expected = CategoricalIndex(data, categories=cats, ordered=ordered)
|
130 |
+
tm.assert_index_equal(result, expected, exact=True)
|
131 |
+
|
132 |
+
# GH#19032
|
133 |
+
result = Index(data, dtype=dtype)
|
134 |
+
tm.assert_index_equal(result, expected, exact=True)
|
135 |
+
|
136 |
+
# error when combining categories/ordered and dtype kwargs
|
137 |
+
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
|
138 |
+
with pytest.raises(ValueError, match=msg):
|
139 |
+
CategoricalIndex(data, categories=cats, dtype=dtype)
|
140 |
+
|
141 |
+
with pytest.raises(ValueError, match=msg):
|
142 |
+
CategoricalIndex(data, ordered=ordered, dtype=dtype)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_equals.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
Categorical,
|
6 |
+
CategoricalIndex,
|
7 |
+
Index,
|
8 |
+
MultiIndex,
|
9 |
+
)
|
10 |
+
|
11 |
+
|
12 |
+
class TestEquals:
|
13 |
+
def test_equals_categorical(self):
|
14 |
+
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
15 |
+
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
|
16 |
+
|
17 |
+
assert ci1.equals(ci1)
|
18 |
+
assert not ci1.equals(ci2)
|
19 |
+
assert ci1.equals(ci1.astype(object))
|
20 |
+
assert ci1.astype(object).equals(ci1)
|
21 |
+
|
22 |
+
assert (ci1 == ci1).all()
|
23 |
+
assert not (ci1 != ci1).all()
|
24 |
+
assert not (ci1 > ci1).all()
|
25 |
+
assert not (ci1 < ci1).all()
|
26 |
+
assert (ci1 <= ci1).all()
|
27 |
+
assert (ci1 >= ci1).all()
|
28 |
+
|
29 |
+
assert not (ci1 == 1).all()
|
30 |
+
assert (ci1 == Index(["a", "b"])).all()
|
31 |
+
assert (ci1 == ci1.values).all()
|
32 |
+
|
33 |
+
# invalid comparisons
|
34 |
+
with pytest.raises(ValueError, match="Lengths must match"):
|
35 |
+
ci1 == Index(["a", "b", "c"])
|
36 |
+
|
37 |
+
msg = "Categoricals can only be compared if 'categories' are the same"
|
38 |
+
with pytest.raises(TypeError, match=msg):
|
39 |
+
ci1 == ci2
|
40 |
+
with pytest.raises(TypeError, match=msg):
|
41 |
+
ci1 == Categorical(ci1.values, ordered=False)
|
42 |
+
with pytest.raises(TypeError, match=msg):
|
43 |
+
ci1 == Categorical(ci1.values, categories=list("abc"))
|
44 |
+
|
45 |
+
# tests
|
46 |
+
# make sure that we are testing for category inclusion properly
|
47 |
+
ci = CategoricalIndex(list("aabca"), categories=["c", "a", "b"])
|
48 |
+
assert not ci.equals(list("aabca"))
|
49 |
+
# Same categories, but different order
|
50 |
+
# Unordered
|
51 |
+
assert ci.equals(CategoricalIndex(list("aabca")))
|
52 |
+
# Ordered
|
53 |
+
assert not ci.equals(CategoricalIndex(list("aabca"), ordered=True))
|
54 |
+
assert ci.equals(ci.copy())
|
55 |
+
|
56 |
+
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
|
57 |
+
assert not ci.equals(list("aabca"))
|
58 |
+
assert not ci.equals(CategoricalIndex(list("aabca")))
|
59 |
+
assert ci.equals(ci.copy())
|
60 |
+
|
61 |
+
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
|
62 |
+
assert not ci.equals(list("aabca") + [np.nan])
|
63 |
+
assert ci.equals(CategoricalIndex(list("aabca") + [np.nan]))
|
64 |
+
assert not ci.equals(CategoricalIndex(list("aabca") + [np.nan], ordered=True))
|
65 |
+
assert ci.equals(ci.copy())
|
66 |
+
|
67 |
+
def test_equals_categorical_unordered(self):
|
68 |
+
# https://github.com/pandas-dev/pandas/issues/16603
|
69 |
+
a = CategoricalIndex(["A"], categories=["A", "B"])
|
70 |
+
b = CategoricalIndex(["A"], categories=["B", "A"])
|
71 |
+
c = CategoricalIndex(["C"], categories=["B", "A"])
|
72 |
+
assert a.equals(b)
|
73 |
+
assert not a.equals(c)
|
74 |
+
assert not b.equals(c)
|
75 |
+
|
76 |
+
def test_equals_non_category(self):
|
77 |
+
# GH#37667 Case where other contains a value not among ci's
|
78 |
+
# categories ("D") and also contains np.nan
|
79 |
+
ci = CategoricalIndex(["A", "B", np.nan, np.nan])
|
80 |
+
other = Index(["A", "B", "D", np.nan])
|
81 |
+
|
82 |
+
assert not ci.equals(other)
|
83 |
+
|
84 |
+
def test_equals_multiindex(self):
|
85 |
+
# dont raise NotImplementedError when calling is_dtype_compat
|
86 |
+
|
87 |
+
mi = MultiIndex.from_arrays([["A", "B", "C", "D"], range(4)])
|
88 |
+
ci = mi.to_flat_index().astype("category")
|
89 |
+
|
90 |
+
assert not ci.equals(mi)
|
91 |
+
|
92 |
+
def test_equals_string_dtype(self, any_string_dtype):
|
93 |
+
# GH#55364
|
94 |
+
idx = CategoricalIndex(list("abc"), name="B")
|
95 |
+
other = Index(["a", "b", "c"], name="B", dtype=any_string_dtype)
|
96 |
+
assert idx.equals(other)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_fillna.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import CategoricalIndex
|
5 |
+
import pandas._testing as tm
|
6 |
+
|
7 |
+
|
8 |
+
class TestFillNA:
|
9 |
+
def test_fillna_categorical(self):
|
10 |
+
# GH#11343
|
11 |
+
idx = CategoricalIndex([1.0, np.nan, 3.0, 1.0], name="x")
|
12 |
+
# fill by value in categories
|
13 |
+
exp = CategoricalIndex([1.0, 1.0, 3.0, 1.0], name="x")
|
14 |
+
tm.assert_index_equal(idx.fillna(1.0), exp)
|
15 |
+
|
16 |
+
cat = idx._data
|
17 |
+
|
18 |
+
# fill by value not in categories raises TypeError on EA, casts on CI
|
19 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
20 |
+
with pytest.raises(TypeError, match=msg):
|
21 |
+
cat.fillna(2.0)
|
22 |
+
|
23 |
+
result = idx.fillna(2.0)
|
24 |
+
expected = idx.astype(object).fillna(2.0)
|
25 |
+
tm.assert_index_equal(result, expected)
|
26 |
+
|
27 |
+
def test_fillna_copies_with_no_nas(self):
|
28 |
+
# Nothing to fill, should still get a copy for the Categorical method,
|
29 |
+
# but OK to get a view on CategoricalIndex method
|
30 |
+
ci = CategoricalIndex([0, 1, 1])
|
31 |
+
result = ci.fillna(0)
|
32 |
+
assert result is not ci
|
33 |
+
assert tm.shares_memory(result, ci)
|
34 |
+
|
35 |
+
# But at the EA level we always get a copy.
|
36 |
+
cat = ci._data
|
37 |
+
result = cat.fillna(0)
|
38 |
+
assert result._ndarray is not cat._ndarray
|
39 |
+
assert result._ndarray.base is None
|
40 |
+
assert not tm.shares_memory(result, cat)
|
41 |
+
|
42 |
+
def test_fillna_validates_with_no_nas(self):
|
43 |
+
# We validate the fill value even if fillna is a no-op
|
44 |
+
ci = CategoricalIndex([2, 3, 3])
|
45 |
+
cat = ci._data
|
46 |
+
|
47 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
48 |
+
res = ci.fillna(False)
|
49 |
+
# nothing to fill, so we dont cast
|
50 |
+
tm.assert_index_equal(res, ci)
|
51 |
+
|
52 |
+
# Same check directly on the Categorical
|
53 |
+
with pytest.raises(TypeError, match=msg):
|
54 |
+
cat.fillna(False)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_formats.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Tests for CategoricalIndex.__repr__ and related methods.
|
3 |
+
"""
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
from pandas._config import using_pyarrow_string_dtype
|
7 |
+
import pandas._config.config as cf
|
8 |
+
|
9 |
+
from pandas import CategoricalIndex
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
class TestCategoricalIndexRepr:
|
14 |
+
def test_format_different_scalar_lengths(self):
|
15 |
+
# GH#35439
|
16 |
+
idx = CategoricalIndex(["aaaaaaaaa", "b"])
|
17 |
+
expected = ["aaaaaaaaa", "b"]
|
18 |
+
msg = r"CategoricalIndex\.format is deprecated"
|
19 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
20 |
+
assert idx.format() == expected
|
21 |
+
|
22 |
+
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="repr different")
|
23 |
+
def test_string_categorical_index_repr(self):
|
24 |
+
# short
|
25 |
+
idx = CategoricalIndex(["a", "bb", "ccc"])
|
26 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc'], categories=['a', 'bb', 'ccc'], ordered=False, dtype='category')""" # noqa: E501
|
27 |
+
assert repr(idx) == expected
|
28 |
+
|
29 |
+
# multiple lines
|
30 |
+
idx = CategoricalIndex(["a", "bb", "ccc"] * 10)
|
31 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',
|
32 |
+
'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb',
|
33 |
+
'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],
|
34 |
+
categories=['a', 'bb', 'ccc'], ordered=False, dtype='category')""" # noqa: E501
|
35 |
+
|
36 |
+
assert repr(idx) == expected
|
37 |
+
|
38 |
+
# truncated
|
39 |
+
idx = CategoricalIndex(["a", "bb", "ccc"] * 100)
|
40 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',
|
41 |
+
...
|
42 |
+
'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],
|
43 |
+
categories=['a', 'bb', 'ccc'], ordered=False, dtype='category', length=300)""" # noqa: E501
|
44 |
+
|
45 |
+
assert repr(idx) == expected
|
46 |
+
|
47 |
+
# larger categories
|
48 |
+
idx = CategoricalIndex(list("abcdefghijklmmo"))
|
49 |
+
expected = """CategoricalIndex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
|
50 |
+
'm', 'm', 'o'],
|
51 |
+
categories=['a', 'b', 'c', 'd', ..., 'k', 'l', 'm', 'o'], ordered=False, dtype='category')""" # noqa: E501
|
52 |
+
|
53 |
+
assert repr(idx) == expected
|
54 |
+
|
55 |
+
# short
|
56 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"])
|
57 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう'], categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa: E501
|
58 |
+
assert repr(idx) == expected
|
59 |
+
|
60 |
+
# multiple lines
|
61 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 10)
|
62 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ',
|
63 |
+
'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
64 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
65 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa: E501
|
66 |
+
|
67 |
+
assert repr(idx) == expected
|
68 |
+
|
69 |
+
# truncated
|
70 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 100)
|
71 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ',
|
72 |
+
...
|
73 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
74 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category', length=300)""" # noqa: E501
|
75 |
+
|
76 |
+
assert repr(idx) == expected
|
77 |
+
|
78 |
+
# larger categories
|
79 |
+
idx = CategoricalIndex(list("あいうえおかきくけこさしすせそ"))
|
80 |
+
expected = """CategoricalIndex(['あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ', 'さ', 'し',
|
81 |
+
'す', 'せ', 'そ'],
|
82 |
+
categories=['あ', 'い', 'う', 'え', ..., 'し', 'す', 'せ', 'そ'], ordered=False, dtype='category')""" # noqa: E501
|
83 |
+
|
84 |
+
assert repr(idx) == expected
|
85 |
+
|
86 |
+
# Enable Unicode option -----------------------------------------
|
87 |
+
with cf.option_context("display.unicode.east_asian_width", True):
|
88 |
+
# short
|
89 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"])
|
90 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう'], categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa: E501
|
91 |
+
assert repr(idx) == expected
|
92 |
+
|
93 |
+
# multiple lines
|
94 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 10)
|
95 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
96 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',
|
97 |
+
'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
98 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
99 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa: E501
|
100 |
+
|
101 |
+
assert repr(idx) == expected
|
102 |
+
|
103 |
+
# truncated
|
104 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 100)
|
105 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
106 |
+
'ううう', 'あ',
|
107 |
+
...
|
108 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',
|
109 |
+
'あ', 'いい', 'ううう'],
|
110 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category', length=300)""" # noqa: E501
|
111 |
+
|
112 |
+
assert repr(idx) == expected
|
113 |
+
|
114 |
+
# larger categories
|
115 |
+
idx = CategoricalIndex(list("あいうえおかきくけこさしすせそ"))
|
116 |
+
expected = """CategoricalIndex(['あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ',
|
117 |
+
'さ', 'し', 'す', 'せ', 'そ'],
|
118 |
+
categories=['あ', 'い', 'う', 'え', ..., 'し', 'す', 'せ', 'そ'], ordered=False, dtype='category')""" # noqa: E501
|
119 |
+
|
120 |
+
assert repr(idx) == expected
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_indexing.py
ADDED
@@ -0,0 +1,420 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas.errors import InvalidIndexError
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
from pandas import (
|
8 |
+
CategoricalIndex,
|
9 |
+
Index,
|
10 |
+
IntervalIndex,
|
11 |
+
Timestamp,
|
12 |
+
)
|
13 |
+
import pandas._testing as tm
|
14 |
+
|
15 |
+
|
16 |
+
class TestTake:
|
17 |
+
def test_take_fill_value(self):
|
18 |
+
# GH 12631
|
19 |
+
|
20 |
+
# numeric category
|
21 |
+
idx = CategoricalIndex([1, 2, 3], name="xxx")
|
22 |
+
result = idx.take(np.array([1, 0, -1]))
|
23 |
+
expected = CategoricalIndex([2, 1, 3], name="xxx")
|
24 |
+
tm.assert_index_equal(result, expected)
|
25 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
26 |
+
|
27 |
+
# fill_value
|
28 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
29 |
+
expected = CategoricalIndex([2, 1, np.nan], categories=[1, 2, 3], name="xxx")
|
30 |
+
tm.assert_index_equal(result, expected)
|
31 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
32 |
+
|
33 |
+
# allow_fill=False
|
34 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
35 |
+
expected = CategoricalIndex([2, 1, 3], name="xxx")
|
36 |
+
tm.assert_index_equal(result, expected)
|
37 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
38 |
+
|
39 |
+
# object category
|
40 |
+
idx = CategoricalIndex(
|
41 |
+
list("CBA"), categories=list("ABC"), ordered=True, name="xxx"
|
42 |
+
)
|
43 |
+
result = idx.take(np.array([1, 0, -1]))
|
44 |
+
expected = CategoricalIndex(
|
45 |
+
list("BCA"), categories=list("ABC"), ordered=True, name="xxx"
|
46 |
+
)
|
47 |
+
tm.assert_index_equal(result, expected)
|
48 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
49 |
+
|
50 |
+
# fill_value
|
51 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
52 |
+
expected = CategoricalIndex(
|
53 |
+
["B", "C", np.nan], categories=list("ABC"), ordered=True, name="xxx"
|
54 |
+
)
|
55 |
+
tm.assert_index_equal(result, expected)
|
56 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
57 |
+
|
58 |
+
# allow_fill=False
|
59 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
60 |
+
expected = CategoricalIndex(
|
61 |
+
list("BCA"), categories=list("ABC"), ordered=True, name="xxx"
|
62 |
+
)
|
63 |
+
tm.assert_index_equal(result, expected)
|
64 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
65 |
+
|
66 |
+
msg = (
|
67 |
+
"When allow_fill=True and fill_value is not None, "
|
68 |
+
"all indices must be >= -1"
|
69 |
+
)
|
70 |
+
with pytest.raises(ValueError, match=msg):
|
71 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
72 |
+
with pytest.raises(ValueError, match=msg):
|
73 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
74 |
+
|
75 |
+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
|
76 |
+
with pytest.raises(IndexError, match=msg):
|
77 |
+
idx.take(np.array([1, -5]))
|
78 |
+
|
79 |
+
def test_take_fill_value_datetime(self):
|
80 |
+
# datetime category
|
81 |
+
idx = pd.DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], name="xxx")
|
82 |
+
idx = CategoricalIndex(idx)
|
83 |
+
result = idx.take(np.array([1, 0, -1]))
|
84 |
+
expected = pd.DatetimeIndex(
|
85 |
+
["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx"
|
86 |
+
)
|
87 |
+
expected = CategoricalIndex(expected)
|
88 |
+
tm.assert_index_equal(result, expected)
|
89 |
+
|
90 |
+
# fill_value
|
91 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
92 |
+
expected = pd.DatetimeIndex(["2011-02-01", "2011-01-01", "NaT"], name="xxx")
|
93 |
+
exp_cats = pd.DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"])
|
94 |
+
expected = CategoricalIndex(expected, categories=exp_cats)
|
95 |
+
tm.assert_index_equal(result, expected)
|
96 |
+
|
97 |
+
# allow_fill=False
|
98 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
99 |
+
expected = pd.DatetimeIndex(
|
100 |
+
["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx"
|
101 |
+
)
|
102 |
+
expected = CategoricalIndex(expected)
|
103 |
+
tm.assert_index_equal(result, expected)
|
104 |
+
|
105 |
+
msg = (
|
106 |
+
"When allow_fill=True and fill_value is not None, "
|
107 |
+
"all indices must be >= -1"
|
108 |
+
)
|
109 |
+
with pytest.raises(ValueError, match=msg):
|
110 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
111 |
+
with pytest.raises(ValueError, match=msg):
|
112 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
113 |
+
|
114 |
+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
|
115 |
+
with pytest.raises(IndexError, match=msg):
|
116 |
+
idx.take(np.array([1, -5]))
|
117 |
+
|
118 |
+
def test_take_invalid_kwargs(self):
|
119 |
+
idx = CategoricalIndex([1, 2, 3], name="foo")
|
120 |
+
indices = [1, 0, -1]
|
121 |
+
|
122 |
+
msg = r"take\(\) got an unexpected keyword argument 'foo'"
|
123 |
+
with pytest.raises(TypeError, match=msg):
|
124 |
+
idx.take(indices, foo=2)
|
125 |
+
|
126 |
+
msg = "the 'out' parameter is not supported"
|
127 |
+
with pytest.raises(ValueError, match=msg):
|
128 |
+
idx.take(indices, out=indices)
|
129 |
+
|
130 |
+
msg = "the 'mode' parameter is not supported"
|
131 |
+
with pytest.raises(ValueError, match=msg):
|
132 |
+
idx.take(indices, mode="clip")
|
133 |
+
|
134 |
+
|
135 |
+
class TestGetLoc:
|
136 |
+
def test_get_loc(self):
|
137 |
+
# GH 12531
|
138 |
+
cidx1 = CategoricalIndex(list("abcde"), categories=list("edabc"))
|
139 |
+
idx1 = Index(list("abcde"))
|
140 |
+
assert cidx1.get_loc("a") == idx1.get_loc("a")
|
141 |
+
assert cidx1.get_loc("e") == idx1.get_loc("e")
|
142 |
+
|
143 |
+
for i in [cidx1, idx1]:
|
144 |
+
with pytest.raises(KeyError, match="'NOT-EXIST'"):
|
145 |
+
i.get_loc("NOT-EXIST")
|
146 |
+
|
147 |
+
# non-unique
|
148 |
+
cidx2 = CategoricalIndex(list("aacded"), categories=list("edabc"))
|
149 |
+
idx2 = Index(list("aacded"))
|
150 |
+
|
151 |
+
# results in bool array
|
152 |
+
res = cidx2.get_loc("d")
|
153 |
+
tm.assert_numpy_array_equal(res, idx2.get_loc("d"))
|
154 |
+
tm.assert_numpy_array_equal(
|
155 |
+
res, np.array([False, False, False, True, False, True])
|
156 |
+
)
|
157 |
+
# unique element results in scalar
|
158 |
+
res = cidx2.get_loc("e")
|
159 |
+
assert res == idx2.get_loc("e")
|
160 |
+
assert res == 4
|
161 |
+
|
162 |
+
for i in [cidx2, idx2]:
|
163 |
+
with pytest.raises(KeyError, match="'NOT-EXIST'"):
|
164 |
+
i.get_loc("NOT-EXIST")
|
165 |
+
|
166 |
+
# non-unique, sliceable
|
167 |
+
cidx3 = CategoricalIndex(list("aabbb"), categories=list("abc"))
|
168 |
+
idx3 = Index(list("aabbb"))
|
169 |
+
|
170 |
+
# results in slice
|
171 |
+
res = cidx3.get_loc("a")
|
172 |
+
assert res == idx3.get_loc("a")
|
173 |
+
assert res == slice(0, 2, None)
|
174 |
+
|
175 |
+
res = cidx3.get_loc("b")
|
176 |
+
assert res == idx3.get_loc("b")
|
177 |
+
assert res == slice(2, 5, None)
|
178 |
+
|
179 |
+
for i in [cidx3, idx3]:
|
180 |
+
with pytest.raises(KeyError, match="'c'"):
|
181 |
+
i.get_loc("c")
|
182 |
+
|
183 |
+
def test_get_loc_unique(self):
|
184 |
+
cidx = CategoricalIndex(list("abc"))
|
185 |
+
result = cidx.get_loc("b")
|
186 |
+
assert result == 1
|
187 |
+
|
188 |
+
def test_get_loc_monotonic_nonunique(self):
|
189 |
+
cidx = CategoricalIndex(list("abbc"))
|
190 |
+
result = cidx.get_loc("b")
|
191 |
+
expected = slice(1, 3, None)
|
192 |
+
assert result == expected
|
193 |
+
|
194 |
+
def test_get_loc_nonmonotonic_nonunique(self):
|
195 |
+
cidx = CategoricalIndex(list("abcb"))
|
196 |
+
result = cidx.get_loc("b")
|
197 |
+
expected = np.array([False, True, False, True], dtype=bool)
|
198 |
+
tm.assert_numpy_array_equal(result, expected)
|
199 |
+
|
200 |
+
def test_get_loc_nan(self):
|
201 |
+
# GH#41933
|
202 |
+
ci = CategoricalIndex(["A", "B", np.nan])
|
203 |
+
res = ci.get_loc(np.nan)
|
204 |
+
|
205 |
+
assert res == 2
|
206 |
+
|
207 |
+
|
208 |
+
class TestGetIndexer:
|
209 |
+
def test_get_indexer_base(self):
|
210 |
+
# Determined by cat ordering.
|
211 |
+
idx = CategoricalIndex(list("cab"), categories=list("cab"))
|
212 |
+
expected = np.arange(len(idx), dtype=np.intp)
|
213 |
+
|
214 |
+
actual = idx.get_indexer(idx)
|
215 |
+
tm.assert_numpy_array_equal(expected, actual)
|
216 |
+
|
217 |
+
with pytest.raises(ValueError, match="Invalid fill method"):
|
218 |
+
idx.get_indexer(idx, method="invalid")
|
219 |
+
|
220 |
+
def test_get_indexer_requires_unique(self):
|
221 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
222 |
+
oidx = Index(np.array(ci))
|
223 |
+
|
224 |
+
msg = "Reindexing only valid with uniquely valued Index objects"
|
225 |
+
|
226 |
+
for n in [1, 2, 5, len(ci)]:
|
227 |
+
finder = oidx[np.random.default_rng(2).integers(0, len(ci), size=n)]
|
228 |
+
|
229 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
230 |
+
ci.get_indexer(finder)
|
231 |
+
|
232 |
+
# see gh-17323
|
233 |
+
#
|
234 |
+
# Even when indexer is equal to the
|
235 |
+
# members in the index, we should
|
236 |
+
# respect duplicates instead of taking
|
237 |
+
# the fast-track path.
|
238 |
+
for finder in [list("aabbca"), list("aababca")]:
|
239 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
240 |
+
ci.get_indexer(finder)
|
241 |
+
|
242 |
+
def test_get_indexer_non_unique(self):
|
243 |
+
idx1 = CategoricalIndex(list("aabcde"), categories=list("edabc"))
|
244 |
+
idx2 = CategoricalIndex(list("abf"))
|
245 |
+
|
246 |
+
for indexer in [idx2, list("abf"), Index(list("abf"))]:
|
247 |
+
msg = "Reindexing only valid with uniquely valued Index objects"
|
248 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
249 |
+
idx1.get_indexer(indexer)
|
250 |
+
|
251 |
+
r1, _ = idx1.get_indexer_non_unique(indexer)
|
252 |
+
expected = np.array([0, 1, 2, -1], dtype=np.intp)
|
253 |
+
tm.assert_almost_equal(r1, expected)
|
254 |
+
|
255 |
+
def test_get_indexer_method(self):
|
256 |
+
idx1 = CategoricalIndex(list("aabcde"), categories=list("edabc"))
|
257 |
+
idx2 = CategoricalIndex(list("abf"))
|
258 |
+
|
259 |
+
msg = "method pad not yet implemented for CategoricalIndex"
|
260 |
+
with pytest.raises(NotImplementedError, match=msg):
|
261 |
+
idx2.get_indexer(idx1, method="pad")
|
262 |
+
msg = "method backfill not yet implemented for CategoricalIndex"
|
263 |
+
with pytest.raises(NotImplementedError, match=msg):
|
264 |
+
idx2.get_indexer(idx1, method="backfill")
|
265 |
+
|
266 |
+
msg = "method nearest not yet implemented for CategoricalIndex"
|
267 |
+
with pytest.raises(NotImplementedError, match=msg):
|
268 |
+
idx2.get_indexer(idx1, method="nearest")
|
269 |
+
|
270 |
+
def test_get_indexer_array(self):
|
271 |
+
arr = np.array(
|
272 |
+
[Timestamp("1999-12-31 00:00:00"), Timestamp("2000-12-31 00:00:00")],
|
273 |
+
dtype=object,
|
274 |
+
)
|
275 |
+
cats = [Timestamp("1999-12-31 00:00:00"), Timestamp("2000-12-31 00:00:00")]
|
276 |
+
ci = CategoricalIndex(cats, categories=cats, ordered=False, dtype="category")
|
277 |
+
result = ci.get_indexer(arr)
|
278 |
+
expected = np.array([0, 1], dtype="intp")
|
279 |
+
tm.assert_numpy_array_equal(result, expected)
|
280 |
+
|
281 |
+
def test_get_indexer_same_categories_same_order(self):
|
282 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"])
|
283 |
+
|
284 |
+
result = ci.get_indexer(CategoricalIndex(["b", "b"], categories=["a", "b"]))
|
285 |
+
expected = np.array([1, 1], dtype="intp")
|
286 |
+
tm.assert_numpy_array_equal(result, expected)
|
287 |
+
|
288 |
+
def test_get_indexer_same_categories_different_order(self):
|
289 |
+
# https://github.com/pandas-dev/pandas/issues/19551
|
290 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"])
|
291 |
+
|
292 |
+
result = ci.get_indexer(CategoricalIndex(["b", "b"], categories=["b", "a"]))
|
293 |
+
expected = np.array([1, 1], dtype="intp")
|
294 |
+
tm.assert_numpy_array_equal(result, expected)
|
295 |
+
|
296 |
+
def test_get_indexer_nans_in_index_and_target(self):
|
297 |
+
# GH 45361
|
298 |
+
ci = CategoricalIndex([1, 2, np.nan, 3])
|
299 |
+
other1 = [2, 3, 4, np.nan]
|
300 |
+
res1 = ci.get_indexer(other1)
|
301 |
+
expected1 = np.array([1, 3, -1, 2], dtype=np.intp)
|
302 |
+
tm.assert_numpy_array_equal(res1, expected1)
|
303 |
+
other2 = [1, 4, 2, 3]
|
304 |
+
res2 = ci.get_indexer(other2)
|
305 |
+
expected2 = np.array([0, -1, 1, 3], dtype=np.intp)
|
306 |
+
tm.assert_numpy_array_equal(res2, expected2)
|
307 |
+
|
308 |
+
|
309 |
+
class TestWhere:
|
310 |
+
def test_where(self, listlike_box):
|
311 |
+
klass = listlike_box
|
312 |
+
|
313 |
+
i = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
314 |
+
cond = [True] * len(i)
|
315 |
+
expected = i
|
316 |
+
result = i.where(klass(cond))
|
317 |
+
tm.assert_index_equal(result, expected)
|
318 |
+
|
319 |
+
cond = [False] + [True] * (len(i) - 1)
|
320 |
+
expected = CategoricalIndex([np.nan] + i[1:].tolist(), categories=i.categories)
|
321 |
+
result = i.where(klass(cond))
|
322 |
+
tm.assert_index_equal(result, expected)
|
323 |
+
|
324 |
+
def test_where_non_categories(self):
|
325 |
+
ci = CategoricalIndex(["a", "b", "c", "d"])
|
326 |
+
mask = np.array([True, False, True, False])
|
327 |
+
|
328 |
+
result = ci.where(mask, 2)
|
329 |
+
expected = Index(["a", 2, "c", 2], dtype=object)
|
330 |
+
tm.assert_index_equal(result, expected)
|
331 |
+
|
332 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
333 |
+
with pytest.raises(TypeError, match=msg):
|
334 |
+
# Test the Categorical method directly
|
335 |
+
ci._data._where(mask, 2)
|
336 |
+
|
337 |
+
|
338 |
+
class TestContains:
|
339 |
+
def test_contains(self):
|
340 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"), ordered=False)
|
341 |
+
|
342 |
+
assert "a" in ci
|
343 |
+
assert "z" not in ci
|
344 |
+
assert "e" not in ci
|
345 |
+
assert np.nan not in ci
|
346 |
+
|
347 |
+
# assert codes NOT in index
|
348 |
+
assert 0 not in ci
|
349 |
+
assert 1 not in ci
|
350 |
+
|
351 |
+
def test_contains_nan(self):
|
352 |
+
ci = CategoricalIndex(list("aabbca") + [np.nan], categories=list("cabdef"))
|
353 |
+
assert np.nan in ci
|
354 |
+
|
355 |
+
@pytest.mark.parametrize("unwrap", [True, False])
|
356 |
+
def test_contains_na_dtype(self, unwrap):
|
357 |
+
dti = pd.date_range("2016-01-01", periods=100).insert(0, pd.NaT)
|
358 |
+
pi = dti.to_period("D")
|
359 |
+
tdi = dti - dti[-1]
|
360 |
+
ci = CategoricalIndex(dti)
|
361 |
+
|
362 |
+
obj = ci
|
363 |
+
if unwrap:
|
364 |
+
obj = ci._data
|
365 |
+
|
366 |
+
assert np.nan in obj
|
367 |
+
assert None in obj
|
368 |
+
assert pd.NaT in obj
|
369 |
+
assert np.datetime64("NaT") in obj
|
370 |
+
assert np.timedelta64("NaT") not in obj
|
371 |
+
|
372 |
+
obj2 = CategoricalIndex(tdi)
|
373 |
+
if unwrap:
|
374 |
+
obj2 = obj2._data
|
375 |
+
|
376 |
+
assert np.nan in obj2
|
377 |
+
assert None in obj2
|
378 |
+
assert pd.NaT in obj2
|
379 |
+
assert np.datetime64("NaT") not in obj2
|
380 |
+
assert np.timedelta64("NaT") in obj2
|
381 |
+
|
382 |
+
obj3 = CategoricalIndex(pi)
|
383 |
+
if unwrap:
|
384 |
+
obj3 = obj3._data
|
385 |
+
|
386 |
+
assert np.nan in obj3
|
387 |
+
assert None in obj3
|
388 |
+
assert pd.NaT in obj3
|
389 |
+
assert np.datetime64("NaT") not in obj3
|
390 |
+
assert np.timedelta64("NaT") not in obj3
|
391 |
+
|
392 |
+
@pytest.mark.parametrize(
|
393 |
+
"item, expected",
|
394 |
+
[
|
395 |
+
(pd.Interval(0, 1), True),
|
396 |
+
(1.5, True),
|
397 |
+
(pd.Interval(0.5, 1.5), False),
|
398 |
+
("a", False),
|
399 |
+
(Timestamp(1), False),
|
400 |
+
(pd.Timedelta(1), False),
|
401 |
+
],
|
402 |
+
ids=str,
|
403 |
+
)
|
404 |
+
def test_contains_interval(self, item, expected):
|
405 |
+
# GH 23705
|
406 |
+
ci = CategoricalIndex(IntervalIndex.from_breaks(range(3)))
|
407 |
+
result = item in ci
|
408 |
+
assert result is expected
|
409 |
+
|
410 |
+
def test_contains_list(self):
|
411 |
+
# GH#21729
|
412 |
+
idx = CategoricalIndex([1, 2, 3])
|
413 |
+
|
414 |
+
assert "a" not in idx
|
415 |
+
|
416 |
+
with pytest.raises(TypeError, match="unhashable type"):
|
417 |
+
["a"] in idx
|
418 |
+
|
419 |
+
with pytest.raises(TypeError, match="unhashable type"):
|
420 |
+
["a", "b"] in idx
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_map.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
from pandas import (
|
6 |
+
CategoricalIndex,
|
7 |
+
Index,
|
8 |
+
Series,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
@pytest.mark.parametrize(
|
14 |
+
"data, categories",
|
15 |
+
[
|
16 |
+
(list("abcbca"), list("cab")),
|
17 |
+
(pd.interval_range(0, 3).repeat(3), pd.interval_range(0, 3)),
|
18 |
+
],
|
19 |
+
ids=["string", "interval"],
|
20 |
+
)
|
21 |
+
def test_map_str(data, categories, ordered):
|
22 |
+
# GH 31202 - override base class since we want to maintain categorical/ordered
|
23 |
+
index = CategoricalIndex(data, categories=categories, ordered=ordered)
|
24 |
+
result = index.map(str)
|
25 |
+
expected = CategoricalIndex(
|
26 |
+
map(str, data), categories=map(str, categories), ordered=ordered
|
27 |
+
)
|
28 |
+
tm.assert_index_equal(result, expected)
|
29 |
+
|
30 |
+
|
31 |
+
def test_map():
|
32 |
+
ci = CategoricalIndex(list("ABABC"), categories=list("CBA"), ordered=True)
|
33 |
+
result = ci.map(lambda x: x.lower())
|
34 |
+
exp = CategoricalIndex(list("ababc"), categories=list("cba"), ordered=True)
|
35 |
+
tm.assert_index_equal(result, exp)
|
36 |
+
|
37 |
+
ci = CategoricalIndex(
|
38 |
+
list("ABABC"), categories=list("BAC"), ordered=False, name="XXX"
|
39 |
+
)
|
40 |
+
result = ci.map(lambda x: x.lower())
|
41 |
+
exp = CategoricalIndex(
|
42 |
+
list("ababc"), categories=list("bac"), ordered=False, name="XXX"
|
43 |
+
)
|
44 |
+
tm.assert_index_equal(result, exp)
|
45 |
+
|
46 |
+
# GH 12766: Return an index not an array
|
47 |
+
tm.assert_index_equal(
|
48 |
+
ci.map(lambda x: 1), Index(np.array([1] * 5, dtype=np.int64), name="XXX")
|
49 |
+
)
|
50 |
+
|
51 |
+
# change categories dtype
|
52 |
+
ci = CategoricalIndex(list("ABABC"), categories=list("BAC"), ordered=False)
|
53 |
+
|
54 |
+
def f(x):
|
55 |
+
return {"A": 10, "B": 20, "C": 30}.get(x)
|
56 |
+
|
57 |
+
result = ci.map(f)
|
58 |
+
exp = CategoricalIndex([10, 20, 10, 20, 30], categories=[20, 10, 30], ordered=False)
|
59 |
+
tm.assert_index_equal(result, exp)
|
60 |
+
|
61 |
+
result = ci.map(Series([10, 20, 30], index=["A", "B", "C"]))
|
62 |
+
tm.assert_index_equal(result, exp)
|
63 |
+
|
64 |
+
result = ci.map({"A": 10, "B": 20, "C": 30})
|
65 |
+
tm.assert_index_equal(result, exp)
|
66 |
+
|
67 |
+
|
68 |
+
def test_map_with_categorical_series():
|
69 |
+
# GH 12756
|
70 |
+
a = Index([1, 2, 3, 4])
|
71 |
+
b = Series(["even", "odd", "even", "odd"], dtype="category")
|
72 |
+
c = Series(["even", "odd", "even", "odd"])
|
73 |
+
|
74 |
+
exp = CategoricalIndex(["odd", "even", "odd", np.nan])
|
75 |
+
tm.assert_index_equal(a.map(b), exp)
|
76 |
+
exp = Index(["odd", "even", "odd", np.nan])
|
77 |
+
tm.assert_index_equal(a.map(c), exp)
|
78 |
+
|
79 |
+
|
80 |
+
@pytest.mark.parametrize(
|
81 |
+
("data", "f", "expected"),
|
82 |
+
(
|
83 |
+
([1, 1, np.nan], pd.isna, CategoricalIndex([False, False, np.nan])),
|
84 |
+
([1, 2, np.nan], pd.isna, Index([False, False, np.nan])),
|
85 |
+
([1, 1, np.nan], {1: False}, CategoricalIndex([False, False, np.nan])),
|
86 |
+
([1, 2, np.nan], {1: False, 2: False}, Index([False, False, np.nan])),
|
87 |
+
(
|
88 |
+
[1, 1, np.nan],
|
89 |
+
Series([False, False]),
|
90 |
+
CategoricalIndex([False, False, np.nan]),
|
91 |
+
),
|
92 |
+
(
|
93 |
+
[1, 2, np.nan],
|
94 |
+
Series([False, False, False]),
|
95 |
+
Index([False, False, np.nan]),
|
96 |
+
),
|
97 |
+
),
|
98 |
+
)
|
99 |
+
def test_map_with_nan_ignore(data, f, expected): # GH 24241
|
100 |
+
values = CategoricalIndex(data)
|
101 |
+
result = values.map(f, na_action="ignore")
|
102 |
+
tm.assert_index_equal(result, expected)
|
103 |
+
|
104 |
+
|
105 |
+
@pytest.mark.parametrize(
|
106 |
+
("data", "f", "expected"),
|
107 |
+
(
|
108 |
+
([1, 1, np.nan], pd.isna, Index([False, False, True])),
|
109 |
+
([1, 2, np.nan], pd.isna, Index([False, False, True])),
|
110 |
+
([1, 1, np.nan], {1: False}, CategoricalIndex([False, False, np.nan])),
|
111 |
+
([1, 2, np.nan], {1: False, 2: False}, Index([False, False, np.nan])),
|
112 |
+
(
|
113 |
+
[1, 1, np.nan],
|
114 |
+
Series([False, False]),
|
115 |
+
CategoricalIndex([False, False, np.nan]),
|
116 |
+
),
|
117 |
+
(
|
118 |
+
[1, 2, np.nan],
|
119 |
+
Series([False, False, False]),
|
120 |
+
Index([False, False, np.nan]),
|
121 |
+
),
|
122 |
+
),
|
123 |
+
)
|
124 |
+
def test_map_with_nan_none(data, f, expected): # GH 24241
|
125 |
+
values = CategoricalIndex(data)
|
126 |
+
result = values.map(f, na_action=None)
|
127 |
+
tm.assert_index_equal(result, expected)
|
128 |
+
|
129 |
+
|
130 |
+
def test_map_with_dict_or_series():
|
131 |
+
orig_values = ["a", "B", 1, "a"]
|
132 |
+
new_values = ["one", 2, 3.0, "one"]
|
133 |
+
cur_index = CategoricalIndex(orig_values, name="XXX")
|
134 |
+
expected = CategoricalIndex(new_values, name="XXX", categories=[3.0, 2, "one"])
|
135 |
+
|
136 |
+
mapper = Series(new_values[:-1], index=orig_values[:-1])
|
137 |
+
result = cur_index.map(mapper)
|
138 |
+
# Order of categories in result can be different
|
139 |
+
tm.assert_index_equal(result, expected)
|
140 |
+
|
141 |
+
mapper = dict(zip(orig_values[:-1], new_values[:-1]))
|
142 |
+
result = cur_index.map(mapper)
|
143 |
+
# Order of categories in result can be different
|
144 |
+
tm.assert_index_equal(result, expected)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_reindex.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
Categorical,
|
6 |
+
CategoricalIndex,
|
7 |
+
Index,
|
8 |
+
Interval,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
class TestReindex:
|
14 |
+
def test_reindex_list_non_unique(self):
|
15 |
+
# GH#11586
|
16 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
17 |
+
ci = CategoricalIndex(["a", "b", "c", "a"])
|
18 |
+
with pytest.raises(ValueError, match=msg):
|
19 |
+
ci.reindex(["a", "c"])
|
20 |
+
|
21 |
+
def test_reindex_categorical_non_unique(self):
|
22 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
23 |
+
ci = CategoricalIndex(["a", "b", "c", "a"])
|
24 |
+
with pytest.raises(ValueError, match=msg):
|
25 |
+
ci.reindex(Categorical(["a", "c"]))
|
26 |
+
|
27 |
+
def test_reindex_list_non_unique_unused_category(self):
|
28 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
29 |
+
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
30 |
+
with pytest.raises(ValueError, match=msg):
|
31 |
+
ci.reindex(["a", "c"])
|
32 |
+
|
33 |
+
def test_reindex_categorical_non_unique_unused_category(self):
|
34 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
35 |
+
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
36 |
+
with pytest.raises(ValueError, match=msg):
|
37 |
+
ci.reindex(Categorical(["a", "c"]))
|
38 |
+
|
39 |
+
def test_reindex_duplicate_target(self):
|
40 |
+
# See GH25459
|
41 |
+
cat = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c", "d"])
|
42 |
+
res, indexer = cat.reindex(["a", "c", "c"])
|
43 |
+
exp = Index(["a", "c", "c"])
|
44 |
+
tm.assert_index_equal(res, exp, exact=True)
|
45 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
|
46 |
+
|
47 |
+
res, indexer = cat.reindex(
|
48 |
+
CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
|
49 |
+
)
|
50 |
+
exp = CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
|
51 |
+
tm.assert_index_equal(res, exp, exact=True)
|
52 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
|
53 |
+
|
54 |
+
def test_reindex_empty_index(self):
|
55 |
+
# See GH16770
|
56 |
+
c = CategoricalIndex([])
|
57 |
+
res, indexer = c.reindex(["a", "b"])
|
58 |
+
tm.assert_index_equal(res, Index(["a", "b"]), exact=True)
|
59 |
+
tm.assert_numpy_array_equal(indexer, np.array([-1, -1], dtype=np.intp))
|
60 |
+
|
61 |
+
def test_reindex_categorical_added_category(self):
|
62 |
+
# GH 42424
|
63 |
+
ci = CategoricalIndex(
|
64 |
+
[Interval(0, 1, closed="right"), Interval(1, 2, closed="right")],
|
65 |
+
ordered=True,
|
66 |
+
)
|
67 |
+
ci_add = CategoricalIndex(
|
68 |
+
[
|
69 |
+
Interval(0, 1, closed="right"),
|
70 |
+
Interval(1, 2, closed="right"),
|
71 |
+
Interval(2, 3, closed="right"),
|
72 |
+
Interval(3, 4, closed="right"),
|
73 |
+
],
|
74 |
+
ordered=True,
|
75 |
+
)
|
76 |
+
result, _ = ci.reindex(ci_add)
|
77 |
+
expected = ci_add
|
78 |
+
tm.assert_index_equal(expected, result)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_setops.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
CategoricalIndex,
|
6 |
+
Index,
|
7 |
+
)
|
8 |
+
import pandas._testing as tm
|
9 |
+
|
10 |
+
|
11 |
+
@pytest.mark.parametrize("na_value", [None, np.nan])
|
12 |
+
def test_difference_with_na(na_value):
|
13 |
+
# GH 57318
|
14 |
+
ci = CategoricalIndex(["a", "b", "c", None])
|
15 |
+
other = Index(["c", na_value])
|
16 |
+
result = ci.difference(other)
|
17 |
+
expected = CategoricalIndex(["a", "b"], categories=["a", "b", "c"])
|
18 |
+
tm.assert_index_equal(result, expected)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (194 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/conftest.cpython-310.pyc
ADDED
Binary file (748 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_analytics.cpython-310.pyc
ADDED
Binary file (7.1 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_compat.cpython-310.pyc
ADDED
Binary file (3.02 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_drop.cpython-310.pyc
ADDED
Binary file (5.44 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_duplicates.cpython-310.pyc
ADDED
Binary file (10.1 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_equivalence.cpython-310.pyc
ADDED
Binary file (6.58 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_formats.cpython-310.pyc
ADDED
Binary file (10.1 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_get_set.cpython-310.pyc
ADDED
Binary file (11 kB). View file
|
|