Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- videochat2/lib/python3.10/site-packages/pandas/io/sas/_sas.cpython-310-x86_64-linux-gnu.so +3 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/common.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/conftest.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/datetimelike.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_any_index.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_base.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_common.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_engines.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_frozen.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_index_new.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_indexing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_numpy_compat.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_setops.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_subclass.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_constructors.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_formats.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_indexing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_pickle.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_reshape.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_setops.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_where.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_constructors.py +44 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_formats.py +148 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_indexing.py +82 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_pickle.py +11 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_where.py +13 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_astype.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_base.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_constructors.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_equals.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_formats.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_indexing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_interval.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_interval_range.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_interval_tree.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_join.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_pickle.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_setops.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_astype.py +248 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_base.py +70 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_constructors.py +478 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_equals.py +36 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_formats.py +105 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_indexing.py +594 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_interval.py +934 -0
.gitattributes
CHANGED
|
@@ -1281,3 +1281,4 @@ videochat2/lib/python3.10/site-packages/pandas/core/indexes/__pycache__/multi.cp
|
|
| 1281 |
videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/generic.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1282 |
videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/frame.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1283 |
videochat2/lib/python3.10/site-packages/pandas/io/formats/__pycache__/style.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 1281 |
videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/generic.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1282 |
videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/frame.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1283 |
videochat2/lib/python3.10/site-packages/pandas/io/formats/__pycache__/style.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1284 |
+
videochat2/lib/python3.10/site-packages/pandas/io/sas/_sas.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
videochat2/lib/python3.10/site-packages/pandas/io/sas/_sas.cpython-310-x86_64-linux-gnu.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ae2272e0830d9faf04229c87a062f20147b4cf2ed94548b456a378ca0d8b0434
|
| 3 |
+
size 239496
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (176 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/common.cpython-310.pyc
ADDED
|
Binary file (25.8 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/conftest.cpython-310.pyc
ADDED
|
Binary file (1.76 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/datetimelike.cpython-310.pyc
ADDED
|
Binary file (4.73 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_any_index.cpython-310.pyc
ADDED
|
Binary file (6.61 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_base.cpython-310.pyc
ADDED
|
Binary file (49.5 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_common.cpython-310.pyc
ADDED
|
Binary file (12.1 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_engines.cpython-310.pyc
ADDED
|
Binary file (5.9 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_frozen.cpython-310.pyc
ADDED
|
Binary file (4.51 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_index_new.cpython-310.pyc
ADDED
|
Binary file (14 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (11.4 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_numpy_compat.cpython-310.pyc
ADDED
|
Binary file (4.41 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_setops.cpython-310.pyc
ADDED
|
Binary file (21.1 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/__pycache__/test_subclass.cpython-310.pyc
ADDED
|
Binary file (1.48 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (187 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_constructors.cpython-310.pyc
ADDED
|
Binary file (1.9 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_formats.cpython-310.pyc
ADDED
|
Binary file (4.36 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (3.57 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_pickle.cpython-310.pyc
ADDED
|
Binary file (555 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_reshape.cpython-310.pyc
ADDED
|
Binary file (3.07 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_setops.cpython-310.pyc
ADDED
|
Binary file (8.05 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__pycache__/test_where.cpython-310.pyc
ADDED
|
Binary file (809 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_constructors.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Index,
|
| 6 |
+
MultiIndex,
|
| 7 |
+
)
|
| 8 |
+
import pandas._testing as tm
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestIndexConstructor:
|
| 12 |
+
# Tests for the Index constructor, specifically for cases that do
|
| 13 |
+
# not return a subclass
|
| 14 |
+
|
| 15 |
+
@pytest.mark.parametrize("value", [1, np.int64(1)])
|
| 16 |
+
def test_constructor_corner(self, value):
|
| 17 |
+
# corner case
|
| 18 |
+
msg = (
|
| 19 |
+
r"Index\(\.\.\.\) must be called with a collection of some "
|
| 20 |
+
f"kind, {value} was passed"
|
| 21 |
+
)
|
| 22 |
+
with pytest.raises(TypeError, match=msg):
|
| 23 |
+
Index(value)
|
| 24 |
+
|
| 25 |
+
@pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]])
|
| 26 |
+
def test_construction_list_mixed_tuples(self, index_vals):
|
| 27 |
+
# see gh-10697: if we are constructing from a mixed list of tuples,
|
| 28 |
+
# make sure that we are independent of the sorting order.
|
| 29 |
+
index = Index(index_vals)
|
| 30 |
+
assert isinstance(index, Index)
|
| 31 |
+
assert not isinstance(index, MultiIndex)
|
| 32 |
+
|
| 33 |
+
def test_constructor_cast(self):
|
| 34 |
+
msg = "could not convert string to float"
|
| 35 |
+
with pytest.raises(ValueError, match=msg):
|
| 36 |
+
Index(["a", "b", "c"], dtype=float)
|
| 37 |
+
|
| 38 |
+
@pytest.mark.parametrize("tuple_list", [[()], [(), ()]])
|
| 39 |
+
def test_construct_empty_tuples(self, tuple_list):
|
| 40 |
+
# GH #45608
|
| 41 |
+
result = Index(tuple_list)
|
| 42 |
+
expected = MultiIndex.from_tuples(tuple_list)
|
| 43 |
+
|
| 44 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_formats.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas._config.config as cf
|
| 5 |
+
|
| 6 |
+
from pandas import Index
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestIndexRendering:
|
| 10 |
+
@pytest.mark.parametrize(
|
| 11 |
+
"index,expected",
|
| 12 |
+
[
|
| 13 |
+
# ASCII
|
| 14 |
+
# short
|
| 15 |
+
(
|
| 16 |
+
Index(["a", "bb", "ccc"]),
|
| 17 |
+
"""Index(['a', 'bb', 'ccc'], dtype='object')""",
|
| 18 |
+
),
|
| 19 |
+
# multiple lines
|
| 20 |
+
(
|
| 21 |
+
Index(["a", "bb", "ccc"] * 10),
|
| 22 |
+
"Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
|
| 23 |
+
"'bb', 'ccc', 'a', 'bb', 'ccc',\n"
|
| 24 |
+
" 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
|
| 25 |
+
"'bb', 'ccc', 'a', 'bb', 'ccc',\n"
|
| 26 |
+
" 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
|
| 27 |
+
" dtype='object')",
|
| 28 |
+
),
|
| 29 |
+
# truncated
|
| 30 |
+
(
|
| 31 |
+
Index(["a", "bb", "ccc"] * 100),
|
| 32 |
+
"Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',\n"
|
| 33 |
+
" ...\n"
|
| 34 |
+
" 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
|
| 35 |
+
" dtype='object', length=300)",
|
| 36 |
+
),
|
| 37 |
+
# Non-ASCII
|
| 38 |
+
# short
|
| 39 |
+
(
|
| 40 |
+
Index(["あ", "いい", "ううう"]),
|
| 41 |
+
"""Index(['あ', 'いい', 'ううう'], dtype='object')""",
|
| 42 |
+
),
|
| 43 |
+
# multiple lines
|
| 44 |
+
(
|
| 45 |
+
Index(["あ", "いい", "ううう"] * 10),
|
| 46 |
+
(
|
| 47 |
+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
| 48 |
+
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
|
| 49 |
+
" 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
| 50 |
+
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
|
| 51 |
+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 52 |
+
"'ううう'],\n"
|
| 53 |
+
" dtype='object')"
|
| 54 |
+
),
|
| 55 |
+
),
|
| 56 |
+
# truncated
|
| 57 |
+
(
|
| 58 |
+
Index(["あ", "いい", "ううう"] * 100),
|
| 59 |
+
(
|
| 60 |
+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
| 61 |
+
"'あ', 'いい', 'ううう', 'あ',\n"
|
| 62 |
+
" ...\n"
|
| 63 |
+
" 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 64 |
+
"'ううう', 'あ', 'いい', 'ううう'],\n"
|
| 65 |
+
" dtype='object', length=300)"
|
| 66 |
+
),
|
| 67 |
+
),
|
| 68 |
+
],
|
| 69 |
+
)
|
| 70 |
+
def test_string_index_repr(self, index, expected):
|
| 71 |
+
result = repr(index)
|
| 72 |
+
assert result == expected
|
| 73 |
+
|
| 74 |
+
@pytest.mark.parametrize(
|
| 75 |
+
"index,expected",
|
| 76 |
+
[
|
| 77 |
+
# short
|
| 78 |
+
(
|
| 79 |
+
Index(["あ", "いい", "ううう"]),
|
| 80 |
+
("Index(['あ', 'いい', 'ううう'], dtype='object')"),
|
| 81 |
+
),
|
| 82 |
+
# multiple lines
|
| 83 |
+
(
|
| 84 |
+
Index(["あ", "いい", "ううう"] * 10),
|
| 85 |
+
(
|
| 86 |
+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 87 |
+
"'ううう', 'あ', 'いい', 'ううう',\n"
|
| 88 |
+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 89 |
+
"'ううう', 'あ', 'いい', 'ううう',\n"
|
| 90 |
+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 91 |
+
"'ううう', 'あ', 'いい', 'ううう',\n"
|
| 92 |
+
" 'あ', 'いい', 'ううう'],\n"
|
| 93 |
+
" dtype='object')"
|
| 94 |
+
""
|
| 95 |
+
),
|
| 96 |
+
),
|
| 97 |
+
# truncated
|
| 98 |
+
(
|
| 99 |
+
Index(["あ", "いい", "ううう"] * 100),
|
| 100 |
+
(
|
| 101 |
+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 102 |
+
"'ううう', 'あ', 'いい', 'ううう',\n"
|
| 103 |
+
" 'あ',\n"
|
| 104 |
+
" ...\n"
|
| 105 |
+
" 'ううう', 'あ', 'いい', 'ううう', 'あ', "
|
| 106 |
+
"'いい', 'ううう', 'あ', 'いい',\n"
|
| 107 |
+
" 'ううう'],\n"
|
| 108 |
+
" dtype='object', length=300)"
|
| 109 |
+
),
|
| 110 |
+
),
|
| 111 |
+
],
|
| 112 |
+
)
|
| 113 |
+
def test_string_index_repr_with_unicode_option(self, index, expected):
|
| 114 |
+
# Enable Unicode option -----------------------------------------
|
| 115 |
+
with cf.option_context("display.unicode.east_asian_width", True):
|
| 116 |
+
result = repr(index)
|
| 117 |
+
assert result == expected
|
| 118 |
+
|
| 119 |
+
def test_repr_summary(self):
|
| 120 |
+
with cf.option_context("display.max_seq_items", 10):
|
| 121 |
+
result = repr(Index(np.arange(1000)))
|
| 122 |
+
assert len(result) < 200
|
| 123 |
+
assert "..." in result
|
| 124 |
+
|
| 125 |
+
def test_summary_bug(self):
|
| 126 |
+
# GH#3869
|
| 127 |
+
ind = Index(["{other}%s", "~:{range}:0"], name="A")
|
| 128 |
+
result = ind._summary()
|
| 129 |
+
# shouldn't be formatted accidentally.
|
| 130 |
+
assert "~:{range}:0" in result
|
| 131 |
+
assert "{other}%s" in result
|
| 132 |
+
|
| 133 |
+
def test_index_repr_bool_nan(self):
|
| 134 |
+
# GH32146
|
| 135 |
+
arr = Index([True, False, np.nan], dtype=object)
|
| 136 |
+
exp1 = arr.format()
|
| 137 |
+
out1 = ["True", "False", "NaN"]
|
| 138 |
+
assert out1 == exp1
|
| 139 |
+
|
| 140 |
+
exp2 = repr(arr)
|
| 141 |
+
out2 = "Index([True, False, nan], dtype='object')"
|
| 142 |
+
assert out2 == exp2
|
| 143 |
+
|
| 144 |
+
def test_format_different_scalar_lengths(self):
|
| 145 |
+
# GH#35439
|
| 146 |
+
idx = Index(["aaaaaaaaa", "b"])
|
| 147 |
+
expected = ["aaaaaaaaa", "b"]
|
| 148 |
+
assert idx.format() == expected
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_indexing.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pandas import (
|
| 6 |
+
Index,
|
| 7 |
+
NaT,
|
| 8 |
+
)
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestGetSliceBounds:
|
| 13 |
+
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
|
| 14 |
+
def test_get_slice_bounds_within(self, side, expected):
|
| 15 |
+
index = Index(list("abcdef"))
|
| 16 |
+
result = index.get_slice_bound("e", side=side)
|
| 17 |
+
assert result == expected
|
| 18 |
+
|
| 19 |
+
@pytest.mark.parametrize("side", ["left", "right"])
|
| 20 |
+
@pytest.mark.parametrize(
|
| 21 |
+
"data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0)]
|
| 22 |
+
)
|
| 23 |
+
def test_get_slice_bounds_outside(self, side, expected, data, bound):
|
| 24 |
+
index = Index(data)
|
| 25 |
+
result = index.get_slice_bound(bound, side=side)
|
| 26 |
+
assert result == expected
|
| 27 |
+
|
| 28 |
+
def test_get_slice_bounds_invalid_side(self):
|
| 29 |
+
with pytest.raises(ValueError, match="Invalid value for side kwarg"):
|
| 30 |
+
Index([]).get_slice_bound("a", side="middle")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class TestGetIndexerNonUnique:
|
| 34 |
+
def test_get_indexer_non_unique_dtype_mismatch(self):
|
| 35 |
+
# GH#25459
|
| 36 |
+
indexes, missing = Index(["A", "B"]).get_indexer_non_unique(Index([0]))
|
| 37 |
+
tm.assert_numpy_array_equal(np.array([-1], dtype=np.intp), indexes)
|
| 38 |
+
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), missing)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class TestGetLoc:
|
| 42 |
+
@pytest.mark.slow # to_flat_index takes a while
|
| 43 |
+
def test_get_loc_tuple_monotonic_above_size_cutoff(self):
|
| 44 |
+
# Go through the libindex path for which using
|
| 45 |
+
# _bin_search vs ndarray.searchsorted makes a difference
|
| 46 |
+
|
| 47 |
+
lev = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
| 48 |
+
dti = pd.date_range("2016-01-01", periods=100)
|
| 49 |
+
|
| 50 |
+
mi = pd.MultiIndex.from_product([lev, range(10**3), dti])
|
| 51 |
+
oidx = mi.to_flat_index()
|
| 52 |
+
|
| 53 |
+
loc = len(oidx) // 2
|
| 54 |
+
tup = oidx[loc]
|
| 55 |
+
|
| 56 |
+
res = oidx.get_loc(tup)
|
| 57 |
+
assert res == loc
|
| 58 |
+
|
| 59 |
+
def test_get_loc_nan_object_dtype_nonmonotonic_nonunique(self):
|
| 60 |
+
# case that goes through _maybe_get_bool_indexer
|
| 61 |
+
idx = Index(["foo", np.nan, None, "foo", 1.0, None], dtype=object)
|
| 62 |
+
|
| 63 |
+
# we dont raise KeyError on nan
|
| 64 |
+
res = idx.get_loc(np.nan)
|
| 65 |
+
assert res == 1
|
| 66 |
+
|
| 67 |
+
# we only match on None, not on np.nan
|
| 68 |
+
res = idx.get_loc(None)
|
| 69 |
+
expected = np.array([False, False, True, False, False, True])
|
| 70 |
+
tm.assert_numpy_array_equal(res, expected)
|
| 71 |
+
|
| 72 |
+
# we don't match at all on mismatched NA
|
| 73 |
+
with pytest.raises(KeyError, match="NaT"):
|
| 74 |
+
idx.get_loc(NaT)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def test_getitem_boolean_ea_indexer():
|
| 78 |
+
# GH#45806
|
| 79 |
+
ser = pd.Series([True, False, pd.NA], dtype="boolean")
|
| 80 |
+
result = ser.index[ser]
|
| 81 |
+
expected = Index([0])
|
| 82 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_pickle.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas import Index
|
| 2 |
+
import pandas._testing as tm
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def test_pickle_preserves_object_dtype():
|
| 6 |
+
# GH#43188, GH#43155 don't infer numeric dtype
|
| 7 |
+
index = Index([1, 2, 3], dtype=object)
|
| 8 |
+
|
| 9 |
+
result = tm.round_trip_pickle(index)
|
| 10 |
+
assert result.dtype == object
|
| 11 |
+
tm.assert_index_equal(index, result)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_where.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from pandas import Index
|
| 4 |
+
import pandas._testing as tm
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestWhere:
|
| 8 |
+
def test_where_intlike_str_doesnt_cast_ints(self):
|
| 9 |
+
idx = Index(range(3))
|
| 10 |
+
mask = np.array([True, False, True])
|
| 11 |
+
res = idx.where(mask, "2")
|
| 12 |
+
expected = Index([0, "2", 2])
|
| 13 |
+
tm.assert_index_equal(res, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (185 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_astype.cpython-310.pyc
ADDED
|
Binary file (8.47 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_base.cpython-310.pyc
ADDED
|
Binary file (2.74 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_constructors.cpython-310.pyc
ADDED
|
Binary file (16.1 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_equals.cpython-310.pyc
ADDED
|
Binary file (1.17 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_formats.cpython-310.pyc
ADDED
|
Binary file (3.05 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (18.1 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_interval.cpython-310.pyc
ADDED
|
Binary file (24.4 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_interval_range.cpython-310.pyc
ADDED
|
Binary file (8.23 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_interval_tree.cpython-310.pyc
ADDED
|
Binary file (7.32 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_join.cpython-310.pyc
ADDED
|
Binary file (1.38 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/__pycache__/test_pickle.cpython-310.pyc
ADDED
|
Binary file (841 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/ind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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_base.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import IntervalIndex
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
from pandas.tests.indexes.common import Base
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestBase(Base):
|
| 10 |
+
"""
|
| 11 |
+
Tests specific to the shared common index tests; unrelated tests should be placed
|
| 12 |
+
in test_interval.py or the specific test file (e.g. test_astype.py)
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
_index_cls = IntervalIndex
|
| 16 |
+
|
| 17 |
+
@pytest.fixture
|
| 18 |
+
def simple_index(self) -> IntervalIndex:
|
| 19 |
+
return self._index_cls.from_breaks(range(11), closed="right")
|
| 20 |
+
|
| 21 |
+
@pytest.fixture
|
| 22 |
+
def index(self):
|
| 23 |
+
return tm.makeIntervalIndex(10)
|
| 24 |
+
|
| 25 |
+
def create_index(self, *, closed="right"):
|
| 26 |
+
return IntervalIndex.from_breaks(range(11), closed=closed)
|
| 27 |
+
|
| 28 |
+
def test_repr_max_seq_item_setting(self):
|
| 29 |
+
# override base test: not a valid repr as we use interval notation
|
| 30 |
+
pass
|
| 31 |
+
|
| 32 |
+
def test_repr_roundtrip(self):
|
| 33 |
+
# override base test: not a valid repr as we use interval notation
|
| 34 |
+
pass
|
| 35 |
+
|
| 36 |
+
def test_take(self, closed):
|
| 37 |
+
index = self.create_index(closed=closed)
|
| 38 |
+
|
| 39 |
+
result = index.take(range(10))
|
| 40 |
+
tm.assert_index_equal(result, index)
|
| 41 |
+
|
| 42 |
+
result = index.take([0, 0, 1])
|
| 43 |
+
expected = IntervalIndex.from_arrays([0, 0, 1], [1, 1, 2], closed=closed)
|
| 44 |
+
tm.assert_index_equal(result, expected)
|
| 45 |
+
|
| 46 |
+
def test_where(self, simple_index, listlike_box):
|
| 47 |
+
klass = listlike_box
|
| 48 |
+
|
| 49 |
+
idx = simple_index
|
| 50 |
+
cond = [True] * len(idx)
|
| 51 |
+
expected = idx
|
| 52 |
+
result = expected.where(klass(cond))
|
| 53 |
+
tm.assert_index_equal(result, expected)
|
| 54 |
+
|
| 55 |
+
cond = [False] + [True] * len(idx[1:])
|
| 56 |
+
expected = IntervalIndex([np.nan] + idx[1:].tolist())
|
| 57 |
+
result = idx.where(klass(cond))
|
| 58 |
+
tm.assert_index_equal(result, expected)
|
| 59 |
+
|
| 60 |
+
def test_getitem_2d_deprecated(self, simple_index):
|
| 61 |
+
# GH#30588 multi-dim indexing is deprecated, but raising is also acceptable
|
| 62 |
+
idx = simple_index
|
| 63 |
+
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
|
| 64 |
+
idx[:, None]
|
| 65 |
+
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
|
| 66 |
+
# GH#44051
|
| 67 |
+
idx[True]
|
| 68 |
+
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
|
| 69 |
+
# GH#44051
|
| 70 |
+
idx[False]
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_constructors.py
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functools import partial
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas.core.dtypes.common import is_categorical_dtype
|
| 7 |
+
from pandas.core.dtypes.dtypes import IntervalDtype
|
| 8 |
+
|
| 9 |
+
from pandas import (
|
| 10 |
+
Categorical,
|
| 11 |
+
CategoricalIndex,
|
| 12 |
+
Index,
|
| 13 |
+
Interval,
|
| 14 |
+
IntervalIndex,
|
| 15 |
+
date_range,
|
| 16 |
+
notna,
|
| 17 |
+
period_range,
|
| 18 |
+
timedelta_range,
|
| 19 |
+
)
|
| 20 |
+
import pandas._testing as tm
|
| 21 |
+
from pandas.core.arrays import IntervalArray
|
| 22 |
+
import pandas.core.common as com
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@pytest.fixture(params=[None, "foo"])
|
| 26 |
+
def name(request):
|
| 27 |
+
return request.param
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class ConstructorTests:
|
| 31 |
+
"""
|
| 32 |
+
Common tests for all variations of IntervalIndex construction. Input data
|
| 33 |
+
to be supplied in breaks format, then converted by the subclass method
|
| 34 |
+
get_kwargs_from_breaks to the expected format.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
@pytest.fixture(
|
| 38 |
+
params=[
|
| 39 |
+
([3, 14, 15, 92, 653], np.int64),
|
| 40 |
+
(np.arange(10, dtype="int64"), np.int64),
|
| 41 |
+
(Index(np.arange(-10, 11, dtype=np.int64)), np.int64),
|
| 42 |
+
(Index(np.arange(10, 31, dtype=np.uint64)), np.uint64),
|
| 43 |
+
(Index(np.arange(20, 30, 0.5), dtype=np.float64), np.float64),
|
| 44 |
+
(date_range("20180101", periods=10), "<M8[ns]"),
|
| 45 |
+
(
|
| 46 |
+
date_range("20180101", periods=10, tz="US/Eastern"),
|
| 47 |
+
"datetime64[ns, US/Eastern]",
|
| 48 |
+
),
|
| 49 |
+
(timedelta_range("1 day", periods=10), "<m8[ns]"),
|
| 50 |
+
]
|
| 51 |
+
)
|
| 52 |
+
def breaks_and_expected_subtype(self, request):
|
| 53 |
+
return request.param
|
| 54 |
+
|
| 55 |
+
def test_constructor(self, constructor, breaks_and_expected_subtype, closed, name):
|
| 56 |
+
breaks, expected_subtype = breaks_and_expected_subtype
|
| 57 |
+
|
| 58 |
+
result_kwargs = self.get_kwargs_from_breaks(breaks, closed)
|
| 59 |
+
|
| 60 |
+
result = constructor(closed=closed, name=name, **result_kwargs)
|
| 61 |
+
|
| 62 |
+
assert result.closed == closed
|
| 63 |
+
assert result.name == name
|
| 64 |
+
assert result.dtype.subtype == expected_subtype
|
| 65 |
+
tm.assert_index_equal(result.left, Index(breaks[:-1], dtype=expected_subtype))
|
| 66 |
+
tm.assert_index_equal(result.right, Index(breaks[1:], dtype=expected_subtype))
|
| 67 |
+
|
| 68 |
+
@pytest.mark.parametrize(
|
| 69 |
+
"breaks, subtype",
|
| 70 |
+
[
|
| 71 |
+
(Index([0, 1, 2, 3, 4], dtype=np.int64), "float64"),
|
| 72 |
+
(Index([0, 1, 2, 3, 4], dtype=np.int64), "datetime64[ns]"),
|
| 73 |
+
(Index([0, 1, 2, 3, 4], dtype=np.int64), "timedelta64[ns]"),
|
| 74 |
+
(Index([0, 1, 2, 3, 4], dtype=np.float64), "int64"),
|
| 75 |
+
(date_range("2017-01-01", periods=5), "int64"),
|
| 76 |
+
(timedelta_range("1 day", periods=5), "int64"),
|
| 77 |
+
],
|
| 78 |
+
)
|
| 79 |
+
def test_constructor_dtype(self, constructor, breaks, subtype):
|
| 80 |
+
# GH 19262: conversion via dtype parameter
|
| 81 |
+
expected_kwargs = self.get_kwargs_from_breaks(breaks.astype(subtype))
|
| 82 |
+
expected = constructor(**expected_kwargs)
|
| 83 |
+
|
| 84 |
+
result_kwargs = self.get_kwargs_from_breaks(breaks)
|
| 85 |
+
iv_dtype = IntervalDtype(subtype, "right")
|
| 86 |
+
for dtype in (iv_dtype, str(iv_dtype)):
|
| 87 |
+
result = constructor(dtype=dtype, **result_kwargs)
|
| 88 |
+
tm.assert_index_equal(result, expected)
|
| 89 |
+
|
| 90 |
+
@pytest.mark.parametrize(
|
| 91 |
+
"breaks",
|
| 92 |
+
[
|
| 93 |
+
Index([0, 1, 2, 3, 4], dtype=np.int64),
|
| 94 |
+
Index([0, 1, 2, 3, 4], dtype=np.uint64),
|
| 95 |
+
Index([0, 1, 2, 3, 4], dtype=np.float64),
|
| 96 |
+
date_range("2017-01-01", periods=5),
|
| 97 |
+
timedelta_range("1 day", periods=5),
|
| 98 |
+
],
|
| 99 |
+
)
|
| 100 |
+
def test_constructor_pass_closed(self, constructor, breaks):
|
| 101 |
+
# not passing closed to IntervalDtype, but to IntervalArray constructor
|
| 102 |
+
iv_dtype = IntervalDtype(breaks.dtype)
|
| 103 |
+
|
| 104 |
+
result_kwargs = self.get_kwargs_from_breaks(breaks)
|
| 105 |
+
|
| 106 |
+
for dtype in (iv_dtype, str(iv_dtype)):
|
| 107 |
+
with tm.assert_produces_warning(None):
|
| 108 |
+
result = constructor(dtype=dtype, closed="left", **result_kwargs)
|
| 109 |
+
assert result.dtype.closed == "left"
|
| 110 |
+
|
| 111 |
+
@pytest.mark.parametrize("breaks", [[np.nan] * 2, [np.nan] * 4, [np.nan] * 50])
|
| 112 |
+
def test_constructor_nan(self, constructor, breaks, closed):
|
| 113 |
+
# GH 18421
|
| 114 |
+
result_kwargs = self.get_kwargs_from_breaks(breaks)
|
| 115 |
+
result = constructor(closed=closed, **result_kwargs)
|
| 116 |
+
|
| 117 |
+
expected_subtype = np.float64
|
| 118 |
+
expected_values = np.array(breaks[:-1], dtype=object)
|
| 119 |
+
|
| 120 |
+
assert result.closed == closed
|
| 121 |
+
assert result.dtype.subtype == expected_subtype
|
| 122 |
+
tm.assert_numpy_array_equal(np.array(result), expected_values)
|
| 123 |
+
|
| 124 |
+
@pytest.mark.parametrize(
|
| 125 |
+
"breaks",
|
| 126 |
+
[
|
| 127 |
+
[],
|
| 128 |
+
np.array([], dtype="int64"),
|
| 129 |
+
np.array([], dtype="uint64"),
|
| 130 |
+
np.array([], dtype="float64"),
|
| 131 |
+
np.array([], dtype="datetime64[ns]"),
|
| 132 |
+
np.array([], dtype="timedelta64[ns]"),
|
| 133 |
+
],
|
| 134 |
+
)
|
| 135 |
+
def test_constructor_empty(self, constructor, breaks, closed):
|
| 136 |
+
# GH 18421
|
| 137 |
+
result_kwargs = self.get_kwargs_from_breaks(breaks)
|
| 138 |
+
result = constructor(closed=closed, **result_kwargs)
|
| 139 |
+
|
| 140 |
+
expected_values = np.array([], dtype=object)
|
| 141 |
+
expected_subtype = getattr(breaks, "dtype", np.int64)
|
| 142 |
+
|
| 143 |
+
assert result.empty
|
| 144 |
+
assert result.closed == closed
|
| 145 |
+
assert result.dtype.subtype == expected_subtype
|
| 146 |
+
tm.assert_numpy_array_equal(np.array(result), expected_values)
|
| 147 |
+
|
| 148 |
+
@pytest.mark.parametrize(
|
| 149 |
+
"breaks",
|
| 150 |
+
[
|
| 151 |
+
tuple("0123456789"),
|
| 152 |
+
list("abcdefghij"),
|
| 153 |
+
np.array(list("abcdefghij"), dtype=object),
|
| 154 |
+
np.array(list("abcdefghij"), dtype="<U1"),
|
| 155 |
+
],
|
| 156 |
+
)
|
| 157 |
+
def test_constructor_string(self, constructor, breaks):
|
| 158 |
+
# GH 19016
|
| 159 |
+
msg = (
|
| 160 |
+
"category, object, and string subtypes are not supported "
|
| 161 |
+
"for IntervalIndex"
|
| 162 |
+
)
|
| 163 |
+
with pytest.raises(TypeError, match=msg):
|
| 164 |
+
constructor(**self.get_kwargs_from_breaks(breaks))
|
| 165 |
+
|
| 166 |
+
@pytest.mark.parametrize("cat_constructor", [Categorical, CategoricalIndex])
|
| 167 |
+
def test_constructor_categorical_valid(self, constructor, cat_constructor):
|
| 168 |
+
# GH 21243/21253
|
| 169 |
+
|
| 170 |
+
breaks = np.arange(10, dtype="int64")
|
| 171 |
+
expected = IntervalIndex.from_breaks(breaks)
|
| 172 |
+
|
| 173 |
+
cat_breaks = cat_constructor(breaks)
|
| 174 |
+
result_kwargs = self.get_kwargs_from_breaks(cat_breaks)
|
| 175 |
+
result = constructor(**result_kwargs)
|
| 176 |
+
tm.assert_index_equal(result, expected)
|
| 177 |
+
|
| 178 |
+
def test_generic_errors(self, constructor):
|
| 179 |
+
# filler input data to be used when supplying invalid kwargs
|
| 180 |
+
filler = self.get_kwargs_from_breaks(range(10))
|
| 181 |
+
|
| 182 |
+
# invalid closed
|
| 183 |
+
msg = "closed must be one of 'right', 'left', 'both', 'neither'"
|
| 184 |
+
with pytest.raises(ValueError, match=msg):
|
| 185 |
+
constructor(closed="invalid", **filler)
|
| 186 |
+
|
| 187 |
+
# unsupported dtype
|
| 188 |
+
msg = "dtype must be an IntervalDtype, got int64"
|
| 189 |
+
with pytest.raises(TypeError, match=msg):
|
| 190 |
+
constructor(dtype="int64", **filler)
|
| 191 |
+
|
| 192 |
+
# invalid dtype
|
| 193 |
+
msg = "data type [\"']invalid[\"'] not understood"
|
| 194 |
+
with pytest.raises(TypeError, match=msg):
|
| 195 |
+
constructor(dtype="invalid", **filler)
|
| 196 |
+
|
| 197 |
+
# no point in nesting periods in an IntervalIndex
|
| 198 |
+
periods = period_range("2000-01-01", periods=10)
|
| 199 |
+
periods_kwargs = self.get_kwargs_from_breaks(periods)
|
| 200 |
+
msg = "Period dtypes are not supported, use a PeriodIndex instead"
|
| 201 |
+
with pytest.raises(ValueError, match=msg):
|
| 202 |
+
constructor(**periods_kwargs)
|
| 203 |
+
|
| 204 |
+
# decreasing values
|
| 205 |
+
decreasing_kwargs = self.get_kwargs_from_breaks(range(10, -1, -1))
|
| 206 |
+
msg = "left side of interval must be <= right side"
|
| 207 |
+
with pytest.raises(ValueError, match=msg):
|
| 208 |
+
constructor(**decreasing_kwargs)
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
class TestFromArrays(ConstructorTests):
|
| 212 |
+
"""Tests specific to IntervalIndex.from_arrays"""
|
| 213 |
+
|
| 214 |
+
@pytest.fixture
|
| 215 |
+
def constructor(self):
|
| 216 |
+
return IntervalIndex.from_arrays
|
| 217 |
+
|
| 218 |
+
def get_kwargs_from_breaks(self, breaks, closed="right"):
|
| 219 |
+
"""
|
| 220 |
+
converts intervals in breaks format to a dictionary of kwargs to
|
| 221 |
+
specific to the format expected by IntervalIndex.from_arrays
|
| 222 |
+
"""
|
| 223 |
+
return {"left": breaks[:-1], "right": breaks[1:]}
|
| 224 |
+
|
| 225 |
+
def test_constructor_errors(self):
|
| 226 |
+
# GH 19016: categorical data
|
| 227 |
+
data = Categorical(list("01234abcde"), ordered=True)
|
| 228 |
+
msg = (
|
| 229 |
+
"category, object, and string subtypes are not supported "
|
| 230 |
+
"for IntervalIndex"
|
| 231 |
+
)
|
| 232 |
+
with pytest.raises(TypeError, match=msg):
|
| 233 |
+
IntervalIndex.from_arrays(data[:-1], data[1:])
|
| 234 |
+
|
| 235 |
+
# unequal length
|
| 236 |
+
left = [0, 1, 2]
|
| 237 |
+
right = [2, 3]
|
| 238 |
+
msg = "left and right must have the same length"
|
| 239 |
+
with pytest.raises(ValueError, match=msg):
|
| 240 |
+
IntervalIndex.from_arrays(left, right)
|
| 241 |
+
|
| 242 |
+
@pytest.mark.parametrize(
|
| 243 |
+
"left_subtype, right_subtype", [(np.int64, np.float64), (np.float64, np.int64)]
|
| 244 |
+
)
|
| 245 |
+
def test_mixed_float_int(self, left_subtype, right_subtype):
|
| 246 |
+
"""mixed int/float left/right results in float for both sides"""
|
| 247 |
+
left = np.arange(9, dtype=left_subtype)
|
| 248 |
+
right = np.arange(1, 10, dtype=right_subtype)
|
| 249 |
+
result = IntervalIndex.from_arrays(left, right)
|
| 250 |
+
|
| 251 |
+
expected_left = Index(left, dtype=np.float64)
|
| 252 |
+
expected_right = Index(right, dtype=np.float64)
|
| 253 |
+
expected_subtype = np.float64
|
| 254 |
+
|
| 255 |
+
tm.assert_index_equal(result.left, expected_left)
|
| 256 |
+
tm.assert_index_equal(result.right, expected_right)
|
| 257 |
+
assert result.dtype.subtype == expected_subtype
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
class TestFromBreaks(ConstructorTests):
|
| 261 |
+
"""Tests specific to IntervalIndex.from_breaks"""
|
| 262 |
+
|
| 263 |
+
@pytest.fixture
|
| 264 |
+
def constructor(self):
|
| 265 |
+
return IntervalIndex.from_breaks
|
| 266 |
+
|
| 267 |
+
def get_kwargs_from_breaks(self, breaks, closed="right"):
|
| 268 |
+
"""
|
| 269 |
+
converts intervals in breaks format to a dictionary of kwargs to
|
| 270 |
+
specific to the format expected by IntervalIndex.from_breaks
|
| 271 |
+
"""
|
| 272 |
+
return {"breaks": breaks}
|
| 273 |
+
|
| 274 |
+
def test_constructor_errors(self):
|
| 275 |
+
# GH 19016: categorical data
|
| 276 |
+
data = Categorical(list("01234abcde"), ordered=True)
|
| 277 |
+
msg = (
|
| 278 |
+
"category, object, and string subtypes are not supported "
|
| 279 |
+
"for IntervalIndex"
|
| 280 |
+
)
|
| 281 |
+
with pytest.raises(TypeError, match=msg):
|
| 282 |
+
IntervalIndex.from_breaks(data)
|
| 283 |
+
|
| 284 |
+
def test_length_one(self):
|
| 285 |
+
"""breaks of length one produce an empty IntervalIndex"""
|
| 286 |
+
breaks = [0]
|
| 287 |
+
result = IntervalIndex.from_breaks(breaks)
|
| 288 |
+
expected = IntervalIndex.from_breaks([])
|
| 289 |
+
tm.assert_index_equal(result, expected)
|
| 290 |
+
|
| 291 |
+
def test_left_right_dont_share_data(self):
|
| 292 |
+
# GH#36310
|
| 293 |
+
breaks = np.arange(5)
|
| 294 |
+
result = IntervalIndex.from_breaks(breaks)._data
|
| 295 |
+
assert result._left.base is None or result._left.base is not result._right.base
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
class TestFromTuples(ConstructorTests):
|
| 299 |
+
"""Tests specific to IntervalIndex.from_tuples"""
|
| 300 |
+
|
| 301 |
+
@pytest.fixture
|
| 302 |
+
def constructor(self):
|
| 303 |
+
return IntervalIndex.from_tuples
|
| 304 |
+
|
| 305 |
+
def get_kwargs_from_breaks(self, breaks, closed="right"):
|
| 306 |
+
"""
|
| 307 |
+
converts intervals in breaks format to a dictionary of kwargs to
|
| 308 |
+
specific to the format expected by IntervalIndex.from_tuples
|
| 309 |
+
"""
|
| 310 |
+
if tm.is_unsigned_integer_dtype(breaks):
|
| 311 |
+
pytest.skip(f"{breaks.dtype} not relevant IntervalIndex.from_tuples tests")
|
| 312 |
+
|
| 313 |
+
if len(breaks) == 0:
|
| 314 |
+
return {"data": breaks}
|
| 315 |
+
|
| 316 |
+
tuples = list(zip(breaks[:-1], breaks[1:]))
|
| 317 |
+
if isinstance(breaks, (list, tuple)):
|
| 318 |
+
return {"data": tuples}
|
| 319 |
+
elif is_categorical_dtype(breaks):
|
| 320 |
+
return {"data": breaks._constructor(tuples)}
|
| 321 |
+
return {"data": com.asarray_tuplesafe(tuples)}
|
| 322 |
+
|
| 323 |
+
def test_constructor_errors(self):
|
| 324 |
+
# non-tuple
|
| 325 |
+
tuples = [(0, 1), 2, (3, 4)]
|
| 326 |
+
msg = "IntervalIndex.from_tuples received an invalid item, 2"
|
| 327 |
+
with pytest.raises(TypeError, match=msg.format(t=tuples)):
|
| 328 |
+
IntervalIndex.from_tuples(tuples)
|
| 329 |
+
|
| 330 |
+
# too few/many items
|
| 331 |
+
tuples = [(0, 1), (2,), (3, 4)]
|
| 332 |
+
msg = "IntervalIndex.from_tuples requires tuples of length 2, got {t}"
|
| 333 |
+
with pytest.raises(ValueError, match=msg.format(t=tuples)):
|
| 334 |
+
IntervalIndex.from_tuples(tuples)
|
| 335 |
+
|
| 336 |
+
tuples = [(0, 1), (2, 3, 4), (5, 6)]
|
| 337 |
+
with pytest.raises(ValueError, match=msg.format(t=tuples)):
|
| 338 |
+
IntervalIndex.from_tuples(tuples)
|
| 339 |
+
|
| 340 |
+
def test_na_tuples(self):
|
| 341 |
+
# tuple (NA, NA) evaluates the same as NA as an element
|
| 342 |
+
na_tuple = [(0, 1), (np.nan, np.nan), (2, 3)]
|
| 343 |
+
idx_na_tuple = IntervalIndex.from_tuples(na_tuple)
|
| 344 |
+
idx_na_element = IntervalIndex.from_tuples([(0, 1), np.nan, (2, 3)])
|
| 345 |
+
tm.assert_index_equal(idx_na_tuple, idx_na_element)
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
class TestClassConstructors(ConstructorTests):
|
| 349 |
+
"""Tests specific to the IntervalIndex/Index constructors"""
|
| 350 |
+
|
| 351 |
+
@pytest.fixture(
|
| 352 |
+
params=[IntervalIndex, partial(Index, dtype="interval")],
|
| 353 |
+
ids=["IntervalIndex", "Index"],
|
| 354 |
+
)
|
| 355 |
+
def klass(self, request):
|
| 356 |
+
# We use a separate fixture here to include Index.__new__ with dtype kwarg
|
| 357 |
+
return request.param
|
| 358 |
+
|
| 359 |
+
@pytest.fixture
|
| 360 |
+
def constructor(self):
|
| 361 |
+
return IntervalIndex
|
| 362 |
+
|
| 363 |
+
def get_kwargs_from_breaks(self, breaks, closed="right"):
|
| 364 |
+
"""
|
| 365 |
+
converts intervals in breaks format to a dictionary of kwargs to
|
| 366 |
+
specific to the format expected by the IntervalIndex/Index constructors
|
| 367 |
+
"""
|
| 368 |
+
if tm.is_unsigned_integer_dtype(breaks):
|
| 369 |
+
pytest.skip(f"{breaks.dtype} not relevant for class constructor tests")
|
| 370 |
+
|
| 371 |
+
if len(breaks) == 0:
|
| 372 |
+
return {"data": breaks}
|
| 373 |
+
|
| 374 |
+
ivs = [
|
| 375 |
+
Interval(left, right, closed) if notna(left) else left
|
| 376 |
+
for left, right in zip(breaks[:-1], breaks[1:])
|
| 377 |
+
]
|
| 378 |
+
|
| 379 |
+
if isinstance(breaks, list):
|
| 380 |
+
return {"data": ivs}
|
| 381 |
+
elif is_categorical_dtype(breaks):
|
| 382 |
+
return {"data": breaks._constructor(ivs)}
|
| 383 |
+
return {"data": np.array(ivs, dtype=object)}
|
| 384 |
+
|
| 385 |
+
def test_generic_errors(self, constructor):
|
| 386 |
+
"""
|
| 387 |
+
override the base class implementation since errors are handled
|
| 388 |
+
differently; checks unnecessary since caught at the Interval level
|
| 389 |
+
"""
|
| 390 |
+
|
| 391 |
+
def test_constructor_string(self):
|
| 392 |
+
# GH23013
|
| 393 |
+
# When forming the interval from breaks,
|
| 394 |
+
# the interval of strings is already forbidden.
|
| 395 |
+
pass
|
| 396 |
+
|
| 397 |
+
def test_constructor_errors(self, klass):
|
| 398 |
+
# mismatched closed within intervals with no constructor override
|
| 399 |
+
ivs = [Interval(0, 1, closed="right"), Interval(2, 3, closed="left")]
|
| 400 |
+
msg = "intervals must all be closed on the same side"
|
| 401 |
+
with pytest.raises(ValueError, match=msg):
|
| 402 |
+
klass(ivs)
|
| 403 |
+
|
| 404 |
+
# scalar
|
| 405 |
+
msg = (
|
| 406 |
+
r"(IntervalIndex|Index)\(...\) must be called with a collection of "
|
| 407 |
+
"some kind, 5 was passed"
|
| 408 |
+
)
|
| 409 |
+
with pytest.raises(TypeError, match=msg):
|
| 410 |
+
klass(5)
|
| 411 |
+
|
| 412 |
+
# not an interval; dtype depends on 32bit/windows builds
|
| 413 |
+
msg = "type <class 'numpy.int(32|64)'> with value 0 is not an interval"
|
| 414 |
+
with pytest.raises(TypeError, match=msg):
|
| 415 |
+
klass([0, 1])
|
| 416 |
+
|
| 417 |
+
@pytest.mark.parametrize(
|
| 418 |
+
"data, closed",
|
| 419 |
+
[
|
| 420 |
+
([], "both"),
|
| 421 |
+
([np.nan, np.nan], "neither"),
|
| 422 |
+
(
|
| 423 |
+
[Interval(0, 3, closed="neither"), Interval(2, 5, closed="neither")],
|
| 424 |
+
"left",
|
| 425 |
+
),
|
| 426 |
+
(
|
| 427 |
+
[Interval(0, 3, closed="left"), Interval(2, 5, closed="right")],
|
| 428 |
+
"neither",
|
| 429 |
+
),
|
| 430 |
+
(IntervalIndex.from_breaks(range(5), closed="both"), "right"),
|
| 431 |
+
],
|
| 432 |
+
)
|
| 433 |
+
def test_override_inferred_closed(self, constructor, data, closed):
|
| 434 |
+
# GH 19370
|
| 435 |
+
if isinstance(data, IntervalIndex):
|
| 436 |
+
tuples = data.to_tuples()
|
| 437 |
+
else:
|
| 438 |
+
tuples = [(iv.left, iv.right) if notna(iv) else iv for iv in data]
|
| 439 |
+
expected = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 440 |
+
result = constructor(data, closed=closed)
|
| 441 |
+
tm.assert_index_equal(result, expected)
|
| 442 |
+
|
| 443 |
+
@pytest.mark.parametrize(
|
| 444 |
+
"values_constructor", [list, np.array, IntervalIndex, IntervalArray]
|
| 445 |
+
)
|
| 446 |
+
def test_index_object_dtype(self, values_constructor):
|
| 447 |
+
# Index(intervals, dtype=object) is an Index (not an IntervalIndex)
|
| 448 |
+
intervals = [Interval(0, 1), Interval(1, 2), Interval(2, 3)]
|
| 449 |
+
values = values_constructor(intervals)
|
| 450 |
+
result = Index(values, dtype=object)
|
| 451 |
+
|
| 452 |
+
assert type(result) is Index
|
| 453 |
+
tm.assert_numpy_array_equal(result.values, np.array(values))
|
| 454 |
+
|
| 455 |
+
def test_index_mixed_closed(self):
|
| 456 |
+
# GH27172
|
| 457 |
+
intervals = [
|
| 458 |
+
Interval(0, 1, closed="left"),
|
| 459 |
+
Interval(1, 2, closed="right"),
|
| 460 |
+
Interval(2, 3, closed="neither"),
|
| 461 |
+
Interval(3, 4, closed="both"),
|
| 462 |
+
]
|
| 463 |
+
result = Index(intervals)
|
| 464 |
+
expected = Index(intervals, dtype=object)
|
| 465 |
+
tm.assert_index_equal(result, expected)
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
def test_dtype_closed_mismatch():
|
| 469 |
+
# GH#38394 closed specified in both dtype and IntervalIndex constructor
|
| 470 |
+
|
| 471 |
+
dtype = IntervalDtype(np.int64, "left")
|
| 472 |
+
|
| 473 |
+
msg = "closed keyword does not match dtype.closed"
|
| 474 |
+
with pytest.raises(ValueError, match=msg):
|
| 475 |
+
IntervalIndex([], dtype=dtype, closed="neither")
|
| 476 |
+
|
| 477 |
+
with pytest.raises(ValueError, match=msg):
|
| 478 |
+
IntervalArray([], dtype=dtype, closed="neither")
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_equals.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
IntervalIndex,
|
| 5 |
+
date_range,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestEquals:
|
| 10 |
+
def test_equals(self, closed):
|
| 11 |
+
expected = IntervalIndex.from_breaks(np.arange(5), closed=closed)
|
| 12 |
+
assert expected.equals(expected)
|
| 13 |
+
assert expected.equals(expected.copy())
|
| 14 |
+
|
| 15 |
+
assert not expected.equals(expected.astype(object))
|
| 16 |
+
assert not expected.equals(np.array(expected))
|
| 17 |
+
assert not expected.equals(list(expected))
|
| 18 |
+
|
| 19 |
+
assert not expected.equals([1, 2])
|
| 20 |
+
assert not expected.equals(np.array([1, 2]))
|
| 21 |
+
assert not expected.equals(date_range("20130101", periods=2))
|
| 22 |
+
|
| 23 |
+
expected_name1 = IntervalIndex.from_breaks(
|
| 24 |
+
np.arange(5), closed=closed, name="foo"
|
| 25 |
+
)
|
| 26 |
+
expected_name2 = IntervalIndex.from_breaks(
|
| 27 |
+
np.arange(5), closed=closed, name="bar"
|
| 28 |
+
)
|
| 29 |
+
assert expected.equals(expected_name1)
|
| 30 |
+
assert expected_name1.equals(expected_name2)
|
| 31 |
+
|
| 32 |
+
for other_closed in {"left", "right", "both", "neither"} - {closed}:
|
| 33 |
+
expected_other_closed = IntervalIndex.from_breaks(
|
| 34 |
+
np.arange(5), closed=other_closed
|
| 35 |
+
)
|
| 36 |
+
assert not expected.equals(expected_other_closed)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_formats.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
DataFrame,
|
| 6 |
+
Index,
|
| 7 |
+
Interval,
|
| 8 |
+
IntervalIndex,
|
| 9 |
+
Series,
|
| 10 |
+
Timedelta,
|
| 11 |
+
Timestamp,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestIntervalIndexRendering:
|
| 17 |
+
def test_frame_repr(self):
|
| 18 |
+
# https://github.com/pandas-dev/pandas/pull/24134/files
|
| 19 |
+
df = DataFrame(
|
| 20 |
+
{"A": [1, 2, 3, 4]}, index=IntervalIndex.from_breaks([0, 1, 2, 3, 4])
|
| 21 |
+
)
|
| 22 |
+
result = repr(df)
|
| 23 |
+
expected = " A\n(0, 1] 1\n(1, 2] 2\n(2, 3] 3\n(3, 4] 4"
|
| 24 |
+
assert result == expected
|
| 25 |
+
|
| 26 |
+
@pytest.mark.parametrize(
|
| 27 |
+
"constructor,expected",
|
| 28 |
+
[
|
| 29 |
+
(
|
| 30 |
+
Series,
|
| 31 |
+
(
|
| 32 |
+
"(0.0, 1.0] a\n"
|
| 33 |
+
"NaN b\n"
|
| 34 |
+
"(2.0, 3.0] c\n"
|
| 35 |
+
"dtype: object"
|
| 36 |
+
),
|
| 37 |
+
),
|
| 38 |
+
(DataFrame, (" 0\n(0.0, 1.0] a\nNaN b\n(2.0, 3.0] c")),
|
| 39 |
+
],
|
| 40 |
+
)
|
| 41 |
+
def test_repr_missing(self, constructor, expected):
|
| 42 |
+
# GH 25984
|
| 43 |
+
index = IntervalIndex.from_tuples([(0, 1), np.nan, (2, 3)])
|
| 44 |
+
obj = constructor(list("abc"), index=index)
|
| 45 |
+
result = repr(obj)
|
| 46 |
+
assert result == expected
|
| 47 |
+
|
| 48 |
+
def test_repr_floats(self):
|
| 49 |
+
# GH 32553
|
| 50 |
+
|
| 51 |
+
markers = Series(
|
| 52 |
+
["foo", "bar"],
|
| 53 |
+
index=IntervalIndex(
|
| 54 |
+
[
|
| 55 |
+
Interval(left, right)
|
| 56 |
+
for left, right in zip(
|
| 57 |
+
Index([329.973, 345.137], dtype="float64"),
|
| 58 |
+
Index([345.137, 360.191], dtype="float64"),
|
| 59 |
+
)
|
| 60 |
+
]
|
| 61 |
+
),
|
| 62 |
+
)
|
| 63 |
+
result = str(markers)
|
| 64 |
+
expected = "(329.973, 345.137] foo\n(345.137, 360.191] bar\ndtype: object"
|
| 65 |
+
assert result == expected
|
| 66 |
+
|
| 67 |
+
@pytest.mark.parametrize(
|
| 68 |
+
"tuples, closed, expected_data",
|
| 69 |
+
[
|
| 70 |
+
([(0, 1), (1, 2), (2, 3)], "left", ["[0, 1)", "[1, 2)", "[2, 3)"]),
|
| 71 |
+
(
|
| 72 |
+
[(0.5, 1.0), np.nan, (2.0, 3.0)],
|
| 73 |
+
"right",
|
| 74 |
+
["(0.5, 1.0]", "NaN", "(2.0, 3.0]"],
|
| 75 |
+
),
|
| 76 |
+
(
|
| 77 |
+
[
|
| 78 |
+
(Timestamp("20180101"), Timestamp("20180102")),
|
| 79 |
+
np.nan,
|
| 80 |
+
((Timestamp("20180102"), Timestamp("20180103"))),
|
| 81 |
+
],
|
| 82 |
+
"both",
|
| 83 |
+
["[2018-01-01, 2018-01-02]", "NaN", "[2018-01-02, 2018-01-03]"],
|
| 84 |
+
),
|
| 85 |
+
(
|
| 86 |
+
[
|
| 87 |
+
(Timedelta("0 days"), Timedelta("1 days")),
|
| 88 |
+
(Timedelta("1 days"), Timedelta("2 days")),
|
| 89 |
+
np.nan,
|
| 90 |
+
],
|
| 91 |
+
"neither",
|
| 92 |
+
[
|
| 93 |
+
"(0 days 00:00:00, 1 days 00:00:00)",
|
| 94 |
+
"(1 days 00:00:00, 2 days 00:00:00)",
|
| 95 |
+
"NaN",
|
| 96 |
+
],
|
| 97 |
+
),
|
| 98 |
+
],
|
| 99 |
+
)
|
| 100 |
+
def test_to_native_types(self, tuples, closed, expected_data):
|
| 101 |
+
# GH 28210
|
| 102 |
+
index = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 103 |
+
result = index._format_native_types()
|
| 104 |
+
expected = np.array(expected_data)
|
| 105 |
+
tm.assert_numpy_array_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/index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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas.errors import InvalidIndexError
|
| 7 |
+
|
| 8 |
+
from pandas import (
|
| 9 |
+
NA,
|
| 10 |
+
CategoricalIndex,
|
| 11 |
+
DatetimeIndex,
|
| 12 |
+
Index,
|
| 13 |
+
Interval,
|
| 14 |
+
IntervalIndex,
|
| 15 |
+
MultiIndex,
|
| 16 |
+
NaT,
|
| 17 |
+
Timedelta,
|
| 18 |
+
Timestamp,
|
| 19 |
+
array,
|
| 20 |
+
date_range,
|
| 21 |
+
interval_range,
|
| 22 |
+
period_range,
|
| 23 |
+
timedelta_range,
|
| 24 |
+
)
|
| 25 |
+
import pandas._testing as tm
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class TestGetLoc:
|
| 29 |
+
@pytest.mark.parametrize("side", ["right", "left", "both", "neither"])
|
| 30 |
+
def test_get_loc_interval(self, closed, side):
|
| 31 |
+
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=closed)
|
| 32 |
+
|
| 33 |
+
for bound in [[0, 1], [1, 2], [2, 3], [3, 4], [0, 2], [2.5, 3], [-1, 4]]:
|
| 34 |
+
# if get_loc is supplied an interval, it should only search
|
| 35 |
+
# for exact matches, not overlaps or covers, else KeyError.
|
| 36 |
+
msg = re.escape(f"Interval({bound[0]}, {bound[1]}, closed='{side}')")
|
| 37 |
+
if closed == side:
|
| 38 |
+
if bound == [0, 1]:
|
| 39 |
+
assert idx.get_loc(Interval(0, 1, closed=side)) == 0
|
| 40 |
+
elif bound == [2, 3]:
|
| 41 |
+
assert idx.get_loc(Interval(2, 3, closed=side)) == 1
|
| 42 |
+
else:
|
| 43 |
+
with pytest.raises(KeyError, match=msg):
|
| 44 |
+
idx.get_loc(Interval(*bound, closed=side))
|
| 45 |
+
else:
|
| 46 |
+
with pytest.raises(KeyError, match=msg):
|
| 47 |
+
idx.get_loc(Interval(*bound, closed=side))
|
| 48 |
+
|
| 49 |
+
@pytest.mark.parametrize("scalar", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5])
|
| 50 |
+
def test_get_loc_scalar(self, closed, scalar):
|
| 51 |
+
# correct = {side: {query: answer}}.
|
| 52 |
+
# If query is not in the dict, that query should raise a KeyError
|
| 53 |
+
correct = {
|
| 54 |
+
"right": {0.5: 0, 1: 0, 2.5: 1, 3: 1},
|
| 55 |
+
"left": {0: 0, 0.5: 0, 2: 1, 2.5: 1},
|
| 56 |
+
"both": {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1},
|
| 57 |
+
"neither": {0.5: 0, 2.5: 1},
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=closed)
|
| 61 |
+
|
| 62 |
+
# if get_loc is supplied a scalar, it should return the index of
|
| 63 |
+
# the interval which contains the scalar, or KeyError.
|
| 64 |
+
if scalar in correct[closed].keys():
|
| 65 |
+
assert idx.get_loc(scalar) == correct[closed][scalar]
|
| 66 |
+
else:
|
| 67 |
+
with pytest.raises(KeyError, match=str(scalar)):
|
| 68 |
+
idx.get_loc(scalar)
|
| 69 |
+
|
| 70 |
+
@pytest.mark.parametrize("scalar", [-1, 0, 0.5, 3, 4.5, 5, 6])
|
| 71 |
+
def test_get_loc_length_one_scalar(self, scalar, closed):
|
| 72 |
+
# GH 20921
|
| 73 |
+
index = IntervalIndex.from_tuples([(0, 5)], closed=closed)
|
| 74 |
+
if scalar in index[0]:
|
| 75 |
+
result = index.get_loc(scalar)
|
| 76 |
+
assert result == 0
|
| 77 |
+
else:
|
| 78 |
+
with pytest.raises(KeyError, match=str(scalar)):
|
| 79 |
+
index.get_loc(scalar)
|
| 80 |
+
|
| 81 |
+
@pytest.mark.parametrize("other_closed", ["left", "right", "both", "neither"])
|
| 82 |
+
@pytest.mark.parametrize("left, right", [(0, 5), (-1, 4), (-1, 6), (6, 7)])
|
| 83 |
+
def test_get_loc_length_one_interval(self, left, right, closed, other_closed):
|
| 84 |
+
# GH 20921
|
| 85 |
+
index = IntervalIndex.from_tuples([(0, 5)], closed=closed)
|
| 86 |
+
interval = Interval(left, right, closed=other_closed)
|
| 87 |
+
if interval == index[0]:
|
| 88 |
+
result = index.get_loc(interval)
|
| 89 |
+
assert result == 0
|
| 90 |
+
else:
|
| 91 |
+
with pytest.raises(
|
| 92 |
+
KeyError,
|
| 93 |
+
match=re.escape(f"Interval({left}, {right}, closed='{other_closed}')"),
|
| 94 |
+
):
|
| 95 |
+
index.get_loc(interval)
|
| 96 |
+
|
| 97 |
+
# Make consistent with test_interval_new.py (see #16316, #16386)
|
| 98 |
+
@pytest.mark.parametrize(
|
| 99 |
+
"breaks",
|
| 100 |
+
[
|
| 101 |
+
date_range("20180101", periods=4),
|
| 102 |
+
date_range("20180101", periods=4, tz="US/Eastern"),
|
| 103 |
+
timedelta_range("0 days", periods=4),
|
| 104 |
+
],
|
| 105 |
+
ids=lambda x: str(x.dtype),
|
| 106 |
+
)
|
| 107 |
+
def test_get_loc_datetimelike_nonoverlapping(self, breaks):
|
| 108 |
+
# GH 20636
|
| 109 |
+
# nonoverlapping = IntervalIndex method and no i8 conversion
|
| 110 |
+
index = IntervalIndex.from_breaks(breaks)
|
| 111 |
+
|
| 112 |
+
value = index[0].mid
|
| 113 |
+
result = index.get_loc(value)
|
| 114 |
+
expected = 0
|
| 115 |
+
assert result == expected
|
| 116 |
+
|
| 117 |
+
interval = Interval(index[0].left, index[0].right)
|
| 118 |
+
result = index.get_loc(interval)
|
| 119 |
+
expected = 0
|
| 120 |
+
assert result == expected
|
| 121 |
+
|
| 122 |
+
@pytest.mark.parametrize(
|
| 123 |
+
"arrays",
|
| 124 |
+
[
|
| 125 |
+
(date_range("20180101", periods=4), date_range("20180103", periods=4)),
|
| 126 |
+
(
|
| 127 |
+
date_range("20180101", periods=4, tz="US/Eastern"),
|
| 128 |
+
date_range("20180103", periods=4, tz="US/Eastern"),
|
| 129 |
+
),
|
| 130 |
+
(
|
| 131 |
+
timedelta_range("0 days", periods=4),
|
| 132 |
+
timedelta_range("2 days", periods=4),
|
| 133 |
+
),
|
| 134 |
+
],
|
| 135 |
+
ids=lambda x: str(x[0].dtype),
|
| 136 |
+
)
|
| 137 |
+
def test_get_loc_datetimelike_overlapping(self, arrays):
|
| 138 |
+
# GH 20636
|
| 139 |
+
index = IntervalIndex.from_arrays(*arrays)
|
| 140 |
+
|
| 141 |
+
value = index[0].mid + Timedelta("12 hours")
|
| 142 |
+
result = index.get_loc(value)
|
| 143 |
+
expected = slice(0, 2, None)
|
| 144 |
+
assert result == expected
|
| 145 |
+
|
| 146 |
+
interval = Interval(index[0].left, index[0].right)
|
| 147 |
+
result = index.get_loc(interval)
|
| 148 |
+
expected = 0
|
| 149 |
+
assert result == expected
|
| 150 |
+
|
| 151 |
+
@pytest.mark.parametrize(
|
| 152 |
+
"values",
|
| 153 |
+
[
|
| 154 |
+
date_range("2018-01-04", periods=4, freq="-1D"),
|
| 155 |
+
date_range("2018-01-04", periods=4, freq="-1D", tz="US/Eastern"),
|
| 156 |
+
timedelta_range("3 days", periods=4, freq="-1D"),
|
| 157 |
+
np.arange(3.0, -1.0, -1.0),
|
| 158 |
+
np.arange(3, -1, -1),
|
| 159 |
+
],
|
| 160 |
+
ids=lambda x: str(x.dtype),
|
| 161 |
+
)
|
| 162 |
+
def test_get_loc_decreasing(self, values):
|
| 163 |
+
# GH 25860
|
| 164 |
+
index = IntervalIndex.from_arrays(values[1:], values[:-1])
|
| 165 |
+
result = index.get_loc(index[0])
|
| 166 |
+
expected = 0
|
| 167 |
+
assert result == expected
|
| 168 |
+
|
| 169 |
+
@pytest.mark.parametrize("key", [[5], (2, 3)])
|
| 170 |
+
def test_get_loc_non_scalar_errors(self, key):
|
| 171 |
+
# GH 31117
|
| 172 |
+
idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)])
|
| 173 |
+
|
| 174 |
+
msg = str(key)
|
| 175 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
| 176 |
+
idx.get_loc(key)
|
| 177 |
+
|
| 178 |
+
def test_get_indexer_with_nans(self):
|
| 179 |
+
# GH#41831
|
| 180 |
+
index = IntervalIndex([np.nan, Interval(1, 2), np.nan])
|
| 181 |
+
|
| 182 |
+
expected = np.array([True, False, True])
|
| 183 |
+
for key in [None, np.nan, NA]:
|
| 184 |
+
assert key in index
|
| 185 |
+
result = index.get_loc(key)
|
| 186 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 187 |
+
|
| 188 |
+
for key in [NaT, np.timedelta64("NaT", "ns"), np.datetime64("NaT", "ns")]:
|
| 189 |
+
with pytest.raises(KeyError, match=str(key)):
|
| 190 |
+
index.get_loc(key)
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
class TestGetIndexer:
|
| 194 |
+
@pytest.mark.parametrize(
|
| 195 |
+
"query, expected",
|
| 196 |
+
[
|
| 197 |
+
([Interval(2, 4, closed="right")], [1]),
|
| 198 |
+
([Interval(2, 4, closed="left")], [-1]),
|
| 199 |
+
([Interval(2, 4, closed="both")], [-1]),
|
| 200 |
+
([Interval(2, 4, closed="neither")], [-1]),
|
| 201 |
+
([Interval(1, 4, closed="right")], [-1]),
|
| 202 |
+
([Interval(0, 4, closed="right")], [-1]),
|
| 203 |
+
([Interval(0.5, 1.5, closed="right")], [-1]),
|
| 204 |
+
([Interval(2, 4, closed="right"), Interval(0, 1, closed="right")], [1, -1]),
|
| 205 |
+
([Interval(2, 4, closed="right"), Interval(2, 4, closed="right")], [1, 1]),
|
| 206 |
+
([Interval(5, 7, closed="right"), Interval(2, 4, closed="right")], [2, 1]),
|
| 207 |
+
([Interval(2, 4, closed="right"), Interval(2, 4, closed="left")], [1, -1]),
|
| 208 |
+
],
|
| 209 |
+
)
|
| 210 |
+
def test_get_indexer_with_interval(self, query, expected):
|
| 211 |
+
tuples = [(0, 2), (2, 4), (5, 7)]
|
| 212 |
+
index = IntervalIndex.from_tuples(tuples, closed="right")
|
| 213 |
+
|
| 214 |
+
result = index.get_indexer(query)
|
| 215 |
+
expected = np.array(expected, dtype="intp")
|
| 216 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 217 |
+
|
| 218 |
+
@pytest.mark.parametrize(
|
| 219 |
+
"query, expected",
|
| 220 |
+
[
|
| 221 |
+
([-0.5], [-1]),
|
| 222 |
+
([0], [-1]),
|
| 223 |
+
([0.5], [0]),
|
| 224 |
+
([1], [0]),
|
| 225 |
+
([1.5], [1]),
|
| 226 |
+
([2], [1]),
|
| 227 |
+
([2.5], [-1]),
|
| 228 |
+
([3], [-1]),
|
| 229 |
+
([3.5], [2]),
|
| 230 |
+
([4], [2]),
|
| 231 |
+
([4.5], [-1]),
|
| 232 |
+
([1, 2], [0, 1]),
|
| 233 |
+
([1, 2, 3], [0, 1, -1]),
|
| 234 |
+
([1, 2, 3, 4], [0, 1, -1, 2]),
|
| 235 |
+
([1, 2, 3, 4, 2], [0, 1, -1, 2, 1]),
|
| 236 |
+
],
|
| 237 |
+
)
|
| 238 |
+
def test_get_indexer_with_int_and_float(self, query, expected):
|
| 239 |
+
tuples = [(0, 1), (1, 2), (3, 4)]
|
| 240 |
+
index = IntervalIndex.from_tuples(tuples, closed="right")
|
| 241 |
+
|
| 242 |
+
result = index.get_indexer(query)
|
| 243 |
+
expected = np.array(expected, dtype="intp")
|
| 244 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 245 |
+
|
| 246 |
+
@pytest.mark.parametrize("item", [[3], np.arange(0.5, 5, 0.5)])
|
| 247 |
+
def test_get_indexer_length_one(self, item, closed):
|
| 248 |
+
# GH 17284
|
| 249 |
+
index = IntervalIndex.from_tuples([(0, 5)], closed=closed)
|
| 250 |
+
result = index.get_indexer(item)
|
| 251 |
+
expected = np.array([0] * len(item), dtype="intp")
|
| 252 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 253 |
+
|
| 254 |
+
@pytest.mark.parametrize("size", [1, 5])
|
| 255 |
+
def test_get_indexer_length_one_interval(self, size, closed):
|
| 256 |
+
# GH 17284
|
| 257 |
+
index = IntervalIndex.from_tuples([(0, 5)], closed=closed)
|
| 258 |
+
result = index.get_indexer([Interval(0, 5, closed)] * size)
|
| 259 |
+
expected = np.array([0] * size, dtype="intp")
|
| 260 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 261 |
+
|
| 262 |
+
@pytest.mark.parametrize(
|
| 263 |
+
"target",
|
| 264 |
+
[
|
| 265 |
+
IntervalIndex.from_tuples([(7, 8), (1, 2), (3, 4), (0, 1)]),
|
| 266 |
+
IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4), np.nan]),
|
| 267 |
+
IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)], closed="both"),
|
| 268 |
+
[-1, 0, 0.5, 1, 2, 2.5, np.nan],
|
| 269 |
+
["foo", "foo", "bar", "baz"],
|
| 270 |
+
],
|
| 271 |
+
)
|
| 272 |
+
def test_get_indexer_categorical(self, target, ordered):
|
| 273 |
+
# GH 30063: categorical and non-categorical results should be consistent
|
| 274 |
+
index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)])
|
| 275 |
+
categorical_target = CategoricalIndex(target, ordered=ordered)
|
| 276 |
+
|
| 277 |
+
result = index.get_indexer(categorical_target)
|
| 278 |
+
expected = index.get_indexer(target)
|
| 279 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 280 |
+
|
| 281 |
+
def test_get_indexer_categorical_with_nans(self):
|
| 282 |
+
# GH#41934 nans in both index and in target
|
| 283 |
+
ii = IntervalIndex.from_breaks(range(5))
|
| 284 |
+
ii2 = ii.append(IntervalIndex([np.nan]))
|
| 285 |
+
ci2 = CategoricalIndex(ii2)
|
| 286 |
+
|
| 287 |
+
result = ii2.get_indexer(ci2)
|
| 288 |
+
expected = np.arange(5, dtype=np.intp)
|
| 289 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 290 |
+
|
| 291 |
+
# not-all-matches
|
| 292 |
+
result = ii2[1:].get_indexer(ci2[::-1])
|
| 293 |
+
expected = np.array([3, 2, 1, 0, -1], dtype=np.intp)
|
| 294 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 295 |
+
|
| 296 |
+
# non-unique target, non-unique nans
|
| 297 |
+
result = ii2.get_indexer(ci2.append(ci2))
|
| 298 |
+
expected = np.array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4], dtype=np.intp)
|
| 299 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 300 |
+
|
| 301 |
+
def test_get_indexer_datetime(self):
|
| 302 |
+
ii = IntervalIndex.from_breaks(date_range("2018-01-01", periods=4))
|
| 303 |
+
result = ii.get_indexer(DatetimeIndex(["2018-01-02"]))
|
| 304 |
+
expected = np.array([0], dtype=np.intp)
|
| 305 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 306 |
+
|
| 307 |
+
result = ii.get_indexer(DatetimeIndex(["2018-01-02"]).astype(str))
|
| 308 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 309 |
+
|
| 310 |
+
# TODO this should probably be deprecated?
|
| 311 |
+
# https://github.com/pandas-dev/pandas/issues/47772
|
| 312 |
+
result = ii.get_indexer(DatetimeIndex(["2018-01-02"]).asi8)
|
| 313 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 314 |
+
|
| 315 |
+
@pytest.mark.parametrize(
|
| 316 |
+
"tuples, closed",
|
| 317 |
+
[
|
| 318 |
+
([(0, 2), (1, 3), (3, 4)], "neither"),
|
| 319 |
+
([(0, 5), (1, 4), (6, 7)], "left"),
|
| 320 |
+
([(0, 1), (0, 1), (1, 2)], "right"),
|
| 321 |
+
([(0, 1), (2, 3), (3, 4)], "both"),
|
| 322 |
+
],
|
| 323 |
+
)
|
| 324 |
+
def test_get_indexer_errors(self, tuples, closed):
|
| 325 |
+
# IntervalIndex needs non-overlapping for uniqueness when querying
|
| 326 |
+
index = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 327 |
+
|
| 328 |
+
msg = (
|
| 329 |
+
"cannot handle overlapping indices; use "
|
| 330 |
+
"IntervalIndex.get_indexer_non_unique"
|
| 331 |
+
)
|
| 332 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
| 333 |
+
index.get_indexer([0, 2])
|
| 334 |
+
|
| 335 |
+
@pytest.mark.parametrize(
|
| 336 |
+
"query, expected",
|
| 337 |
+
[
|
| 338 |
+
([-0.5], ([-1], [0])),
|
| 339 |
+
([0], ([0], [])),
|
| 340 |
+
([0.5], ([0], [])),
|
| 341 |
+
([1], ([0, 1], [])),
|
| 342 |
+
([1.5], ([0, 1], [])),
|
| 343 |
+
([2], ([0, 1, 2], [])),
|
| 344 |
+
([2.5], ([1, 2], [])),
|
| 345 |
+
([3], ([2], [])),
|
| 346 |
+
([3.5], ([2], [])),
|
| 347 |
+
([4], ([-1], [0])),
|
| 348 |
+
([4.5], ([-1], [0])),
|
| 349 |
+
([1, 2], ([0, 1, 0, 1, 2], [])),
|
| 350 |
+
([1, 2, 3], ([0, 1, 0, 1, 2, 2], [])),
|
| 351 |
+
([1, 2, 3, 4], ([0, 1, 0, 1, 2, 2, -1], [3])),
|
| 352 |
+
([1, 2, 3, 4, 2], ([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], [3])),
|
| 353 |
+
],
|
| 354 |
+
)
|
| 355 |
+
def test_get_indexer_non_unique_with_int_and_float(self, query, expected):
|
| 356 |
+
tuples = [(0, 2.5), (1, 3), (2, 4)]
|
| 357 |
+
index = IntervalIndex.from_tuples(tuples, closed="left")
|
| 358 |
+
|
| 359 |
+
result_indexer, result_missing = index.get_indexer_non_unique(query)
|
| 360 |
+
expected_indexer = np.array(expected[0], dtype="intp")
|
| 361 |
+
expected_missing = np.array(expected[1], dtype="intp")
|
| 362 |
+
|
| 363 |
+
tm.assert_numpy_array_equal(result_indexer, expected_indexer)
|
| 364 |
+
tm.assert_numpy_array_equal(result_missing, expected_missing)
|
| 365 |
+
|
| 366 |
+
# TODO we may also want to test get_indexer for the case when
|
| 367 |
+
# the intervals are duplicated, decreasing, non-monotonic, etc..
|
| 368 |
+
|
| 369 |
+
def test_get_indexer_non_monotonic(self):
|
| 370 |
+
# GH 16410
|
| 371 |
+
idx1 = IntervalIndex.from_tuples([(2, 3), (4, 5), (0, 1)])
|
| 372 |
+
idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (6, 7), (8, 9)])
|
| 373 |
+
result = idx1.get_indexer(idx2)
|
| 374 |
+
expected = np.array([2, 0, -1, -1], dtype=np.intp)
|
| 375 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 376 |
+
|
| 377 |
+
result = idx1.get_indexer(idx1[1:])
|
| 378 |
+
expected = np.array([1, 2], dtype=np.intp)
|
| 379 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 380 |
+
|
| 381 |
+
def test_get_indexer_with_nans(self):
|
| 382 |
+
# GH#41831
|
| 383 |
+
index = IntervalIndex([np.nan, np.nan])
|
| 384 |
+
other = IntervalIndex([np.nan])
|
| 385 |
+
|
| 386 |
+
assert not index._index_as_unique
|
| 387 |
+
|
| 388 |
+
result = index.get_indexer_for(other)
|
| 389 |
+
expected = np.array([0, 1], dtype=np.intp)
|
| 390 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 391 |
+
|
| 392 |
+
def test_get_index_non_unique_non_monotonic(self):
|
| 393 |
+
# GH#44084 (root cause)
|
| 394 |
+
index = IntervalIndex.from_tuples(
|
| 395 |
+
[(0.0, 1.0), (1.0, 2.0), (0.0, 1.0), (1.0, 2.0)]
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
result, _ = index.get_indexer_non_unique([Interval(1.0, 2.0)])
|
| 399 |
+
expected = np.array([1, 3], dtype=np.intp)
|
| 400 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 401 |
+
|
| 402 |
+
def test_get_indexer_multiindex_with_intervals(self):
|
| 403 |
+
# GH#44084 (MultiIndex case as reported)
|
| 404 |
+
interval_index = IntervalIndex.from_tuples(
|
| 405 |
+
[(2.0, 3.0), (0.0, 1.0), (1.0, 2.0)], name="interval"
|
| 406 |
+
)
|
| 407 |
+
foo_index = Index([1, 2, 3], name="foo")
|
| 408 |
+
|
| 409 |
+
multi_index = MultiIndex.from_product([foo_index, interval_index])
|
| 410 |
+
|
| 411 |
+
result = multi_index.get_level_values("interval").get_indexer_for(
|
| 412 |
+
[Interval(0.0, 1.0)]
|
| 413 |
+
)
|
| 414 |
+
expected = np.array([1, 4, 7], dtype=np.intp)
|
| 415 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 416 |
+
|
| 417 |
+
@pytest.mark.parametrize("box", [IntervalIndex, array, list])
|
| 418 |
+
def test_get_indexer_interval_index(self, box):
|
| 419 |
+
# GH#30178
|
| 420 |
+
rng = period_range("2022-07-01", freq="D", periods=3)
|
| 421 |
+
idx = box(interval_range(Timestamp("2022-07-01"), freq="3D", periods=3))
|
| 422 |
+
|
| 423 |
+
actual = rng.get_indexer(idx)
|
| 424 |
+
expected = np.array([-1, -1, -1], dtype=np.intp)
|
| 425 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 426 |
+
|
| 427 |
+
|
| 428 |
+
class TestSliceLocs:
|
| 429 |
+
def test_slice_locs_with_interval(self):
|
| 430 |
+
# increasing monotonically
|
| 431 |
+
index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)])
|
| 432 |
+
|
| 433 |
+
assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3)
|
| 434 |
+
assert index.slice_locs(start=Interval(0, 2)) == (0, 3)
|
| 435 |
+
assert index.slice_locs(end=Interval(2, 4)) == (0, 3)
|
| 436 |
+
assert index.slice_locs(end=Interval(0, 2)) == (0, 1)
|
| 437 |
+
assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 1)
|
| 438 |
+
|
| 439 |
+
# decreasing monotonically
|
| 440 |
+
index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)])
|
| 441 |
+
|
| 442 |
+
assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (2, 1)
|
| 443 |
+
assert index.slice_locs(start=Interval(0, 2)) == (2, 3)
|
| 444 |
+
assert index.slice_locs(end=Interval(2, 4)) == (0, 1)
|
| 445 |
+
assert index.slice_locs(end=Interval(0, 2)) == (0, 3)
|
| 446 |
+
assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 3)
|
| 447 |
+
|
| 448 |
+
# sorted duplicates
|
| 449 |
+
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)])
|
| 450 |
+
|
| 451 |
+
assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3)
|
| 452 |
+
assert index.slice_locs(start=Interval(0, 2)) == (0, 3)
|
| 453 |
+
assert index.slice_locs(end=Interval(2, 4)) == (0, 3)
|
| 454 |
+
assert index.slice_locs(end=Interval(0, 2)) == (0, 2)
|
| 455 |
+
assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2)
|
| 456 |
+
|
| 457 |
+
# unsorted duplicates
|
| 458 |
+
index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)])
|
| 459 |
+
|
| 460 |
+
with pytest.raises(
|
| 461 |
+
KeyError,
|
| 462 |
+
match=re.escape(
|
| 463 |
+
'"Cannot get left slice bound for non-unique label: '
|
| 464 |
+
"Interval(0, 2, closed='right')\""
|
| 465 |
+
),
|
| 466 |
+
):
|
| 467 |
+
index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))
|
| 468 |
+
|
| 469 |
+
with pytest.raises(
|
| 470 |
+
KeyError,
|
| 471 |
+
match=re.escape(
|
| 472 |
+
'"Cannot get left slice bound for non-unique label: '
|
| 473 |
+
"Interval(0, 2, closed='right')\""
|
| 474 |
+
),
|
| 475 |
+
):
|
| 476 |
+
index.slice_locs(start=Interval(0, 2))
|
| 477 |
+
|
| 478 |
+
assert index.slice_locs(end=Interval(2, 4)) == (0, 2)
|
| 479 |
+
|
| 480 |
+
with pytest.raises(
|
| 481 |
+
KeyError,
|
| 482 |
+
match=re.escape(
|
| 483 |
+
'"Cannot get right slice bound for non-unique label: '
|
| 484 |
+
"Interval(0, 2, closed='right')\""
|
| 485 |
+
),
|
| 486 |
+
):
|
| 487 |
+
index.slice_locs(end=Interval(0, 2))
|
| 488 |
+
|
| 489 |
+
with pytest.raises(
|
| 490 |
+
KeyError,
|
| 491 |
+
match=re.escape(
|
| 492 |
+
'"Cannot get right slice bound for non-unique label: '
|
| 493 |
+
"Interval(0, 2, closed='right')\""
|
| 494 |
+
),
|
| 495 |
+
):
|
| 496 |
+
index.slice_locs(start=Interval(2, 4), end=Interval(0, 2))
|
| 497 |
+
|
| 498 |
+
# another unsorted duplicates
|
| 499 |
+
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)])
|
| 500 |
+
|
| 501 |
+
assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3)
|
| 502 |
+
assert index.slice_locs(start=Interval(0, 2)) == (0, 4)
|
| 503 |
+
assert index.slice_locs(end=Interval(2, 4)) == (0, 3)
|
| 504 |
+
assert index.slice_locs(end=Interval(0, 2)) == (0, 2)
|
| 505 |
+
assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2)
|
| 506 |
+
|
| 507 |
+
def test_slice_locs_with_ints_and_floats_succeeds(self):
|
| 508 |
+
# increasing non-overlapping
|
| 509 |
+
index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)])
|
| 510 |
+
|
| 511 |
+
assert index.slice_locs(0, 1) == (0, 1)
|
| 512 |
+
assert index.slice_locs(0, 2) == (0, 2)
|
| 513 |
+
assert index.slice_locs(0, 3) == (0, 2)
|
| 514 |
+
assert index.slice_locs(3, 1) == (2, 1)
|
| 515 |
+
assert index.slice_locs(3, 4) == (2, 3)
|
| 516 |
+
assert index.slice_locs(0, 4) == (0, 3)
|
| 517 |
+
|
| 518 |
+
# decreasing non-overlapping
|
| 519 |
+
index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)])
|
| 520 |
+
assert index.slice_locs(0, 1) == (3, 3)
|
| 521 |
+
assert index.slice_locs(0, 2) == (3, 2)
|
| 522 |
+
assert index.slice_locs(0, 3) == (3, 1)
|
| 523 |
+
assert index.slice_locs(3, 1) == (1, 3)
|
| 524 |
+
assert index.slice_locs(3, 4) == (1, 1)
|
| 525 |
+
assert index.slice_locs(0, 4) == (3, 1)
|
| 526 |
+
|
| 527 |
+
@pytest.mark.parametrize("query", [[0, 1], [0, 2], [0, 3], [0, 4]])
|
| 528 |
+
@pytest.mark.parametrize(
|
| 529 |
+
"tuples",
|
| 530 |
+
[
|
| 531 |
+
[(0, 2), (1, 3), (2, 4)],
|
| 532 |
+
[(2, 4), (1, 3), (0, 2)],
|
| 533 |
+
[(0, 2), (0, 2), (2, 4)],
|
| 534 |
+
[(0, 2), (2, 4), (0, 2)],
|
| 535 |
+
[(0, 2), (0, 2), (2, 4), (1, 3)],
|
| 536 |
+
],
|
| 537 |
+
)
|
| 538 |
+
def test_slice_locs_with_ints_and_floats_errors(self, tuples, query):
|
| 539 |
+
start, stop = query
|
| 540 |
+
index = IntervalIndex.from_tuples(tuples)
|
| 541 |
+
with pytest.raises(
|
| 542 |
+
KeyError,
|
| 543 |
+
match=(
|
| 544 |
+
"'can only get slices from an IntervalIndex if bounds are "
|
| 545 |
+
"non-overlapping and all monotonic increasing or decreasing'"
|
| 546 |
+
),
|
| 547 |
+
):
|
| 548 |
+
index.slice_locs(start, stop)
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
class TestPutmask:
|
| 552 |
+
@pytest.mark.parametrize("tz", ["US/Pacific", None])
|
| 553 |
+
def test_putmask_dt64(self, tz):
|
| 554 |
+
# GH#37968
|
| 555 |
+
dti = date_range("2016-01-01", periods=9, tz=tz)
|
| 556 |
+
idx = IntervalIndex.from_breaks(dti)
|
| 557 |
+
mask = np.zeros(idx.shape, dtype=bool)
|
| 558 |
+
mask[0:3] = True
|
| 559 |
+
|
| 560 |
+
result = idx.putmask(mask, idx[-1])
|
| 561 |
+
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
|
| 562 |
+
tm.assert_index_equal(result, expected)
|
| 563 |
+
|
| 564 |
+
def test_putmask_td64(self):
|
| 565 |
+
# GH#37968
|
| 566 |
+
dti = date_range("2016-01-01", periods=9)
|
| 567 |
+
tdi = dti - dti[0]
|
| 568 |
+
idx = IntervalIndex.from_breaks(tdi)
|
| 569 |
+
mask = np.zeros(idx.shape, dtype=bool)
|
| 570 |
+
mask[0:3] = True
|
| 571 |
+
|
| 572 |
+
result = idx.putmask(mask, idx[-1])
|
| 573 |
+
expected = IntervalIndex([idx[-1]] * 3 + list(idx[3:]))
|
| 574 |
+
tm.assert_index_equal(result, expected)
|
| 575 |
+
|
| 576 |
+
|
| 577 |
+
class TestContains:
|
| 578 |
+
# .__contains__, not .contains
|
| 579 |
+
|
| 580 |
+
def test_contains_dunder(self):
|
| 581 |
+
index = IntervalIndex.from_arrays([0, 1], [1, 2], closed="right")
|
| 582 |
+
|
| 583 |
+
# __contains__ requires perfect matches to intervals.
|
| 584 |
+
assert 0 not in index
|
| 585 |
+
assert 1 not in index
|
| 586 |
+
assert 2 not in index
|
| 587 |
+
|
| 588 |
+
assert Interval(0, 1, closed="right") in index
|
| 589 |
+
assert Interval(0, 2, closed="right") not in index
|
| 590 |
+
assert Interval(0, 0.5, closed="right") not in index
|
| 591 |
+
assert Interval(3, 5, closed="right") not in index
|
| 592 |
+
assert Interval(-1, 0, closed="left") not in index
|
| 593 |
+
assert Interval(0, 1, closed="left") not in index
|
| 594 |
+
assert Interval(0, 1, closed="both") not in index
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/interval/test_interval.py
ADDED
|
@@ -0,0 +1,934 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
from pandas import (
|
| 9 |
+
Index,
|
| 10 |
+
Interval,
|
| 11 |
+
IntervalIndex,
|
| 12 |
+
Timedelta,
|
| 13 |
+
Timestamp,
|
| 14 |
+
date_range,
|
| 15 |
+
interval_range,
|
| 16 |
+
isna,
|
| 17 |
+
notna,
|
| 18 |
+
timedelta_range,
|
| 19 |
+
)
|
| 20 |
+
import pandas._testing as tm
|
| 21 |
+
import pandas.core.common as com
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@pytest.fixture(params=[None, "foo"])
|
| 25 |
+
def name(request):
|
| 26 |
+
return request.param
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TestIntervalIndex:
|
| 30 |
+
index = IntervalIndex.from_arrays([0, 1], [1, 2])
|
| 31 |
+
|
| 32 |
+
def create_index(self, closed="right"):
|
| 33 |
+
return IntervalIndex.from_breaks(range(11), closed=closed)
|
| 34 |
+
|
| 35 |
+
def create_index_with_nan(self, closed="right"):
|
| 36 |
+
mask = [True, False] + [True] * 8
|
| 37 |
+
return IntervalIndex.from_arrays(
|
| 38 |
+
np.where(mask, np.arange(10), np.nan),
|
| 39 |
+
np.where(mask, np.arange(1, 11), np.nan),
|
| 40 |
+
closed=closed,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def test_properties(self, closed):
|
| 44 |
+
index = self.create_index(closed=closed)
|
| 45 |
+
assert len(index) == 10
|
| 46 |
+
assert index.size == 10
|
| 47 |
+
assert index.shape == (10,)
|
| 48 |
+
|
| 49 |
+
tm.assert_index_equal(index.left, Index(np.arange(10, dtype=np.int64)))
|
| 50 |
+
tm.assert_index_equal(index.right, Index(np.arange(1, 11, dtype=np.int64)))
|
| 51 |
+
tm.assert_index_equal(index.mid, Index(np.arange(0.5, 10.5, dtype=np.float64)))
|
| 52 |
+
|
| 53 |
+
assert index.closed == closed
|
| 54 |
+
|
| 55 |
+
ivs = [
|
| 56 |
+
Interval(left, right, closed)
|
| 57 |
+
for left, right in zip(range(10), range(1, 11))
|
| 58 |
+
]
|
| 59 |
+
expected = np.array(ivs, dtype=object)
|
| 60 |
+
tm.assert_numpy_array_equal(np.asarray(index), expected)
|
| 61 |
+
|
| 62 |
+
# with nans
|
| 63 |
+
index = self.create_index_with_nan(closed=closed)
|
| 64 |
+
assert len(index) == 10
|
| 65 |
+
assert index.size == 10
|
| 66 |
+
assert index.shape == (10,)
|
| 67 |
+
|
| 68 |
+
expected_left = Index([0, np.nan, 2, 3, 4, 5, 6, 7, 8, 9])
|
| 69 |
+
expected_right = expected_left + 1
|
| 70 |
+
expected_mid = expected_left + 0.5
|
| 71 |
+
tm.assert_index_equal(index.left, expected_left)
|
| 72 |
+
tm.assert_index_equal(index.right, expected_right)
|
| 73 |
+
tm.assert_index_equal(index.mid, expected_mid)
|
| 74 |
+
|
| 75 |
+
assert index.closed == closed
|
| 76 |
+
|
| 77 |
+
ivs = [
|
| 78 |
+
Interval(left, right, closed) if notna(left) else np.nan
|
| 79 |
+
for left, right in zip(expected_left, expected_right)
|
| 80 |
+
]
|
| 81 |
+
expected = np.array(ivs, dtype=object)
|
| 82 |
+
tm.assert_numpy_array_equal(np.asarray(index), expected)
|
| 83 |
+
|
| 84 |
+
@pytest.mark.parametrize(
|
| 85 |
+
"breaks",
|
| 86 |
+
[
|
| 87 |
+
[1, 1, 2, 5, 15, 53, 217, 1014, 5335, 31240, 201608],
|
| 88 |
+
[-np.inf, -100, -10, 0.5, 1, 1.5, 3.8, 101, 202, np.inf],
|
| 89 |
+
pd.to_datetime(["20170101", "20170202", "20170303", "20170404"]),
|
| 90 |
+
pd.to_timedelta(["1ns", "2ms", "3s", "4min", "5H", "6D"]),
|
| 91 |
+
],
|
| 92 |
+
)
|
| 93 |
+
def test_length(self, closed, breaks):
|
| 94 |
+
# GH 18789
|
| 95 |
+
index = IntervalIndex.from_breaks(breaks, closed=closed)
|
| 96 |
+
result = index.length
|
| 97 |
+
expected = Index(iv.length for iv in index)
|
| 98 |
+
tm.assert_index_equal(result, expected)
|
| 99 |
+
|
| 100 |
+
# with NA
|
| 101 |
+
index = index.insert(1, np.nan)
|
| 102 |
+
result = index.length
|
| 103 |
+
expected = Index(iv.length if notna(iv) else iv for iv in index)
|
| 104 |
+
tm.assert_index_equal(result, expected)
|
| 105 |
+
|
| 106 |
+
def test_with_nans(self, closed):
|
| 107 |
+
index = self.create_index(closed=closed)
|
| 108 |
+
assert index.hasnans is False
|
| 109 |
+
|
| 110 |
+
result = index.isna()
|
| 111 |
+
expected = np.zeros(len(index), dtype=bool)
|
| 112 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 113 |
+
|
| 114 |
+
result = index.notna()
|
| 115 |
+
expected = np.ones(len(index), dtype=bool)
|
| 116 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 117 |
+
|
| 118 |
+
index = self.create_index_with_nan(closed=closed)
|
| 119 |
+
assert index.hasnans is True
|
| 120 |
+
|
| 121 |
+
result = index.isna()
|
| 122 |
+
expected = np.array([False, True] + [False] * (len(index) - 2))
|
| 123 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 124 |
+
|
| 125 |
+
result = index.notna()
|
| 126 |
+
expected = np.array([True, False] + [True] * (len(index) - 2))
|
| 127 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 128 |
+
|
| 129 |
+
def test_copy(self, closed):
|
| 130 |
+
expected = self.create_index(closed=closed)
|
| 131 |
+
|
| 132 |
+
result = expected.copy()
|
| 133 |
+
assert result.equals(expected)
|
| 134 |
+
|
| 135 |
+
result = expected.copy(deep=True)
|
| 136 |
+
assert result.equals(expected)
|
| 137 |
+
assert result.left is not expected.left
|
| 138 |
+
|
| 139 |
+
def test_ensure_copied_data(self, closed):
|
| 140 |
+
# exercise the copy flag in the constructor
|
| 141 |
+
|
| 142 |
+
# not copying
|
| 143 |
+
index = self.create_index(closed=closed)
|
| 144 |
+
result = IntervalIndex(index, copy=False)
|
| 145 |
+
tm.assert_numpy_array_equal(
|
| 146 |
+
index.left.values, result.left.values, check_same="same"
|
| 147 |
+
)
|
| 148 |
+
tm.assert_numpy_array_equal(
|
| 149 |
+
index.right.values, result.right.values, check_same="same"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
# by-definition make a copy
|
| 153 |
+
result = IntervalIndex(np.array(index), copy=False)
|
| 154 |
+
tm.assert_numpy_array_equal(
|
| 155 |
+
index.left.values, result.left.values, check_same="copy"
|
| 156 |
+
)
|
| 157 |
+
tm.assert_numpy_array_equal(
|
| 158 |
+
index.right.values, result.right.values, check_same="copy"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
def test_delete(self, closed):
|
| 162 |
+
breaks = np.arange(1, 11, dtype=np.int64)
|
| 163 |
+
expected = IntervalIndex.from_breaks(breaks, closed=closed)
|
| 164 |
+
result = self.create_index(closed=closed).delete(0)
|
| 165 |
+
tm.assert_index_equal(result, expected)
|
| 166 |
+
|
| 167 |
+
@pytest.mark.parametrize(
|
| 168 |
+
"data",
|
| 169 |
+
[
|
| 170 |
+
interval_range(0, periods=10, closed="neither"),
|
| 171 |
+
interval_range(1.7, periods=8, freq=2.5, closed="both"),
|
| 172 |
+
interval_range(Timestamp("20170101"), periods=12, closed="left"),
|
| 173 |
+
interval_range(Timedelta("1 day"), periods=6, closed="right"),
|
| 174 |
+
],
|
| 175 |
+
)
|
| 176 |
+
def test_insert(self, data):
|
| 177 |
+
item = data[0]
|
| 178 |
+
idx_item = IntervalIndex([item])
|
| 179 |
+
|
| 180 |
+
# start
|
| 181 |
+
expected = idx_item.append(data)
|
| 182 |
+
result = data.insert(0, item)
|
| 183 |
+
tm.assert_index_equal(result, expected)
|
| 184 |
+
|
| 185 |
+
# end
|
| 186 |
+
expected = data.append(idx_item)
|
| 187 |
+
result = data.insert(len(data), item)
|
| 188 |
+
tm.assert_index_equal(result, expected)
|
| 189 |
+
|
| 190 |
+
# mid
|
| 191 |
+
expected = data[:3].append(idx_item).append(data[3:])
|
| 192 |
+
result = data.insert(3, item)
|
| 193 |
+
tm.assert_index_equal(result, expected)
|
| 194 |
+
|
| 195 |
+
# invalid type
|
| 196 |
+
res = data.insert(1, "foo")
|
| 197 |
+
expected = data.astype(object).insert(1, "foo")
|
| 198 |
+
tm.assert_index_equal(res, expected)
|
| 199 |
+
|
| 200 |
+
msg = "can only insert Interval objects and NA into an IntervalArray"
|
| 201 |
+
with pytest.raises(TypeError, match=msg):
|
| 202 |
+
data._data.insert(1, "foo")
|
| 203 |
+
|
| 204 |
+
# invalid closed
|
| 205 |
+
msg = "'value.closed' is 'left', expected 'right'."
|
| 206 |
+
for closed in {"left", "right", "both", "neither"} - {item.closed}:
|
| 207 |
+
msg = f"'value.closed' is '{closed}', expected '{item.closed}'."
|
| 208 |
+
bad_item = Interval(item.left, item.right, closed=closed)
|
| 209 |
+
res = data.insert(1, bad_item)
|
| 210 |
+
expected = data.astype(object).insert(1, bad_item)
|
| 211 |
+
tm.assert_index_equal(res, expected)
|
| 212 |
+
with pytest.raises(ValueError, match=msg):
|
| 213 |
+
data._data.insert(1, bad_item)
|
| 214 |
+
|
| 215 |
+
# GH 18295 (test missing)
|
| 216 |
+
na_idx = IntervalIndex([np.nan], closed=data.closed)
|
| 217 |
+
for na in [np.nan, None, pd.NA]:
|
| 218 |
+
expected = data[:1].append(na_idx).append(data[1:])
|
| 219 |
+
result = data.insert(1, na)
|
| 220 |
+
tm.assert_index_equal(result, expected)
|
| 221 |
+
|
| 222 |
+
if data.left.dtype.kind not in ["m", "M"]:
|
| 223 |
+
# trying to insert pd.NaT into a numeric-dtyped Index should cast
|
| 224 |
+
expected = data.astype(object).insert(1, pd.NaT)
|
| 225 |
+
|
| 226 |
+
msg = "can only insert Interval objects and NA into an IntervalArray"
|
| 227 |
+
with pytest.raises(TypeError, match=msg):
|
| 228 |
+
data._data.insert(1, pd.NaT)
|
| 229 |
+
|
| 230 |
+
result = data.insert(1, pd.NaT)
|
| 231 |
+
tm.assert_index_equal(result, expected)
|
| 232 |
+
|
| 233 |
+
def test_is_unique_interval(self, closed):
|
| 234 |
+
"""
|
| 235 |
+
Interval specific tests for is_unique in addition to base class tests
|
| 236 |
+
"""
|
| 237 |
+
# unique overlapping - distinct endpoints
|
| 238 |
+
idx = IntervalIndex.from_tuples([(0, 1), (0.5, 1.5)], closed=closed)
|
| 239 |
+
assert idx.is_unique is True
|
| 240 |
+
|
| 241 |
+
# unique overlapping - shared endpoints
|
| 242 |
+
idx = IntervalIndex.from_tuples([(1, 2), (1, 3), (2, 3)], closed=closed)
|
| 243 |
+
assert idx.is_unique is True
|
| 244 |
+
|
| 245 |
+
# unique nested
|
| 246 |
+
idx = IntervalIndex.from_tuples([(-1, 1), (-2, 2)], closed=closed)
|
| 247 |
+
assert idx.is_unique is True
|
| 248 |
+
|
| 249 |
+
# unique NaN
|
| 250 |
+
idx = IntervalIndex.from_tuples([(np.NaN, np.NaN)], closed=closed)
|
| 251 |
+
assert idx.is_unique is True
|
| 252 |
+
|
| 253 |
+
# non-unique NaN
|
| 254 |
+
idx = IntervalIndex.from_tuples(
|
| 255 |
+
[(np.NaN, np.NaN), (np.NaN, np.NaN)], closed=closed
|
| 256 |
+
)
|
| 257 |
+
assert idx.is_unique is False
|
| 258 |
+
|
| 259 |
+
def test_monotonic(self, closed):
|
| 260 |
+
# increasing non-overlapping
|
| 261 |
+
idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)], closed=closed)
|
| 262 |
+
assert idx.is_monotonic_increasing is True
|
| 263 |
+
assert idx._is_strictly_monotonic_increasing is True
|
| 264 |
+
assert idx.is_monotonic_decreasing is False
|
| 265 |
+
assert idx._is_strictly_monotonic_decreasing is False
|
| 266 |
+
|
| 267 |
+
# decreasing non-overlapping
|
| 268 |
+
idx = IntervalIndex.from_tuples([(4, 5), (2, 3), (1, 2)], closed=closed)
|
| 269 |
+
assert idx.is_monotonic_increasing is False
|
| 270 |
+
assert idx._is_strictly_monotonic_increasing is False
|
| 271 |
+
assert idx.is_monotonic_decreasing is True
|
| 272 |
+
assert idx._is_strictly_monotonic_decreasing is True
|
| 273 |
+
|
| 274 |
+
# unordered non-overlapping
|
| 275 |
+
idx = IntervalIndex.from_tuples([(0, 1), (4, 5), (2, 3)], closed=closed)
|
| 276 |
+
assert idx.is_monotonic_increasing is False
|
| 277 |
+
assert idx._is_strictly_monotonic_increasing is False
|
| 278 |
+
assert idx.is_monotonic_decreasing is False
|
| 279 |
+
assert idx._is_strictly_monotonic_decreasing is False
|
| 280 |
+
|
| 281 |
+
# increasing overlapping
|
| 282 |
+
idx = IntervalIndex.from_tuples([(0, 2), (0.5, 2.5), (1, 3)], closed=closed)
|
| 283 |
+
assert idx.is_monotonic_increasing is True
|
| 284 |
+
assert idx._is_strictly_monotonic_increasing is True
|
| 285 |
+
assert idx.is_monotonic_decreasing is False
|
| 286 |
+
assert idx._is_strictly_monotonic_decreasing is False
|
| 287 |
+
|
| 288 |
+
# decreasing overlapping
|
| 289 |
+
idx = IntervalIndex.from_tuples([(1, 3), (0.5, 2.5), (0, 2)], closed=closed)
|
| 290 |
+
assert idx.is_monotonic_increasing is False
|
| 291 |
+
assert idx._is_strictly_monotonic_increasing is False
|
| 292 |
+
assert idx.is_monotonic_decreasing is True
|
| 293 |
+
assert idx._is_strictly_monotonic_decreasing is True
|
| 294 |
+
|
| 295 |
+
# unordered overlapping
|
| 296 |
+
idx = IntervalIndex.from_tuples([(0.5, 2.5), (0, 2), (1, 3)], closed=closed)
|
| 297 |
+
assert idx.is_monotonic_increasing is False
|
| 298 |
+
assert idx._is_strictly_monotonic_increasing is False
|
| 299 |
+
assert idx.is_monotonic_decreasing is False
|
| 300 |
+
assert idx._is_strictly_monotonic_decreasing is False
|
| 301 |
+
|
| 302 |
+
# increasing overlapping shared endpoints
|
| 303 |
+
idx = IntervalIndex.from_tuples([(1, 2), (1, 3), (2, 3)], closed=closed)
|
| 304 |
+
assert idx.is_monotonic_increasing is True
|
| 305 |
+
assert idx._is_strictly_monotonic_increasing is True
|
| 306 |
+
assert idx.is_monotonic_decreasing is False
|
| 307 |
+
assert idx._is_strictly_monotonic_decreasing is False
|
| 308 |
+
|
| 309 |
+
# decreasing overlapping shared endpoints
|
| 310 |
+
idx = IntervalIndex.from_tuples([(2, 3), (1, 3), (1, 2)], closed=closed)
|
| 311 |
+
assert idx.is_monotonic_increasing is False
|
| 312 |
+
assert idx._is_strictly_monotonic_increasing is False
|
| 313 |
+
assert idx.is_monotonic_decreasing is True
|
| 314 |
+
assert idx._is_strictly_monotonic_decreasing is True
|
| 315 |
+
|
| 316 |
+
# stationary
|
| 317 |
+
idx = IntervalIndex.from_tuples([(0, 1), (0, 1)], closed=closed)
|
| 318 |
+
assert idx.is_monotonic_increasing is True
|
| 319 |
+
assert idx._is_strictly_monotonic_increasing is False
|
| 320 |
+
assert idx.is_monotonic_decreasing is True
|
| 321 |
+
assert idx._is_strictly_monotonic_decreasing is False
|
| 322 |
+
|
| 323 |
+
# empty
|
| 324 |
+
idx = IntervalIndex([], closed=closed)
|
| 325 |
+
assert idx.is_monotonic_increasing is True
|
| 326 |
+
assert idx._is_strictly_monotonic_increasing is True
|
| 327 |
+
assert idx.is_monotonic_decreasing is True
|
| 328 |
+
assert idx._is_strictly_monotonic_decreasing is True
|
| 329 |
+
|
| 330 |
+
def test_is_monotonic_with_nans(self):
|
| 331 |
+
# GH#41831
|
| 332 |
+
index = IntervalIndex([np.nan, np.nan])
|
| 333 |
+
|
| 334 |
+
assert not index.is_monotonic_increasing
|
| 335 |
+
assert not index._is_strictly_monotonic_increasing
|
| 336 |
+
assert not index.is_monotonic_increasing
|
| 337 |
+
assert not index._is_strictly_monotonic_decreasing
|
| 338 |
+
assert not index.is_monotonic_decreasing
|
| 339 |
+
|
| 340 |
+
def test_get_item(self, closed):
|
| 341 |
+
i = IntervalIndex.from_arrays((0, 1, np.nan), (1, 2, np.nan), closed=closed)
|
| 342 |
+
assert i[0] == Interval(0.0, 1.0, closed=closed)
|
| 343 |
+
assert i[1] == Interval(1.0, 2.0, closed=closed)
|
| 344 |
+
assert isna(i[2])
|
| 345 |
+
|
| 346 |
+
result = i[0:1]
|
| 347 |
+
expected = IntervalIndex.from_arrays((0.0,), (1.0,), closed=closed)
|
| 348 |
+
tm.assert_index_equal(result, expected)
|
| 349 |
+
|
| 350 |
+
result = i[0:2]
|
| 351 |
+
expected = IntervalIndex.from_arrays((0.0, 1), (1.0, 2.0), closed=closed)
|
| 352 |
+
tm.assert_index_equal(result, expected)
|
| 353 |
+
|
| 354 |
+
result = i[1:3]
|
| 355 |
+
expected = IntervalIndex.from_arrays(
|
| 356 |
+
(1.0, np.nan), (2.0, np.nan), closed=closed
|
| 357 |
+
)
|
| 358 |
+
tm.assert_index_equal(result, expected)
|
| 359 |
+
|
| 360 |
+
@pytest.mark.parametrize(
|
| 361 |
+
"breaks",
|
| 362 |
+
[
|
| 363 |
+
date_range("20180101", periods=4),
|
| 364 |
+
date_range("20180101", periods=4, tz="US/Eastern"),
|
| 365 |
+
timedelta_range("0 days", periods=4),
|
| 366 |
+
],
|
| 367 |
+
ids=lambda x: str(x.dtype),
|
| 368 |
+
)
|
| 369 |
+
def test_maybe_convert_i8(self, breaks):
|
| 370 |
+
# GH 20636
|
| 371 |
+
index = IntervalIndex.from_breaks(breaks)
|
| 372 |
+
|
| 373 |
+
# intervalindex
|
| 374 |
+
result = index._maybe_convert_i8(index)
|
| 375 |
+
expected = IntervalIndex.from_breaks(breaks.asi8)
|
| 376 |
+
tm.assert_index_equal(result, expected)
|
| 377 |
+
|
| 378 |
+
# interval
|
| 379 |
+
interval = Interval(breaks[0], breaks[1])
|
| 380 |
+
result = index._maybe_convert_i8(interval)
|
| 381 |
+
expected = Interval(breaks[0]._value, breaks[1]._value)
|
| 382 |
+
assert result == expected
|
| 383 |
+
|
| 384 |
+
# datetimelike index
|
| 385 |
+
result = index._maybe_convert_i8(breaks)
|
| 386 |
+
expected = Index(breaks.asi8)
|
| 387 |
+
tm.assert_index_equal(result, expected)
|
| 388 |
+
|
| 389 |
+
# datetimelike scalar
|
| 390 |
+
result = index._maybe_convert_i8(breaks[0])
|
| 391 |
+
expected = breaks[0]._value
|
| 392 |
+
assert result == expected
|
| 393 |
+
|
| 394 |
+
# list-like of datetimelike scalars
|
| 395 |
+
result = index._maybe_convert_i8(list(breaks))
|
| 396 |
+
expected = Index(breaks.asi8)
|
| 397 |
+
tm.assert_index_equal(result, expected)
|
| 398 |
+
|
| 399 |
+
@pytest.mark.parametrize(
|
| 400 |
+
"breaks",
|
| 401 |
+
[date_range("2018-01-01", periods=5), timedelta_range("0 days", periods=5)],
|
| 402 |
+
)
|
| 403 |
+
def test_maybe_convert_i8_nat(self, breaks):
|
| 404 |
+
# GH 20636
|
| 405 |
+
index = IntervalIndex.from_breaks(breaks)
|
| 406 |
+
|
| 407 |
+
to_convert = breaks._constructor([pd.NaT] * 3)
|
| 408 |
+
expected = Index([np.nan] * 3, dtype=np.float64)
|
| 409 |
+
result = index._maybe_convert_i8(to_convert)
|
| 410 |
+
tm.assert_index_equal(result, expected)
|
| 411 |
+
|
| 412 |
+
to_convert = to_convert.insert(0, breaks[0])
|
| 413 |
+
expected = expected.insert(0, float(breaks[0]._value))
|
| 414 |
+
result = index._maybe_convert_i8(to_convert)
|
| 415 |
+
tm.assert_index_equal(result, expected)
|
| 416 |
+
|
| 417 |
+
@pytest.mark.parametrize(
|
| 418 |
+
"make_key",
|
| 419 |
+
[lambda breaks: breaks, list],
|
| 420 |
+
ids=["lambda", "list"],
|
| 421 |
+
)
|
| 422 |
+
def test_maybe_convert_i8_numeric(self, make_key, any_real_numpy_dtype):
|
| 423 |
+
# GH 20636
|
| 424 |
+
breaks = np.arange(5, dtype=any_real_numpy_dtype)
|
| 425 |
+
index = IntervalIndex.from_breaks(breaks)
|
| 426 |
+
key = make_key(breaks)
|
| 427 |
+
|
| 428 |
+
result = index._maybe_convert_i8(key)
|
| 429 |
+
kind = breaks.dtype.kind
|
| 430 |
+
expected_dtype = {"i": np.int64, "u": np.uint64, "f": np.float64}[kind]
|
| 431 |
+
expected = Index(key, dtype=expected_dtype)
|
| 432 |
+
tm.assert_index_equal(result, expected)
|
| 433 |
+
|
| 434 |
+
@pytest.mark.parametrize(
|
| 435 |
+
"make_key",
|
| 436 |
+
[
|
| 437 |
+
IntervalIndex.from_breaks,
|
| 438 |
+
lambda breaks: Interval(breaks[0], breaks[1]),
|
| 439 |
+
lambda breaks: breaks[0],
|
| 440 |
+
],
|
| 441 |
+
ids=["IntervalIndex", "Interval", "scalar"],
|
| 442 |
+
)
|
| 443 |
+
def test_maybe_convert_i8_numeric_identical(self, make_key, any_real_numpy_dtype):
|
| 444 |
+
# GH 20636
|
| 445 |
+
breaks = np.arange(5, dtype=any_real_numpy_dtype)
|
| 446 |
+
index = IntervalIndex.from_breaks(breaks)
|
| 447 |
+
key = make_key(breaks)
|
| 448 |
+
|
| 449 |
+
# test if _maybe_convert_i8 won't change key if an Interval or IntervalIndex
|
| 450 |
+
result = index._maybe_convert_i8(key)
|
| 451 |
+
assert result is key
|
| 452 |
+
|
| 453 |
+
@pytest.mark.parametrize(
|
| 454 |
+
"breaks1, breaks2",
|
| 455 |
+
permutations(
|
| 456 |
+
[
|
| 457 |
+
date_range("20180101", periods=4),
|
| 458 |
+
date_range("20180101", periods=4, tz="US/Eastern"),
|
| 459 |
+
timedelta_range("0 days", periods=4),
|
| 460 |
+
],
|
| 461 |
+
2,
|
| 462 |
+
),
|
| 463 |
+
ids=lambda x: str(x.dtype),
|
| 464 |
+
)
|
| 465 |
+
@pytest.mark.parametrize(
|
| 466 |
+
"make_key",
|
| 467 |
+
[
|
| 468 |
+
IntervalIndex.from_breaks,
|
| 469 |
+
lambda breaks: Interval(breaks[0], breaks[1]),
|
| 470 |
+
lambda breaks: breaks,
|
| 471 |
+
lambda breaks: breaks[0],
|
| 472 |
+
list,
|
| 473 |
+
],
|
| 474 |
+
ids=["IntervalIndex", "Interval", "Index", "scalar", "list"],
|
| 475 |
+
)
|
| 476 |
+
def test_maybe_convert_i8_errors(self, breaks1, breaks2, make_key):
|
| 477 |
+
# GH 20636
|
| 478 |
+
index = IntervalIndex.from_breaks(breaks1)
|
| 479 |
+
key = make_key(breaks2)
|
| 480 |
+
|
| 481 |
+
msg = (
|
| 482 |
+
f"Cannot index an IntervalIndex of subtype {breaks1.dtype} with "
|
| 483 |
+
f"values of dtype {breaks2.dtype}"
|
| 484 |
+
)
|
| 485 |
+
msg = re.escape(msg)
|
| 486 |
+
with pytest.raises(ValueError, match=msg):
|
| 487 |
+
index._maybe_convert_i8(key)
|
| 488 |
+
|
| 489 |
+
def test_contains_method(self):
|
| 490 |
+
# can select values that are IN the range of a value
|
| 491 |
+
i = IntervalIndex.from_arrays([0, 1], [1, 2])
|
| 492 |
+
|
| 493 |
+
expected = np.array([False, False], dtype="bool")
|
| 494 |
+
actual = i.contains(0)
|
| 495 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 496 |
+
actual = i.contains(3)
|
| 497 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 498 |
+
|
| 499 |
+
expected = np.array([True, False], dtype="bool")
|
| 500 |
+
actual = i.contains(0.5)
|
| 501 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 502 |
+
actual = i.contains(1)
|
| 503 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 504 |
+
|
| 505 |
+
# __contains__ not implemented for "interval in interval", follow
|
| 506 |
+
# that for the contains method for now
|
| 507 |
+
with pytest.raises(
|
| 508 |
+
NotImplementedError, match="contains not implemented for two"
|
| 509 |
+
):
|
| 510 |
+
i.contains(Interval(0, 1))
|
| 511 |
+
|
| 512 |
+
def test_dropna(self, closed):
|
| 513 |
+
expected = IntervalIndex.from_tuples([(0.0, 1.0), (1.0, 2.0)], closed=closed)
|
| 514 |
+
|
| 515 |
+
ii = IntervalIndex.from_tuples([(0, 1), (1, 2), np.nan], closed=closed)
|
| 516 |
+
result = ii.dropna()
|
| 517 |
+
tm.assert_index_equal(result, expected)
|
| 518 |
+
|
| 519 |
+
ii = IntervalIndex.from_arrays([0, 1, np.nan], [1, 2, np.nan], closed=closed)
|
| 520 |
+
result = ii.dropna()
|
| 521 |
+
tm.assert_index_equal(result, expected)
|
| 522 |
+
|
| 523 |
+
def test_non_contiguous(self, closed):
|
| 524 |
+
index = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=closed)
|
| 525 |
+
target = [0.5, 1.5, 2.5]
|
| 526 |
+
actual = index.get_indexer(target)
|
| 527 |
+
expected = np.array([0, -1, 1], dtype="intp")
|
| 528 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 529 |
+
|
| 530 |
+
assert 1.5 not in index
|
| 531 |
+
|
| 532 |
+
def test_isin(self, closed):
|
| 533 |
+
index = self.create_index(closed=closed)
|
| 534 |
+
|
| 535 |
+
expected = np.array([True] + [False] * (len(index) - 1))
|
| 536 |
+
result = index.isin(index[:1])
|
| 537 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 538 |
+
|
| 539 |
+
result = index.isin([index[0]])
|
| 540 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 541 |
+
|
| 542 |
+
other = IntervalIndex.from_breaks(np.arange(-2, 10), closed=closed)
|
| 543 |
+
expected = np.array([True] * (len(index) - 1) + [False])
|
| 544 |
+
result = index.isin(other)
|
| 545 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 546 |
+
|
| 547 |
+
result = index.isin(other.tolist())
|
| 548 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 549 |
+
|
| 550 |
+
for other_closed in ["right", "left", "both", "neither"]:
|
| 551 |
+
other = self.create_index(closed=other_closed)
|
| 552 |
+
expected = np.repeat(closed == other_closed, len(index))
|
| 553 |
+
result = index.isin(other)
|
| 554 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 555 |
+
|
| 556 |
+
result = index.isin(other.tolist())
|
| 557 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 558 |
+
|
| 559 |
+
def test_comparison(self):
|
| 560 |
+
actual = Interval(0, 1) < self.index
|
| 561 |
+
expected = np.array([False, True])
|
| 562 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 563 |
+
|
| 564 |
+
actual = Interval(0.5, 1.5) < self.index
|
| 565 |
+
expected = np.array([False, True])
|
| 566 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 567 |
+
actual = self.index > Interval(0.5, 1.5)
|
| 568 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 569 |
+
|
| 570 |
+
actual = self.index == self.index
|
| 571 |
+
expected = np.array([True, True])
|
| 572 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 573 |
+
actual = self.index <= self.index
|
| 574 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 575 |
+
actual = self.index >= self.index
|
| 576 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 577 |
+
|
| 578 |
+
actual = self.index < self.index
|
| 579 |
+
expected = np.array([False, False])
|
| 580 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 581 |
+
actual = self.index > self.index
|
| 582 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 583 |
+
|
| 584 |
+
actual = self.index == IntervalIndex.from_breaks([0, 1, 2], "left")
|
| 585 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 586 |
+
|
| 587 |
+
actual = self.index == self.index.values
|
| 588 |
+
tm.assert_numpy_array_equal(actual, np.array([True, True]))
|
| 589 |
+
actual = self.index.values == self.index
|
| 590 |
+
tm.assert_numpy_array_equal(actual, np.array([True, True]))
|
| 591 |
+
actual = self.index <= self.index.values
|
| 592 |
+
tm.assert_numpy_array_equal(actual, np.array([True, True]))
|
| 593 |
+
actual = self.index != self.index.values
|
| 594 |
+
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
| 595 |
+
actual = self.index > self.index.values
|
| 596 |
+
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
| 597 |
+
actual = self.index.values > self.index
|
| 598 |
+
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
| 599 |
+
|
| 600 |
+
# invalid comparisons
|
| 601 |
+
actual = self.index == 0
|
| 602 |
+
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
| 603 |
+
actual = self.index == self.index.left
|
| 604 |
+
tm.assert_numpy_array_equal(actual, np.array([False, False]))
|
| 605 |
+
|
| 606 |
+
msg = "|".join(
|
| 607 |
+
[
|
| 608 |
+
"not supported between instances of 'int' and '.*.Interval'",
|
| 609 |
+
r"Invalid comparison between dtype=interval\[int64, right\] and ",
|
| 610 |
+
]
|
| 611 |
+
)
|
| 612 |
+
with pytest.raises(TypeError, match=msg):
|
| 613 |
+
self.index > 0
|
| 614 |
+
with pytest.raises(TypeError, match=msg):
|
| 615 |
+
self.index <= 0
|
| 616 |
+
with pytest.raises(TypeError, match=msg):
|
| 617 |
+
self.index > np.arange(2)
|
| 618 |
+
|
| 619 |
+
msg = "Lengths must match to compare"
|
| 620 |
+
with pytest.raises(ValueError, match=msg):
|
| 621 |
+
self.index > np.arange(3)
|
| 622 |
+
|
| 623 |
+
def test_missing_values(self, closed):
|
| 624 |
+
idx = Index(
|
| 625 |
+
[np.nan, Interval(0, 1, closed=closed), Interval(1, 2, closed=closed)]
|
| 626 |
+
)
|
| 627 |
+
idx2 = IntervalIndex.from_arrays([np.nan, 0, 1], [np.nan, 1, 2], closed=closed)
|
| 628 |
+
assert idx.equals(idx2)
|
| 629 |
+
|
| 630 |
+
msg = (
|
| 631 |
+
"missing values must be missing in the same location both left "
|
| 632 |
+
"and right sides"
|
| 633 |
+
)
|
| 634 |
+
with pytest.raises(ValueError, match=msg):
|
| 635 |
+
IntervalIndex.from_arrays(
|
| 636 |
+
[np.nan, 0, 1], np.array([0, 1, 2]), closed=closed
|
| 637 |
+
)
|
| 638 |
+
|
| 639 |
+
tm.assert_numpy_array_equal(isna(idx), np.array([True, False, False]))
|
| 640 |
+
|
| 641 |
+
def test_sort_values(self, closed):
|
| 642 |
+
index = self.create_index(closed=closed)
|
| 643 |
+
|
| 644 |
+
result = index.sort_values()
|
| 645 |
+
tm.assert_index_equal(result, index)
|
| 646 |
+
|
| 647 |
+
result = index.sort_values(ascending=False)
|
| 648 |
+
tm.assert_index_equal(result, index[::-1])
|
| 649 |
+
|
| 650 |
+
# with nan
|
| 651 |
+
index = IntervalIndex([Interval(1, 2), np.nan, Interval(0, 1)])
|
| 652 |
+
|
| 653 |
+
result = index.sort_values()
|
| 654 |
+
expected = IntervalIndex([Interval(0, 1), Interval(1, 2), np.nan])
|
| 655 |
+
tm.assert_index_equal(result, expected)
|
| 656 |
+
|
| 657 |
+
result = index.sort_values(ascending=False, na_position="first")
|
| 658 |
+
expected = IntervalIndex([np.nan, Interval(1, 2), Interval(0, 1)])
|
| 659 |
+
tm.assert_index_equal(result, expected)
|
| 660 |
+
|
| 661 |
+
@pytest.mark.parametrize("tz", [None, "US/Eastern"])
|
| 662 |
+
def test_datetime(self, tz):
|
| 663 |
+
start = Timestamp("2000-01-01", tz=tz)
|
| 664 |
+
dates = date_range(start=start, periods=10)
|
| 665 |
+
index = IntervalIndex.from_breaks(dates)
|
| 666 |
+
|
| 667 |
+
# test mid
|
| 668 |
+
start = Timestamp("2000-01-01T12:00", tz=tz)
|
| 669 |
+
expected = date_range(start=start, periods=9)
|
| 670 |
+
tm.assert_index_equal(index.mid, expected)
|
| 671 |
+
|
| 672 |
+
# __contains__ doesn't check individual points
|
| 673 |
+
assert Timestamp("2000-01-01", tz=tz) not in index
|
| 674 |
+
assert Timestamp("2000-01-01T12", tz=tz) not in index
|
| 675 |
+
assert Timestamp("2000-01-02", tz=tz) not in index
|
| 676 |
+
iv_true = Interval(
|
| 677 |
+
Timestamp("2000-01-02", tz=tz), Timestamp("2000-01-03", tz=tz)
|
| 678 |
+
)
|
| 679 |
+
iv_false = Interval(
|
| 680 |
+
Timestamp("1999-12-31", tz=tz), Timestamp("2000-01-01", tz=tz)
|
| 681 |
+
)
|
| 682 |
+
assert iv_true in index
|
| 683 |
+
assert iv_false not in index
|
| 684 |
+
|
| 685 |
+
# .contains does check individual points
|
| 686 |
+
assert not index.contains(Timestamp("2000-01-01", tz=tz)).any()
|
| 687 |
+
assert index.contains(Timestamp("2000-01-01T12", tz=tz)).any()
|
| 688 |
+
assert index.contains(Timestamp("2000-01-02", tz=tz)).any()
|
| 689 |
+
|
| 690 |
+
# test get_indexer
|
| 691 |
+
start = Timestamp("1999-12-31T12:00", tz=tz)
|
| 692 |
+
target = date_range(start=start, periods=7, freq="12H")
|
| 693 |
+
actual = index.get_indexer(target)
|
| 694 |
+
expected = np.array([-1, -1, 0, 0, 1, 1, 2], dtype="intp")
|
| 695 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 696 |
+
|
| 697 |
+
start = Timestamp("2000-01-08T18:00", tz=tz)
|
| 698 |
+
target = date_range(start=start, periods=7, freq="6H")
|
| 699 |
+
actual = index.get_indexer(target)
|
| 700 |
+
expected = np.array([7, 7, 8, 8, 8, 8, -1], dtype="intp")
|
| 701 |
+
tm.assert_numpy_array_equal(actual, expected)
|
| 702 |
+
|
| 703 |
+
def test_append(self, closed):
|
| 704 |
+
index1 = IntervalIndex.from_arrays([0, 1], [1, 2], closed=closed)
|
| 705 |
+
index2 = IntervalIndex.from_arrays([1, 2], [2, 3], closed=closed)
|
| 706 |
+
|
| 707 |
+
result = index1.append(index2)
|
| 708 |
+
expected = IntervalIndex.from_arrays([0, 1, 1, 2], [1, 2, 2, 3], closed=closed)
|
| 709 |
+
tm.assert_index_equal(result, expected)
|
| 710 |
+
|
| 711 |
+
result = index1.append([index1, index2])
|
| 712 |
+
expected = IntervalIndex.from_arrays(
|
| 713 |
+
[0, 1, 0, 1, 1, 2], [1, 2, 1, 2, 2, 3], closed=closed
|
| 714 |
+
)
|
| 715 |
+
tm.assert_index_equal(result, expected)
|
| 716 |
+
|
| 717 |
+
for other_closed in {"left", "right", "both", "neither"} - {closed}:
|
| 718 |
+
index_other_closed = IntervalIndex.from_arrays(
|
| 719 |
+
[0, 1], [1, 2], closed=other_closed
|
| 720 |
+
)
|
| 721 |
+
result = index1.append(index_other_closed)
|
| 722 |
+
expected = index1.astype(object).append(index_other_closed.astype(object))
|
| 723 |
+
tm.assert_index_equal(result, expected)
|
| 724 |
+
|
| 725 |
+
def test_is_non_overlapping_monotonic(self, closed):
|
| 726 |
+
# Should be True in all cases
|
| 727 |
+
tpls = [(0, 1), (2, 3), (4, 5), (6, 7)]
|
| 728 |
+
idx = IntervalIndex.from_tuples(tpls, closed=closed)
|
| 729 |
+
assert idx.is_non_overlapping_monotonic is True
|
| 730 |
+
|
| 731 |
+
idx = IntervalIndex.from_tuples(tpls[::-1], closed=closed)
|
| 732 |
+
assert idx.is_non_overlapping_monotonic is True
|
| 733 |
+
|
| 734 |
+
# Should be False in all cases (overlapping)
|
| 735 |
+
tpls = [(0, 2), (1, 3), (4, 5), (6, 7)]
|
| 736 |
+
idx = IntervalIndex.from_tuples(tpls, closed=closed)
|
| 737 |
+
assert idx.is_non_overlapping_monotonic is False
|
| 738 |
+
|
| 739 |
+
idx = IntervalIndex.from_tuples(tpls[::-1], closed=closed)
|
| 740 |
+
assert idx.is_non_overlapping_monotonic is False
|
| 741 |
+
|
| 742 |
+
# Should be False in all cases (non-monotonic)
|
| 743 |
+
tpls = [(0, 1), (2, 3), (6, 7), (4, 5)]
|
| 744 |
+
idx = IntervalIndex.from_tuples(tpls, closed=closed)
|
| 745 |
+
assert idx.is_non_overlapping_monotonic is False
|
| 746 |
+
|
| 747 |
+
idx = IntervalIndex.from_tuples(tpls[::-1], closed=closed)
|
| 748 |
+
assert idx.is_non_overlapping_monotonic is False
|
| 749 |
+
|
| 750 |
+
# Should be False for closed='both', otherwise True (GH16560)
|
| 751 |
+
if closed == "both":
|
| 752 |
+
idx = IntervalIndex.from_breaks(range(4), closed=closed)
|
| 753 |
+
assert idx.is_non_overlapping_monotonic is False
|
| 754 |
+
else:
|
| 755 |
+
idx = IntervalIndex.from_breaks(range(4), closed=closed)
|
| 756 |
+
assert idx.is_non_overlapping_monotonic is True
|
| 757 |
+
|
| 758 |
+
@pytest.mark.parametrize(
|
| 759 |
+
"start, shift, na_value",
|
| 760 |
+
[
|
| 761 |
+
(0, 1, np.nan),
|
| 762 |
+
(Timestamp("2018-01-01"), Timedelta("1 day"), pd.NaT),
|
| 763 |
+
(Timedelta("0 days"), Timedelta("1 day"), pd.NaT),
|
| 764 |
+
],
|
| 765 |
+
)
|
| 766 |
+
def test_is_overlapping(self, start, shift, na_value, closed):
|
| 767 |
+
# GH 23309
|
| 768 |
+
# see test_interval_tree.py for extensive tests; interface tests here
|
| 769 |
+
|
| 770 |
+
# non-overlapping
|
| 771 |
+
tuples = [(start + n * shift, start + (n + 1) * shift) for n in (0, 2, 4)]
|
| 772 |
+
index = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 773 |
+
assert index.is_overlapping is False
|
| 774 |
+
|
| 775 |
+
# non-overlapping with NA
|
| 776 |
+
tuples = [(na_value, na_value)] + tuples + [(na_value, na_value)]
|
| 777 |
+
index = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 778 |
+
assert index.is_overlapping is False
|
| 779 |
+
|
| 780 |
+
# overlapping
|
| 781 |
+
tuples = [(start + n * shift, start + (n + 2) * shift) for n in range(3)]
|
| 782 |
+
index = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 783 |
+
assert index.is_overlapping is True
|
| 784 |
+
|
| 785 |
+
# overlapping with NA
|
| 786 |
+
tuples = [(na_value, na_value)] + tuples + [(na_value, na_value)]
|
| 787 |
+
index = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 788 |
+
assert index.is_overlapping is True
|
| 789 |
+
|
| 790 |
+
# common endpoints
|
| 791 |
+
tuples = [(start + n * shift, start + (n + 1) * shift) for n in range(3)]
|
| 792 |
+
index = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 793 |
+
result = index.is_overlapping
|
| 794 |
+
expected = closed == "both"
|
| 795 |
+
assert result is expected
|
| 796 |
+
|
| 797 |
+
# common endpoints with NA
|
| 798 |
+
tuples = [(na_value, na_value)] + tuples + [(na_value, na_value)]
|
| 799 |
+
index = IntervalIndex.from_tuples(tuples, closed=closed)
|
| 800 |
+
result = index.is_overlapping
|
| 801 |
+
assert result is expected
|
| 802 |
+
|
| 803 |
+
# intervals with duplicate left values
|
| 804 |
+
a = [10, 15, 20, 25, 30, 35, 40, 45, 45, 50, 55, 60, 65, 70, 75, 80, 85]
|
| 805 |
+
b = [15, 20, 25, 30, 35, 40, 45, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90]
|
| 806 |
+
index = IntervalIndex.from_arrays(a, b, closed="right")
|
| 807 |
+
result = index.is_overlapping
|
| 808 |
+
assert result is False
|
| 809 |
+
|
| 810 |
+
@pytest.mark.parametrize(
|
| 811 |
+
"tuples",
|
| 812 |
+
[
|
| 813 |
+
list(zip(range(10), range(1, 11))),
|
| 814 |
+
list(
|
| 815 |
+
zip(
|
| 816 |
+
date_range("20170101", periods=10),
|
| 817 |
+
date_range("20170101", periods=10),
|
| 818 |
+
)
|
| 819 |
+
),
|
| 820 |
+
list(
|
| 821 |
+
zip(
|
| 822 |
+
timedelta_range("0 days", periods=10),
|
| 823 |
+
timedelta_range("1 day", periods=10),
|
| 824 |
+
)
|
| 825 |
+
),
|
| 826 |
+
],
|
| 827 |
+
)
|
| 828 |
+
def test_to_tuples(self, tuples):
|
| 829 |
+
# GH 18756
|
| 830 |
+
idx = IntervalIndex.from_tuples(tuples)
|
| 831 |
+
result = idx.to_tuples()
|
| 832 |
+
expected = Index(com.asarray_tuplesafe(tuples))
|
| 833 |
+
tm.assert_index_equal(result, expected)
|
| 834 |
+
|
| 835 |
+
@pytest.mark.parametrize(
|
| 836 |
+
"tuples",
|
| 837 |
+
[
|
| 838 |
+
list(zip(range(10), range(1, 11))) + [np.nan],
|
| 839 |
+
list(
|
| 840 |
+
zip(
|
| 841 |
+
date_range("20170101", periods=10),
|
| 842 |
+
date_range("20170101", periods=10),
|
| 843 |
+
)
|
| 844 |
+
)
|
| 845 |
+
+ [np.nan],
|
| 846 |
+
list(
|
| 847 |
+
zip(
|
| 848 |
+
timedelta_range("0 days", periods=10),
|
| 849 |
+
timedelta_range("1 day", periods=10),
|
| 850 |
+
)
|
| 851 |
+
)
|
| 852 |
+
+ [np.nan],
|
| 853 |
+
],
|
| 854 |
+
)
|
| 855 |
+
@pytest.mark.parametrize("na_tuple", [True, False])
|
| 856 |
+
def test_to_tuples_na(self, tuples, na_tuple):
|
| 857 |
+
# GH 18756
|
| 858 |
+
idx = IntervalIndex.from_tuples(tuples)
|
| 859 |
+
result = idx.to_tuples(na_tuple=na_tuple)
|
| 860 |
+
|
| 861 |
+
# check the non-NA portion
|
| 862 |
+
expected_notna = Index(com.asarray_tuplesafe(tuples[:-1]))
|
| 863 |
+
result_notna = result[:-1]
|
| 864 |
+
tm.assert_index_equal(result_notna, expected_notna)
|
| 865 |
+
|
| 866 |
+
# check the NA portion
|
| 867 |
+
result_na = result[-1]
|
| 868 |
+
if na_tuple:
|
| 869 |
+
assert isinstance(result_na, tuple)
|
| 870 |
+
assert len(result_na) == 2
|
| 871 |
+
assert all(isna(x) for x in result_na)
|
| 872 |
+
else:
|
| 873 |
+
assert isna(result_na)
|
| 874 |
+
|
| 875 |
+
def test_nbytes(self):
|
| 876 |
+
# GH 19209
|
| 877 |
+
left = np.arange(0, 4, dtype="i8")
|
| 878 |
+
right = np.arange(1, 5, dtype="i8")
|
| 879 |
+
|
| 880 |
+
result = IntervalIndex.from_arrays(left, right).nbytes
|
| 881 |
+
expected = 64 # 4 * 8 * 2
|
| 882 |
+
assert result == expected
|
| 883 |
+
|
| 884 |
+
@pytest.mark.parametrize("new_closed", ["left", "right", "both", "neither"])
|
| 885 |
+
def test_set_closed(self, name, closed, new_closed):
|
| 886 |
+
# GH 21670
|
| 887 |
+
index = interval_range(0, 5, closed=closed, name=name)
|
| 888 |
+
result = index.set_closed(new_closed)
|
| 889 |
+
expected = interval_range(0, 5, closed=new_closed, name=name)
|
| 890 |
+
tm.assert_index_equal(result, expected)
|
| 891 |
+
|
| 892 |
+
@pytest.mark.parametrize("bad_closed", ["foo", 10, "LEFT", True, False])
|
| 893 |
+
def test_set_closed_errors(self, bad_closed):
|
| 894 |
+
# GH 21670
|
| 895 |
+
index = interval_range(0, 5)
|
| 896 |
+
msg = f"invalid option for 'closed': {bad_closed}"
|
| 897 |
+
with pytest.raises(ValueError, match=msg):
|
| 898 |
+
index.set_closed(bad_closed)
|
| 899 |
+
|
| 900 |
+
def test_is_all_dates(self):
|
| 901 |
+
# GH 23576
|
| 902 |
+
year_2017 = Interval(
|
| 903 |
+
Timestamp("2017-01-01 00:00:00"), Timestamp("2018-01-01 00:00:00")
|
| 904 |
+
)
|
| 905 |
+
year_2017_index = IntervalIndex([year_2017])
|
| 906 |
+
assert not year_2017_index._is_all_dates
|
| 907 |
+
|
| 908 |
+
|
| 909 |
+
def test_dir():
|
| 910 |
+
# GH#27571 dir(interval_index) should not raise
|
| 911 |
+
index = IntervalIndex.from_arrays([0, 1], [1, 2])
|
| 912 |
+
result = dir(index)
|
| 913 |
+
assert "str" not in result
|
| 914 |
+
|
| 915 |
+
|
| 916 |
+
def test_searchsorted_different_argument_classes(listlike_box):
|
| 917 |
+
# https://github.com/pandas-dev/pandas/issues/32762
|
| 918 |
+
values = IntervalIndex([Interval(0, 1), Interval(1, 2)])
|
| 919 |
+
result = values.searchsorted(listlike_box(values))
|
| 920 |
+
expected = np.array([0, 1], dtype=result.dtype)
|
| 921 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 922 |
+
|
| 923 |
+
result = values._data.searchsorted(listlike_box(values))
|
| 924 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 925 |
+
|
| 926 |
+
|
| 927 |
+
@pytest.mark.parametrize(
|
| 928 |
+
"arg", [[1, 2], ["a", "b"], [Timestamp("2020-01-01", tz="Europe/London")] * 2]
|
| 929 |
+
)
|
| 930 |
+
def test_searchsorted_invalid_argument(arg):
|
| 931 |
+
values = IntervalIndex([Interval(0, 1), Interval(1, 2)])
|
| 932 |
+
msg = "'<' not supported between instances of 'pandas._libs.interval.Interval' and "
|
| 933 |
+
with pytest.raises(TypeError, match=msg):
|
| 934 |
+
values.searchsorted(arg)
|