Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/conftest.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_any_index.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_base.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_common.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_datetimelike.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_engines.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_frozen.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_index_new.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_indexing.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_numpy_compat.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_old_base.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_setops.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_subclass.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_append.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_astype.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_constructors.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_equals.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_fillna.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_map.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_setops.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_astype.py +90 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_category.py +394 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_constructors.py +142 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_equals.py +96 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_formats.py +120 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_reindex.py +78 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/interval/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_astype.py +248 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_pickle.py +13 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_setops.py +208 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/conftest.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_analytics.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_astype.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_compat.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_constructors.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_conversion.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_copy.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_drop.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_duplicates.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_equivalence.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_formats.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_get_level_values.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_get_set.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_indexing.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_integrity.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_isin.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_join.cpython-310.pyc +0 -0
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (193 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/conftest.cpython-310.pyc
ADDED
Binary file (1.28 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_any_index.cpython-310.pyc
ADDED
Binary file (6.77 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_base.cpython-310.pyc
ADDED
Binary file (53.3 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_common.cpython-310.pyc
ADDED
Binary file (13 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_datetimelike.cpython-310.pyc
ADDED
Binary file (5.88 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_engines.cpython-310.pyc
ADDED
Binary file (5.91 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_frozen.cpython-310.pyc
ADDED
Binary file (4.52 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_index_new.cpython-310.pyc
ADDED
Binary file (15.1 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (11.6 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_numpy_compat.cpython-310.pyc
ADDED
Binary file (4.48 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_old_base.cpython-310.pyc
ADDED
Binary file (30.1 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_setops.cpython-310.pyc
ADDED
Binary file (23.3 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_subclass.cpython-310.pyc
ADDED
Binary file (1.53 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_append.cpython-310.pyc
ADDED
Binary file (2.76 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (2.37 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (3.91 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_equals.cpython-310.pyc
ADDED
Binary file (3.08 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_fillna.cpython-310.pyc
ADDED
Binary file (1.83 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_map.cpython-310.pyc
ADDED
Binary file (3.75 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_setops.cpython-310.pyc
ADDED
Binary file (736 Bytes). View file
|
|
llmeval-env/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
|
llmeval-env/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 |
+
)
|
llmeval-env/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)
|
llmeval-env/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)
|
llmeval-env/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
|
llmeval-env/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)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/interval/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_astype.py
ADDED
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
from pandas.core.dtypes.dtypes import (
|
7 |
+
CategoricalDtype,
|
8 |
+
IntervalDtype,
|
9 |
+
)
|
10 |
+
|
11 |
+
from pandas import (
|
12 |
+
CategoricalIndex,
|
13 |
+
Index,
|
14 |
+
IntervalIndex,
|
15 |
+
NaT,
|
16 |
+
Timedelta,
|
17 |
+
Timestamp,
|
18 |
+
interval_range,
|
19 |
+
)
|
20 |
+
import pandas._testing as tm
|
21 |
+
|
22 |
+
|
23 |
+
class AstypeTests:
|
24 |
+
"""Tests common to IntervalIndex with any subtype"""
|
25 |
+
|
26 |
+
def test_astype_idempotent(self, index):
|
27 |
+
result = index.astype("interval")
|
28 |
+
tm.assert_index_equal(result, index)
|
29 |
+
|
30 |
+
result = index.astype(index.dtype)
|
31 |
+
tm.assert_index_equal(result, index)
|
32 |
+
|
33 |
+
def test_astype_object(self, index):
|
34 |
+
result = index.astype(object)
|
35 |
+
expected = Index(index.values, dtype="object")
|
36 |
+
tm.assert_index_equal(result, expected)
|
37 |
+
assert not result.equals(index)
|
38 |
+
|
39 |
+
def test_astype_category(self, index):
|
40 |
+
result = index.astype("category")
|
41 |
+
expected = CategoricalIndex(index.values)
|
42 |
+
tm.assert_index_equal(result, expected)
|
43 |
+
|
44 |
+
result = index.astype(CategoricalDtype())
|
45 |
+
tm.assert_index_equal(result, expected)
|
46 |
+
|
47 |
+
# non-default params
|
48 |
+
categories = index.dropna().unique().values[:-1]
|
49 |
+
dtype = CategoricalDtype(categories=categories, ordered=True)
|
50 |
+
result = index.astype(dtype)
|
51 |
+
expected = CategoricalIndex(index.values, categories=categories, ordered=True)
|
52 |
+
tm.assert_index_equal(result, expected)
|
53 |
+
|
54 |
+
@pytest.mark.parametrize(
|
55 |
+
"dtype",
|
56 |
+
[
|
57 |
+
"int64",
|
58 |
+
"uint64",
|
59 |
+
"float64",
|
60 |
+
"complex128",
|
61 |
+
"period[M]",
|
62 |
+
"timedelta64",
|
63 |
+
"timedelta64[ns]",
|
64 |
+
"datetime64",
|
65 |
+
"datetime64[ns]",
|
66 |
+
"datetime64[ns, US/Eastern]",
|
67 |
+
],
|
68 |
+
)
|
69 |
+
def test_astype_cannot_cast(self, index, dtype):
|
70 |
+
msg = "Cannot cast IntervalIndex to dtype"
|
71 |
+
with pytest.raises(TypeError, match=msg):
|
72 |
+
index.astype(dtype)
|
73 |
+
|
74 |
+
def test_astype_invalid_dtype(self, index):
|
75 |
+
msg = "data type [\"']fake_dtype[\"'] not understood"
|
76 |
+
with pytest.raises(TypeError, match=msg):
|
77 |
+
index.astype("fake_dtype")
|
78 |
+
|
79 |
+
|
80 |
+
class TestIntSubtype(AstypeTests):
|
81 |
+
"""Tests specific to IntervalIndex with integer-like subtype"""
|
82 |
+
|
83 |
+
indexes = [
|
84 |
+
IntervalIndex.from_breaks(np.arange(-10, 11, dtype="int64")),
|
85 |
+
IntervalIndex.from_breaks(np.arange(100, dtype="uint64"), closed="left"),
|
86 |
+
]
|
87 |
+
|
88 |
+
@pytest.fixture(params=indexes)
|
89 |
+
def index(self, request):
|
90 |
+
return request.param
|
91 |
+
|
92 |
+
@pytest.mark.parametrize(
|
93 |
+
"subtype", ["float64", "datetime64[ns]", "timedelta64[ns]"]
|
94 |
+
)
|
95 |
+
def test_subtype_conversion(self, index, subtype):
|
96 |
+
dtype = IntervalDtype(subtype, index.closed)
|
97 |
+
result = index.astype(dtype)
|
98 |
+
expected = IntervalIndex.from_arrays(
|
99 |
+
index.left.astype(subtype), index.right.astype(subtype), closed=index.closed
|
100 |
+
)
|
101 |
+
tm.assert_index_equal(result, expected)
|
102 |
+
|
103 |
+
@pytest.mark.parametrize(
|
104 |
+
"subtype_start, subtype_end", [("int64", "uint64"), ("uint64", "int64")]
|
105 |
+
)
|
106 |
+
def test_subtype_integer(self, subtype_start, subtype_end):
|
107 |
+
index = IntervalIndex.from_breaks(np.arange(100, dtype=subtype_start))
|
108 |
+
dtype = IntervalDtype(subtype_end, index.closed)
|
109 |
+
result = index.astype(dtype)
|
110 |
+
expected = IntervalIndex.from_arrays(
|
111 |
+
index.left.astype(subtype_end),
|
112 |
+
index.right.astype(subtype_end),
|
113 |
+
closed=index.closed,
|
114 |
+
)
|
115 |
+
tm.assert_index_equal(result, expected)
|
116 |
+
|
117 |
+
@pytest.mark.xfail(reason="GH#15832")
|
118 |
+
def test_subtype_integer_errors(self):
|
119 |
+
# int64 -> uint64 fails with negative values
|
120 |
+
index = interval_range(-10, 10)
|
121 |
+
dtype = IntervalDtype("uint64", "right")
|
122 |
+
|
123 |
+
# Until we decide what the exception message _should_ be, we
|
124 |
+
# assert something that it should _not_ be.
|
125 |
+
# We should _not_ be getting a message suggesting that the -10
|
126 |
+
# has been wrapped around to a large-positive integer
|
127 |
+
msg = "^(?!(left side of interval must be <= right side))"
|
128 |
+
with pytest.raises(ValueError, match=msg):
|
129 |
+
index.astype(dtype)
|
130 |
+
|
131 |
+
|
132 |
+
class TestFloatSubtype(AstypeTests):
|
133 |
+
"""Tests specific to IntervalIndex with float subtype"""
|
134 |
+
|
135 |
+
indexes = [
|
136 |
+
interval_range(-10.0, 10.0, closed="neither"),
|
137 |
+
IntervalIndex.from_arrays(
|
138 |
+
[-1.5, np.nan, 0.0, 0.0, 1.5], [-0.5, np.nan, 1.0, 1.0, 3.0], closed="both"
|
139 |
+
),
|
140 |
+
]
|
141 |
+
|
142 |
+
@pytest.fixture(params=indexes)
|
143 |
+
def index(self, request):
|
144 |
+
return request.param
|
145 |
+
|
146 |
+
@pytest.mark.parametrize("subtype", ["int64", "uint64"])
|
147 |
+
def test_subtype_integer(self, subtype):
|
148 |
+
index = interval_range(0.0, 10.0)
|
149 |
+
dtype = IntervalDtype(subtype, "right")
|
150 |
+
result = index.astype(dtype)
|
151 |
+
expected = IntervalIndex.from_arrays(
|
152 |
+
index.left.astype(subtype), index.right.astype(subtype), closed=index.closed
|
153 |
+
)
|
154 |
+
tm.assert_index_equal(result, expected)
|
155 |
+
|
156 |
+
# raises with NA
|
157 |
+
msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
|
158 |
+
with pytest.raises(ValueError, match=msg):
|
159 |
+
index.insert(0, np.nan).astype(dtype)
|
160 |
+
|
161 |
+
@pytest.mark.parametrize("subtype", ["int64", "uint64"])
|
162 |
+
def test_subtype_integer_with_non_integer_borders(self, subtype):
|
163 |
+
index = interval_range(0.0, 3.0, freq=0.25)
|
164 |
+
dtype = IntervalDtype(subtype, "right")
|
165 |
+
result = index.astype(dtype)
|
166 |
+
expected = IntervalIndex.from_arrays(
|
167 |
+
index.left.astype(subtype), index.right.astype(subtype), closed=index.closed
|
168 |
+
)
|
169 |
+
tm.assert_index_equal(result, expected)
|
170 |
+
|
171 |
+
def test_subtype_integer_errors(self):
|
172 |
+
# float64 -> uint64 fails with negative values
|
173 |
+
index = interval_range(-10.0, 10.0)
|
174 |
+
dtype = IntervalDtype("uint64", "right")
|
175 |
+
msg = re.escape(
|
176 |
+
"Cannot convert interval[float64, right] to interval[uint64, right]; "
|
177 |
+
"subtypes are incompatible"
|
178 |
+
)
|
179 |
+
with pytest.raises(TypeError, match=msg):
|
180 |
+
index.astype(dtype)
|
181 |
+
|
182 |
+
@pytest.mark.parametrize("subtype", ["datetime64[ns]", "timedelta64[ns]"])
|
183 |
+
def test_subtype_datetimelike(self, index, subtype):
|
184 |
+
dtype = IntervalDtype(subtype, "right")
|
185 |
+
msg = "Cannot convert .* to .*; subtypes are incompatible"
|
186 |
+
with pytest.raises(TypeError, match=msg):
|
187 |
+
index.astype(dtype)
|
188 |
+
|
189 |
+
|
190 |
+
class TestDatetimelikeSubtype(AstypeTests):
|
191 |
+
"""Tests specific to IntervalIndex with datetime-like subtype"""
|
192 |
+
|
193 |
+
indexes = [
|
194 |
+
interval_range(Timestamp("2018-01-01"), periods=10, closed="neither"),
|
195 |
+
interval_range(Timestamp("2018-01-01"), periods=10).insert(2, NaT),
|
196 |
+
interval_range(Timestamp("2018-01-01", tz="US/Eastern"), periods=10),
|
197 |
+
interval_range(Timedelta("0 days"), periods=10, closed="both"),
|
198 |
+
interval_range(Timedelta("0 days"), periods=10).insert(2, NaT),
|
199 |
+
]
|
200 |
+
|
201 |
+
@pytest.fixture(params=indexes)
|
202 |
+
def index(self, request):
|
203 |
+
return request.param
|
204 |
+
|
205 |
+
@pytest.mark.parametrize("subtype", ["int64", "uint64"])
|
206 |
+
def test_subtype_integer(self, index, subtype):
|
207 |
+
dtype = IntervalDtype(subtype, "right")
|
208 |
+
|
209 |
+
if subtype != "int64":
|
210 |
+
msg = (
|
211 |
+
r"Cannot convert interval\[(timedelta64|datetime64)\[ns.*\], .*\] "
|
212 |
+
r"to interval\[uint64, .*\]"
|
213 |
+
)
|
214 |
+
with pytest.raises(TypeError, match=msg):
|
215 |
+
index.astype(dtype)
|
216 |
+
return
|
217 |
+
|
218 |
+
result = index.astype(dtype)
|
219 |
+
new_left = index.left.astype(subtype)
|
220 |
+
new_right = index.right.astype(subtype)
|
221 |
+
|
222 |
+
expected = IntervalIndex.from_arrays(new_left, new_right, closed=index.closed)
|
223 |
+
tm.assert_index_equal(result, expected)
|
224 |
+
|
225 |
+
def test_subtype_float(self, index):
|
226 |
+
dtype = IntervalDtype("float64", "right")
|
227 |
+
msg = "Cannot convert .* to .*; subtypes are incompatible"
|
228 |
+
with pytest.raises(TypeError, match=msg):
|
229 |
+
index.astype(dtype)
|
230 |
+
|
231 |
+
def test_subtype_datetimelike(self):
|
232 |
+
# datetime -> timedelta raises
|
233 |
+
dtype = IntervalDtype("timedelta64[ns]", "right")
|
234 |
+
msg = "Cannot convert .* to .*; subtypes are incompatible"
|
235 |
+
|
236 |
+
index = interval_range(Timestamp("2018-01-01"), periods=10)
|
237 |
+
with pytest.raises(TypeError, match=msg):
|
238 |
+
index.astype(dtype)
|
239 |
+
|
240 |
+
index = interval_range(Timestamp("2018-01-01", tz="CET"), periods=10)
|
241 |
+
with pytest.raises(TypeError, match=msg):
|
242 |
+
index.astype(dtype)
|
243 |
+
|
244 |
+
# timedelta -> datetime raises
|
245 |
+
dtype = IntervalDtype("datetime64[ns]", "right")
|
246 |
+
index = interval_range(Timedelta("0 days"), periods=10)
|
247 |
+
with pytest.raises(TypeError, match=msg):
|
248 |
+
index.astype(dtype)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_pickle.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import IntervalIndex
|
4 |
+
import pandas._testing as tm
|
5 |
+
|
6 |
+
|
7 |
+
class TestPickle:
|
8 |
+
@pytest.mark.parametrize("closed", ["left", "right", "both"])
|
9 |
+
def test_pickle_round_trip_closed(self, closed):
|
10 |
+
# https://github.com/pandas-dev/pandas/issues/35658
|
11 |
+
idx = IntervalIndex.from_tuples([(1, 2), (2, 3)], closed=closed)
|
12 |
+
result = tm.round_trip_pickle(idx)
|
13 |
+
tm.assert_index_equal(result, idx)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_setops.py
ADDED
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
Index,
|
6 |
+
IntervalIndex,
|
7 |
+
Timestamp,
|
8 |
+
interval_range,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
def monotonic_index(start, end, dtype="int64", closed="right"):
|
14 |
+
return IntervalIndex.from_breaks(np.arange(start, end, dtype=dtype), closed=closed)
|
15 |
+
|
16 |
+
|
17 |
+
def empty_index(dtype="int64", closed="right"):
|
18 |
+
return IntervalIndex(np.array([], dtype=dtype), closed=closed)
|
19 |
+
|
20 |
+
|
21 |
+
class TestIntervalIndex:
|
22 |
+
def test_union(self, closed, sort):
|
23 |
+
index = monotonic_index(0, 11, closed=closed)
|
24 |
+
other = monotonic_index(5, 13, closed=closed)
|
25 |
+
|
26 |
+
expected = monotonic_index(0, 13, closed=closed)
|
27 |
+
result = index[::-1].union(other, sort=sort)
|
28 |
+
if sort in (None, True):
|
29 |
+
tm.assert_index_equal(result, expected)
|
30 |
+
else:
|
31 |
+
tm.assert_index_equal(result.sort_values(), expected)
|
32 |
+
|
33 |
+
result = other[::-1].union(index, sort=sort)
|
34 |
+
if sort in (None, True):
|
35 |
+
tm.assert_index_equal(result, expected)
|
36 |
+
else:
|
37 |
+
tm.assert_index_equal(result.sort_values(), expected)
|
38 |
+
|
39 |
+
tm.assert_index_equal(index.union(index, sort=sort), index)
|
40 |
+
tm.assert_index_equal(index.union(index[:1], sort=sort), index)
|
41 |
+
|
42 |
+
def test_union_empty_result(self, closed, sort):
|
43 |
+
# GH 19101: empty result, same dtype
|
44 |
+
index = empty_index(dtype="int64", closed=closed)
|
45 |
+
result = index.union(index, sort=sort)
|
46 |
+
tm.assert_index_equal(result, index)
|
47 |
+
|
48 |
+
# GH 19101: empty result, different numeric dtypes -> common dtype is f8
|
49 |
+
other = empty_index(dtype="float64", closed=closed)
|
50 |
+
result = index.union(other, sort=sort)
|
51 |
+
expected = other
|
52 |
+
tm.assert_index_equal(result, expected)
|
53 |
+
|
54 |
+
other = index.union(index, sort=sort)
|
55 |
+
tm.assert_index_equal(result, expected)
|
56 |
+
|
57 |
+
other = empty_index(dtype="uint64", closed=closed)
|
58 |
+
result = index.union(other, sort=sort)
|
59 |
+
tm.assert_index_equal(result, expected)
|
60 |
+
|
61 |
+
result = other.union(index, sort=sort)
|
62 |
+
tm.assert_index_equal(result, expected)
|
63 |
+
|
64 |
+
def test_intersection(self, closed, sort):
|
65 |
+
index = monotonic_index(0, 11, closed=closed)
|
66 |
+
other = monotonic_index(5, 13, closed=closed)
|
67 |
+
|
68 |
+
expected = monotonic_index(5, 11, closed=closed)
|
69 |
+
result = index[::-1].intersection(other, sort=sort)
|
70 |
+
if sort in (None, True):
|
71 |
+
tm.assert_index_equal(result, expected)
|
72 |
+
else:
|
73 |
+
tm.assert_index_equal(result.sort_values(), expected)
|
74 |
+
|
75 |
+
result = other[::-1].intersection(index, sort=sort)
|
76 |
+
if sort in (None, True):
|
77 |
+
tm.assert_index_equal(result, expected)
|
78 |
+
else:
|
79 |
+
tm.assert_index_equal(result.sort_values(), expected)
|
80 |
+
|
81 |
+
tm.assert_index_equal(index.intersection(index, sort=sort), index)
|
82 |
+
|
83 |
+
# GH 26225: nested intervals
|
84 |
+
index = IntervalIndex.from_tuples([(1, 2), (1, 3), (1, 4), (0, 2)])
|
85 |
+
other = IntervalIndex.from_tuples([(1, 2), (1, 3)])
|
86 |
+
expected = IntervalIndex.from_tuples([(1, 2), (1, 3)])
|
87 |
+
result = index.intersection(other)
|
88 |
+
tm.assert_index_equal(result, expected)
|
89 |
+
|
90 |
+
# GH 26225
|
91 |
+
index = IntervalIndex.from_tuples([(0, 3), (0, 2)])
|
92 |
+
other = IntervalIndex.from_tuples([(0, 2), (1, 3)])
|
93 |
+
expected = IntervalIndex.from_tuples([(0, 2)])
|
94 |
+
result = index.intersection(other)
|
95 |
+
tm.assert_index_equal(result, expected)
|
96 |
+
|
97 |
+
# GH 26225: duplicate nan element
|
98 |
+
index = IntervalIndex([np.nan, np.nan])
|
99 |
+
other = IntervalIndex([np.nan])
|
100 |
+
expected = IntervalIndex([np.nan])
|
101 |
+
result = index.intersection(other)
|
102 |
+
tm.assert_index_equal(result, expected)
|
103 |
+
|
104 |
+
def test_intersection_empty_result(self, closed, sort):
|
105 |
+
index = monotonic_index(0, 11, closed=closed)
|
106 |
+
|
107 |
+
# GH 19101: empty result, same dtype
|
108 |
+
other = monotonic_index(300, 314, closed=closed)
|
109 |
+
expected = empty_index(dtype="int64", closed=closed)
|
110 |
+
result = index.intersection(other, sort=sort)
|
111 |
+
tm.assert_index_equal(result, expected)
|
112 |
+
|
113 |
+
# GH 19101: empty result, different numeric dtypes -> common dtype is float64
|
114 |
+
other = monotonic_index(300, 314, dtype="float64", closed=closed)
|
115 |
+
result = index.intersection(other, sort=sort)
|
116 |
+
expected = other[:0]
|
117 |
+
tm.assert_index_equal(result, expected)
|
118 |
+
|
119 |
+
other = monotonic_index(300, 314, dtype="uint64", closed=closed)
|
120 |
+
result = index.intersection(other, sort=sort)
|
121 |
+
tm.assert_index_equal(result, expected)
|
122 |
+
|
123 |
+
def test_intersection_duplicates(self):
|
124 |
+
# GH#38743
|
125 |
+
index = IntervalIndex.from_tuples([(1, 2), (1, 2), (2, 3), (3, 4)])
|
126 |
+
other = IntervalIndex.from_tuples([(1, 2), (2, 3)])
|
127 |
+
expected = IntervalIndex.from_tuples([(1, 2), (2, 3)])
|
128 |
+
result = index.intersection(other)
|
129 |
+
tm.assert_index_equal(result, expected)
|
130 |
+
|
131 |
+
def test_difference(self, closed, sort):
|
132 |
+
index = IntervalIndex.from_arrays([1, 0, 3, 2], [1, 2, 3, 4], closed=closed)
|
133 |
+
result = index.difference(index[:1], sort=sort)
|
134 |
+
expected = index[1:]
|
135 |
+
if sort is None:
|
136 |
+
expected = expected.sort_values()
|
137 |
+
tm.assert_index_equal(result, expected)
|
138 |
+
|
139 |
+
# GH 19101: empty result, same dtype
|
140 |
+
result = index.difference(index, sort=sort)
|
141 |
+
expected = empty_index(dtype="int64", closed=closed)
|
142 |
+
tm.assert_index_equal(result, expected)
|
143 |
+
|
144 |
+
# GH 19101: empty result, different dtypes
|
145 |
+
other = IntervalIndex.from_arrays(
|
146 |
+
index.left.astype("float64"), index.right, closed=closed
|
147 |
+
)
|
148 |
+
result = index.difference(other, sort=sort)
|
149 |
+
tm.assert_index_equal(result, expected)
|
150 |
+
|
151 |
+
def test_symmetric_difference(self, closed, sort):
|
152 |
+
index = monotonic_index(0, 11, closed=closed)
|
153 |
+
result = index[1:].symmetric_difference(index[:-1], sort=sort)
|
154 |
+
expected = IntervalIndex([index[0], index[-1]])
|
155 |
+
if sort in (None, True):
|
156 |
+
tm.assert_index_equal(result, expected)
|
157 |
+
else:
|
158 |
+
tm.assert_index_equal(result.sort_values(), expected)
|
159 |
+
|
160 |
+
# GH 19101: empty result, same dtype
|
161 |
+
result = index.symmetric_difference(index, sort=sort)
|
162 |
+
expected = empty_index(dtype="int64", closed=closed)
|
163 |
+
if sort in (None, True):
|
164 |
+
tm.assert_index_equal(result, expected)
|
165 |
+
else:
|
166 |
+
tm.assert_index_equal(result.sort_values(), expected)
|
167 |
+
|
168 |
+
# GH 19101: empty result, different dtypes
|
169 |
+
other = IntervalIndex.from_arrays(
|
170 |
+
index.left.astype("float64"), index.right, closed=closed
|
171 |
+
)
|
172 |
+
result = index.symmetric_difference(other, sort=sort)
|
173 |
+
expected = empty_index(dtype="float64", closed=closed)
|
174 |
+
tm.assert_index_equal(result, expected)
|
175 |
+
|
176 |
+
@pytest.mark.filterwarnings("ignore:'<' not supported between:RuntimeWarning")
|
177 |
+
@pytest.mark.parametrize(
|
178 |
+
"op_name", ["union", "intersection", "difference", "symmetric_difference"]
|
179 |
+
)
|
180 |
+
def test_set_incompatible_types(self, closed, op_name, sort):
|
181 |
+
index = monotonic_index(0, 11, closed=closed)
|
182 |
+
set_op = getattr(index, op_name)
|
183 |
+
|
184 |
+
# TODO: standardize return type of non-union setops type(self vs other)
|
185 |
+
# non-IntervalIndex
|
186 |
+
if op_name == "difference":
|
187 |
+
expected = index
|
188 |
+
else:
|
189 |
+
expected = getattr(index.astype("O"), op_name)(Index([1, 2, 3]))
|
190 |
+
result = set_op(Index([1, 2, 3]), sort=sort)
|
191 |
+
tm.assert_index_equal(result, expected)
|
192 |
+
|
193 |
+
# mixed closed -> cast to object
|
194 |
+
for other_closed in {"right", "left", "both", "neither"} - {closed}:
|
195 |
+
other = monotonic_index(0, 11, closed=other_closed)
|
196 |
+
expected = getattr(index.astype(object), op_name)(other, sort=sort)
|
197 |
+
if op_name == "difference":
|
198 |
+
expected = index
|
199 |
+
result = set_op(other, sort=sort)
|
200 |
+
tm.assert_index_equal(result, expected)
|
201 |
+
|
202 |
+
# GH 19016: incompatible dtypes -> cast to object
|
203 |
+
other = interval_range(Timestamp("20180101"), periods=9, closed=closed)
|
204 |
+
expected = getattr(index.astype(object), op_name)(other, sort=sort)
|
205 |
+
if op_name == "difference":
|
206 |
+
expected = index
|
207 |
+
result = set_op(other, sort=sort)
|
208 |
+
tm.assert_index_equal(result, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (199 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/conftest.cpython-310.pyc
ADDED
Binary file (753 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_analytics.cpython-310.pyc
ADDED
Binary file (7.11 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (1.2 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_compat.cpython-310.pyc
ADDED
Binary file (3.02 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (22.6 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_conversion.cpython-310.pyc
ADDED
Binary file (3.94 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_copy.cpython-310.pyc
ADDED
Binary file (2.41 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_drop.cpython-310.pyc
ADDED
Binary file (5.44 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_duplicates.cpython-310.pyc
ADDED
Binary file (10.1 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_equivalence.cpython-310.pyc
ADDED
Binary file (6.58 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_formats.cpython-310.pyc
ADDED
Binary file (10.1 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_get_level_values.cpython-310.pyc
ADDED
Binary file (4.03 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_get_set.cpython-310.pyc
ADDED
Binary file (11 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (29.5 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_integrity.cpython-310.pyc
ADDED
Binary file (8.97 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_isin.cpython-310.pyc
ADDED
Binary file (3.56 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/multi/__pycache__/test_join.cpython-310.pyc
ADDED
Binary file (6.71 kB). View file
|
|