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 +4 -0
- videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/frame.cpython-310.pyc +3 -0
- videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/generic.cpython-310.pyc +3 -0
- videochat2/lib/python3.10/site-packages/pandas/core/indexes/__pycache__/multi.cpython-310.pyc +3 -0
- videochat2/lib/python3.10/site-packages/pandas/io/formats/__pycache__/style.cpython-310.pyc +3 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_reshape.py +85 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_setops.py +259 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_append.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_astype.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_category.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_constructors.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_equals.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_fillna.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_formats.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_indexing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_map.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_reindex.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_append.py +62 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_astype.py +90 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_category.py +396 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_constructors.py +142 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_equals.py +90 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_fillna.py +54 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_formats.py +113 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_indexing.py +422 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_map.py +115 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_reindex.py +78 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_drop_duplicates.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_equals.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_indexing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_is_monotonic.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_nat.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_sort_values.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_value_counts.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_drop_duplicates.py +89 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_equals.py +180 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_indexing.py +45 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_is_monotonic.py +46 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_nat.py +53 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_sort_values.py +315 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_value_counts.py +103 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/__pycache__/test_constructors.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/__pycache__/test_delete.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/__pycache__/test_formats.cpython-310.pyc +0 -0
.gitattributes
CHANGED
|
@@ -1277,3 +1277,7 @@ videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/timezones.cpython-31
|
|
| 1277 |
videochat2/lib/python3.10/site-packages/pandas/core/groupby/__pycache__/groupby.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1278 |
videochat2/lib/python3.10/site-packages/pandas/core/indexes/__pycache__/base.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1279 |
videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/series.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1277 |
videochat2/lib/python3.10/site-packages/pandas/core/groupby/__pycache__/groupby.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1278 |
videochat2/lib/python3.10/site-packages/pandas/core/indexes/__pycache__/base.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1279 |
videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/series.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1280 |
+
videochat2/lib/python3.10/site-packages/pandas/core/indexes/__pycache__/multi.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
|
videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/frame.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:31a7f8b966f0b02756ea53249aa03c4fbe1748cd727d2d1bcd66d6f5ccf23e33
|
| 3 |
+
size 327418
|
videochat2/lib/python3.10/site-packages/pandas/core/__pycache__/generic.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:182b3a78af2e6da3ccc23a18df85c42817c751f6342c1fcace667ea7e5ff9f75
|
| 3 |
+
size 348454
|
videochat2/lib/python3.10/site-packages/pandas/core/indexes/__pycache__/multi.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b36a3e05ea02b8f4bf2ebb40aca19c0a7cd0c6276834aa917fe0c6b9d4907cf4
|
| 3 |
+
size 102108
|
videochat2/lib/python3.10/site-packages/pandas/io/formats/__pycache__/style.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2420fe2903c45612936b4a836b8a724a1d3bcc5ef1ac7ee15aaf7675dd61f90c
|
| 3 |
+
size 131223
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_reshape.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for ndarray-like method on the base Index class
|
| 3 |
+
"""
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from pandas import Index
|
| 8 |
+
import pandas._testing as tm
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestReshape:
|
| 12 |
+
def test_repeat(self):
|
| 13 |
+
repeats = 2
|
| 14 |
+
index = Index([1, 2, 3])
|
| 15 |
+
expected = Index([1, 1, 2, 2, 3, 3])
|
| 16 |
+
|
| 17 |
+
result = index.repeat(repeats)
|
| 18 |
+
tm.assert_index_equal(result, expected)
|
| 19 |
+
|
| 20 |
+
def test_insert(self):
|
| 21 |
+
# GH 7256
|
| 22 |
+
# validate neg/pos inserts
|
| 23 |
+
result = Index(["b", "c", "d"])
|
| 24 |
+
|
| 25 |
+
# test 0th element
|
| 26 |
+
tm.assert_index_equal(Index(["a", "b", "c", "d"]), result.insert(0, "a"))
|
| 27 |
+
|
| 28 |
+
# test Nth element that follows Python list behavior
|
| 29 |
+
tm.assert_index_equal(Index(["b", "c", "e", "d"]), result.insert(-1, "e"))
|
| 30 |
+
|
| 31 |
+
# test loc +/- neq (0, -1)
|
| 32 |
+
tm.assert_index_equal(result.insert(1, "z"), result.insert(-2, "z"))
|
| 33 |
+
|
| 34 |
+
# test empty
|
| 35 |
+
null_index = Index([])
|
| 36 |
+
tm.assert_index_equal(Index(["a"]), null_index.insert(0, "a"))
|
| 37 |
+
|
| 38 |
+
def test_insert_missing(self, nulls_fixture):
|
| 39 |
+
# GH#22295
|
| 40 |
+
# test there is no mangling of NA values
|
| 41 |
+
expected = Index(["a", nulls_fixture, "b", "c"])
|
| 42 |
+
result = Index(list("abc")).insert(1, nulls_fixture)
|
| 43 |
+
tm.assert_index_equal(result, expected)
|
| 44 |
+
|
| 45 |
+
@pytest.mark.parametrize(
|
| 46 |
+
"val", [(1, 2), np.datetime64("2019-12-31"), np.timedelta64(1, "D")]
|
| 47 |
+
)
|
| 48 |
+
@pytest.mark.parametrize("loc", [-1, 2])
|
| 49 |
+
def test_insert_datetime_into_object(self, loc, val):
|
| 50 |
+
# GH#44509
|
| 51 |
+
idx = Index(["1", "2", "3"])
|
| 52 |
+
result = idx.insert(loc, val)
|
| 53 |
+
expected = Index(["1", "2", val, "3"])
|
| 54 |
+
tm.assert_index_equal(result, expected)
|
| 55 |
+
assert type(expected[2]) is type(val)
|
| 56 |
+
|
| 57 |
+
@pytest.mark.parametrize(
|
| 58 |
+
"pos,expected",
|
| 59 |
+
[
|
| 60 |
+
(0, Index(["b", "c", "d"], name="index")),
|
| 61 |
+
(-1, Index(["a", "b", "c"], name="index")),
|
| 62 |
+
],
|
| 63 |
+
)
|
| 64 |
+
def test_delete(self, pos, expected):
|
| 65 |
+
index = Index(["a", "b", "c", "d"], name="index")
|
| 66 |
+
result = index.delete(pos)
|
| 67 |
+
tm.assert_index_equal(result, expected)
|
| 68 |
+
assert result.name == expected.name
|
| 69 |
+
|
| 70 |
+
def test_delete_raises(self):
|
| 71 |
+
index = Index(["a", "b", "c", "d"], name="index")
|
| 72 |
+
msg = "index 5 is out of bounds for axis 0 with size 4"
|
| 73 |
+
with pytest.raises(IndexError, match=msg):
|
| 74 |
+
index.delete(5)
|
| 75 |
+
|
| 76 |
+
def test_append_multiple(self):
|
| 77 |
+
index = Index(["a", "b", "c", "d", "e", "f"])
|
| 78 |
+
|
| 79 |
+
foos = [index[:2], index[2:4], index[4:]]
|
| 80 |
+
result = foos[0].append(foos[1:])
|
| 81 |
+
tm.assert_index_equal(result, index)
|
| 82 |
+
|
| 83 |
+
# empty
|
| 84 |
+
result = index.append([])
|
| 85 |
+
tm.assert_index_equal(result, index)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/base_class/test_setops.py
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from pandas import (
|
| 8 |
+
Index,
|
| 9 |
+
Series,
|
| 10 |
+
)
|
| 11 |
+
import pandas._testing as tm
|
| 12 |
+
from pandas.core.algorithms import safe_sort
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TestIndexSetOps:
|
| 16 |
+
@pytest.mark.parametrize(
|
| 17 |
+
"method", ["union", "intersection", "difference", "symmetric_difference"]
|
| 18 |
+
)
|
| 19 |
+
def test_setops_sort_validation(self, method):
|
| 20 |
+
idx1 = Index(["a", "b"])
|
| 21 |
+
idx2 = Index(["b", "c"])
|
| 22 |
+
|
| 23 |
+
with pytest.raises(ValueError, match="The 'sort' keyword only takes"):
|
| 24 |
+
getattr(idx1, method)(idx2, sort=2)
|
| 25 |
+
|
| 26 |
+
# sort=True is supported as of GH#??
|
| 27 |
+
getattr(idx1, method)(idx2, sort=True)
|
| 28 |
+
|
| 29 |
+
def test_setops_preserve_object_dtype(self):
|
| 30 |
+
idx = Index([1, 2, 3], dtype=object)
|
| 31 |
+
result = idx.intersection(idx[1:])
|
| 32 |
+
expected = idx[1:]
|
| 33 |
+
tm.assert_index_equal(result, expected)
|
| 34 |
+
|
| 35 |
+
# if other is not monotonic increasing, intersection goes through
|
| 36 |
+
# a different route
|
| 37 |
+
result = idx.intersection(idx[1:][::-1])
|
| 38 |
+
tm.assert_index_equal(result, expected)
|
| 39 |
+
|
| 40 |
+
result = idx._union(idx[1:], sort=None)
|
| 41 |
+
expected = idx
|
| 42 |
+
tm.assert_numpy_array_equal(result, expected.values)
|
| 43 |
+
|
| 44 |
+
result = idx.union(idx[1:], sort=None)
|
| 45 |
+
tm.assert_index_equal(result, expected)
|
| 46 |
+
|
| 47 |
+
# if other is not monotonic increasing, _union goes through
|
| 48 |
+
# a different route
|
| 49 |
+
result = idx._union(idx[1:][::-1], sort=None)
|
| 50 |
+
tm.assert_numpy_array_equal(result, expected.values)
|
| 51 |
+
|
| 52 |
+
result = idx.union(idx[1:][::-1], sort=None)
|
| 53 |
+
tm.assert_index_equal(result, expected)
|
| 54 |
+
|
| 55 |
+
def test_union_base(self):
|
| 56 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 57 |
+
first = index[3:]
|
| 58 |
+
second = index[:5]
|
| 59 |
+
|
| 60 |
+
result = first.union(second)
|
| 61 |
+
|
| 62 |
+
expected = Index([0, 1, 2, "a", "b", "c"])
|
| 63 |
+
tm.assert_index_equal(result, expected)
|
| 64 |
+
|
| 65 |
+
@pytest.mark.parametrize("klass", [np.array, Series, list])
|
| 66 |
+
def test_union_different_type_base(self, klass):
|
| 67 |
+
# GH 10149
|
| 68 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 69 |
+
first = index[3:]
|
| 70 |
+
second = index[:5]
|
| 71 |
+
|
| 72 |
+
result = first.union(klass(second.values))
|
| 73 |
+
|
| 74 |
+
assert tm.equalContents(result, index)
|
| 75 |
+
|
| 76 |
+
def test_union_sort_other_incomparable(self):
|
| 77 |
+
# https://github.com/pandas-dev/pandas/issues/24959
|
| 78 |
+
idx = Index([1, pd.Timestamp("2000")])
|
| 79 |
+
# default (sort=None)
|
| 80 |
+
with tm.assert_produces_warning(RuntimeWarning):
|
| 81 |
+
result = idx.union(idx[:1])
|
| 82 |
+
|
| 83 |
+
tm.assert_index_equal(result, idx)
|
| 84 |
+
|
| 85 |
+
# sort=None
|
| 86 |
+
with tm.assert_produces_warning(RuntimeWarning):
|
| 87 |
+
result = idx.union(idx[:1], sort=None)
|
| 88 |
+
tm.assert_index_equal(result, idx)
|
| 89 |
+
|
| 90 |
+
# sort=False
|
| 91 |
+
result = idx.union(idx[:1], sort=False)
|
| 92 |
+
tm.assert_index_equal(result, idx)
|
| 93 |
+
|
| 94 |
+
def test_union_sort_other_incomparable_true(self):
|
| 95 |
+
idx = Index([1, pd.Timestamp("2000")])
|
| 96 |
+
with pytest.raises(TypeError, match=".*"):
|
| 97 |
+
idx.union(idx[:1], sort=True)
|
| 98 |
+
|
| 99 |
+
def test_intersection_equal_sort_true(self):
|
| 100 |
+
idx = Index(["c", "a", "b"])
|
| 101 |
+
sorted_ = Index(["a", "b", "c"])
|
| 102 |
+
tm.assert_index_equal(idx.intersection(idx, sort=True), sorted_)
|
| 103 |
+
|
| 104 |
+
def test_intersection_base(self, sort):
|
| 105 |
+
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
| 106 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 107 |
+
first = index[:5]
|
| 108 |
+
second = index[:3]
|
| 109 |
+
|
| 110 |
+
expected = Index([0, 1, "a"]) if sort is None else Index([0, "a", 1])
|
| 111 |
+
result = first.intersection(second, sort=sort)
|
| 112 |
+
tm.assert_index_equal(result, expected)
|
| 113 |
+
|
| 114 |
+
@pytest.mark.parametrize("klass", [np.array, Series, list])
|
| 115 |
+
def test_intersection_different_type_base(self, klass, sort):
|
| 116 |
+
# GH 10149
|
| 117 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 118 |
+
first = index[:5]
|
| 119 |
+
second = index[:3]
|
| 120 |
+
|
| 121 |
+
result = first.intersection(klass(second.values), sort=sort)
|
| 122 |
+
assert tm.equalContents(result, second)
|
| 123 |
+
|
| 124 |
+
def test_intersection_nosort(self):
|
| 125 |
+
result = Index(["c", "b", "a"]).intersection(["b", "a"])
|
| 126 |
+
expected = Index(["b", "a"])
|
| 127 |
+
tm.assert_index_equal(result, expected)
|
| 128 |
+
|
| 129 |
+
def test_intersection_equal_sort(self):
|
| 130 |
+
idx = Index(["c", "a", "b"])
|
| 131 |
+
tm.assert_index_equal(idx.intersection(idx, sort=False), idx)
|
| 132 |
+
tm.assert_index_equal(idx.intersection(idx, sort=None), idx)
|
| 133 |
+
|
| 134 |
+
def test_intersection_str_dates(self, sort):
|
| 135 |
+
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]
|
| 136 |
+
|
| 137 |
+
i1 = Index(dt_dates, dtype=object)
|
| 138 |
+
i2 = Index(["aa"], dtype=object)
|
| 139 |
+
result = i2.intersection(i1, sort=sort)
|
| 140 |
+
|
| 141 |
+
assert len(result) == 0
|
| 142 |
+
|
| 143 |
+
@pytest.mark.parametrize(
|
| 144 |
+
"index2,expected_arr",
|
| 145 |
+
[(Index(["B", "D"]), ["B"]), (Index(["B", "D", "A"]), ["A", "B"])],
|
| 146 |
+
)
|
| 147 |
+
def test_intersection_non_monotonic_non_unique(self, index2, expected_arr, sort):
|
| 148 |
+
# non-monotonic non-unique
|
| 149 |
+
index1 = Index(["A", "B", "A", "C"])
|
| 150 |
+
expected = Index(expected_arr, dtype="object")
|
| 151 |
+
result = index1.intersection(index2, sort=sort)
|
| 152 |
+
if sort is None:
|
| 153 |
+
expected = expected.sort_values()
|
| 154 |
+
tm.assert_index_equal(result, expected)
|
| 155 |
+
|
| 156 |
+
def test_difference_base(self, sort):
|
| 157 |
+
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
| 158 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 159 |
+
first = index[:4]
|
| 160 |
+
second = index[3:]
|
| 161 |
+
|
| 162 |
+
result = first.difference(second, sort)
|
| 163 |
+
expected = Index([0, "a", 1])
|
| 164 |
+
if sort is None:
|
| 165 |
+
expected = Index(safe_sort(expected))
|
| 166 |
+
tm.assert_index_equal(result, expected)
|
| 167 |
+
|
| 168 |
+
def test_symmetric_difference(self):
|
| 169 |
+
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
| 170 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 171 |
+
first = index[:4]
|
| 172 |
+
second = index[3:]
|
| 173 |
+
|
| 174 |
+
result = first.symmetric_difference(second)
|
| 175 |
+
expected = Index([0, 1, 2, "a", "c"])
|
| 176 |
+
tm.assert_index_equal(result, expected)
|
| 177 |
+
|
| 178 |
+
@pytest.mark.parametrize(
|
| 179 |
+
"method,expected,sort",
|
| 180 |
+
[
|
| 181 |
+
(
|
| 182 |
+
"intersection",
|
| 183 |
+
np.array(
|
| 184 |
+
[(1, "A"), (2, "A"), (1, "B"), (2, "B")],
|
| 185 |
+
dtype=[("num", int), ("let", "a1")],
|
| 186 |
+
),
|
| 187 |
+
False,
|
| 188 |
+
),
|
| 189 |
+
(
|
| 190 |
+
"intersection",
|
| 191 |
+
np.array(
|
| 192 |
+
[(1, "A"), (1, "B"), (2, "A"), (2, "B")],
|
| 193 |
+
dtype=[("num", int), ("let", "a1")],
|
| 194 |
+
),
|
| 195 |
+
None,
|
| 196 |
+
),
|
| 197 |
+
(
|
| 198 |
+
"union",
|
| 199 |
+
np.array(
|
| 200 |
+
[(1, "A"), (1, "B"), (1, "C"), (2, "A"), (2, "B"), (2, "C")],
|
| 201 |
+
dtype=[("num", int), ("let", "a1")],
|
| 202 |
+
),
|
| 203 |
+
None,
|
| 204 |
+
),
|
| 205 |
+
],
|
| 206 |
+
)
|
| 207 |
+
def test_tuple_union_bug(self, method, expected, sort):
|
| 208 |
+
index1 = Index(
|
| 209 |
+
np.array(
|
| 210 |
+
[(1, "A"), (2, "A"), (1, "B"), (2, "B")],
|
| 211 |
+
dtype=[("num", int), ("let", "a1")],
|
| 212 |
+
)
|
| 213 |
+
)
|
| 214 |
+
index2 = Index(
|
| 215 |
+
np.array(
|
| 216 |
+
[(1, "A"), (2, "A"), (1, "B"), (2, "B"), (1, "C"), (2, "C")],
|
| 217 |
+
dtype=[("num", int), ("let", "a1")],
|
| 218 |
+
)
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
result = getattr(index1, method)(index2, sort=sort)
|
| 222 |
+
assert result.ndim == 1
|
| 223 |
+
|
| 224 |
+
expected = Index(expected)
|
| 225 |
+
tm.assert_index_equal(result, expected)
|
| 226 |
+
|
| 227 |
+
@pytest.mark.parametrize("first_list", [["b", "a"], []])
|
| 228 |
+
@pytest.mark.parametrize("second_list", [["a", "b"], []])
|
| 229 |
+
@pytest.mark.parametrize(
|
| 230 |
+
"first_name, second_name, expected_name",
|
| 231 |
+
[("A", "B", None), (None, "B", None), ("A", None, None)],
|
| 232 |
+
)
|
| 233 |
+
def test_union_name_preservation(
|
| 234 |
+
self, first_list, second_list, first_name, second_name, expected_name, sort
|
| 235 |
+
):
|
| 236 |
+
first = Index(first_list, name=first_name)
|
| 237 |
+
second = Index(second_list, name=second_name)
|
| 238 |
+
union = first.union(second, sort=sort)
|
| 239 |
+
|
| 240 |
+
vals = set(first_list).union(second_list)
|
| 241 |
+
|
| 242 |
+
if sort is None and len(first_list) > 0 and len(second_list) > 0:
|
| 243 |
+
expected = Index(sorted(vals), name=expected_name)
|
| 244 |
+
tm.assert_index_equal(union, expected)
|
| 245 |
+
else:
|
| 246 |
+
expected = Index(vals, name=expected_name)
|
| 247 |
+
tm.equalContents(union, expected)
|
| 248 |
+
|
| 249 |
+
@pytest.mark.parametrize(
|
| 250 |
+
"diff_type, expected",
|
| 251 |
+
[["difference", [1, "B"]], ["symmetric_difference", [1, 2, "B", "C"]]],
|
| 252 |
+
)
|
| 253 |
+
def test_difference_object_type(self, diff_type, expected):
|
| 254 |
+
# GH 13432
|
| 255 |
+
idx1 = Index([0, 1, "A", "B"])
|
| 256 |
+
idx2 = Index([0, 2, "A", "C"])
|
| 257 |
+
result = getattr(idx1, diff_type)(idx2)
|
| 258 |
+
expected = Index(expected)
|
| 259 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (188 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_append.cpython-310.pyc
ADDED
|
Binary file (2.74 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_astype.cpython-310.pyc
ADDED
|
Binary file (2.34 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_category.cpython-310.pyc
ADDED
|
Binary file (11.5 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_constructors.cpython-310.pyc
ADDED
|
Binary file (3.89 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_equals.cpython-310.pyc
ADDED
|
Binary file (2.77 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_fillna.cpython-310.pyc
ADDED
|
Binary file (1.81 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_formats.cpython-310.pyc
ADDED
|
Binary file (5.01 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (13 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_map.cpython-310.pyc
ADDED
|
Binary file (3.68 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/__pycache__/test_reindex.cpython-310.pyc
ADDED
|
Binary file (3.2 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_append.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
CategoricalIndex,
|
| 5 |
+
Index,
|
| 6 |
+
)
|
| 7 |
+
import pandas._testing as tm
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestAppend:
|
| 11 |
+
@pytest.fixture
|
| 12 |
+
def ci(self):
|
| 13 |
+
categories = list("cab")
|
| 14 |
+
return CategoricalIndex(list("aabbca"), categories=categories, ordered=False)
|
| 15 |
+
|
| 16 |
+
def test_append(self, ci):
|
| 17 |
+
# append cats with the same categories
|
| 18 |
+
result = ci[:3].append(ci[3:])
|
| 19 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 20 |
+
|
| 21 |
+
foos = [ci[:1], ci[1:3], ci[3:]]
|
| 22 |
+
result = foos[0].append(foos[1:])
|
| 23 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 24 |
+
|
| 25 |
+
def test_append_empty(self, ci):
|
| 26 |
+
# empty
|
| 27 |
+
result = ci.append([])
|
| 28 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 29 |
+
|
| 30 |
+
def test_append_mismatched_categories(self, ci):
|
| 31 |
+
# appending with different categories or reordered is not ok
|
| 32 |
+
msg = "all inputs must be Index"
|
| 33 |
+
with pytest.raises(TypeError, match=msg):
|
| 34 |
+
ci.append(ci.values.set_categories(list("abcd")))
|
| 35 |
+
with pytest.raises(TypeError, match=msg):
|
| 36 |
+
ci.append(ci.values.reorder_categories(list("abc")))
|
| 37 |
+
|
| 38 |
+
def test_append_category_objects(self, ci):
|
| 39 |
+
# with objects
|
| 40 |
+
result = ci.append(Index(["c", "a"]))
|
| 41 |
+
expected = CategoricalIndex(list("aabbcaca"), categories=ci.categories)
|
| 42 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 43 |
+
|
| 44 |
+
def test_append_non_categories(self, ci):
|
| 45 |
+
# invalid objects -> cast to object via concat_compat
|
| 46 |
+
result = ci.append(Index(["a", "d"]))
|
| 47 |
+
expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"])
|
| 48 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 49 |
+
|
| 50 |
+
def test_append_object(self, ci):
|
| 51 |
+
# GH#14298 - if base object is not categorical -> coerce to object
|
| 52 |
+
result = Index(["c", "a"]).append(ci)
|
| 53 |
+
expected = Index(list("caaabbca"))
|
| 54 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 55 |
+
|
| 56 |
+
def test_append_to_another(self):
|
| 57 |
+
# hits Index._concat
|
| 58 |
+
fst = Index(["a", "b"])
|
| 59 |
+
snd = CategoricalIndex(["d", "e"])
|
| 60 |
+
result = fst.append(snd)
|
| 61 |
+
expected = Index(["a", "b", "d", "e"])
|
| 62 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_astype.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import date
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas import (
|
| 7 |
+
Categorical,
|
| 8 |
+
CategoricalDtype,
|
| 9 |
+
CategoricalIndex,
|
| 10 |
+
Index,
|
| 11 |
+
IntervalIndex,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestAstype:
|
| 17 |
+
def test_astype(self):
|
| 18 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 19 |
+
|
| 20 |
+
result = ci.astype(object)
|
| 21 |
+
tm.assert_index_equal(result, Index(np.array(ci)))
|
| 22 |
+
|
| 23 |
+
# this IS equal, but not the same class
|
| 24 |
+
assert result.equals(ci)
|
| 25 |
+
assert isinstance(result, Index)
|
| 26 |
+
assert not isinstance(result, CategoricalIndex)
|
| 27 |
+
|
| 28 |
+
# interval
|
| 29 |
+
ii = IntervalIndex.from_arrays(left=[-0.001, 2.0], right=[2, 4], closed="right")
|
| 30 |
+
|
| 31 |
+
ci = CategoricalIndex(
|
| 32 |
+
Categorical.from_codes([0, 1, -1], categories=ii, ordered=True)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
result = ci.astype("interval")
|
| 36 |
+
expected = ii.take([0, 1, -1], allow_fill=True, fill_value=np.nan)
|
| 37 |
+
tm.assert_index_equal(result, expected)
|
| 38 |
+
|
| 39 |
+
result = IntervalIndex(result.values)
|
| 40 |
+
tm.assert_index_equal(result, expected)
|
| 41 |
+
|
| 42 |
+
@pytest.mark.parametrize("name", [None, "foo"])
|
| 43 |
+
@pytest.mark.parametrize("dtype_ordered", [True, False])
|
| 44 |
+
@pytest.mark.parametrize("index_ordered", [True, False])
|
| 45 |
+
def test_astype_category(self, name, dtype_ordered, index_ordered):
|
| 46 |
+
# GH#18630
|
| 47 |
+
index = CategoricalIndex(
|
| 48 |
+
list("aabbca"), categories=list("cab"), ordered=index_ordered
|
| 49 |
+
)
|
| 50 |
+
if name:
|
| 51 |
+
index = index.rename(name)
|
| 52 |
+
|
| 53 |
+
# standard categories
|
| 54 |
+
dtype = CategoricalDtype(ordered=dtype_ordered)
|
| 55 |
+
result = index.astype(dtype)
|
| 56 |
+
expected = CategoricalIndex(
|
| 57 |
+
index.tolist(),
|
| 58 |
+
name=name,
|
| 59 |
+
categories=index.categories,
|
| 60 |
+
ordered=dtype_ordered,
|
| 61 |
+
)
|
| 62 |
+
tm.assert_index_equal(result, expected)
|
| 63 |
+
|
| 64 |
+
# non-standard categories
|
| 65 |
+
dtype = CategoricalDtype(index.unique().tolist()[:-1], dtype_ordered)
|
| 66 |
+
result = index.astype(dtype)
|
| 67 |
+
expected = CategoricalIndex(index.tolist(), name=name, dtype=dtype)
|
| 68 |
+
tm.assert_index_equal(result, expected)
|
| 69 |
+
|
| 70 |
+
if dtype_ordered is False:
|
| 71 |
+
# dtype='category' can't specify ordered, so only test once
|
| 72 |
+
result = index.astype("category")
|
| 73 |
+
expected = index
|
| 74 |
+
tm.assert_index_equal(result, expected)
|
| 75 |
+
|
| 76 |
+
@pytest.mark.parametrize("box", [True, False])
|
| 77 |
+
def test_categorical_date_roundtrip(self, box):
|
| 78 |
+
# astype to categorical and back should preserve date objects
|
| 79 |
+
v = date.today()
|
| 80 |
+
|
| 81 |
+
obj = Index([v, v])
|
| 82 |
+
assert obj.dtype == object
|
| 83 |
+
if box:
|
| 84 |
+
obj = obj.array
|
| 85 |
+
|
| 86 |
+
cat = obj.astype("category")
|
| 87 |
+
|
| 88 |
+
rtrip = cat.astype(object)
|
| 89 |
+
assert rtrip.dtype == object
|
| 90 |
+
assert type(rtrip[0]) is date
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_category.py
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas._libs import index as libindex
|
| 5 |
+
from pandas._libs.arrays import NDArrayBacked
|
| 6 |
+
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from pandas import (
|
| 9 |
+
Categorical,
|
| 10 |
+
CategoricalDtype,
|
| 11 |
+
)
|
| 12 |
+
import pandas._testing as tm
|
| 13 |
+
from pandas.core.indexes.api import (
|
| 14 |
+
CategoricalIndex,
|
| 15 |
+
Index,
|
| 16 |
+
)
|
| 17 |
+
from pandas.tests.indexes.common import Base
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class TestCategoricalIndex(Base):
|
| 21 |
+
_index_cls = CategoricalIndex
|
| 22 |
+
|
| 23 |
+
@pytest.fixture
|
| 24 |
+
def simple_index(self) -> CategoricalIndex:
|
| 25 |
+
return self._index_cls(list("aabbca"), categories=list("cab"), ordered=False)
|
| 26 |
+
|
| 27 |
+
@pytest.fixture
|
| 28 |
+
def index(self):
|
| 29 |
+
return tm.makeCategoricalIndex(100)
|
| 30 |
+
|
| 31 |
+
def create_index(self, *, categories=None, ordered=False):
|
| 32 |
+
if categories is None:
|
| 33 |
+
categories = list("cab")
|
| 34 |
+
return CategoricalIndex(list("aabbca"), categories=categories, ordered=ordered)
|
| 35 |
+
|
| 36 |
+
def test_can_hold_identifiers(self):
|
| 37 |
+
idx = self.create_index(categories=list("abcd"))
|
| 38 |
+
key = idx[0]
|
| 39 |
+
assert idx._can_hold_identifiers_and_holds_name(key) is True
|
| 40 |
+
|
| 41 |
+
def test_insert(self, simple_index):
|
| 42 |
+
ci = simple_index
|
| 43 |
+
categories = ci.categories
|
| 44 |
+
|
| 45 |
+
# test 0th element
|
| 46 |
+
result = ci.insert(0, "a")
|
| 47 |
+
expected = CategoricalIndex(list("aaabbca"), categories=categories)
|
| 48 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 49 |
+
|
| 50 |
+
# test Nth element that follows Python list behavior
|
| 51 |
+
result = ci.insert(-1, "a")
|
| 52 |
+
expected = CategoricalIndex(list("aabbcaa"), categories=categories)
|
| 53 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 54 |
+
|
| 55 |
+
# test empty
|
| 56 |
+
result = CategoricalIndex([], categories=categories).insert(0, "a")
|
| 57 |
+
expected = CategoricalIndex(["a"], categories=categories)
|
| 58 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 59 |
+
|
| 60 |
+
# invalid -> cast to object
|
| 61 |
+
expected = ci.astype(object).insert(0, "d")
|
| 62 |
+
result = ci.insert(0, "d")
|
| 63 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 64 |
+
|
| 65 |
+
# GH 18295 (test missing)
|
| 66 |
+
expected = CategoricalIndex(["a", np.nan, "a", "b", "c", "b"])
|
| 67 |
+
for na in (np.nan, pd.NaT, None):
|
| 68 |
+
result = CategoricalIndex(list("aabcb")).insert(1, na)
|
| 69 |
+
tm.assert_index_equal(result, expected)
|
| 70 |
+
|
| 71 |
+
def test_insert_na_mismatched_dtype(self):
|
| 72 |
+
ci = CategoricalIndex([0, 1, 1])
|
| 73 |
+
result = ci.insert(0, pd.NaT)
|
| 74 |
+
expected = Index([pd.NaT, 0, 1, 1], dtype=object)
|
| 75 |
+
tm.assert_index_equal(result, expected)
|
| 76 |
+
|
| 77 |
+
def test_delete(self, simple_index):
|
| 78 |
+
ci = simple_index
|
| 79 |
+
categories = ci.categories
|
| 80 |
+
|
| 81 |
+
result = ci.delete(0)
|
| 82 |
+
expected = CategoricalIndex(list("abbca"), categories=categories)
|
| 83 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 84 |
+
|
| 85 |
+
result = ci.delete(-1)
|
| 86 |
+
expected = CategoricalIndex(list("aabbc"), categories=categories)
|
| 87 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 88 |
+
|
| 89 |
+
with tm.external_error_raised((IndexError, ValueError)):
|
| 90 |
+
# Either depending on NumPy version
|
| 91 |
+
ci.delete(10)
|
| 92 |
+
|
| 93 |
+
@pytest.mark.parametrize(
|
| 94 |
+
"data, non_lexsorted_data",
|
| 95 |
+
[[[1, 2, 3], [9, 0, 1, 2, 3]], [list("abc"), list("fabcd")]],
|
| 96 |
+
)
|
| 97 |
+
def test_is_monotonic(self, data, non_lexsorted_data):
|
| 98 |
+
c = CategoricalIndex(data)
|
| 99 |
+
assert c.is_monotonic_increasing is True
|
| 100 |
+
assert c.is_monotonic_decreasing is False
|
| 101 |
+
|
| 102 |
+
c = CategoricalIndex(data, ordered=True)
|
| 103 |
+
assert c.is_monotonic_increasing is True
|
| 104 |
+
assert c.is_monotonic_decreasing is False
|
| 105 |
+
|
| 106 |
+
c = CategoricalIndex(data, categories=reversed(data))
|
| 107 |
+
assert c.is_monotonic_increasing is False
|
| 108 |
+
assert c.is_monotonic_decreasing is True
|
| 109 |
+
|
| 110 |
+
c = CategoricalIndex(data, categories=reversed(data), ordered=True)
|
| 111 |
+
assert c.is_monotonic_increasing is False
|
| 112 |
+
assert c.is_monotonic_decreasing is True
|
| 113 |
+
|
| 114 |
+
# test when data is neither monotonic increasing nor decreasing
|
| 115 |
+
reordered_data = [data[0], data[2], data[1]]
|
| 116 |
+
c = CategoricalIndex(reordered_data, categories=reversed(data))
|
| 117 |
+
assert c.is_monotonic_increasing is False
|
| 118 |
+
assert c.is_monotonic_decreasing is False
|
| 119 |
+
|
| 120 |
+
# non lexsorted categories
|
| 121 |
+
categories = non_lexsorted_data
|
| 122 |
+
|
| 123 |
+
c = CategoricalIndex(categories[:2], categories=categories)
|
| 124 |
+
assert c.is_monotonic_increasing is True
|
| 125 |
+
assert c.is_monotonic_decreasing is False
|
| 126 |
+
|
| 127 |
+
c = CategoricalIndex(categories[1:3], categories=categories)
|
| 128 |
+
assert c.is_monotonic_increasing is True
|
| 129 |
+
assert c.is_monotonic_decreasing is False
|
| 130 |
+
|
| 131 |
+
def test_has_duplicates(self):
|
| 132 |
+
idx = CategoricalIndex([0, 0, 0], name="foo")
|
| 133 |
+
assert idx.is_unique is False
|
| 134 |
+
assert idx.has_duplicates is True
|
| 135 |
+
|
| 136 |
+
idx = CategoricalIndex([0, 1], categories=[2, 3], name="foo")
|
| 137 |
+
assert idx.is_unique is False
|
| 138 |
+
assert idx.has_duplicates is True
|
| 139 |
+
|
| 140 |
+
idx = CategoricalIndex([0, 1, 2, 3], categories=[1, 2, 3], name="foo")
|
| 141 |
+
assert idx.is_unique is True
|
| 142 |
+
assert idx.has_duplicates is False
|
| 143 |
+
|
| 144 |
+
@pytest.mark.parametrize(
|
| 145 |
+
"data, categories, expected",
|
| 146 |
+
[
|
| 147 |
+
(
|
| 148 |
+
[1, 1, 1],
|
| 149 |
+
[1, 2, 3],
|
| 150 |
+
{
|
| 151 |
+
"first": np.array([False, True, True]),
|
| 152 |
+
"last": np.array([True, True, False]),
|
| 153 |
+
False: np.array([True, True, True]),
|
| 154 |
+
},
|
| 155 |
+
),
|
| 156 |
+
(
|
| 157 |
+
[1, 1, 1],
|
| 158 |
+
list("abc"),
|
| 159 |
+
{
|
| 160 |
+
"first": np.array([False, True, True]),
|
| 161 |
+
"last": np.array([True, True, False]),
|
| 162 |
+
False: np.array([True, True, True]),
|
| 163 |
+
},
|
| 164 |
+
),
|
| 165 |
+
(
|
| 166 |
+
[2, "a", "b"],
|
| 167 |
+
list("abc"),
|
| 168 |
+
{
|
| 169 |
+
"first": np.zeros(shape=(3), dtype=np.bool_),
|
| 170 |
+
"last": np.zeros(shape=(3), dtype=np.bool_),
|
| 171 |
+
False: np.zeros(shape=(3), dtype=np.bool_),
|
| 172 |
+
},
|
| 173 |
+
),
|
| 174 |
+
(
|
| 175 |
+
list("abb"),
|
| 176 |
+
list("abc"),
|
| 177 |
+
{
|
| 178 |
+
"first": np.array([False, False, True]),
|
| 179 |
+
"last": np.array([False, True, False]),
|
| 180 |
+
False: np.array([False, True, True]),
|
| 181 |
+
},
|
| 182 |
+
),
|
| 183 |
+
],
|
| 184 |
+
)
|
| 185 |
+
def test_drop_duplicates(self, data, categories, expected):
|
| 186 |
+
idx = CategoricalIndex(data, categories=categories, name="foo")
|
| 187 |
+
for keep, e in expected.items():
|
| 188 |
+
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), e)
|
| 189 |
+
e = idx[~e]
|
| 190 |
+
result = idx.drop_duplicates(keep=keep)
|
| 191 |
+
tm.assert_index_equal(result, e)
|
| 192 |
+
|
| 193 |
+
@pytest.mark.parametrize(
|
| 194 |
+
"data, categories, expected_data",
|
| 195 |
+
[
|
| 196 |
+
([1, 1, 1], [1, 2, 3], [1]),
|
| 197 |
+
([1, 1, 1], list("abc"), [np.nan]),
|
| 198 |
+
([1, 2, "a"], [1, 2, 3], [1, 2, np.nan]),
|
| 199 |
+
([2, "a", "b"], list("abc"), [np.nan, "a", "b"]),
|
| 200 |
+
],
|
| 201 |
+
)
|
| 202 |
+
def test_unique(self, data, categories, expected_data, ordered):
|
| 203 |
+
dtype = CategoricalDtype(categories, ordered=ordered)
|
| 204 |
+
|
| 205 |
+
idx = CategoricalIndex(data, dtype=dtype)
|
| 206 |
+
expected = CategoricalIndex(expected_data, dtype=dtype)
|
| 207 |
+
tm.assert_index_equal(idx.unique(), expected)
|
| 208 |
+
|
| 209 |
+
def test_repr_roundtrip(self):
|
| 210 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
| 211 |
+
str(ci)
|
| 212 |
+
tm.assert_index_equal(eval(repr(ci)), ci, exact=True)
|
| 213 |
+
|
| 214 |
+
# formatting
|
| 215 |
+
str(ci)
|
| 216 |
+
|
| 217 |
+
# long format
|
| 218 |
+
# this is not reprable
|
| 219 |
+
ci = CategoricalIndex(np.random.randint(0, 5, size=100))
|
| 220 |
+
str(ci)
|
| 221 |
+
|
| 222 |
+
def test_isin(self):
|
| 223 |
+
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
|
| 224 |
+
tm.assert_numpy_array_equal(
|
| 225 |
+
ci.isin(["c"]), np.array([False, False, False, True, False, False])
|
| 226 |
+
)
|
| 227 |
+
tm.assert_numpy_array_equal(
|
| 228 |
+
ci.isin(["c", "a", "b"]), np.array([True] * 5 + [False])
|
| 229 |
+
)
|
| 230 |
+
tm.assert_numpy_array_equal(
|
| 231 |
+
ci.isin(["c", "a", "b", np.nan]), np.array([True] * 6)
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
# mismatched categorical -> coerced to ndarray so doesn't matter
|
| 235 |
+
result = ci.isin(ci.set_categories(list("abcdefghi")))
|
| 236 |
+
expected = np.array([True] * 6)
|
| 237 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 238 |
+
|
| 239 |
+
result = ci.isin(ci.set_categories(list("defghi")))
|
| 240 |
+
expected = np.array([False] * 5 + [True])
|
| 241 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 242 |
+
|
| 243 |
+
def test_identical(self):
|
| 244 |
+
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
| 245 |
+
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
|
| 246 |
+
assert ci1.identical(ci1)
|
| 247 |
+
assert ci1.identical(ci1.copy())
|
| 248 |
+
assert not ci1.identical(ci2)
|
| 249 |
+
|
| 250 |
+
def test_ensure_copied_data(self, index):
|
| 251 |
+
# gh-12309: Check the "copy" argument of each
|
| 252 |
+
# Index.__new__ is honored.
|
| 253 |
+
#
|
| 254 |
+
# Must be tested separately from other indexes because
|
| 255 |
+
# self.values is not an ndarray.
|
| 256 |
+
|
| 257 |
+
result = CategoricalIndex(index.values, copy=True)
|
| 258 |
+
tm.assert_index_equal(index, result)
|
| 259 |
+
assert not np.shares_memory(result._data._codes, index._data._codes)
|
| 260 |
+
|
| 261 |
+
result = CategoricalIndex(index.values, copy=False)
|
| 262 |
+
assert result._data._codes is index._data._codes
|
| 263 |
+
|
| 264 |
+
def test_frame_repr(self):
|
| 265 |
+
df = pd.DataFrame({"A": [1, 2, 3]}, index=CategoricalIndex(["a", "b", "c"]))
|
| 266 |
+
result = repr(df)
|
| 267 |
+
expected = " A\na 1\nb 2\nc 3"
|
| 268 |
+
assert result == expected
|
| 269 |
+
|
| 270 |
+
def test_reindex_base(self):
|
| 271 |
+
# See test_reindex.py
|
| 272 |
+
pass
|
| 273 |
+
|
| 274 |
+
def test_map_str(self):
|
| 275 |
+
# See test_map.py
|
| 276 |
+
pass
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
class TestCategoricalIndex2:
|
| 280 |
+
# Tests that are not overriding a test in Base
|
| 281 |
+
|
| 282 |
+
def test_view_i8(self):
|
| 283 |
+
# GH#25464
|
| 284 |
+
ci = tm.makeCategoricalIndex(100)
|
| 285 |
+
msg = "When changing to a larger dtype, its size must be a divisor"
|
| 286 |
+
with pytest.raises(ValueError, match=msg):
|
| 287 |
+
ci.view("i8")
|
| 288 |
+
with pytest.raises(ValueError, match=msg):
|
| 289 |
+
ci._data.view("i8")
|
| 290 |
+
|
| 291 |
+
ci = ci[:-4] # length divisible by 8
|
| 292 |
+
|
| 293 |
+
res = ci.view("i8")
|
| 294 |
+
expected = ci._data.codes.view("i8")
|
| 295 |
+
tm.assert_numpy_array_equal(res, expected)
|
| 296 |
+
|
| 297 |
+
cat = ci._data
|
| 298 |
+
tm.assert_numpy_array_equal(cat.view("i8"), expected)
|
| 299 |
+
|
| 300 |
+
@pytest.mark.parametrize(
|
| 301 |
+
"dtype, engine_type",
|
| 302 |
+
[
|
| 303 |
+
(np.int8, libindex.Int8Engine),
|
| 304 |
+
(np.int16, libindex.Int16Engine),
|
| 305 |
+
(np.int32, libindex.Int32Engine),
|
| 306 |
+
(np.int64, libindex.Int64Engine),
|
| 307 |
+
],
|
| 308 |
+
)
|
| 309 |
+
def test_engine_type(self, dtype, engine_type):
|
| 310 |
+
if dtype != np.int64:
|
| 311 |
+
# num. of uniques required to push CategoricalIndex.codes to a
|
| 312 |
+
# dtype (128 categories required for .codes dtype to be int16 etc.)
|
| 313 |
+
num_uniques = {np.int8: 1, np.int16: 128, np.int32: 32768}[dtype]
|
| 314 |
+
ci = CategoricalIndex(range(num_uniques))
|
| 315 |
+
else:
|
| 316 |
+
# having 2**32 - 2**31 categories would be very memory-intensive,
|
| 317 |
+
# so we cheat a bit with the dtype
|
| 318 |
+
ci = CategoricalIndex(range(32768)) # == 2**16 - 2**(16 - 1)
|
| 319 |
+
arr = ci.values._ndarray.astype("int64")
|
| 320 |
+
NDArrayBacked.__init__(ci._data, arr, ci.dtype)
|
| 321 |
+
assert np.issubdtype(ci.codes.dtype, dtype)
|
| 322 |
+
assert isinstance(ci._engine, engine_type)
|
| 323 |
+
|
| 324 |
+
@pytest.mark.parametrize(
|
| 325 |
+
"func,op_name",
|
| 326 |
+
[
|
| 327 |
+
(lambda idx: idx - idx, "__sub__"),
|
| 328 |
+
(lambda idx: idx + idx, "__add__"),
|
| 329 |
+
(lambda idx: idx - ["a", "b"], "__sub__"),
|
| 330 |
+
(lambda idx: idx + ["a", "b"], "__add__"),
|
| 331 |
+
(lambda idx: ["a", "b"] - idx, "__rsub__"),
|
| 332 |
+
(lambda idx: ["a", "b"] + idx, "__radd__"),
|
| 333 |
+
],
|
| 334 |
+
)
|
| 335 |
+
def test_disallow_addsub_ops(self, func, op_name):
|
| 336 |
+
# GH 10039
|
| 337 |
+
# set ops (+/-) raise TypeError
|
| 338 |
+
idx = Index(Categorical(["a", "b"]))
|
| 339 |
+
cat_or_list = "'(Categorical|list)' and '(Categorical|list)'"
|
| 340 |
+
msg = "|".join(
|
| 341 |
+
[
|
| 342 |
+
f"cannot perform {op_name} with this index type: CategoricalIndex",
|
| 343 |
+
"can only concatenate list",
|
| 344 |
+
rf"unsupported operand type\(s\) for [\+-]: {cat_or_list}",
|
| 345 |
+
]
|
| 346 |
+
)
|
| 347 |
+
with pytest.raises(TypeError, match=msg):
|
| 348 |
+
func(idx)
|
| 349 |
+
|
| 350 |
+
def test_method_delegation(self):
|
| 351 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
| 352 |
+
result = ci.set_categories(list("cab"))
|
| 353 |
+
tm.assert_index_equal(
|
| 354 |
+
result, CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 358 |
+
result = ci.rename_categories(list("efg"))
|
| 359 |
+
tm.assert_index_equal(
|
| 360 |
+
result, CategoricalIndex(list("ffggef"), categories=list("efg"))
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
# GH18862 (let rename_categories take callables)
|
| 364 |
+
result = ci.rename_categories(lambda x: x.upper())
|
| 365 |
+
tm.assert_index_equal(
|
| 366 |
+
result, CategoricalIndex(list("AABBCA"), categories=list("CAB"))
|
| 367 |
+
)
|
| 368 |
+
|
| 369 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 370 |
+
result = ci.add_categories(["d"])
|
| 371 |
+
tm.assert_index_equal(
|
| 372 |
+
result, CategoricalIndex(list("aabbca"), categories=list("cabd"))
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 376 |
+
result = ci.remove_categories(["c"])
|
| 377 |
+
tm.assert_index_equal(
|
| 378 |
+
result,
|
| 379 |
+
CategoricalIndex(list("aabb") + [np.nan] + ["a"], categories=list("ab")),
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
| 383 |
+
result = ci.as_unordered()
|
| 384 |
+
tm.assert_index_equal(result, ci)
|
| 385 |
+
|
| 386 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
| 387 |
+
result = ci.as_ordered()
|
| 388 |
+
tm.assert_index_equal(
|
| 389 |
+
result,
|
| 390 |
+
CategoricalIndex(list("aabbca"), categories=list("cabdef"), ordered=True),
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
# invalid
|
| 394 |
+
msg = "cannot use inplace with CategoricalIndex"
|
| 395 |
+
with pytest.raises(ValueError, match=msg):
|
| 396 |
+
ci.set_categories(list("cab"), inplace=True)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_constructors.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Categorical,
|
| 6 |
+
CategoricalDtype,
|
| 7 |
+
CategoricalIndex,
|
| 8 |
+
Index,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestCategoricalIndexConstructors:
|
| 14 |
+
def test_construction_disallows_scalar(self):
|
| 15 |
+
msg = "must be called with a collection of some kind"
|
| 16 |
+
with pytest.raises(TypeError, match=msg):
|
| 17 |
+
CategoricalIndex(data=1, categories=list("abcd"), ordered=False)
|
| 18 |
+
with pytest.raises(TypeError, match=msg):
|
| 19 |
+
CategoricalIndex(categories=list("abcd"), ordered=False)
|
| 20 |
+
|
| 21 |
+
def test_construction(self):
|
| 22 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("abcd"), ordered=False)
|
| 23 |
+
categories = ci.categories
|
| 24 |
+
|
| 25 |
+
result = Index(ci)
|
| 26 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 27 |
+
assert not result.ordered
|
| 28 |
+
|
| 29 |
+
result = Index(ci.values)
|
| 30 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 31 |
+
assert not result.ordered
|
| 32 |
+
|
| 33 |
+
# empty
|
| 34 |
+
result = CategoricalIndex([], categories=categories)
|
| 35 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
| 36 |
+
tm.assert_numpy_array_equal(result.codes, np.array([], dtype="int8"))
|
| 37 |
+
assert not result.ordered
|
| 38 |
+
|
| 39 |
+
# passing categories
|
| 40 |
+
result = CategoricalIndex(list("aabbca"), categories=categories)
|
| 41 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
| 42 |
+
tm.assert_numpy_array_equal(
|
| 43 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
c = Categorical(list("aabbca"))
|
| 47 |
+
result = CategoricalIndex(c)
|
| 48 |
+
tm.assert_index_equal(result.categories, Index(list("abc")))
|
| 49 |
+
tm.assert_numpy_array_equal(
|
| 50 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
| 51 |
+
)
|
| 52 |
+
assert not result.ordered
|
| 53 |
+
|
| 54 |
+
result = CategoricalIndex(c, categories=categories)
|
| 55 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
| 56 |
+
tm.assert_numpy_array_equal(
|
| 57 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
| 58 |
+
)
|
| 59 |
+
assert not result.ordered
|
| 60 |
+
|
| 61 |
+
ci = CategoricalIndex(c, categories=list("abcd"))
|
| 62 |
+
result = CategoricalIndex(ci)
|
| 63 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
| 64 |
+
tm.assert_numpy_array_equal(
|
| 65 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
| 66 |
+
)
|
| 67 |
+
assert not result.ordered
|
| 68 |
+
|
| 69 |
+
result = CategoricalIndex(ci, categories=list("ab"))
|
| 70 |
+
tm.assert_index_equal(result.categories, Index(list("ab")))
|
| 71 |
+
tm.assert_numpy_array_equal(
|
| 72 |
+
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
|
| 73 |
+
)
|
| 74 |
+
assert not result.ordered
|
| 75 |
+
|
| 76 |
+
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
|
| 77 |
+
tm.assert_index_equal(result.categories, Index(list("ab")))
|
| 78 |
+
tm.assert_numpy_array_equal(
|
| 79 |
+
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
|
| 80 |
+
)
|
| 81 |
+
assert result.ordered
|
| 82 |
+
|
| 83 |
+
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
|
| 84 |
+
expected = CategoricalIndex(
|
| 85 |
+
ci, categories=list("ab"), ordered=True, dtype="category"
|
| 86 |
+
)
|
| 87 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 88 |
+
|
| 89 |
+
# turn me to an Index
|
| 90 |
+
result = Index(np.array(ci))
|
| 91 |
+
assert isinstance(result, Index)
|
| 92 |
+
assert not isinstance(result, CategoricalIndex)
|
| 93 |
+
|
| 94 |
+
def test_construction_with_dtype(self):
|
| 95 |
+
# specify dtype
|
| 96 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("abc"), ordered=False)
|
| 97 |
+
|
| 98 |
+
result = Index(np.array(ci), dtype="category")
|
| 99 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 100 |
+
|
| 101 |
+
result = Index(np.array(ci).tolist(), dtype="category")
|
| 102 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 103 |
+
|
| 104 |
+
# these are generally only equal when the categories are reordered
|
| 105 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 106 |
+
|
| 107 |
+
result = Index(np.array(ci), dtype="category").reorder_categories(ci.categories)
|
| 108 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 109 |
+
|
| 110 |
+
# make sure indexes are handled
|
| 111 |
+
idx = Index(range(3))
|
| 112 |
+
expected = CategoricalIndex([0, 1, 2], categories=idx, ordered=True)
|
| 113 |
+
result = CategoricalIndex(idx, categories=idx, ordered=True)
|
| 114 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 115 |
+
|
| 116 |
+
def test_construction_empty_with_bool_categories(self):
|
| 117 |
+
# see GH#22702
|
| 118 |
+
cat = CategoricalIndex([], categories=[True, False])
|
| 119 |
+
categories = sorted(cat.categories.tolist())
|
| 120 |
+
assert categories == [False, True]
|
| 121 |
+
|
| 122 |
+
def test_construction_with_categorical_dtype(self):
|
| 123 |
+
# construction with CategoricalDtype
|
| 124 |
+
# GH#18109
|
| 125 |
+
data, cats, ordered = "a a b b".split(), "c b a".split(), True
|
| 126 |
+
dtype = CategoricalDtype(categories=cats, ordered=ordered)
|
| 127 |
+
|
| 128 |
+
result = CategoricalIndex(data, dtype=dtype)
|
| 129 |
+
expected = CategoricalIndex(data, categories=cats, ordered=ordered)
|
| 130 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 131 |
+
|
| 132 |
+
# GH#19032
|
| 133 |
+
result = Index(data, dtype=dtype)
|
| 134 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 135 |
+
|
| 136 |
+
# error when combining categories/ordered and dtype kwargs
|
| 137 |
+
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
|
| 138 |
+
with pytest.raises(ValueError, match=msg):
|
| 139 |
+
CategoricalIndex(data, categories=cats, dtype=dtype)
|
| 140 |
+
|
| 141 |
+
with pytest.raises(ValueError, match=msg):
|
| 142 |
+
CategoricalIndex(data, ordered=ordered, dtype=dtype)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_equals.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Categorical,
|
| 6 |
+
CategoricalIndex,
|
| 7 |
+
Index,
|
| 8 |
+
MultiIndex,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestEquals:
|
| 13 |
+
def test_equals_categorical(self):
|
| 14 |
+
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
| 15 |
+
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
|
| 16 |
+
|
| 17 |
+
assert ci1.equals(ci1)
|
| 18 |
+
assert not ci1.equals(ci2)
|
| 19 |
+
assert ci1.equals(ci1.astype(object))
|
| 20 |
+
assert ci1.astype(object).equals(ci1)
|
| 21 |
+
|
| 22 |
+
assert (ci1 == ci1).all()
|
| 23 |
+
assert not (ci1 != ci1).all()
|
| 24 |
+
assert not (ci1 > ci1).all()
|
| 25 |
+
assert not (ci1 < ci1).all()
|
| 26 |
+
assert (ci1 <= ci1).all()
|
| 27 |
+
assert (ci1 >= ci1).all()
|
| 28 |
+
|
| 29 |
+
assert not (ci1 == 1).all()
|
| 30 |
+
assert (ci1 == Index(["a", "b"])).all()
|
| 31 |
+
assert (ci1 == ci1.values).all()
|
| 32 |
+
|
| 33 |
+
# invalid comparisons
|
| 34 |
+
with pytest.raises(ValueError, match="Lengths must match"):
|
| 35 |
+
ci1 == Index(["a", "b", "c"])
|
| 36 |
+
|
| 37 |
+
msg = "Categoricals can only be compared if 'categories' are the same"
|
| 38 |
+
with pytest.raises(TypeError, match=msg):
|
| 39 |
+
ci1 == ci2
|
| 40 |
+
with pytest.raises(TypeError, match=msg):
|
| 41 |
+
ci1 == Categorical(ci1.values, ordered=False)
|
| 42 |
+
with pytest.raises(TypeError, match=msg):
|
| 43 |
+
ci1 == Categorical(ci1.values, categories=list("abc"))
|
| 44 |
+
|
| 45 |
+
# tests
|
| 46 |
+
# make sure that we are testing for category inclusion properly
|
| 47 |
+
ci = CategoricalIndex(list("aabca"), categories=["c", "a", "b"])
|
| 48 |
+
assert not ci.equals(list("aabca"))
|
| 49 |
+
# Same categories, but different order
|
| 50 |
+
# Unordered
|
| 51 |
+
assert ci.equals(CategoricalIndex(list("aabca")))
|
| 52 |
+
# Ordered
|
| 53 |
+
assert not ci.equals(CategoricalIndex(list("aabca"), ordered=True))
|
| 54 |
+
assert ci.equals(ci.copy())
|
| 55 |
+
|
| 56 |
+
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
|
| 57 |
+
assert not ci.equals(list("aabca"))
|
| 58 |
+
assert not ci.equals(CategoricalIndex(list("aabca")))
|
| 59 |
+
assert ci.equals(ci.copy())
|
| 60 |
+
|
| 61 |
+
ci = CategoricalIndex(list("aabca") + [np.nan], categories=["c", "a", "b"])
|
| 62 |
+
assert not ci.equals(list("aabca") + [np.nan])
|
| 63 |
+
assert ci.equals(CategoricalIndex(list("aabca") + [np.nan]))
|
| 64 |
+
assert not ci.equals(CategoricalIndex(list("aabca") + [np.nan], ordered=True))
|
| 65 |
+
assert ci.equals(ci.copy())
|
| 66 |
+
|
| 67 |
+
def test_equals_categorical_unordered(self):
|
| 68 |
+
# https://github.com/pandas-dev/pandas/issues/16603
|
| 69 |
+
a = CategoricalIndex(["A"], categories=["A", "B"])
|
| 70 |
+
b = CategoricalIndex(["A"], categories=["B", "A"])
|
| 71 |
+
c = CategoricalIndex(["C"], categories=["B", "A"])
|
| 72 |
+
assert a.equals(b)
|
| 73 |
+
assert not a.equals(c)
|
| 74 |
+
assert not b.equals(c)
|
| 75 |
+
|
| 76 |
+
def test_equals_non_category(self):
|
| 77 |
+
# GH#37667 Case where other contains a value not among ci's
|
| 78 |
+
# categories ("D") and also contains np.nan
|
| 79 |
+
ci = CategoricalIndex(["A", "B", np.nan, np.nan])
|
| 80 |
+
other = Index(["A", "B", "D", np.nan])
|
| 81 |
+
|
| 82 |
+
assert not ci.equals(other)
|
| 83 |
+
|
| 84 |
+
def test_equals_multiindex(self):
|
| 85 |
+
# dont raise NotImplementedError when calling is_dtype_compat
|
| 86 |
+
|
| 87 |
+
mi = MultiIndex.from_arrays([["A", "B", "C", "D"], range(4)])
|
| 88 |
+
ci = mi.to_flat_index().astype("category")
|
| 89 |
+
|
| 90 |
+
assert not ci.equals(mi)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_fillna.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import CategoricalIndex
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TestFillNA:
|
| 9 |
+
def test_fillna_categorical(self):
|
| 10 |
+
# GH#11343
|
| 11 |
+
idx = CategoricalIndex([1.0, np.nan, 3.0, 1.0], name="x")
|
| 12 |
+
# fill by value in categories
|
| 13 |
+
exp = CategoricalIndex([1.0, 1.0, 3.0, 1.0], name="x")
|
| 14 |
+
tm.assert_index_equal(idx.fillna(1.0), exp)
|
| 15 |
+
|
| 16 |
+
cat = idx._data
|
| 17 |
+
|
| 18 |
+
# fill by value not in categories raises TypeError on EA, casts on CI
|
| 19 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
| 20 |
+
with pytest.raises(TypeError, match=msg):
|
| 21 |
+
cat.fillna(2.0)
|
| 22 |
+
|
| 23 |
+
result = idx.fillna(2.0)
|
| 24 |
+
expected = idx.astype(object).fillna(2.0)
|
| 25 |
+
tm.assert_index_equal(result, expected)
|
| 26 |
+
|
| 27 |
+
def test_fillna_copies_with_no_nas(self):
|
| 28 |
+
# Nothing to fill, should still get a copy for the Categorical method,
|
| 29 |
+
# but OK to get a view on CategoricalIndex method
|
| 30 |
+
ci = CategoricalIndex([0, 1, 1])
|
| 31 |
+
result = ci.fillna(0)
|
| 32 |
+
assert result is not ci
|
| 33 |
+
assert tm.shares_memory(result, ci)
|
| 34 |
+
|
| 35 |
+
# But at the EA level we always get a copy.
|
| 36 |
+
cat = ci._data
|
| 37 |
+
result = cat.fillna(0)
|
| 38 |
+
assert result._ndarray is not cat._ndarray
|
| 39 |
+
assert result._ndarray.base is None
|
| 40 |
+
assert not tm.shares_memory(result, cat)
|
| 41 |
+
|
| 42 |
+
def test_fillna_validates_with_no_nas(self):
|
| 43 |
+
# We validate the fill value even if fillna is a no-op
|
| 44 |
+
ci = CategoricalIndex([2, 3, 3])
|
| 45 |
+
cat = ci._data
|
| 46 |
+
|
| 47 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
| 48 |
+
res = ci.fillna(False)
|
| 49 |
+
# nothing to fill, so we dont cast
|
| 50 |
+
tm.assert_index_equal(res, ci)
|
| 51 |
+
|
| 52 |
+
# Same check directly on the Categorical
|
| 53 |
+
with pytest.raises(TypeError, match=msg):
|
| 54 |
+
cat.fillna(False)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_formats.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for CategoricalIndex.__repr__ and related methods.
|
| 3 |
+
"""
|
| 4 |
+
import pandas._config.config as cf
|
| 5 |
+
|
| 6 |
+
from pandas import CategoricalIndex
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestCategoricalIndexRepr:
|
| 10 |
+
def test_format_different_scalar_lengths(self):
|
| 11 |
+
# GH#35439
|
| 12 |
+
idx = CategoricalIndex(["aaaaaaaaa", "b"])
|
| 13 |
+
expected = ["aaaaaaaaa", "b"]
|
| 14 |
+
assert idx.format() == expected
|
| 15 |
+
|
| 16 |
+
def test_string_categorical_index_repr(self):
|
| 17 |
+
# short
|
| 18 |
+
idx = CategoricalIndex(["a", "bb", "ccc"])
|
| 19 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc'], categories=['a', 'bb', 'ccc'], ordered=False, dtype='category')""" # noqa:E501
|
| 20 |
+
assert repr(idx) == expected
|
| 21 |
+
|
| 22 |
+
# multiple lines
|
| 23 |
+
idx = CategoricalIndex(["a", "bb", "ccc"] * 10)
|
| 24 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',
|
| 25 |
+
'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb',
|
| 26 |
+
'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],
|
| 27 |
+
categories=['a', 'bb', 'ccc'], ordered=False, dtype='category')""" # noqa:E501
|
| 28 |
+
|
| 29 |
+
assert repr(idx) == expected
|
| 30 |
+
|
| 31 |
+
# truncated
|
| 32 |
+
idx = CategoricalIndex(["a", "bb", "ccc"] * 100)
|
| 33 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',
|
| 34 |
+
...
|
| 35 |
+
'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],
|
| 36 |
+
categories=['a', 'bb', 'ccc'], ordered=False, dtype='category', length=300)""" # noqa:E501
|
| 37 |
+
|
| 38 |
+
assert repr(idx) == expected
|
| 39 |
+
|
| 40 |
+
# larger categories
|
| 41 |
+
idx = CategoricalIndex(list("abcdefghijklmmo"))
|
| 42 |
+
expected = """CategoricalIndex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
|
| 43 |
+
'm', 'm', 'o'],
|
| 44 |
+
categories=['a', 'b', 'c', 'd', ..., 'k', 'l', 'm', 'o'], ordered=False, dtype='category')""" # noqa:E501
|
| 45 |
+
|
| 46 |
+
assert repr(idx) == expected
|
| 47 |
+
|
| 48 |
+
# short
|
| 49 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"])
|
| 50 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう'], categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa:E501
|
| 51 |
+
assert repr(idx) == expected
|
| 52 |
+
|
| 53 |
+
# multiple lines
|
| 54 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 10)
|
| 55 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ',
|
| 56 |
+
'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
| 57 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
| 58 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa:E501
|
| 59 |
+
|
| 60 |
+
assert repr(idx) == expected
|
| 61 |
+
|
| 62 |
+
# truncated
|
| 63 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 100)
|
| 64 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ',
|
| 65 |
+
...
|
| 66 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
| 67 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category', length=300)""" # noqa:E501
|
| 68 |
+
|
| 69 |
+
assert repr(idx) == expected
|
| 70 |
+
|
| 71 |
+
# larger categories
|
| 72 |
+
idx = CategoricalIndex(list("あいうえおかきくけこさしすせそ"))
|
| 73 |
+
expected = """CategoricalIndex(['あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ', 'さ', 'し',
|
| 74 |
+
'す', 'せ', 'そ'],
|
| 75 |
+
categories=['あ', 'い', 'う', 'え', ..., 'し', 'す', 'せ', 'そ'], ordered=False, dtype='category')""" # noqa:E501
|
| 76 |
+
|
| 77 |
+
assert repr(idx) == expected
|
| 78 |
+
|
| 79 |
+
# Enable Unicode option -----------------------------------------
|
| 80 |
+
with cf.option_context("display.unicode.east_asian_width", True):
|
| 81 |
+
# short
|
| 82 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"])
|
| 83 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう'], categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa:E501
|
| 84 |
+
assert repr(idx) == expected
|
| 85 |
+
|
| 86 |
+
# multiple lines
|
| 87 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 10)
|
| 88 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
| 89 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',
|
| 90 |
+
'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
| 91 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
| 92 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa:E501
|
| 93 |
+
|
| 94 |
+
assert repr(idx) == expected
|
| 95 |
+
|
| 96 |
+
# truncated
|
| 97 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 100)
|
| 98 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
| 99 |
+
'ううう', 'あ',
|
| 100 |
+
...
|
| 101 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',
|
| 102 |
+
'あ', 'いい', 'ううう'],
|
| 103 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category', length=300)""" # noqa:E501
|
| 104 |
+
|
| 105 |
+
assert repr(idx) == expected
|
| 106 |
+
|
| 107 |
+
# larger categories
|
| 108 |
+
idx = CategoricalIndex(list("あいうえおかきくけこさしすせそ"))
|
| 109 |
+
expected = """CategoricalIndex(['あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ',
|
| 110 |
+
'さ', 'し', 'す', 'せ', 'そ'],
|
| 111 |
+
categories=['あ', 'い', 'う', 'え', ..., 'し', 'す', 'せ', 'そ'], ordered=False, dtype='category')""" # noqa:E501
|
| 112 |
+
|
| 113 |
+
assert repr(idx) == expected
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_indexing.py
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas.errors import InvalidIndexError
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from pandas import (
|
| 8 |
+
CategoricalIndex,
|
| 9 |
+
Index,
|
| 10 |
+
IntervalIndex,
|
| 11 |
+
Timestamp,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestTake:
|
| 17 |
+
def test_take_fill_value(self):
|
| 18 |
+
# GH 12631
|
| 19 |
+
|
| 20 |
+
# numeric category
|
| 21 |
+
idx = CategoricalIndex([1, 2, 3], name="xxx")
|
| 22 |
+
result = idx.take(np.array([1, 0, -1]))
|
| 23 |
+
expected = CategoricalIndex([2, 1, 3], name="xxx")
|
| 24 |
+
tm.assert_index_equal(result, expected)
|
| 25 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 26 |
+
|
| 27 |
+
# fill_value
|
| 28 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
| 29 |
+
expected = CategoricalIndex([2, 1, np.nan], categories=[1, 2, 3], name="xxx")
|
| 30 |
+
tm.assert_index_equal(result, expected)
|
| 31 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 32 |
+
|
| 33 |
+
# allow_fill=False
|
| 34 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
| 35 |
+
expected = CategoricalIndex([2, 1, 3], name="xxx")
|
| 36 |
+
tm.assert_index_equal(result, expected)
|
| 37 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 38 |
+
|
| 39 |
+
# object category
|
| 40 |
+
idx = CategoricalIndex(
|
| 41 |
+
list("CBA"), categories=list("ABC"), ordered=True, name="xxx"
|
| 42 |
+
)
|
| 43 |
+
result = idx.take(np.array([1, 0, -1]))
|
| 44 |
+
expected = CategoricalIndex(
|
| 45 |
+
list("BCA"), categories=list("ABC"), ordered=True, name="xxx"
|
| 46 |
+
)
|
| 47 |
+
tm.assert_index_equal(result, expected)
|
| 48 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 49 |
+
|
| 50 |
+
# fill_value
|
| 51 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
| 52 |
+
expected = CategoricalIndex(
|
| 53 |
+
["B", "C", np.nan], categories=list("ABC"), ordered=True, name="xxx"
|
| 54 |
+
)
|
| 55 |
+
tm.assert_index_equal(result, expected)
|
| 56 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 57 |
+
|
| 58 |
+
# allow_fill=False
|
| 59 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
| 60 |
+
expected = CategoricalIndex(
|
| 61 |
+
list("BCA"), categories=list("ABC"), ordered=True, name="xxx"
|
| 62 |
+
)
|
| 63 |
+
tm.assert_index_equal(result, expected)
|
| 64 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 65 |
+
|
| 66 |
+
msg = (
|
| 67 |
+
"When allow_fill=True and fill_value is not None, "
|
| 68 |
+
"all indices must be >= -1"
|
| 69 |
+
)
|
| 70 |
+
with pytest.raises(ValueError, match=msg):
|
| 71 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
| 72 |
+
with pytest.raises(ValueError, match=msg):
|
| 73 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
| 74 |
+
|
| 75 |
+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
|
| 76 |
+
with pytest.raises(IndexError, match=msg):
|
| 77 |
+
idx.take(np.array([1, -5]))
|
| 78 |
+
|
| 79 |
+
def test_take_fill_value_datetime(self):
|
| 80 |
+
# datetime category
|
| 81 |
+
idx = pd.DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], name="xxx")
|
| 82 |
+
idx = CategoricalIndex(idx)
|
| 83 |
+
result = idx.take(np.array([1, 0, -1]))
|
| 84 |
+
expected = pd.DatetimeIndex(
|
| 85 |
+
["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx"
|
| 86 |
+
)
|
| 87 |
+
expected = CategoricalIndex(expected)
|
| 88 |
+
tm.assert_index_equal(result, expected)
|
| 89 |
+
|
| 90 |
+
# fill_value
|
| 91 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
| 92 |
+
expected = pd.DatetimeIndex(["2011-02-01", "2011-01-01", "NaT"], name="xxx")
|
| 93 |
+
exp_cats = pd.DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"])
|
| 94 |
+
expected = CategoricalIndex(expected, categories=exp_cats)
|
| 95 |
+
tm.assert_index_equal(result, expected)
|
| 96 |
+
|
| 97 |
+
# allow_fill=False
|
| 98 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
| 99 |
+
expected = pd.DatetimeIndex(
|
| 100 |
+
["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx"
|
| 101 |
+
)
|
| 102 |
+
expected = CategoricalIndex(expected)
|
| 103 |
+
tm.assert_index_equal(result, expected)
|
| 104 |
+
|
| 105 |
+
msg = (
|
| 106 |
+
"When allow_fill=True and fill_value is not None, "
|
| 107 |
+
"all indices must be >= -1"
|
| 108 |
+
)
|
| 109 |
+
with pytest.raises(ValueError, match=msg):
|
| 110 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
| 111 |
+
with pytest.raises(ValueError, match=msg):
|
| 112 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
| 113 |
+
|
| 114 |
+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
|
| 115 |
+
with pytest.raises(IndexError, match=msg):
|
| 116 |
+
idx.take(np.array([1, -5]))
|
| 117 |
+
|
| 118 |
+
def test_take_invalid_kwargs(self):
|
| 119 |
+
idx = CategoricalIndex([1, 2, 3], name="foo")
|
| 120 |
+
indices = [1, 0, -1]
|
| 121 |
+
|
| 122 |
+
msg = r"take\(\) got an unexpected keyword argument 'foo'"
|
| 123 |
+
with pytest.raises(TypeError, match=msg):
|
| 124 |
+
idx.take(indices, foo=2)
|
| 125 |
+
|
| 126 |
+
msg = "the 'out' parameter is not supported"
|
| 127 |
+
with pytest.raises(ValueError, match=msg):
|
| 128 |
+
idx.take(indices, out=indices)
|
| 129 |
+
|
| 130 |
+
msg = "the 'mode' parameter is not supported"
|
| 131 |
+
with pytest.raises(ValueError, match=msg):
|
| 132 |
+
idx.take(indices, mode="clip")
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
class TestGetLoc:
|
| 136 |
+
def test_get_loc(self):
|
| 137 |
+
# GH 12531
|
| 138 |
+
cidx1 = CategoricalIndex(list("abcde"), categories=list("edabc"))
|
| 139 |
+
idx1 = Index(list("abcde"))
|
| 140 |
+
assert cidx1.get_loc("a") == idx1.get_loc("a")
|
| 141 |
+
assert cidx1.get_loc("e") == idx1.get_loc("e")
|
| 142 |
+
|
| 143 |
+
for i in [cidx1, idx1]:
|
| 144 |
+
with pytest.raises(KeyError, match="'NOT-EXIST'"):
|
| 145 |
+
i.get_loc("NOT-EXIST")
|
| 146 |
+
|
| 147 |
+
# non-unique
|
| 148 |
+
cidx2 = CategoricalIndex(list("aacded"), categories=list("edabc"))
|
| 149 |
+
idx2 = Index(list("aacded"))
|
| 150 |
+
|
| 151 |
+
# results in bool array
|
| 152 |
+
res = cidx2.get_loc("d")
|
| 153 |
+
tm.assert_numpy_array_equal(res, idx2.get_loc("d"))
|
| 154 |
+
tm.assert_numpy_array_equal(
|
| 155 |
+
res, np.array([False, False, False, True, False, True])
|
| 156 |
+
)
|
| 157 |
+
# unique element results in scalar
|
| 158 |
+
res = cidx2.get_loc("e")
|
| 159 |
+
assert res == idx2.get_loc("e")
|
| 160 |
+
assert res == 4
|
| 161 |
+
|
| 162 |
+
for i in [cidx2, idx2]:
|
| 163 |
+
with pytest.raises(KeyError, match="'NOT-EXIST'"):
|
| 164 |
+
i.get_loc("NOT-EXIST")
|
| 165 |
+
|
| 166 |
+
# non-unique, sliceable
|
| 167 |
+
cidx3 = CategoricalIndex(list("aabbb"), categories=list("abc"))
|
| 168 |
+
idx3 = Index(list("aabbb"))
|
| 169 |
+
|
| 170 |
+
# results in slice
|
| 171 |
+
res = cidx3.get_loc("a")
|
| 172 |
+
assert res == idx3.get_loc("a")
|
| 173 |
+
assert res == slice(0, 2, None)
|
| 174 |
+
|
| 175 |
+
res = cidx3.get_loc("b")
|
| 176 |
+
assert res == idx3.get_loc("b")
|
| 177 |
+
assert res == slice(2, 5, None)
|
| 178 |
+
|
| 179 |
+
for i in [cidx3, idx3]:
|
| 180 |
+
with pytest.raises(KeyError, match="'c'"):
|
| 181 |
+
i.get_loc("c")
|
| 182 |
+
|
| 183 |
+
def test_get_loc_unique(self):
|
| 184 |
+
cidx = CategoricalIndex(list("abc"))
|
| 185 |
+
result = cidx.get_loc("b")
|
| 186 |
+
assert result == 1
|
| 187 |
+
|
| 188 |
+
def test_get_loc_monotonic_nonunique(self):
|
| 189 |
+
cidx = CategoricalIndex(list("abbc"))
|
| 190 |
+
result = cidx.get_loc("b")
|
| 191 |
+
expected = slice(1, 3, None)
|
| 192 |
+
assert result == expected
|
| 193 |
+
|
| 194 |
+
def test_get_loc_nonmonotonic_nonunique(self):
|
| 195 |
+
cidx = CategoricalIndex(list("abcb"))
|
| 196 |
+
result = cidx.get_loc("b")
|
| 197 |
+
expected = np.array([False, True, False, True], dtype=bool)
|
| 198 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 199 |
+
|
| 200 |
+
def test_get_loc_nan(self):
|
| 201 |
+
# GH#41933
|
| 202 |
+
ci = CategoricalIndex(["A", "B", np.nan])
|
| 203 |
+
res = ci.get_loc(np.nan)
|
| 204 |
+
|
| 205 |
+
assert res == 2
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
class TestGetIndexer:
|
| 209 |
+
def test_get_indexer_base(self):
|
| 210 |
+
# Determined by cat ordering.
|
| 211 |
+
idx = CategoricalIndex(list("cab"), categories=list("cab"))
|
| 212 |
+
expected = np.arange(len(idx), dtype=np.intp)
|
| 213 |
+
|
| 214 |
+
actual = idx.get_indexer(idx)
|
| 215 |
+
tm.assert_numpy_array_equal(expected, actual)
|
| 216 |
+
|
| 217 |
+
with pytest.raises(ValueError, match="Invalid fill method"):
|
| 218 |
+
idx.get_indexer(idx, method="invalid")
|
| 219 |
+
|
| 220 |
+
def test_get_indexer_requires_unique(self):
|
| 221 |
+
np.random.seed(123456789)
|
| 222 |
+
|
| 223 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 224 |
+
oidx = Index(np.array(ci))
|
| 225 |
+
|
| 226 |
+
msg = "Reindexing only valid with uniquely valued Index objects"
|
| 227 |
+
|
| 228 |
+
for n in [1, 2, 5, len(ci)]:
|
| 229 |
+
finder = oidx[np.random.randint(0, len(ci), size=n)]
|
| 230 |
+
|
| 231 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
| 232 |
+
ci.get_indexer(finder)
|
| 233 |
+
|
| 234 |
+
# see gh-17323
|
| 235 |
+
#
|
| 236 |
+
# Even when indexer is equal to the
|
| 237 |
+
# members in the index, we should
|
| 238 |
+
# respect duplicates instead of taking
|
| 239 |
+
# the fast-track path.
|
| 240 |
+
for finder in [list("aabbca"), list("aababca")]:
|
| 241 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
| 242 |
+
ci.get_indexer(finder)
|
| 243 |
+
|
| 244 |
+
def test_get_indexer_non_unique(self):
|
| 245 |
+
idx1 = CategoricalIndex(list("aabcde"), categories=list("edabc"))
|
| 246 |
+
idx2 = CategoricalIndex(list("abf"))
|
| 247 |
+
|
| 248 |
+
for indexer in [idx2, list("abf"), Index(list("abf"))]:
|
| 249 |
+
msg = "Reindexing only valid with uniquely valued Index objects"
|
| 250 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
| 251 |
+
idx1.get_indexer(indexer)
|
| 252 |
+
|
| 253 |
+
r1, _ = idx1.get_indexer_non_unique(indexer)
|
| 254 |
+
expected = np.array([0, 1, 2, -1], dtype=np.intp)
|
| 255 |
+
tm.assert_almost_equal(r1, expected)
|
| 256 |
+
|
| 257 |
+
def test_get_indexer_method(self):
|
| 258 |
+
idx1 = CategoricalIndex(list("aabcde"), categories=list("edabc"))
|
| 259 |
+
idx2 = CategoricalIndex(list("abf"))
|
| 260 |
+
|
| 261 |
+
msg = "method pad not yet implemented for CategoricalIndex"
|
| 262 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 263 |
+
idx2.get_indexer(idx1, method="pad")
|
| 264 |
+
msg = "method backfill not yet implemented for CategoricalIndex"
|
| 265 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 266 |
+
idx2.get_indexer(idx1, method="backfill")
|
| 267 |
+
|
| 268 |
+
msg = "method nearest not yet implemented for CategoricalIndex"
|
| 269 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 270 |
+
idx2.get_indexer(idx1, method="nearest")
|
| 271 |
+
|
| 272 |
+
def test_get_indexer_array(self):
|
| 273 |
+
arr = np.array(
|
| 274 |
+
[Timestamp("1999-12-31 00:00:00"), Timestamp("2000-12-31 00:00:00")],
|
| 275 |
+
dtype=object,
|
| 276 |
+
)
|
| 277 |
+
cats = [Timestamp("1999-12-31 00:00:00"), Timestamp("2000-12-31 00:00:00")]
|
| 278 |
+
ci = CategoricalIndex(cats, categories=cats, ordered=False, dtype="category")
|
| 279 |
+
result = ci.get_indexer(arr)
|
| 280 |
+
expected = np.array([0, 1], dtype="intp")
|
| 281 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 282 |
+
|
| 283 |
+
def test_get_indexer_same_categories_same_order(self):
|
| 284 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"])
|
| 285 |
+
|
| 286 |
+
result = ci.get_indexer(CategoricalIndex(["b", "b"], categories=["a", "b"]))
|
| 287 |
+
expected = np.array([1, 1], dtype="intp")
|
| 288 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 289 |
+
|
| 290 |
+
def test_get_indexer_same_categories_different_order(self):
|
| 291 |
+
# https://github.com/pandas-dev/pandas/issues/19551
|
| 292 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"])
|
| 293 |
+
|
| 294 |
+
result = ci.get_indexer(CategoricalIndex(["b", "b"], categories=["b", "a"]))
|
| 295 |
+
expected = np.array([1, 1], dtype="intp")
|
| 296 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 297 |
+
|
| 298 |
+
def test_get_indexer_nans_in_index_and_target(self):
|
| 299 |
+
# GH 45361
|
| 300 |
+
ci = CategoricalIndex([1, 2, np.nan, 3])
|
| 301 |
+
other1 = [2, 3, 4, np.nan]
|
| 302 |
+
res1 = ci.get_indexer(other1)
|
| 303 |
+
expected1 = np.array([1, 3, -1, 2], dtype=np.intp)
|
| 304 |
+
tm.assert_numpy_array_equal(res1, expected1)
|
| 305 |
+
other2 = [1, 4, 2, 3]
|
| 306 |
+
res2 = ci.get_indexer(other2)
|
| 307 |
+
expected2 = np.array([0, -1, 1, 3], dtype=np.intp)
|
| 308 |
+
tm.assert_numpy_array_equal(res2, expected2)
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
class TestWhere:
|
| 312 |
+
def test_where(self, listlike_box):
|
| 313 |
+
klass = listlike_box
|
| 314 |
+
|
| 315 |
+
i = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 316 |
+
cond = [True] * len(i)
|
| 317 |
+
expected = i
|
| 318 |
+
result = i.where(klass(cond))
|
| 319 |
+
tm.assert_index_equal(result, expected)
|
| 320 |
+
|
| 321 |
+
cond = [False] + [True] * (len(i) - 1)
|
| 322 |
+
expected = CategoricalIndex([np.nan] + i[1:].tolist(), categories=i.categories)
|
| 323 |
+
result = i.where(klass(cond))
|
| 324 |
+
tm.assert_index_equal(result, expected)
|
| 325 |
+
|
| 326 |
+
def test_where_non_categories(self):
|
| 327 |
+
ci = CategoricalIndex(["a", "b", "c", "d"])
|
| 328 |
+
mask = np.array([True, False, True, False])
|
| 329 |
+
|
| 330 |
+
result = ci.where(mask, 2)
|
| 331 |
+
expected = Index(["a", 2, "c", 2], dtype=object)
|
| 332 |
+
tm.assert_index_equal(result, expected)
|
| 333 |
+
|
| 334 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
| 335 |
+
with pytest.raises(TypeError, match=msg):
|
| 336 |
+
# Test the Categorical method directly
|
| 337 |
+
ci._data._where(mask, 2)
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
class TestContains:
|
| 341 |
+
def test_contains(self):
|
| 342 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"), ordered=False)
|
| 343 |
+
|
| 344 |
+
assert "a" in ci
|
| 345 |
+
assert "z" not in ci
|
| 346 |
+
assert "e" not in ci
|
| 347 |
+
assert np.nan not in ci
|
| 348 |
+
|
| 349 |
+
# assert codes NOT in index
|
| 350 |
+
assert 0 not in ci
|
| 351 |
+
assert 1 not in ci
|
| 352 |
+
|
| 353 |
+
def test_contains_nan(self):
|
| 354 |
+
ci = CategoricalIndex(list("aabbca") + [np.nan], categories=list("cabdef"))
|
| 355 |
+
assert np.nan in ci
|
| 356 |
+
|
| 357 |
+
@pytest.mark.parametrize("unwrap", [True, False])
|
| 358 |
+
def test_contains_na_dtype(self, unwrap):
|
| 359 |
+
dti = pd.date_range("2016-01-01", periods=100).insert(0, pd.NaT)
|
| 360 |
+
pi = dti.to_period("D")
|
| 361 |
+
tdi = dti - dti[-1]
|
| 362 |
+
ci = CategoricalIndex(dti)
|
| 363 |
+
|
| 364 |
+
obj = ci
|
| 365 |
+
if unwrap:
|
| 366 |
+
obj = ci._data
|
| 367 |
+
|
| 368 |
+
assert np.nan in obj
|
| 369 |
+
assert None in obj
|
| 370 |
+
assert pd.NaT in obj
|
| 371 |
+
assert np.datetime64("NaT") in obj
|
| 372 |
+
assert np.timedelta64("NaT") not in obj
|
| 373 |
+
|
| 374 |
+
obj2 = CategoricalIndex(tdi)
|
| 375 |
+
if unwrap:
|
| 376 |
+
obj2 = obj2._data
|
| 377 |
+
|
| 378 |
+
assert np.nan in obj2
|
| 379 |
+
assert None in obj2
|
| 380 |
+
assert pd.NaT in obj2
|
| 381 |
+
assert np.datetime64("NaT") not in obj2
|
| 382 |
+
assert np.timedelta64("NaT") in obj2
|
| 383 |
+
|
| 384 |
+
obj3 = CategoricalIndex(pi)
|
| 385 |
+
if unwrap:
|
| 386 |
+
obj3 = obj3._data
|
| 387 |
+
|
| 388 |
+
assert np.nan in obj3
|
| 389 |
+
assert None in obj3
|
| 390 |
+
assert pd.NaT in obj3
|
| 391 |
+
assert np.datetime64("NaT") not in obj3
|
| 392 |
+
assert np.timedelta64("NaT") not in obj3
|
| 393 |
+
|
| 394 |
+
@pytest.mark.parametrize(
|
| 395 |
+
"item, expected",
|
| 396 |
+
[
|
| 397 |
+
(pd.Interval(0, 1), True),
|
| 398 |
+
(1.5, True),
|
| 399 |
+
(pd.Interval(0.5, 1.5), False),
|
| 400 |
+
("a", False),
|
| 401 |
+
(Timestamp(1), False),
|
| 402 |
+
(pd.Timedelta(1), False),
|
| 403 |
+
],
|
| 404 |
+
ids=str,
|
| 405 |
+
)
|
| 406 |
+
def test_contains_interval(self, item, expected):
|
| 407 |
+
# GH 23705
|
| 408 |
+
ci = CategoricalIndex(IntervalIndex.from_breaks(range(3)))
|
| 409 |
+
result = item in ci
|
| 410 |
+
assert result is expected
|
| 411 |
+
|
| 412 |
+
def test_contains_list(self):
|
| 413 |
+
# GH#21729
|
| 414 |
+
idx = CategoricalIndex([1, 2, 3])
|
| 415 |
+
|
| 416 |
+
assert "a" not in idx
|
| 417 |
+
|
| 418 |
+
with pytest.raises(TypeError, match="unhashable type"):
|
| 419 |
+
["a"] in idx
|
| 420 |
+
|
| 421 |
+
with pytest.raises(TypeError, match="unhashable type"):
|
| 422 |
+
["a", "b"] in idx
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_map.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pandas import (
|
| 6 |
+
CategoricalIndex,
|
| 7 |
+
Index,
|
| 8 |
+
Series,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestMap:
|
| 14 |
+
@pytest.mark.parametrize(
|
| 15 |
+
"data, categories",
|
| 16 |
+
[
|
| 17 |
+
(list("abcbca"), list("cab")),
|
| 18 |
+
(pd.interval_range(0, 3).repeat(3), pd.interval_range(0, 3)),
|
| 19 |
+
],
|
| 20 |
+
ids=["string", "interval"],
|
| 21 |
+
)
|
| 22 |
+
def test_map_str(self, data, categories, ordered):
|
| 23 |
+
# GH 31202 - override base class since we want to maintain categorical/ordered
|
| 24 |
+
index = CategoricalIndex(data, categories=categories, ordered=ordered)
|
| 25 |
+
result = index.map(str)
|
| 26 |
+
expected = CategoricalIndex(
|
| 27 |
+
map(str, data), categories=map(str, categories), ordered=ordered
|
| 28 |
+
)
|
| 29 |
+
tm.assert_index_equal(result, expected)
|
| 30 |
+
|
| 31 |
+
def test_map(self):
|
| 32 |
+
ci = CategoricalIndex(list("ABABC"), categories=list("CBA"), ordered=True)
|
| 33 |
+
result = ci.map(lambda x: x.lower())
|
| 34 |
+
exp = CategoricalIndex(list("ababc"), categories=list("cba"), ordered=True)
|
| 35 |
+
tm.assert_index_equal(result, exp)
|
| 36 |
+
|
| 37 |
+
ci = CategoricalIndex(
|
| 38 |
+
list("ABABC"), categories=list("BAC"), ordered=False, name="XXX"
|
| 39 |
+
)
|
| 40 |
+
result = ci.map(lambda x: x.lower())
|
| 41 |
+
exp = CategoricalIndex(
|
| 42 |
+
list("ababc"), categories=list("bac"), ordered=False, name="XXX"
|
| 43 |
+
)
|
| 44 |
+
tm.assert_index_equal(result, exp)
|
| 45 |
+
|
| 46 |
+
# GH 12766: Return an index not an array
|
| 47 |
+
tm.assert_index_equal(
|
| 48 |
+
ci.map(lambda x: 1), Index(np.array([1] * 5, dtype=np.int64), name="XXX")
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# change categories dtype
|
| 52 |
+
ci = CategoricalIndex(list("ABABC"), categories=list("BAC"), ordered=False)
|
| 53 |
+
|
| 54 |
+
def f(x):
|
| 55 |
+
return {"A": 10, "B": 20, "C": 30}.get(x)
|
| 56 |
+
|
| 57 |
+
result = ci.map(f)
|
| 58 |
+
exp = CategoricalIndex(
|
| 59 |
+
[10, 20, 10, 20, 30], categories=[20, 10, 30], ordered=False
|
| 60 |
+
)
|
| 61 |
+
tm.assert_index_equal(result, exp)
|
| 62 |
+
|
| 63 |
+
result = ci.map(Series([10, 20, 30], index=["A", "B", "C"]))
|
| 64 |
+
tm.assert_index_equal(result, exp)
|
| 65 |
+
|
| 66 |
+
result = ci.map({"A": 10, "B": 20, "C": 30})
|
| 67 |
+
tm.assert_index_equal(result, exp)
|
| 68 |
+
|
| 69 |
+
def test_map_with_categorical_series(self):
|
| 70 |
+
# GH 12756
|
| 71 |
+
a = Index([1, 2, 3, 4])
|
| 72 |
+
b = Series(["even", "odd", "even", "odd"], dtype="category")
|
| 73 |
+
c = Series(["even", "odd", "even", "odd"])
|
| 74 |
+
|
| 75 |
+
exp = CategoricalIndex(["odd", "even", "odd", np.nan])
|
| 76 |
+
tm.assert_index_equal(a.map(b), exp)
|
| 77 |
+
exp = Index(["odd", "even", "odd", np.nan])
|
| 78 |
+
tm.assert_index_equal(a.map(c), exp)
|
| 79 |
+
|
| 80 |
+
@pytest.mark.parametrize(
|
| 81 |
+
("data", "f"),
|
| 82 |
+
(
|
| 83 |
+
([1, 1, np.nan], pd.isna),
|
| 84 |
+
([1, 2, np.nan], pd.isna),
|
| 85 |
+
([1, 1, np.nan], {1: False}),
|
| 86 |
+
([1, 2, np.nan], {1: False, 2: False}),
|
| 87 |
+
([1, 1, np.nan], Series([False, False])),
|
| 88 |
+
([1, 2, np.nan], Series([False, False, False])),
|
| 89 |
+
),
|
| 90 |
+
)
|
| 91 |
+
def test_map_with_nan(self, data, f): # GH 24241
|
| 92 |
+
values = pd.Categorical(data)
|
| 93 |
+
result = values.map(f)
|
| 94 |
+
if data[1] == 1:
|
| 95 |
+
expected = pd.Categorical([False, False, np.nan])
|
| 96 |
+
tm.assert_categorical_equal(result, expected)
|
| 97 |
+
else:
|
| 98 |
+
expected = Index([False, False, np.nan])
|
| 99 |
+
tm.assert_index_equal(result, expected)
|
| 100 |
+
|
| 101 |
+
def test_map_with_dict_or_series(self):
|
| 102 |
+
orig_values = ["a", "B", 1, "a"]
|
| 103 |
+
new_values = ["one", 2, 3.0, "one"]
|
| 104 |
+
cur_index = CategoricalIndex(orig_values, name="XXX")
|
| 105 |
+
expected = CategoricalIndex(new_values, name="XXX", categories=[3.0, 2, "one"])
|
| 106 |
+
|
| 107 |
+
mapper = Series(new_values[:-1], index=orig_values[:-1])
|
| 108 |
+
result = cur_index.map(mapper)
|
| 109 |
+
# Order of categories in result can be different
|
| 110 |
+
tm.assert_index_equal(result, expected)
|
| 111 |
+
|
| 112 |
+
mapper = dict(zip(orig_values[:-1], new_values[:-1]))
|
| 113 |
+
result = cur_index.map(mapper)
|
| 114 |
+
# Order of categories in result can be different
|
| 115 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/categorical/test_reindex.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Categorical,
|
| 6 |
+
CategoricalIndex,
|
| 7 |
+
Index,
|
| 8 |
+
Interval,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestReindex:
|
| 14 |
+
def test_reindex_list_non_unique(self):
|
| 15 |
+
# GH#11586
|
| 16 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
| 17 |
+
ci = CategoricalIndex(["a", "b", "c", "a"])
|
| 18 |
+
with pytest.raises(ValueError, match=msg):
|
| 19 |
+
ci.reindex(["a", "c"])
|
| 20 |
+
|
| 21 |
+
def test_reindex_categorical_non_unique(self):
|
| 22 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
| 23 |
+
ci = CategoricalIndex(["a", "b", "c", "a"])
|
| 24 |
+
with pytest.raises(ValueError, match=msg):
|
| 25 |
+
ci.reindex(Categorical(["a", "c"]))
|
| 26 |
+
|
| 27 |
+
def test_reindex_list_non_unique_unused_category(self):
|
| 28 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
| 29 |
+
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
| 30 |
+
with pytest.raises(ValueError, match=msg):
|
| 31 |
+
ci.reindex(["a", "c"])
|
| 32 |
+
|
| 33 |
+
def test_reindex_categorical_non_unique_unused_category(self):
|
| 34 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
| 35 |
+
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
| 36 |
+
with pytest.raises(ValueError, match=msg):
|
| 37 |
+
ci.reindex(Categorical(["a", "c"]))
|
| 38 |
+
|
| 39 |
+
def test_reindex_duplicate_target(self):
|
| 40 |
+
# See GH25459
|
| 41 |
+
cat = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c", "d"])
|
| 42 |
+
res, indexer = cat.reindex(["a", "c", "c"])
|
| 43 |
+
exp = Index(["a", "c", "c"], dtype="object")
|
| 44 |
+
tm.assert_index_equal(res, exp, exact=True)
|
| 45 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
|
| 46 |
+
|
| 47 |
+
res, indexer = cat.reindex(
|
| 48 |
+
CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
|
| 49 |
+
)
|
| 50 |
+
exp = CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
|
| 51 |
+
tm.assert_index_equal(res, exp, exact=True)
|
| 52 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
|
| 53 |
+
|
| 54 |
+
def test_reindex_empty_index(self):
|
| 55 |
+
# See GH16770
|
| 56 |
+
c = CategoricalIndex([])
|
| 57 |
+
res, indexer = c.reindex(["a", "b"])
|
| 58 |
+
tm.assert_index_equal(res, Index(["a", "b"]), exact=True)
|
| 59 |
+
tm.assert_numpy_array_equal(indexer, np.array([-1, -1], dtype=np.intp))
|
| 60 |
+
|
| 61 |
+
def test_reindex_categorical_added_category(self):
|
| 62 |
+
# GH 42424
|
| 63 |
+
ci = CategoricalIndex(
|
| 64 |
+
[Interval(0, 1, closed="right"), Interval(1, 2, closed="right")],
|
| 65 |
+
ordered=True,
|
| 66 |
+
)
|
| 67 |
+
ci_add = CategoricalIndex(
|
| 68 |
+
[
|
| 69 |
+
Interval(0, 1, closed="right"),
|
| 70 |
+
Interval(1, 2, closed="right"),
|
| 71 |
+
Interval(2, 3, closed="right"),
|
| 72 |
+
Interval(3, 4, closed="right"),
|
| 73 |
+
],
|
| 74 |
+
ordered=True,
|
| 75 |
+
)
|
| 76 |
+
result, _ = ci.reindex(ci_add)
|
| 77 |
+
expected = ci_add
|
| 78 |
+
tm.assert_index_equal(expected, result)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (190 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_drop_duplicates.cpython-310.pyc
ADDED
|
Binary file (2.94 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_equals.cpython-310.pyc
ADDED
|
Binary file (6.03 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (1.39 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_is_monotonic.cpython-310.pyc
ADDED
|
Binary file (1.01 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_nat.cpython-310.pyc
ADDED
|
Binary file (1.94 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_sort_values.cpython-310.pyc
ADDED
|
Binary file (7.61 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_value_counts.cpython-310.pyc
ADDED
|
Binary file (3.28 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_drop_duplicates.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
PeriodIndex,
|
| 6 |
+
Series,
|
| 7 |
+
date_range,
|
| 8 |
+
period_range,
|
| 9 |
+
timedelta_range,
|
| 10 |
+
)
|
| 11 |
+
import pandas._testing as tm
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class DropDuplicates:
|
| 15 |
+
def test_drop_duplicates_metadata(self, idx):
|
| 16 |
+
# GH#10115
|
| 17 |
+
result = idx.drop_duplicates()
|
| 18 |
+
tm.assert_index_equal(idx, result)
|
| 19 |
+
assert idx.freq == result.freq
|
| 20 |
+
|
| 21 |
+
idx_dup = idx.append(idx)
|
| 22 |
+
result = idx_dup.drop_duplicates()
|
| 23 |
+
|
| 24 |
+
expected = idx
|
| 25 |
+
if not isinstance(idx, PeriodIndex):
|
| 26 |
+
# freq is reset except for PeriodIndex
|
| 27 |
+
assert idx_dup.freq is None
|
| 28 |
+
assert result.freq is None
|
| 29 |
+
expected = idx._with_freq(None)
|
| 30 |
+
else:
|
| 31 |
+
assert result.freq == expected.freq
|
| 32 |
+
|
| 33 |
+
tm.assert_index_equal(result, expected)
|
| 34 |
+
|
| 35 |
+
@pytest.mark.parametrize(
|
| 36 |
+
"keep, expected, index",
|
| 37 |
+
[
|
| 38 |
+
(
|
| 39 |
+
"first",
|
| 40 |
+
np.concatenate(([False] * 10, [True] * 5)),
|
| 41 |
+
np.arange(0, 10, dtype=np.int64),
|
| 42 |
+
),
|
| 43 |
+
(
|
| 44 |
+
"last",
|
| 45 |
+
np.concatenate(([True] * 5, [False] * 10)),
|
| 46 |
+
np.arange(5, 15, dtype=np.int64),
|
| 47 |
+
),
|
| 48 |
+
(
|
| 49 |
+
False,
|
| 50 |
+
np.concatenate(([True] * 5, [False] * 5, [True] * 5)),
|
| 51 |
+
np.arange(5, 10, dtype=np.int64),
|
| 52 |
+
),
|
| 53 |
+
],
|
| 54 |
+
)
|
| 55 |
+
def test_drop_duplicates(self, keep, expected, index, idx):
|
| 56 |
+
# to check Index/Series compat
|
| 57 |
+
idx = idx.append(idx[:5])
|
| 58 |
+
|
| 59 |
+
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), expected)
|
| 60 |
+
expected = idx[~expected]
|
| 61 |
+
|
| 62 |
+
result = idx.drop_duplicates(keep=keep)
|
| 63 |
+
tm.assert_index_equal(result, expected)
|
| 64 |
+
|
| 65 |
+
result = Series(idx).drop_duplicates(keep=keep)
|
| 66 |
+
expected = Series(expected, index=index)
|
| 67 |
+
tm.assert_series_equal(result, expected)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class TestDropDuplicatesPeriodIndex(DropDuplicates):
|
| 71 |
+
@pytest.fixture(params=["D", "3D", "H", "2H", "T", "2T", "S", "3S"])
|
| 72 |
+
def freq(self, request):
|
| 73 |
+
return request.param
|
| 74 |
+
|
| 75 |
+
@pytest.fixture
|
| 76 |
+
def idx(self, freq):
|
| 77 |
+
return period_range("2011-01-01", periods=10, freq=freq, name="idx")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class TestDropDuplicatesDatetimeIndex(DropDuplicates):
|
| 81 |
+
@pytest.fixture
|
| 82 |
+
def idx(self, freq_sample):
|
| 83 |
+
return date_range("2011-01-01", freq=freq_sample, periods=10, name="idx")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
class TestDropDuplicatesTimedeltaIndex(DropDuplicates):
|
| 87 |
+
@pytest.fixture
|
| 88 |
+
def idx(self, freq_sample):
|
| 89 |
+
return timedelta_range("1 day", periods=10, freq=freq_sample, name="idx")
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_equals.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests shared for DatetimeIndex/TimedeltaIndex/PeriodIndex
|
| 3 |
+
"""
|
| 4 |
+
from datetime import (
|
| 5 |
+
datetime,
|
| 6 |
+
timedelta,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
import pytest
|
| 11 |
+
|
| 12 |
+
import pandas as pd
|
| 13 |
+
from pandas import (
|
| 14 |
+
CategoricalIndex,
|
| 15 |
+
DatetimeIndex,
|
| 16 |
+
Index,
|
| 17 |
+
PeriodIndex,
|
| 18 |
+
TimedeltaIndex,
|
| 19 |
+
date_range,
|
| 20 |
+
period_range,
|
| 21 |
+
)
|
| 22 |
+
import pandas._testing as tm
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class EqualsTests:
|
| 26 |
+
def test_not_equals_numeric(self, index):
|
| 27 |
+
assert not index.equals(Index(index.asi8))
|
| 28 |
+
assert not index.equals(Index(index.asi8.astype("u8")))
|
| 29 |
+
assert not index.equals(Index(index.asi8).astype("f8"))
|
| 30 |
+
|
| 31 |
+
def test_equals(self, index):
|
| 32 |
+
assert index.equals(index)
|
| 33 |
+
assert index.equals(index.astype(object))
|
| 34 |
+
assert index.equals(CategoricalIndex(index))
|
| 35 |
+
assert index.equals(CategoricalIndex(index.astype(object)))
|
| 36 |
+
|
| 37 |
+
def test_not_equals_non_arraylike(self, index):
|
| 38 |
+
assert not index.equals(list(index))
|
| 39 |
+
|
| 40 |
+
def test_not_equals_strings(self, index):
|
| 41 |
+
other = Index([str(x) for x in index], dtype=object)
|
| 42 |
+
assert not index.equals(other)
|
| 43 |
+
assert not index.equals(CategoricalIndex(other))
|
| 44 |
+
|
| 45 |
+
def test_not_equals_misc_strs(self, index):
|
| 46 |
+
other = Index(list("abc"))
|
| 47 |
+
assert not index.equals(other)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class TestPeriodIndexEquals(EqualsTests):
|
| 51 |
+
@pytest.fixture
|
| 52 |
+
def index(self):
|
| 53 |
+
return period_range("2013-01-01", periods=5, freq="D")
|
| 54 |
+
|
| 55 |
+
# TODO: de-duplicate with other test_equals2 methods
|
| 56 |
+
@pytest.mark.parametrize("freq", ["D", "M"])
|
| 57 |
+
def test_equals2(self, freq):
|
| 58 |
+
# GH#13107
|
| 59 |
+
idx = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq=freq)
|
| 60 |
+
assert idx.equals(idx)
|
| 61 |
+
assert idx.equals(idx.copy())
|
| 62 |
+
assert idx.equals(idx.astype(object))
|
| 63 |
+
assert idx.astype(object).equals(idx)
|
| 64 |
+
assert idx.astype(object).equals(idx.astype(object))
|
| 65 |
+
assert not idx.equals(list(idx))
|
| 66 |
+
assert not idx.equals(pd.Series(idx))
|
| 67 |
+
|
| 68 |
+
idx2 = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq="H")
|
| 69 |
+
assert not idx.equals(idx2)
|
| 70 |
+
assert not idx.equals(idx2.copy())
|
| 71 |
+
assert not idx.equals(idx2.astype(object))
|
| 72 |
+
assert not idx.astype(object).equals(idx2)
|
| 73 |
+
assert not idx.equals(list(idx2))
|
| 74 |
+
assert not idx.equals(pd.Series(idx2))
|
| 75 |
+
|
| 76 |
+
# same internal, different tz
|
| 77 |
+
idx3 = PeriodIndex._simple_new(
|
| 78 |
+
idx._values._simple_new(idx._values.asi8, freq="H")
|
| 79 |
+
)
|
| 80 |
+
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
|
| 81 |
+
assert not idx.equals(idx3)
|
| 82 |
+
assert not idx.equals(idx3.copy())
|
| 83 |
+
assert not idx.equals(idx3.astype(object))
|
| 84 |
+
assert not idx.astype(object).equals(idx3)
|
| 85 |
+
assert not idx.equals(list(idx3))
|
| 86 |
+
assert not idx.equals(pd.Series(idx3))
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class TestDatetimeIndexEquals(EqualsTests):
|
| 90 |
+
@pytest.fixture
|
| 91 |
+
def index(self):
|
| 92 |
+
return date_range("2013-01-01", periods=5)
|
| 93 |
+
|
| 94 |
+
def test_equals2(self):
|
| 95 |
+
# GH#13107
|
| 96 |
+
idx = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"])
|
| 97 |
+
assert idx.equals(idx)
|
| 98 |
+
assert idx.equals(idx.copy())
|
| 99 |
+
assert idx.equals(idx.astype(object))
|
| 100 |
+
assert idx.astype(object).equals(idx)
|
| 101 |
+
assert idx.astype(object).equals(idx.astype(object))
|
| 102 |
+
assert not idx.equals(list(idx))
|
| 103 |
+
assert not idx.equals(pd.Series(idx))
|
| 104 |
+
|
| 105 |
+
idx2 = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"], tz="US/Pacific")
|
| 106 |
+
assert not idx.equals(idx2)
|
| 107 |
+
assert not idx.equals(idx2.copy())
|
| 108 |
+
assert not idx.equals(idx2.astype(object))
|
| 109 |
+
assert not idx.astype(object).equals(idx2)
|
| 110 |
+
assert not idx.equals(list(idx2))
|
| 111 |
+
assert not idx.equals(pd.Series(idx2))
|
| 112 |
+
|
| 113 |
+
# same internal, different tz
|
| 114 |
+
idx3 = DatetimeIndex(idx.asi8, tz="US/Pacific")
|
| 115 |
+
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
|
| 116 |
+
assert not idx.equals(idx3)
|
| 117 |
+
assert not idx.equals(idx3.copy())
|
| 118 |
+
assert not idx.equals(idx3.astype(object))
|
| 119 |
+
assert not idx.astype(object).equals(idx3)
|
| 120 |
+
assert not idx.equals(list(idx3))
|
| 121 |
+
assert not idx.equals(pd.Series(idx3))
|
| 122 |
+
|
| 123 |
+
# check that we do not raise when comparing with OutOfBounds objects
|
| 124 |
+
oob = Index([datetime(2500, 1, 1)] * 3, dtype=object)
|
| 125 |
+
assert not idx.equals(oob)
|
| 126 |
+
assert not idx2.equals(oob)
|
| 127 |
+
assert not idx3.equals(oob)
|
| 128 |
+
|
| 129 |
+
# check that we do not raise when comparing with OutOfBounds dt64
|
| 130 |
+
oob2 = oob.map(np.datetime64)
|
| 131 |
+
assert not idx.equals(oob2)
|
| 132 |
+
assert not idx2.equals(oob2)
|
| 133 |
+
assert not idx3.equals(oob2)
|
| 134 |
+
|
| 135 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 136 |
+
def test_not_equals_bday(self, freq):
|
| 137 |
+
rng = date_range("2009-01-01", "2010-01-01", freq=freq)
|
| 138 |
+
assert not rng.equals(list(rng))
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
class TestTimedeltaIndexEquals(EqualsTests):
|
| 142 |
+
@pytest.fixture
|
| 143 |
+
def index(self):
|
| 144 |
+
return tm.makeTimedeltaIndex(10)
|
| 145 |
+
|
| 146 |
+
def test_equals2(self):
|
| 147 |
+
# GH#13107
|
| 148 |
+
idx = TimedeltaIndex(["1 days", "2 days", "NaT"])
|
| 149 |
+
assert idx.equals(idx)
|
| 150 |
+
assert idx.equals(idx.copy())
|
| 151 |
+
assert idx.equals(idx.astype(object))
|
| 152 |
+
assert idx.astype(object).equals(idx)
|
| 153 |
+
assert idx.astype(object).equals(idx.astype(object))
|
| 154 |
+
assert not idx.equals(list(idx))
|
| 155 |
+
assert not idx.equals(pd.Series(idx))
|
| 156 |
+
|
| 157 |
+
idx2 = TimedeltaIndex(["2 days", "1 days", "NaT"])
|
| 158 |
+
assert not idx.equals(idx2)
|
| 159 |
+
assert not idx.equals(idx2.copy())
|
| 160 |
+
assert not idx.equals(idx2.astype(object))
|
| 161 |
+
assert not idx.astype(object).equals(idx2)
|
| 162 |
+
assert not idx.astype(object).equals(idx2.astype(object))
|
| 163 |
+
assert not idx.equals(list(idx2))
|
| 164 |
+
assert not idx.equals(pd.Series(idx2))
|
| 165 |
+
|
| 166 |
+
# Check that we dont raise OverflowError on comparisons outside the
|
| 167 |
+
# implementation range GH#28532
|
| 168 |
+
oob = Index([timedelta(days=10**6)] * 3, dtype=object)
|
| 169 |
+
assert not idx.equals(oob)
|
| 170 |
+
assert not idx2.equals(oob)
|
| 171 |
+
|
| 172 |
+
oob2 = Index([np.timedelta64(x) for x in oob], dtype=object)
|
| 173 |
+
assert (oob == oob2).all()
|
| 174 |
+
assert not idx.equals(oob2)
|
| 175 |
+
assert not idx2.equals(oob2)
|
| 176 |
+
|
| 177 |
+
oob3 = oob.map(np.timedelta64)
|
| 178 |
+
assert (oob3 == oob).all()
|
| 179 |
+
assert not idx.equals(oob3)
|
| 180 |
+
assert not idx2.equals(oob3)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_indexing.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pandas import (
|
| 6 |
+
DatetimeIndex,
|
| 7 |
+
Index,
|
| 8 |
+
)
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
dtlike_dtypes = [
|
| 12 |
+
np.dtype("timedelta64[ns]"),
|
| 13 |
+
np.dtype("datetime64[ns]"),
|
| 14 |
+
pd.DatetimeTZDtype("ns", "Asia/Tokyo"),
|
| 15 |
+
pd.PeriodDtype("ns"),
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.mark.parametrize("ldtype", dtlike_dtypes)
|
| 20 |
+
@pytest.mark.parametrize("rdtype", dtlike_dtypes)
|
| 21 |
+
def test_get_indexer_non_unique_wrong_dtype(ldtype, rdtype):
|
| 22 |
+
vals = np.tile(3600 * 10**9 * np.arange(3), 2)
|
| 23 |
+
|
| 24 |
+
def construct(dtype):
|
| 25 |
+
if dtype is dtlike_dtypes[-1]:
|
| 26 |
+
# PeriodArray will try to cast ints to strings
|
| 27 |
+
return DatetimeIndex(vals).astype(dtype)
|
| 28 |
+
return Index(vals, dtype=dtype)
|
| 29 |
+
|
| 30 |
+
left = construct(ldtype)
|
| 31 |
+
right = construct(rdtype)
|
| 32 |
+
|
| 33 |
+
result = left.get_indexer_non_unique(right)
|
| 34 |
+
|
| 35 |
+
if ldtype is rdtype:
|
| 36 |
+
ex1 = np.array([0, 3, 1, 4, 2, 5] * 2, dtype=np.intp)
|
| 37 |
+
ex2 = np.array([], dtype=np.intp)
|
| 38 |
+
tm.assert_numpy_array_equal(result[0], ex1)
|
| 39 |
+
tm.assert_numpy_array_equal(result[1], ex2)
|
| 40 |
+
|
| 41 |
+
else:
|
| 42 |
+
no_matches = np.array([-1] * 6, dtype=np.intp)
|
| 43 |
+
missing = np.arange(6, dtype=np.intp)
|
| 44 |
+
tm.assert_numpy_array_equal(result[0], no_matches)
|
| 45 |
+
tm.assert_numpy_array_equal(result[1], missing)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_is_monotonic.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas import (
|
| 2 |
+
Index,
|
| 3 |
+
NaT,
|
| 4 |
+
date_range,
|
| 5 |
+
)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_is_monotonic_with_nat():
|
| 9 |
+
# GH#31437
|
| 10 |
+
# PeriodIndex.is_monotonic_increasing should behave analogously to DatetimeIndex,
|
| 11 |
+
# in particular never be monotonic when we have NaT
|
| 12 |
+
dti = date_range("2016-01-01", periods=3)
|
| 13 |
+
pi = dti.to_period("D")
|
| 14 |
+
tdi = Index(dti.view("timedelta64[ns]"))
|
| 15 |
+
|
| 16 |
+
for obj in [pi, pi._engine, dti, dti._engine, tdi, tdi._engine]:
|
| 17 |
+
if isinstance(obj, Index):
|
| 18 |
+
# i.e. not Engines
|
| 19 |
+
assert obj.is_monotonic_increasing
|
| 20 |
+
assert obj.is_monotonic_increasing
|
| 21 |
+
assert not obj.is_monotonic_decreasing
|
| 22 |
+
assert obj.is_unique
|
| 23 |
+
|
| 24 |
+
dti1 = dti.insert(0, NaT)
|
| 25 |
+
pi1 = dti1.to_period("D")
|
| 26 |
+
tdi1 = Index(dti1.view("timedelta64[ns]"))
|
| 27 |
+
|
| 28 |
+
for obj in [pi1, pi1._engine, dti1, dti1._engine, tdi1, tdi1._engine]:
|
| 29 |
+
if isinstance(obj, Index):
|
| 30 |
+
# i.e. not Engines
|
| 31 |
+
assert not obj.is_monotonic_increasing
|
| 32 |
+
assert not obj.is_monotonic_increasing
|
| 33 |
+
assert not obj.is_monotonic_decreasing
|
| 34 |
+
assert obj.is_unique
|
| 35 |
+
|
| 36 |
+
dti2 = dti.insert(3, NaT)
|
| 37 |
+
pi2 = dti2.to_period("H")
|
| 38 |
+
tdi2 = Index(dti2.view("timedelta64[ns]"))
|
| 39 |
+
|
| 40 |
+
for obj in [pi2, pi2._engine, dti2, dti2._engine, tdi2, tdi2._engine]:
|
| 41 |
+
if isinstance(obj, Index):
|
| 42 |
+
# i.e. not Engines
|
| 43 |
+
assert not obj.is_monotonic_increasing
|
| 44 |
+
assert not obj.is_monotonic_increasing
|
| 45 |
+
assert not obj.is_monotonic_decreasing
|
| 46 |
+
assert obj.is_unique
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_nat.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
DatetimeIndex,
|
| 6 |
+
NaT,
|
| 7 |
+
PeriodIndex,
|
| 8 |
+
TimedeltaIndex,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class NATests:
|
| 14 |
+
def test_nat(self, index_without_na):
|
| 15 |
+
empty_index = index_without_na[:0]
|
| 16 |
+
|
| 17 |
+
index_with_na = index_without_na.copy(deep=True)
|
| 18 |
+
index_with_na._data[1] = NaT
|
| 19 |
+
|
| 20 |
+
assert empty_index._na_value is NaT
|
| 21 |
+
assert index_with_na._na_value is NaT
|
| 22 |
+
assert index_without_na._na_value is NaT
|
| 23 |
+
|
| 24 |
+
idx = index_without_na
|
| 25 |
+
assert idx._can_hold_na
|
| 26 |
+
|
| 27 |
+
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
|
| 28 |
+
assert idx.hasnans is False
|
| 29 |
+
|
| 30 |
+
idx = index_with_na
|
| 31 |
+
assert idx._can_hold_na
|
| 32 |
+
|
| 33 |
+
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
|
| 34 |
+
assert idx.hasnans is True
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class TestDatetimeIndexNA(NATests):
|
| 38 |
+
@pytest.fixture
|
| 39 |
+
def index_without_na(self, tz_naive_fixture):
|
| 40 |
+
tz = tz_naive_fixture
|
| 41 |
+
return DatetimeIndex(["2011-01-01", "2011-01-02"], tz=tz)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class TestTimedeltaIndexNA(NATests):
|
| 45 |
+
@pytest.fixture
|
| 46 |
+
def index_without_na(self):
|
| 47 |
+
return TimedeltaIndex(["1 days", "2 days"])
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class TestPeriodIndexNA(NATests):
|
| 51 |
+
@pytest.fixture
|
| 52 |
+
def index_without_na(self):
|
| 53 |
+
return PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_sort_values.py
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
DatetimeIndex,
|
| 6 |
+
Index,
|
| 7 |
+
NaT,
|
| 8 |
+
PeriodIndex,
|
| 9 |
+
TimedeltaIndex,
|
| 10 |
+
timedelta_range,
|
| 11 |
+
)
|
| 12 |
+
import pandas._testing as tm
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def check_freq_ascending(ordered, orig, ascending):
|
| 16 |
+
"""
|
| 17 |
+
Check the expected freq on a PeriodIndex/DatetimeIndex/TimedeltaIndex
|
| 18 |
+
when the original index is generated (or generate-able) with
|
| 19 |
+
period_range/date_range/timedelta_range.
|
| 20 |
+
"""
|
| 21 |
+
if isinstance(ordered, PeriodIndex):
|
| 22 |
+
assert ordered.freq == orig.freq
|
| 23 |
+
elif isinstance(ordered, (DatetimeIndex, TimedeltaIndex)):
|
| 24 |
+
if ascending:
|
| 25 |
+
assert ordered.freq.n == orig.freq.n
|
| 26 |
+
else:
|
| 27 |
+
assert ordered.freq.n == -1 * orig.freq.n
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def check_freq_nonmonotonic(ordered, orig):
|
| 31 |
+
"""
|
| 32 |
+
Check the expected freq on a PeriodIndex/DatetimeIndex/TimedeltaIndex
|
| 33 |
+
when the original index is _not_ generated (or generate-able) with
|
| 34 |
+
period_range/date_range//timedelta_range.
|
| 35 |
+
"""
|
| 36 |
+
if isinstance(ordered, PeriodIndex):
|
| 37 |
+
assert ordered.freq == orig.freq
|
| 38 |
+
elif isinstance(ordered, (DatetimeIndex, TimedeltaIndex)):
|
| 39 |
+
assert ordered.freq is None
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class TestSortValues:
|
| 43 |
+
@pytest.fixture(params=[DatetimeIndex, TimedeltaIndex, PeriodIndex])
|
| 44 |
+
def non_monotonic_idx(self, request):
|
| 45 |
+
if request.param is DatetimeIndex:
|
| 46 |
+
return DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"])
|
| 47 |
+
elif request.param is PeriodIndex:
|
| 48 |
+
dti = DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"])
|
| 49 |
+
return dti.to_period("D")
|
| 50 |
+
else:
|
| 51 |
+
return TimedeltaIndex(
|
| 52 |
+
["1 day 00:00:05", "1 day 00:00:01", "1 day 00:00:02"]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
def test_argmin_argmax(self, non_monotonic_idx):
|
| 56 |
+
assert non_monotonic_idx.argmin() == 1
|
| 57 |
+
assert non_monotonic_idx.argmax() == 0
|
| 58 |
+
|
| 59 |
+
def test_sort_values(self, non_monotonic_idx):
|
| 60 |
+
idx = non_monotonic_idx
|
| 61 |
+
ordered = idx.sort_values()
|
| 62 |
+
assert ordered.is_monotonic_increasing
|
| 63 |
+
ordered = idx.sort_values(ascending=False)
|
| 64 |
+
assert ordered[::-1].is_monotonic_increasing
|
| 65 |
+
|
| 66 |
+
ordered, dexer = idx.sort_values(return_indexer=True)
|
| 67 |
+
assert ordered.is_monotonic_increasing
|
| 68 |
+
tm.assert_numpy_array_equal(dexer, np.array([1, 2, 0], dtype=np.intp))
|
| 69 |
+
|
| 70 |
+
ordered, dexer = idx.sort_values(return_indexer=True, ascending=False)
|
| 71 |
+
assert ordered[::-1].is_monotonic_increasing
|
| 72 |
+
tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1], dtype=np.intp))
|
| 73 |
+
|
| 74 |
+
def check_sort_values_with_freq(self, idx):
|
| 75 |
+
ordered = idx.sort_values()
|
| 76 |
+
tm.assert_index_equal(ordered, idx)
|
| 77 |
+
check_freq_ascending(ordered, idx, True)
|
| 78 |
+
|
| 79 |
+
ordered = idx.sort_values(ascending=False)
|
| 80 |
+
expected = idx[::-1]
|
| 81 |
+
tm.assert_index_equal(ordered, expected)
|
| 82 |
+
check_freq_ascending(ordered, idx, False)
|
| 83 |
+
|
| 84 |
+
ordered, indexer = idx.sort_values(return_indexer=True)
|
| 85 |
+
tm.assert_index_equal(ordered, idx)
|
| 86 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 1, 2], dtype=np.intp))
|
| 87 |
+
check_freq_ascending(ordered, idx, True)
|
| 88 |
+
|
| 89 |
+
ordered, indexer = idx.sort_values(return_indexer=True, ascending=False)
|
| 90 |
+
expected = idx[::-1]
|
| 91 |
+
tm.assert_index_equal(ordered, expected)
|
| 92 |
+
tm.assert_numpy_array_equal(indexer, np.array([2, 1, 0], dtype=np.intp))
|
| 93 |
+
check_freq_ascending(ordered, idx, False)
|
| 94 |
+
|
| 95 |
+
@pytest.mark.parametrize("freq", ["D", "H"])
|
| 96 |
+
def test_sort_values_with_freq_timedeltaindex(self, freq):
|
| 97 |
+
# GH#10295
|
| 98 |
+
idx = timedelta_range(start=f"1{freq}", periods=3, freq=freq).rename("idx")
|
| 99 |
+
|
| 100 |
+
self.check_sort_values_with_freq(idx)
|
| 101 |
+
|
| 102 |
+
@pytest.mark.parametrize(
|
| 103 |
+
"idx",
|
| 104 |
+
[
|
| 105 |
+
DatetimeIndex(
|
| 106 |
+
["2011-01-01", "2011-01-02", "2011-01-03"], freq="D", name="idx"
|
| 107 |
+
),
|
| 108 |
+
DatetimeIndex(
|
| 109 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"],
|
| 110 |
+
freq="H",
|
| 111 |
+
name="tzidx",
|
| 112 |
+
tz="Asia/Tokyo",
|
| 113 |
+
),
|
| 114 |
+
],
|
| 115 |
+
)
|
| 116 |
+
def test_sort_values_with_freq_datetimeindex(self, idx):
|
| 117 |
+
self.check_sort_values_with_freq(idx)
|
| 118 |
+
|
| 119 |
+
@pytest.mark.parametrize("freq", ["D", "2D", "4D"])
|
| 120 |
+
def test_sort_values_with_freq_periodindex(self, freq):
|
| 121 |
+
# here with_freq refers to being period_range-like
|
| 122 |
+
idx = PeriodIndex(
|
| 123 |
+
["2011-01-01", "2011-01-02", "2011-01-03"], freq=freq, name="idx"
|
| 124 |
+
)
|
| 125 |
+
self.check_sort_values_with_freq(idx)
|
| 126 |
+
|
| 127 |
+
@pytest.mark.parametrize(
|
| 128 |
+
"idx",
|
| 129 |
+
[
|
| 130 |
+
PeriodIndex(["2011", "2012", "2013"], name="pidx", freq="A"),
|
| 131 |
+
Index([2011, 2012, 2013], name="idx"), # for compatibility check
|
| 132 |
+
],
|
| 133 |
+
)
|
| 134 |
+
def test_sort_values_with_freq_periodindex2(self, idx):
|
| 135 |
+
# here with_freq indicates this is period_range-like
|
| 136 |
+
self.check_sort_values_with_freq(idx)
|
| 137 |
+
|
| 138 |
+
def check_sort_values_without_freq(self, idx, expected):
|
| 139 |
+
ordered = idx.sort_values(na_position="first")
|
| 140 |
+
tm.assert_index_equal(ordered, expected)
|
| 141 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 142 |
+
|
| 143 |
+
if not idx.isna().any():
|
| 144 |
+
ordered = idx.sort_values()
|
| 145 |
+
tm.assert_index_equal(ordered, expected)
|
| 146 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 147 |
+
|
| 148 |
+
ordered = idx.sort_values(ascending=False)
|
| 149 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
| 150 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 151 |
+
|
| 152 |
+
ordered, indexer = idx.sort_values(return_indexer=True, na_position="first")
|
| 153 |
+
tm.assert_index_equal(ordered, expected)
|
| 154 |
+
|
| 155 |
+
exp = np.array([0, 4, 3, 1, 2], dtype=np.intp)
|
| 156 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
| 157 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 158 |
+
|
| 159 |
+
if not idx.isna().any():
|
| 160 |
+
ordered, indexer = idx.sort_values(return_indexer=True)
|
| 161 |
+
tm.assert_index_equal(ordered, expected)
|
| 162 |
+
|
| 163 |
+
exp = np.array([0, 4, 3, 1, 2], dtype=np.intp)
|
| 164 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
| 165 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 166 |
+
|
| 167 |
+
ordered, indexer = idx.sort_values(return_indexer=True, ascending=False)
|
| 168 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
| 169 |
+
|
| 170 |
+
exp = np.array([2, 1, 3, 0, 4], dtype=np.intp)
|
| 171 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
| 172 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 173 |
+
|
| 174 |
+
def test_sort_values_without_freq_timedeltaindex(self):
|
| 175 |
+
# GH#10295
|
| 176 |
+
|
| 177 |
+
idx = TimedeltaIndex(
|
| 178 |
+
["1 hour", "3 hour", "5 hour", "2 hour ", "1 hour"], name="idx1"
|
| 179 |
+
)
|
| 180 |
+
expected = TimedeltaIndex(
|
| 181 |
+
["1 hour", "1 hour", "2 hour", "3 hour", "5 hour"], name="idx1"
|
| 182 |
+
)
|
| 183 |
+
self.check_sort_values_without_freq(idx, expected)
|
| 184 |
+
|
| 185 |
+
@pytest.mark.parametrize(
|
| 186 |
+
"index_dates,expected_dates",
|
| 187 |
+
[
|
| 188 |
+
(
|
| 189 |
+
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
|
| 190 |
+
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],
|
| 191 |
+
),
|
| 192 |
+
(
|
| 193 |
+
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
|
| 194 |
+
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],
|
| 195 |
+
),
|
| 196 |
+
(
|
| 197 |
+
[NaT, "2011-01-03", "2011-01-05", "2011-01-02", NaT],
|
| 198 |
+
[NaT, NaT, "2011-01-02", "2011-01-03", "2011-01-05"],
|
| 199 |
+
),
|
| 200 |
+
],
|
| 201 |
+
)
|
| 202 |
+
def test_sort_values_without_freq_datetimeindex(
|
| 203 |
+
self, index_dates, expected_dates, tz_naive_fixture
|
| 204 |
+
):
|
| 205 |
+
tz = tz_naive_fixture
|
| 206 |
+
|
| 207 |
+
# without freq
|
| 208 |
+
idx = DatetimeIndex(index_dates, tz=tz, name="idx")
|
| 209 |
+
expected = DatetimeIndex(expected_dates, tz=tz, name="idx")
|
| 210 |
+
|
| 211 |
+
self.check_sort_values_without_freq(idx, expected)
|
| 212 |
+
|
| 213 |
+
@pytest.mark.parametrize(
|
| 214 |
+
"idx,expected",
|
| 215 |
+
[
|
| 216 |
+
(
|
| 217 |
+
PeriodIndex(
|
| 218 |
+
[
|
| 219 |
+
"2011-01-01",
|
| 220 |
+
"2011-01-03",
|
| 221 |
+
"2011-01-05",
|
| 222 |
+
"2011-01-02",
|
| 223 |
+
"2011-01-01",
|
| 224 |
+
],
|
| 225 |
+
freq="D",
|
| 226 |
+
name="idx1",
|
| 227 |
+
),
|
| 228 |
+
PeriodIndex(
|
| 229 |
+
[
|
| 230 |
+
"2011-01-01",
|
| 231 |
+
"2011-01-01",
|
| 232 |
+
"2011-01-02",
|
| 233 |
+
"2011-01-03",
|
| 234 |
+
"2011-01-05",
|
| 235 |
+
],
|
| 236 |
+
freq="D",
|
| 237 |
+
name="idx1",
|
| 238 |
+
),
|
| 239 |
+
),
|
| 240 |
+
(
|
| 241 |
+
PeriodIndex(
|
| 242 |
+
[
|
| 243 |
+
"2011-01-01",
|
| 244 |
+
"2011-01-03",
|
| 245 |
+
"2011-01-05",
|
| 246 |
+
"2011-01-02",
|
| 247 |
+
"2011-01-01",
|
| 248 |
+
],
|
| 249 |
+
freq="D",
|
| 250 |
+
name="idx2",
|
| 251 |
+
),
|
| 252 |
+
PeriodIndex(
|
| 253 |
+
[
|
| 254 |
+
"2011-01-01",
|
| 255 |
+
"2011-01-01",
|
| 256 |
+
"2011-01-02",
|
| 257 |
+
"2011-01-03",
|
| 258 |
+
"2011-01-05",
|
| 259 |
+
],
|
| 260 |
+
freq="D",
|
| 261 |
+
name="idx2",
|
| 262 |
+
),
|
| 263 |
+
),
|
| 264 |
+
(
|
| 265 |
+
PeriodIndex(
|
| 266 |
+
[NaT, "2011-01-03", "2011-01-05", "2011-01-02", NaT],
|
| 267 |
+
freq="D",
|
| 268 |
+
name="idx3",
|
| 269 |
+
),
|
| 270 |
+
PeriodIndex(
|
| 271 |
+
[NaT, NaT, "2011-01-02", "2011-01-03", "2011-01-05"],
|
| 272 |
+
freq="D",
|
| 273 |
+
name="idx3",
|
| 274 |
+
),
|
| 275 |
+
),
|
| 276 |
+
(
|
| 277 |
+
PeriodIndex(
|
| 278 |
+
["2011", "2013", "2015", "2012", "2011"], name="pidx", freq="A"
|
| 279 |
+
),
|
| 280 |
+
PeriodIndex(
|
| 281 |
+
["2011", "2011", "2012", "2013", "2015"], name="pidx", freq="A"
|
| 282 |
+
),
|
| 283 |
+
),
|
| 284 |
+
(
|
| 285 |
+
# For compatibility check
|
| 286 |
+
Index([2011, 2013, 2015, 2012, 2011], name="idx"),
|
| 287 |
+
Index([2011, 2011, 2012, 2013, 2015], name="idx"),
|
| 288 |
+
),
|
| 289 |
+
],
|
| 290 |
+
)
|
| 291 |
+
def test_sort_values_without_freq_periodindex(self, idx, expected):
|
| 292 |
+
# here without_freq means not generateable by period_range
|
| 293 |
+
self.check_sort_values_without_freq(idx, expected)
|
| 294 |
+
|
| 295 |
+
def test_sort_values_without_freq_periodindex_nat(self):
|
| 296 |
+
# doesn't quite fit into check_sort_values_without_freq
|
| 297 |
+
idx = PeriodIndex(["2011", "2013", "NaT", "2011"], name="pidx", freq="D")
|
| 298 |
+
expected = PeriodIndex(["NaT", "2011", "2011", "2013"], name="pidx", freq="D")
|
| 299 |
+
|
| 300 |
+
ordered = idx.sort_values(na_position="first")
|
| 301 |
+
tm.assert_index_equal(ordered, expected)
|
| 302 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 303 |
+
|
| 304 |
+
ordered = idx.sort_values(ascending=False)
|
| 305 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
| 306 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
def test_order_stability_compat():
|
| 310 |
+
# GH#35922. sort_values is stable both for normal and datetime-like Index
|
| 311 |
+
pidx = PeriodIndex(["2011", "2013", "2015", "2012", "2011"], name="pidx", freq="A")
|
| 312 |
+
iidx = Index([2011, 2013, 2015, 2012, 2011], name="idx")
|
| 313 |
+
ordered1, indexer1 = pidx.sort_values(return_indexer=True, ascending=False)
|
| 314 |
+
ordered2, indexer2 = iidx.sort_values(return_indexer=True, ascending=False)
|
| 315 |
+
tm.assert_numpy_array_equal(indexer1, indexer2)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_value_counts.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
DatetimeIndex,
|
| 5 |
+
NaT,
|
| 6 |
+
PeriodIndex,
|
| 7 |
+
Series,
|
| 8 |
+
TimedeltaIndex,
|
| 9 |
+
date_range,
|
| 10 |
+
period_range,
|
| 11 |
+
timedelta_range,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestValueCounts:
|
| 17 |
+
# GH#7735
|
| 18 |
+
|
| 19 |
+
def test_value_counts_unique_datetimeindex(self, tz_naive_fixture):
|
| 20 |
+
tz = tz_naive_fixture
|
| 21 |
+
orig = date_range("2011-01-01 09:00", freq="H", periods=10, tz=tz)
|
| 22 |
+
self._check_value_counts_with_repeats(orig)
|
| 23 |
+
|
| 24 |
+
def test_value_counts_unique_timedeltaindex(self):
|
| 25 |
+
orig = timedelta_range("1 days 09:00:00", freq="H", periods=10)
|
| 26 |
+
self._check_value_counts_with_repeats(orig)
|
| 27 |
+
|
| 28 |
+
def test_value_counts_unique_periodindex(self):
|
| 29 |
+
orig = period_range("2011-01-01 09:00", freq="H", periods=10)
|
| 30 |
+
self._check_value_counts_with_repeats(orig)
|
| 31 |
+
|
| 32 |
+
def _check_value_counts_with_repeats(self, orig):
|
| 33 |
+
# create repeated values, 'n'th element is repeated by n+1 times
|
| 34 |
+
idx = type(orig)(
|
| 35 |
+
np.repeat(orig._values, range(1, len(orig) + 1)), dtype=orig.dtype
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
exp_idx = orig[::-1]
|
| 39 |
+
if not isinstance(exp_idx, PeriodIndex):
|
| 40 |
+
exp_idx = exp_idx._with_freq(None)
|
| 41 |
+
expected = Series(range(10, 0, -1), index=exp_idx, dtype="int64", name="count")
|
| 42 |
+
|
| 43 |
+
for obj in [idx, Series(idx)]:
|
| 44 |
+
tm.assert_series_equal(obj.value_counts(), expected)
|
| 45 |
+
|
| 46 |
+
tm.assert_index_equal(idx.unique(), orig)
|
| 47 |
+
|
| 48 |
+
def test_value_counts_unique_datetimeindex2(self, tz_naive_fixture):
|
| 49 |
+
tz = tz_naive_fixture
|
| 50 |
+
idx = DatetimeIndex(
|
| 51 |
+
[
|
| 52 |
+
"2013-01-01 09:00",
|
| 53 |
+
"2013-01-01 09:00",
|
| 54 |
+
"2013-01-01 09:00",
|
| 55 |
+
"2013-01-01 08:00",
|
| 56 |
+
"2013-01-01 08:00",
|
| 57 |
+
NaT,
|
| 58 |
+
],
|
| 59 |
+
tz=tz,
|
| 60 |
+
)
|
| 61 |
+
self._check_value_counts_dropna(idx)
|
| 62 |
+
|
| 63 |
+
def test_value_counts_unique_timedeltaindex2(self):
|
| 64 |
+
idx = TimedeltaIndex(
|
| 65 |
+
[
|
| 66 |
+
"1 days 09:00:00",
|
| 67 |
+
"1 days 09:00:00",
|
| 68 |
+
"1 days 09:00:00",
|
| 69 |
+
"1 days 08:00:00",
|
| 70 |
+
"1 days 08:00:00",
|
| 71 |
+
NaT,
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
self._check_value_counts_dropna(idx)
|
| 75 |
+
|
| 76 |
+
def test_value_counts_unique_periodindex2(self):
|
| 77 |
+
idx = PeriodIndex(
|
| 78 |
+
[
|
| 79 |
+
"2013-01-01 09:00",
|
| 80 |
+
"2013-01-01 09:00",
|
| 81 |
+
"2013-01-01 09:00",
|
| 82 |
+
"2013-01-01 08:00",
|
| 83 |
+
"2013-01-01 08:00",
|
| 84 |
+
NaT,
|
| 85 |
+
],
|
| 86 |
+
freq="H",
|
| 87 |
+
)
|
| 88 |
+
self._check_value_counts_dropna(idx)
|
| 89 |
+
|
| 90 |
+
def _check_value_counts_dropna(self, idx):
|
| 91 |
+
exp_idx = idx[[2, 3]]
|
| 92 |
+
expected = Series([3, 2], index=exp_idx, name="count")
|
| 93 |
+
|
| 94 |
+
for obj in [idx, Series(idx)]:
|
| 95 |
+
tm.assert_series_equal(obj.value_counts(), expected)
|
| 96 |
+
|
| 97 |
+
exp_idx = idx[[2, 3, -1]]
|
| 98 |
+
expected = Series([3, 2, 1], index=exp_idx, name="count")
|
| 99 |
+
|
| 100 |
+
for obj in [idx, Series(idx)]:
|
| 101 |
+
tm.assert_series_equal(obj.value_counts(dropna=False), expected)
|
| 102 |
+
|
| 103 |
+
tm.assert_index_equal(idx.unique(), exp_idx)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (187 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/__pycache__/test_constructors.cpython-310.pyc
ADDED
|
Binary file (9.2 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/__pycache__/test_delete.cpython-310.pyc
ADDED
|
Binary file (2.13 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/__pycache__/test_formats.cpython-310.pyc
ADDED
|
Binary file (2.97 kB). View file
|
|
|