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 +2 -0
- videochat2/bin/python3.1 +3 -0
- videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/dtypes.cpython-310-x86_64-linux-gnu.so +3 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_astype.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_comparison.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_construction.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_function.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_indexing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_logical.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_ops.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_reduction.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_repr.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_arithmetic.py +129 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_astype.py +53 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_comparison.py +60 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_construction.py +326 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_function.py +126 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_indexing.py +13 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_logical.py +254 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_ops.py +27 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_reduction.py +62 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_repr.py +13 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/conftest.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_algos.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_analytics.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_api.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_astype.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_constructors.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_dtypes.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_indexing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_missing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_operators.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_replace.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_repr.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_sorting.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_subclass.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_take.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_warnings.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/conftest.py +15 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_algos.py +80 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_analytics.py +335 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_api.py +493 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_astype.py +99 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_constructors.py +759 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_dtypes.py +139 -0
.gitattributes
CHANGED
|
@@ -1260,3 +1260,5 @@ videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/fields.cpython-310-x
|
|
| 1260 |
videochat2/lib/python3.10/site-packages/pandas/_libs/ops.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1261 |
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/parsing.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1262 |
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/tzconversion.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 1260 |
videochat2/lib/python3.10/site-packages/pandas/_libs/ops.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1261 |
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/parsing.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1262 |
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/tzconversion.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1263 |
+
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/dtypes.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1264 |
+
videochat2/bin/python3.1 filter=lfs diff=lfs merge=lfs -text
|
videochat2/bin/python3.1
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1b7b977d2d2e51b6a23b9d38159d054a082df04d52efa47a608eb49b76732223
|
| 3 |
+
size 17225608
|
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/dtypes.cpython-310-x86_64-linux-gnu.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eff7c3a83725d535cd917521c8051869a1b43075c18ccf091583dccde232cc3a
|
| 3 |
+
size 150048
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (183 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
|
Binary file (3.43 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_astype.cpython-310.pyc
ADDED
|
Binary file (1.57 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_comparison.cpython-310.pyc
ADDED
|
Binary file (2.29 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_construction.cpython-310.pyc
ADDED
|
Binary file (8.36 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_function.cpython-310.pyc
ADDED
|
Binary file (3.37 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (630 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_logical.cpython-310.pyc
ADDED
|
Binary file (6.78 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_ops.cpython-310.pyc
ADDED
|
Binary file (1.17 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_reduction.cpython-310.pyc
ADDED
|
Binary file (1.73 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_repr.cpython-310.pyc
ADDED
|
Binary file (641 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_arithmetic.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import operator
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import pandas._testing as tm
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def data():
|
| 12 |
+
"""Fixture returning boolean array with valid and missing values."""
|
| 13 |
+
return pd.array(
|
| 14 |
+
[True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False],
|
| 15 |
+
dtype="boolean",
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.fixture
|
| 20 |
+
def left_array():
|
| 21 |
+
"""Fixture returning boolean array with valid and missing values."""
|
| 22 |
+
return pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@pytest.fixture
|
| 26 |
+
def right_array():
|
| 27 |
+
"""Fixture returning boolean array with valid and missing values."""
|
| 28 |
+
return pd.array([True, False, None] * 3, dtype="boolean")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Basic test for the arithmetic array ops
|
| 32 |
+
# -----------------------------------------------------------------------------
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@pytest.mark.parametrize(
|
| 36 |
+
"opname, exp",
|
| 37 |
+
[
|
| 38 |
+
("add", [True, True, None, True, False, None, None, None, None]),
|
| 39 |
+
("mul", [True, False, None, False, False, None, None, None, None]),
|
| 40 |
+
],
|
| 41 |
+
ids=["add", "mul"],
|
| 42 |
+
)
|
| 43 |
+
def test_add_mul(left_array, right_array, opname, exp):
|
| 44 |
+
op = getattr(operator, opname)
|
| 45 |
+
result = op(left_array, right_array)
|
| 46 |
+
expected = pd.array(exp, dtype="boolean")
|
| 47 |
+
tm.assert_extension_array_equal(result, expected)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_sub(left_array, right_array):
|
| 51 |
+
msg = (
|
| 52 |
+
r"numpy boolean subtract, the `-` operator, is (?:deprecated|not supported), "
|
| 53 |
+
r"use the bitwise_xor, the `\^` operator, or the logical_xor function instead\."
|
| 54 |
+
)
|
| 55 |
+
with pytest.raises(TypeError, match=msg):
|
| 56 |
+
left_array - right_array
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_div(left_array, right_array):
|
| 60 |
+
msg = "operator '.*' not implemented for bool dtypes"
|
| 61 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 62 |
+
# check that we are matching the non-masked Series behavior
|
| 63 |
+
pd.Series(left_array._data) / pd.Series(right_array._data)
|
| 64 |
+
|
| 65 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 66 |
+
left_array / right_array
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@pytest.mark.parametrize(
|
| 70 |
+
"opname",
|
| 71 |
+
[
|
| 72 |
+
"floordiv",
|
| 73 |
+
"mod",
|
| 74 |
+
"pow",
|
| 75 |
+
],
|
| 76 |
+
)
|
| 77 |
+
def test_op_int8(left_array, right_array, opname):
|
| 78 |
+
op = getattr(operator, opname)
|
| 79 |
+
if opname != "mod":
|
| 80 |
+
msg = "operator '.*' not implemented for bool dtypes"
|
| 81 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 82 |
+
result = op(left_array, right_array)
|
| 83 |
+
return
|
| 84 |
+
result = op(left_array, right_array)
|
| 85 |
+
expected = op(left_array.astype("Int8"), right_array.astype("Int8"))
|
| 86 |
+
tm.assert_extension_array_equal(result, expected)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# Test generic characteristics / errors
|
| 90 |
+
# -----------------------------------------------------------------------------
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def test_error_invalid_values(data, all_arithmetic_operators):
|
| 94 |
+
# invalid ops
|
| 95 |
+
|
| 96 |
+
op = all_arithmetic_operators
|
| 97 |
+
s = pd.Series(data)
|
| 98 |
+
ops = getattr(s, op)
|
| 99 |
+
|
| 100 |
+
# invalid scalars
|
| 101 |
+
msg = (
|
| 102 |
+
"did not contain a loop with signature matching types|"
|
| 103 |
+
"BooleanArray cannot perform the operation|"
|
| 104 |
+
"not supported for the input types, and the inputs could not be safely coerced "
|
| 105 |
+
"to any supported types according to the casting rule ''safe''"
|
| 106 |
+
)
|
| 107 |
+
with pytest.raises(TypeError, match=msg):
|
| 108 |
+
ops("foo")
|
| 109 |
+
msg = "|".join(
|
| 110 |
+
[
|
| 111 |
+
r"unsupported operand type\(s\) for",
|
| 112 |
+
"Concatenation operation is not implemented for NumPy arrays",
|
| 113 |
+
]
|
| 114 |
+
)
|
| 115 |
+
with pytest.raises(TypeError, match=msg):
|
| 116 |
+
ops(pd.Timestamp("20180101"))
|
| 117 |
+
|
| 118 |
+
# invalid array-likes
|
| 119 |
+
if op not in ("__mul__", "__rmul__"):
|
| 120 |
+
# TODO(extension) numpy's mul with object array sees booleans as numbers
|
| 121 |
+
msg = "|".join(
|
| 122 |
+
[
|
| 123 |
+
r"unsupported operand type\(s\) for",
|
| 124 |
+
"can only concatenate str",
|
| 125 |
+
"not all arguments converted during string formatting",
|
| 126 |
+
]
|
| 127 |
+
)
|
| 128 |
+
with pytest.raises(TypeError, match=msg):
|
| 129 |
+
ops(pd.Series("foo", index=s.index))
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_astype.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_astype():
|
| 9 |
+
# with missing values
|
| 10 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
| 11 |
+
|
| 12 |
+
with pytest.raises(ValueError, match="cannot convert NA to integer"):
|
| 13 |
+
arr.astype("int64")
|
| 14 |
+
|
| 15 |
+
with pytest.raises(ValueError, match="cannot convert float NaN to"):
|
| 16 |
+
arr.astype("bool")
|
| 17 |
+
|
| 18 |
+
result = arr.astype("float64")
|
| 19 |
+
expected = np.array([1, 0, np.nan], dtype="float64")
|
| 20 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 21 |
+
|
| 22 |
+
result = arr.astype("str")
|
| 23 |
+
expected = np.array(["True", "False", "<NA>"], dtype=f"{tm.ENDIAN}U5")
|
| 24 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 25 |
+
|
| 26 |
+
# no missing values
|
| 27 |
+
arr = pd.array([True, False, True], dtype="boolean")
|
| 28 |
+
result = arr.astype("int64")
|
| 29 |
+
expected = np.array([1, 0, 1], dtype="int64")
|
| 30 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 31 |
+
|
| 32 |
+
result = arr.astype("bool")
|
| 33 |
+
expected = np.array([True, False, True], dtype="bool")
|
| 34 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_astype_to_boolean_array():
|
| 38 |
+
# astype to BooleanArray
|
| 39 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
| 40 |
+
|
| 41 |
+
result = arr.astype("boolean")
|
| 42 |
+
tm.assert_extension_array_equal(result, arr)
|
| 43 |
+
result = arr.astype(pd.BooleanDtype())
|
| 44 |
+
tm.assert_extension_array_equal(result, arr)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_astype_to_integer_array():
|
| 48 |
+
# astype to IntegerArray
|
| 49 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
| 50 |
+
|
| 51 |
+
result = arr.astype("Int64")
|
| 52 |
+
expected = pd.array([1, 0, None], dtype="Int64")
|
| 53 |
+
tm.assert_extension_array_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_comparison.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
from pandas.arrays import BooleanArray
|
| 7 |
+
from pandas.tests.arrays.masked_shared import ComparisonOps
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def data():
|
| 12 |
+
"""Fixture returning boolean array with valid and missing data"""
|
| 13 |
+
return pd.array(
|
| 14 |
+
[True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False],
|
| 15 |
+
dtype="boolean",
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.fixture
|
| 20 |
+
def dtype():
|
| 21 |
+
"""Fixture returning BooleanDtype"""
|
| 22 |
+
return pd.BooleanDtype()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class TestComparisonOps(ComparisonOps):
|
| 26 |
+
def test_compare_scalar(self, data, comparison_op):
|
| 27 |
+
self._compare_other(data, comparison_op, True)
|
| 28 |
+
|
| 29 |
+
def test_compare_array(self, data, comparison_op):
|
| 30 |
+
other = pd.array([True] * len(data), dtype="boolean")
|
| 31 |
+
self._compare_other(data, comparison_op, other)
|
| 32 |
+
other = np.array([True] * len(data))
|
| 33 |
+
self._compare_other(data, comparison_op, other)
|
| 34 |
+
other = pd.Series([True] * len(data))
|
| 35 |
+
self._compare_other(data, comparison_op, other)
|
| 36 |
+
|
| 37 |
+
@pytest.mark.parametrize("other", [True, False, pd.NA])
|
| 38 |
+
def test_scalar(self, other, comparison_op, dtype):
|
| 39 |
+
ComparisonOps.test_scalar(self, other, comparison_op, dtype)
|
| 40 |
+
|
| 41 |
+
def test_array(self, comparison_op):
|
| 42 |
+
op = comparison_op
|
| 43 |
+
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 44 |
+
b = pd.array([True, False, None] * 3, dtype="boolean")
|
| 45 |
+
|
| 46 |
+
result = op(a, b)
|
| 47 |
+
|
| 48 |
+
values = op(a._data, b._data)
|
| 49 |
+
mask = a._mask | b._mask
|
| 50 |
+
expected = BooleanArray(values, mask)
|
| 51 |
+
tm.assert_extension_array_equal(result, expected)
|
| 52 |
+
|
| 53 |
+
# ensure we haven't mutated anything inplace
|
| 54 |
+
result[0] = None
|
| 55 |
+
tm.assert_extension_array_equal(
|
| 56 |
+
a, pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 57 |
+
)
|
| 58 |
+
tm.assert_extension_array_equal(
|
| 59 |
+
b, pd.array([True, False, None] * 3, dtype="boolean")
|
| 60 |
+
)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_construction.py
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
from pandas.arrays import BooleanArray
|
| 7 |
+
from pandas.core.arrays.boolean import coerce_to_array
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_boolean_array_constructor():
|
| 11 |
+
values = np.array([True, False, True, False], dtype="bool")
|
| 12 |
+
mask = np.array([False, False, False, True], dtype="bool")
|
| 13 |
+
|
| 14 |
+
result = BooleanArray(values, mask)
|
| 15 |
+
expected = pd.array([True, False, True, None], dtype="boolean")
|
| 16 |
+
tm.assert_extension_array_equal(result, expected)
|
| 17 |
+
|
| 18 |
+
with pytest.raises(TypeError, match="values should be boolean numpy array"):
|
| 19 |
+
BooleanArray(values.tolist(), mask)
|
| 20 |
+
|
| 21 |
+
with pytest.raises(TypeError, match="mask should be boolean numpy array"):
|
| 22 |
+
BooleanArray(values, mask.tolist())
|
| 23 |
+
|
| 24 |
+
with pytest.raises(TypeError, match="values should be boolean numpy array"):
|
| 25 |
+
BooleanArray(values.astype(int), mask)
|
| 26 |
+
|
| 27 |
+
with pytest.raises(TypeError, match="mask should be boolean numpy array"):
|
| 28 |
+
BooleanArray(values, None)
|
| 29 |
+
|
| 30 |
+
with pytest.raises(ValueError, match="values.shape must match mask.shape"):
|
| 31 |
+
BooleanArray(values.reshape(1, -1), mask)
|
| 32 |
+
|
| 33 |
+
with pytest.raises(ValueError, match="values.shape must match mask.shape"):
|
| 34 |
+
BooleanArray(values, mask.reshape(1, -1))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_boolean_array_constructor_copy():
|
| 38 |
+
values = np.array([True, False, True, False], dtype="bool")
|
| 39 |
+
mask = np.array([False, False, False, True], dtype="bool")
|
| 40 |
+
|
| 41 |
+
result = BooleanArray(values, mask)
|
| 42 |
+
assert result._data is values
|
| 43 |
+
assert result._mask is mask
|
| 44 |
+
|
| 45 |
+
result = BooleanArray(values, mask, copy=True)
|
| 46 |
+
assert result._data is not values
|
| 47 |
+
assert result._mask is not mask
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_to_boolean_array():
|
| 51 |
+
expected = BooleanArray(
|
| 52 |
+
np.array([True, False, True]), np.array([False, False, False])
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
result = pd.array([True, False, True], dtype="boolean")
|
| 56 |
+
tm.assert_extension_array_equal(result, expected)
|
| 57 |
+
result = pd.array(np.array([True, False, True]), dtype="boolean")
|
| 58 |
+
tm.assert_extension_array_equal(result, expected)
|
| 59 |
+
result = pd.array(np.array([True, False, True], dtype=object), dtype="boolean")
|
| 60 |
+
tm.assert_extension_array_equal(result, expected)
|
| 61 |
+
|
| 62 |
+
# with missing values
|
| 63 |
+
expected = BooleanArray(
|
| 64 |
+
np.array([True, False, True]), np.array([False, False, True])
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
result = pd.array([True, False, None], dtype="boolean")
|
| 68 |
+
tm.assert_extension_array_equal(result, expected)
|
| 69 |
+
result = pd.array(np.array([True, False, None], dtype=object), dtype="boolean")
|
| 70 |
+
tm.assert_extension_array_equal(result, expected)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_to_boolean_array_all_none():
|
| 74 |
+
expected = BooleanArray(np.array([True, True, True]), np.array([True, True, True]))
|
| 75 |
+
|
| 76 |
+
result = pd.array([None, None, None], dtype="boolean")
|
| 77 |
+
tm.assert_extension_array_equal(result, expected)
|
| 78 |
+
result = pd.array(np.array([None, None, None], dtype=object), dtype="boolean")
|
| 79 |
+
tm.assert_extension_array_equal(result, expected)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@pytest.mark.parametrize(
|
| 83 |
+
"a, b",
|
| 84 |
+
[
|
| 85 |
+
([True, False, None, np.nan, pd.NA], [True, False, None, None, None]),
|
| 86 |
+
([True, np.nan], [True, None]),
|
| 87 |
+
([True, pd.NA], [True, None]),
|
| 88 |
+
([np.nan, np.nan], [None, None]),
|
| 89 |
+
(np.array([np.nan, np.nan], dtype=float), [None, None]),
|
| 90 |
+
],
|
| 91 |
+
)
|
| 92 |
+
def test_to_boolean_array_missing_indicators(a, b):
|
| 93 |
+
result = pd.array(a, dtype="boolean")
|
| 94 |
+
expected = pd.array(b, dtype="boolean")
|
| 95 |
+
tm.assert_extension_array_equal(result, expected)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
@pytest.mark.parametrize(
|
| 99 |
+
"values",
|
| 100 |
+
[
|
| 101 |
+
["foo", "bar"],
|
| 102 |
+
["1", "2"],
|
| 103 |
+
# "foo",
|
| 104 |
+
[1, 2],
|
| 105 |
+
[1.0, 2.0],
|
| 106 |
+
pd.date_range("20130101", periods=2),
|
| 107 |
+
np.array(["foo"]),
|
| 108 |
+
np.array([1, 2]),
|
| 109 |
+
np.array([1.0, 2.0]),
|
| 110 |
+
[np.nan, {"a": 1}],
|
| 111 |
+
],
|
| 112 |
+
)
|
| 113 |
+
def test_to_boolean_array_error(values):
|
| 114 |
+
# error in converting existing arrays to BooleanArray
|
| 115 |
+
msg = "Need to pass bool-like value"
|
| 116 |
+
with pytest.raises(TypeError, match=msg):
|
| 117 |
+
pd.array(values, dtype="boolean")
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def test_to_boolean_array_from_integer_array():
|
| 121 |
+
result = pd.array(np.array([1, 0, 1, 0]), dtype="boolean")
|
| 122 |
+
expected = pd.array([True, False, True, False], dtype="boolean")
|
| 123 |
+
tm.assert_extension_array_equal(result, expected)
|
| 124 |
+
|
| 125 |
+
# with missing values
|
| 126 |
+
result = pd.array(np.array([1, 0, 1, None]), dtype="boolean")
|
| 127 |
+
expected = pd.array([True, False, True, None], dtype="boolean")
|
| 128 |
+
tm.assert_extension_array_equal(result, expected)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def test_to_boolean_array_from_float_array():
|
| 132 |
+
result = pd.array(np.array([1.0, 0.0, 1.0, 0.0]), dtype="boolean")
|
| 133 |
+
expected = pd.array([True, False, True, False], dtype="boolean")
|
| 134 |
+
tm.assert_extension_array_equal(result, expected)
|
| 135 |
+
|
| 136 |
+
# with missing values
|
| 137 |
+
result = pd.array(np.array([1.0, 0.0, 1.0, np.nan]), dtype="boolean")
|
| 138 |
+
expected = pd.array([True, False, True, None], dtype="boolean")
|
| 139 |
+
tm.assert_extension_array_equal(result, expected)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def test_to_boolean_array_integer_like():
|
| 143 |
+
# integers of 0's and 1's
|
| 144 |
+
result = pd.array([1, 0, 1, 0], dtype="boolean")
|
| 145 |
+
expected = pd.array([True, False, True, False], dtype="boolean")
|
| 146 |
+
tm.assert_extension_array_equal(result, expected)
|
| 147 |
+
|
| 148 |
+
# with missing values
|
| 149 |
+
result = pd.array([1, 0, 1, None], dtype="boolean")
|
| 150 |
+
expected = pd.array([True, False, True, None], dtype="boolean")
|
| 151 |
+
tm.assert_extension_array_equal(result, expected)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def test_coerce_to_array():
|
| 155 |
+
# TODO this is currently not public API
|
| 156 |
+
values = np.array([True, False, True, False], dtype="bool")
|
| 157 |
+
mask = np.array([False, False, False, True], dtype="bool")
|
| 158 |
+
result = BooleanArray(*coerce_to_array(values, mask=mask))
|
| 159 |
+
expected = BooleanArray(values, mask)
|
| 160 |
+
tm.assert_extension_array_equal(result, expected)
|
| 161 |
+
assert result._data is values
|
| 162 |
+
assert result._mask is mask
|
| 163 |
+
result = BooleanArray(*coerce_to_array(values, mask=mask, copy=True))
|
| 164 |
+
expected = BooleanArray(values, mask)
|
| 165 |
+
tm.assert_extension_array_equal(result, expected)
|
| 166 |
+
assert result._data is not values
|
| 167 |
+
assert result._mask is not mask
|
| 168 |
+
|
| 169 |
+
# mixed missing from values and mask
|
| 170 |
+
values = [True, False, None, False]
|
| 171 |
+
mask = np.array([False, False, False, True], dtype="bool")
|
| 172 |
+
result = BooleanArray(*coerce_to_array(values, mask=mask))
|
| 173 |
+
expected = BooleanArray(
|
| 174 |
+
np.array([True, False, True, True]), np.array([False, False, True, True])
|
| 175 |
+
)
|
| 176 |
+
tm.assert_extension_array_equal(result, expected)
|
| 177 |
+
result = BooleanArray(*coerce_to_array(np.array(values, dtype=object), mask=mask))
|
| 178 |
+
tm.assert_extension_array_equal(result, expected)
|
| 179 |
+
result = BooleanArray(*coerce_to_array(values, mask=mask.tolist()))
|
| 180 |
+
tm.assert_extension_array_equal(result, expected)
|
| 181 |
+
|
| 182 |
+
# raise errors for wrong dimension
|
| 183 |
+
values = np.array([True, False, True, False], dtype="bool")
|
| 184 |
+
mask = np.array([False, False, False, True], dtype="bool")
|
| 185 |
+
|
| 186 |
+
# passing 2D values is OK as long as no mask
|
| 187 |
+
coerce_to_array(values.reshape(1, -1))
|
| 188 |
+
|
| 189 |
+
with pytest.raises(ValueError, match="values.shape and mask.shape must match"):
|
| 190 |
+
coerce_to_array(values.reshape(1, -1), mask=mask)
|
| 191 |
+
|
| 192 |
+
with pytest.raises(ValueError, match="values.shape and mask.shape must match"):
|
| 193 |
+
coerce_to_array(values, mask=mask.reshape(1, -1))
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def test_coerce_to_array_from_boolean_array():
|
| 197 |
+
# passing BooleanArray to coerce_to_array
|
| 198 |
+
values = np.array([True, False, True, False], dtype="bool")
|
| 199 |
+
mask = np.array([False, False, False, True], dtype="bool")
|
| 200 |
+
arr = BooleanArray(values, mask)
|
| 201 |
+
result = BooleanArray(*coerce_to_array(arr))
|
| 202 |
+
tm.assert_extension_array_equal(result, arr)
|
| 203 |
+
# no copy
|
| 204 |
+
assert result._data is arr._data
|
| 205 |
+
assert result._mask is arr._mask
|
| 206 |
+
|
| 207 |
+
result = BooleanArray(*coerce_to_array(arr), copy=True)
|
| 208 |
+
tm.assert_extension_array_equal(result, arr)
|
| 209 |
+
assert result._data is not arr._data
|
| 210 |
+
assert result._mask is not arr._mask
|
| 211 |
+
|
| 212 |
+
with pytest.raises(ValueError, match="cannot pass mask for BooleanArray input"):
|
| 213 |
+
coerce_to_array(arr, mask=mask)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def test_coerce_to_numpy_array():
|
| 217 |
+
# with missing values -> object dtype
|
| 218 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
| 219 |
+
result = np.array(arr)
|
| 220 |
+
expected = np.array([True, False, pd.NA], dtype="object")
|
| 221 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 222 |
+
|
| 223 |
+
# also with no missing values -> object dtype
|
| 224 |
+
arr = pd.array([True, False, True], dtype="boolean")
|
| 225 |
+
result = np.array(arr)
|
| 226 |
+
expected = np.array([True, False, True], dtype="object")
|
| 227 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 228 |
+
|
| 229 |
+
# force bool dtype
|
| 230 |
+
result = np.array(arr, dtype="bool")
|
| 231 |
+
expected = np.array([True, False, True], dtype="bool")
|
| 232 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 233 |
+
# with missing values will raise error
|
| 234 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
| 235 |
+
msg = (
|
| 236 |
+
"cannot convert to 'bool'-dtype NumPy array with missing values. "
|
| 237 |
+
"Specify an appropriate 'na_value' for this dtype."
|
| 238 |
+
)
|
| 239 |
+
with pytest.raises(ValueError, match=msg):
|
| 240 |
+
np.array(arr, dtype="bool")
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def test_to_boolean_array_from_strings():
|
| 244 |
+
result = BooleanArray._from_sequence_of_strings(
|
| 245 |
+
np.array(["True", "False", "1", "1.0", "0", "0.0", np.nan], dtype=object)
|
| 246 |
+
)
|
| 247 |
+
expected = BooleanArray(
|
| 248 |
+
np.array([True, False, True, True, False, False, False]),
|
| 249 |
+
np.array([False, False, False, False, False, False, True]),
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
tm.assert_extension_array_equal(result, expected)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def test_to_boolean_array_from_strings_invalid_string():
|
| 256 |
+
with pytest.raises(ValueError, match="cannot be cast"):
|
| 257 |
+
BooleanArray._from_sequence_of_strings(["donkey"])
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
@pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
|
| 261 |
+
def test_to_numpy(box):
|
| 262 |
+
con = pd.Series if box else pd.array
|
| 263 |
+
# default (with or without missing values) -> object dtype
|
| 264 |
+
arr = con([True, False, True], dtype="boolean")
|
| 265 |
+
result = arr.to_numpy()
|
| 266 |
+
expected = np.array([True, False, True], dtype="object")
|
| 267 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 268 |
+
|
| 269 |
+
arr = con([True, False, None], dtype="boolean")
|
| 270 |
+
result = arr.to_numpy()
|
| 271 |
+
expected = np.array([True, False, pd.NA], dtype="object")
|
| 272 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 273 |
+
|
| 274 |
+
arr = con([True, False, None], dtype="boolean")
|
| 275 |
+
result = arr.to_numpy(dtype="str")
|
| 276 |
+
expected = np.array([True, False, pd.NA], dtype=f"{tm.ENDIAN}U5")
|
| 277 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 278 |
+
|
| 279 |
+
# no missing values -> can convert to bool, otherwise raises
|
| 280 |
+
arr = con([True, False, True], dtype="boolean")
|
| 281 |
+
result = arr.to_numpy(dtype="bool")
|
| 282 |
+
expected = np.array([True, False, True], dtype="bool")
|
| 283 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 284 |
+
|
| 285 |
+
arr = con([True, False, None], dtype="boolean")
|
| 286 |
+
with pytest.raises(ValueError, match="cannot convert to 'bool'-dtype"):
|
| 287 |
+
result = arr.to_numpy(dtype="bool")
|
| 288 |
+
|
| 289 |
+
# specify dtype and na_value
|
| 290 |
+
arr = con([True, False, None], dtype="boolean")
|
| 291 |
+
result = arr.to_numpy(dtype=object, na_value=None)
|
| 292 |
+
expected = np.array([True, False, None], dtype="object")
|
| 293 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 294 |
+
|
| 295 |
+
result = arr.to_numpy(dtype=bool, na_value=False)
|
| 296 |
+
expected = np.array([True, False, False], dtype="bool")
|
| 297 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 298 |
+
|
| 299 |
+
result = arr.to_numpy(dtype="int64", na_value=-99)
|
| 300 |
+
expected = np.array([1, 0, -99], dtype="int64")
|
| 301 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 302 |
+
|
| 303 |
+
result = arr.to_numpy(dtype="float64", na_value=np.nan)
|
| 304 |
+
expected = np.array([1, 0, np.nan], dtype="float64")
|
| 305 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 306 |
+
|
| 307 |
+
# converting to int or float without specifying na_value raises
|
| 308 |
+
with pytest.raises(ValueError, match="cannot convert to 'int64'-dtype"):
|
| 309 |
+
arr.to_numpy(dtype="int64")
|
| 310 |
+
with pytest.raises(ValueError, match="cannot convert to 'float64'-dtype"):
|
| 311 |
+
arr.to_numpy(dtype="float64")
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
def test_to_numpy_copy():
|
| 315 |
+
# to_numpy can be zero-copy if no missing values
|
| 316 |
+
arr = pd.array([True, False, True], dtype="boolean")
|
| 317 |
+
result = arr.to_numpy(dtype=bool)
|
| 318 |
+
result[0] = False
|
| 319 |
+
tm.assert_extension_array_equal(
|
| 320 |
+
arr, pd.array([False, False, True], dtype="boolean")
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
arr = pd.array([True, False, True], dtype="boolean")
|
| 324 |
+
result = arr.to_numpy(dtype=bool, copy=True)
|
| 325 |
+
result[0] = False
|
| 326 |
+
tm.assert_extension_array_equal(arr, pd.array([True, False, True], dtype="boolean"))
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_function.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@pytest.mark.parametrize(
|
| 9 |
+
"ufunc", [np.add, np.logical_or, np.logical_and, np.logical_xor]
|
| 10 |
+
)
|
| 11 |
+
def test_ufuncs_binary(ufunc):
|
| 12 |
+
# two BooleanArrays
|
| 13 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 14 |
+
result = ufunc(a, a)
|
| 15 |
+
expected = pd.array(ufunc(a._data, a._data), dtype="boolean")
|
| 16 |
+
expected[a._mask] = np.nan
|
| 17 |
+
tm.assert_extension_array_equal(result, expected)
|
| 18 |
+
|
| 19 |
+
s = pd.Series(a)
|
| 20 |
+
result = ufunc(s, a)
|
| 21 |
+
expected = pd.Series(ufunc(a._data, a._data), dtype="boolean")
|
| 22 |
+
expected[a._mask] = np.nan
|
| 23 |
+
tm.assert_series_equal(result, expected)
|
| 24 |
+
|
| 25 |
+
# Boolean with numpy array
|
| 26 |
+
arr = np.array([True, True, False])
|
| 27 |
+
result = ufunc(a, arr)
|
| 28 |
+
expected = pd.array(ufunc(a._data, arr), dtype="boolean")
|
| 29 |
+
expected[a._mask] = np.nan
|
| 30 |
+
tm.assert_extension_array_equal(result, expected)
|
| 31 |
+
|
| 32 |
+
result = ufunc(arr, a)
|
| 33 |
+
expected = pd.array(ufunc(arr, a._data), dtype="boolean")
|
| 34 |
+
expected[a._mask] = np.nan
|
| 35 |
+
tm.assert_extension_array_equal(result, expected)
|
| 36 |
+
|
| 37 |
+
# BooleanArray with scalar
|
| 38 |
+
result = ufunc(a, True)
|
| 39 |
+
expected = pd.array(ufunc(a._data, True), dtype="boolean")
|
| 40 |
+
expected[a._mask] = np.nan
|
| 41 |
+
tm.assert_extension_array_equal(result, expected)
|
| 42 |
+
|
| 43 |
+
result = ufunc(True, a)
|
| 44 |
+
expected = pd.array(ufunc(True, a._data), dtype="boolean")
|
| 45 |
+
expected[a._mask] = np.nan
|
| 46 |
+
tm.assert_extension_array_equal(result, expected)
|
| 47 |
+
|
| 48 |
+
# not handled types
|
| 49 |
+
msg = r"operand type\(s\) all returned NotImplemented from __array_ufunc__"
|
| 50 |
+
with pytest.raises(TypeError, match=msg):
|
| 51 |
+
ufunc(a, "test")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@pytest.mark.parametrize("ufunc", [np.logical_not])
|
| 55 |
+
def test_ufuncs_unary(ufunc):
|
| 56 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 57 |
+
result = ufunc(a)
|
| 58 |
+
expected = pd.array(ufunc(a._data), dtype="boolean")
|
| 59 |
+
expected[a._mask] = np.nan
|
| 60 |
+
tm.assert_extension_array_equal(result, expected)
|
| 61 |
+
|
| 62 |
+
ser = pd.Series(a)
|
| 63 |
+
result = ufunc(ser)
|
| 64 |
+
expected = pd.Series(ufunc(a._data), dtype="boolean")
|
| 65 |
+
expected[a._mask] = np.nan
|
| 66 |
+
tm.assert_series_equal(result, expected)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_ufunc_numeric():
|
| 70 |
+
# np.sqrt on np.bool_ returns float16, which we upcast to Float32
|
| 71 |
+
# bc we do not have Float16
|
| 72 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
| 73 |
+
|
| 74 |
+
res = np.sqrt(arr)
|
| 75 |
+
|
| 76 |
+
expected = pd.array([1, 0, None], dtype="Float32")
|
| 77 |
+
tm.assert_extension_array_equal(res, expected)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@pytest.mark.parametrize("values", [[True, False], [True, None]])
|
| 81 |
+
def test_ufunc_reduce_raises(values):
|
| 82 |
+
arr = pd.array(values, dtype="boolean")
|
| 83 |
+
|
| 84 |
+
res = np.add.reduce(arr)
|
| 85 |
+
if arr[-1] is pd.NA:
|
| 86 |
+
expected = pd.NA
|
| 87 |
+
else:
|
| 88 |
+
expected = arr._data.sum()
|
| 89 |
+
tm.assert_almost_equal(res, expected)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def test_value_counts_na():
|
| 93 |
+
arr = pd.array([True, False, pd.NA], dtype="boolean")
|
| 94 |
+
result = arr.value_counts(dropna=False)
|
| 95 |
+
expected = pd.Series([1, 1, 1], index=arr, dtype="Int64", name="count")
|
| 96 |
+
assert expected.index.dtype == arr.dtype
|
| 97 |
+
tm.assert_series_equal(result, expected)
|
| 98 |
+
|
| 99 |
+
result = arr.value_counts(dropna=True)
|
| 100 |
+
expected = pd.Series([1, 1], index=arr[:-1], dtype="Int64", name="count")
|
| 101 |
+
assert expected.index.dtype == arr.dtype
|
| 102 |
+
tm.assert_series_equal(result, expected)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def test_value_counts_with_normalize():
|
| 106 |
+
ser = pd.Series([True, False, pd.NA], dtype="boolean")
|
| 107 |
+
result = ser.value_counts(normalize=True)
|
| 108 |
+
expected = pd.Series([1, 1], index=ser[:-1], dtype="Float64", name="proportion") / 2
|
| 109 |
+
assert expected.index.dtype == "boolean"
|
| 110 |
+
tm.assert_series_equal(result, expected)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def test_diff():
|
| 114 |
+
a = pd.array(
|
| 115 |
+
[True, True, False, False, True, None, True, None, False], dtype="boolean"
|
| 116 |
+
)
|
| 117 |
+
result = pd.core.algorithms.diff(a, 1)
|
| 118 |
+
expected = pd.array(
|
| 119 |
+
[None, False, True, False, True, None, None, None, None], dtype="boolean"
|
| 120 |
+
)
|
| 121 |
+
tm.assert_extension_array_equal(result, expected)
|
| 122 |
+
|
| 123 |
+
ser = pd.Series(a)
|
| 124 |
+
result = ser.diff()
|
| 125 |
+
expected = pd.Series(expected)
|
| 126 |
+
tm.assert_series_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_indexing.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@pytest.mark.parametrize("na", [None, np.nan, pd.NA])
|
| 9 |
+
def test_setitem_missing_values(na):
|
| 10 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
| 11 |
+
expected = pd.array([True, None, None], dtype="boolean")
|
| 12 |
+
arr[1] = na
|
| 13 |
+
tm.assert_extension_array_equal(arr, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_logical.py
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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9 |
+
from pandas.core.ops.mask_ops import (
|
| 10 |
+
kleene_and,
|
| 11 |
+
kleene_or,
|
| 12 |
+
kleene_xor,
|
| 13 |
+
)
|
| 14 |
+
from pandas.tests.extension.base import BaseOpsUtil
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class TestLogicalOps(BaseOpsUtil):
|
| 18 |
+
def test_numpy_scalars_ok(self, all_logical_operators):
|
| 19 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 20 |
+
op = getattr(a, all_logical_operators)
|
| 21 |
+
|
| 22 |
+
tm.assert_extension_array_equal(op(True), op(np.bool_(True)))
|
| 23 |
+
tm.assert_extension_array_equal(op(False), op(np.bool_(False)))
|
| 24 |
+
|
| 25 |
+
def get_op_from_name(self, op_name):
|
| 26 |
+
short_opname = op_name.strip("_")
|
| 27 |
+
short_opname = short_opname if "xor" in short_opname else short_opname + "_"
|
| 28 |
+
try:
|
| 29 |
+
op = getattr(operator, short_opname)
|
| 30 |
+
except AttributeError:
|
| 31 |
+
# Assume it is the reverse operator
|
| 32 |
+
rop = getattr(operator, short_opname[1:])
|
| 33 |
+
op = lambda x, y: rop(y, x)
|
| 34 |
+
|
| 35 |
+
return op
|
| 36 |
+
|
| 37 |
+
def test_empty_ok(self, all_logical_operators):
|
| 38 |
+
a = pd.array([], dtype="boolean")
|
| 39 |
+
op_name = all_logical_operators
|
| 40 |
+
result = getattr(a, op_name)(True)
|
| 41 |
+
tm.assert_extension_array_equal(a, result)
|
| 42 |
+
|
| 43 |
+
result = getattr(a, op_name)(False)
|
| 44 |
+
tm.assert_extension_array_equal(a, result)
|
| 45 |
+
|
| 46 |
+
result = getattr(a, op_name)(pd.NA)
|
| 47 |
+
tm.assert_extension_array_equal(a, result)
|
| 48 |
+
|
| 49 |
+
@pytest.mark.parametrize(
|
| 50 |
+
"other", ["a", pd.Timestamp(2017, 1, 1, 12), np.timedelta64(4)]
|
| 51 |
+
)
|
| 52 |
+
def test_eq_mismatched_type(self, other):
|
| 53 |
+
# GH-44499
|
| 54 |
+
arr = pd.array([True, False])
|
| 55 |
+
result = arr == other
|
| 56 |
+
expected = pd.array([False, False])
|
| 57 |
+
tm.assert_extension_array_equal(result, expected)
|
| 58 |
+
|
| 59 |
+
result = arr != other
|
| 60 |
+
expected = pd.array([True, True])
|
| 61 |
+
tm.assert_extension_array_equal(result, expected)
|
| 62 |
+
|
| 63 |
+
def test_logical_length_mismatch_raises(self, all_logical_operators):
|
| 64 |
+
op_name = all_logical_operators
|
| 65 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 66 |
+
msg = "Lengths must match"
|
| 67 |
+
|
| 68 |
+
with pytest.raises(ValueError, match=msg):
|
| 69 |
+
getattr(a, op_name)([True, False])
|
| 70 |
+
|
| 71 |
+
with pytest.raises(ValueError, match=msg):
|
| 72 |
+
getattr(a, op_name)(np.array([True, False]))
|
| 73 |
+
|
| 74 |
+
with pytest.raises(ValueError, match=msg):
|
| 75 |
+
getattr(a, op_name)(pd.array([True, False], dtype="boolean"))
|
| 76 |
+
|
| 77 |
+
def test_logical_nan_raises(self, all_logical_operators):
|
| 78 |
+
op_name = all_logical_operators
|
| 79 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 80 |
+
msg = "Got float instead"
|
| 81 |
+
|
| 82 |
+
with pytest.raises(TypeError, match=msg):
|
| 83 |
+
getattr(a, op_name)(np.nan)
|
| 84 |
+
|
| 85 |
+
@pytest.mark.parametrize("other", ["a", 1])
|
| 86 |
+
def test_non_bool_or_na_other_raises(self, other, all_logical_operators):
|
| 87 |
+
a = pd.array([True, False], dtype="boolean")
|
| 88 |
+
with pytest.raises(TypeError, match=str(type(other).__name__)):
|
| 89 |
+
getattr(a, all_logical_operators)(other)
|
| 90 |
+
|
| 91 |
+
def test_kleene_or(self):
|
| 92 |
+
# A clear test of behavior.
|
| 93 |
+
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 94 |
+
b = pd.array([True, False, None] * 3, dtype="boolean")
|
| 95 |
+
result = a | b
|
| 96 |
+
expected = pd.array(
|
| 97 |
+
[True, True, True, True, False, None, True, None, None], dtype="boolean"
|
| 98 |
+
)
|
| 99 |
+
tm.assert_extension_array_equal(result, expected)
|
| 100 |
+
|
| 101 |
+
result = b | a
|
| 102 |
+
tm.assert_extension_array_equal(result, expected)
|
| 103 |
+
|
| 104 |
+
# ensure we haven't mutated anything inplace
|
| 105 |
+
tm.assert_extension_array_equal(
|
| 106 |
+
a, pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 107 |
+
)
|
| 108 |
+
tm.assert_extension_array_equal(
|
| 109 |
+
b, pd.array([True, False, None] * 3, dtype="boolean")
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
@pytest.mark.parametrize(
|
| 113 |
+
"other, expected",
|
| 114 |
+
[
|
| 115 |
+
(pd.NA, [True, None, None]),
|
| 116 |
+
(True, [True, True, True]),
|
| 117 |
+
(np.bool_(True), [True, True, True]),
|
| 118 |
+
(False, [True, False, None]),
|
| 119 |
+
(np.bool_(False), [True, False, None]),
|
| 120 |
+
],
|
| 121 |
+
)
|
| 122 |
+
def test_kleene_or_scalar(self, other, expected):
|
| 123 |
+
# TODO: test True & False
|
| 124 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 125 |
+
result = a | other
|
| 126 |
+
expected = pd.array(expected, dtype="boolean")
|
| 127 |
+
tm.assert_extension_array_equal(result, expected)
|
| 128 |
+
|
| 129 |
+
result = other | a
|
| 130 |
+
tm.assert_extension_array_equal(result, expected)
|
| 131 |
+
|
| 132 |
+
# ensure we haven't mutated anything inplace
|
| 133 |
+
tm.assert_extension_array_equal(
|
| 134 |
+
a, pd.array([True, False, None], dtype="boolean")
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
def test_kleene_and(self):
|
| 138 |
+
# A clear test of behavior.
|
| 139 |
+
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 140 |
+
b = pd.array([True, False, None] * 3, dtype="boolean")
|
| 141 |
+
result = a & b
|
| 142 |
+
expected = pd.array(
|
| 143 |
+
[True, False, None, False, False, False, None, False, None], dtype="boolean"
|
| 144 |
+
)
|
| 145 |
+
tm.assert_extension_array_equal(result, expected)
|
| 146 |
+
|
| 147 |
+
result = b & a
|
| 148 |
+
tm.assert_extension_array_equal(result, expected)
|
| 149 |
+
|
| 150 |
+
# ensure we haven't mutated anything inplace
|
| 151 |
+
tm.assert_extension_array_equal(
|
| 152 |
+
a, pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 153 |
+
)
|
| 154 |
+
tm.assert_extension_array_equal(
|
| 155 |
+
b, pd.array([True, False, None] * 3, dtype="boolean")
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
@pytest.mark.parametrize(
|
| 159 |
+
"other, expected",
|
| 160 |
+
[
|
| 161 |
+
(pd.NA, [None, False, None]),
|
| 162 |
+
(True, [True, False, None]),
|
| 163 |
+
(False, [False, False, False]),
|
| 164 |
+
(np.bool_(True), [True, False, None]),
|
| 165 |
+
(np.bool_(False), [False, False, False]),
|
| 166 |
+
],
|
| 167 |
+
)
|
| 168 |
+
def test_kleene_and_scalar(self, other, expected):
|
| 169 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 170 |
+
result = a & other
|
| 171 |
+
expected = pd.array(expected, dtype="boolean")
|
| 172 |
+
tm.assert_extension_array_equal(result, expected)
|
| 173 |
+
|
| 174 |
+
result = other & a
|
| 175 |
+
tm.assert_extension_array_equal(result, expected)
|
| 176 |
+
|
| 177 |
+
# ensure we haven't mutated anything inplace
|
| 178 |
+
tm.assert_extension_array_equal(
|
| 179 |
+
a, pd.array([True, False, None], dtype="boolean")
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
def test_kleene_xor(self):
|
| 183 |
+
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 184 |
+
b = pd.array([True, False, None] * 3, dtype="boolean")
|
| 185 |
+
result = a ^ b
|
| 186 |
+
expected = pd.array(
|
| 187 |
+
[False, True, None, True, False, None, None, None, None], dtype="boolean"
|
| 188 |
+
)
|
| 189 |
+
tm.assert_extension_array_equal(result, expected)
|
| 190 |
+
|
| 191 |
+
result = b ^ a
|
| 192 |
+
tm.assert_extension_array_equal(result, expected)
|
| 193 |
+
|
| 194 |
+
# ensure we haven't mutated anything inplace
|
| 195 |
+
tm.assert_extension_array_equal(
|
| 196 |
+
a, pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 197 |
+
)
|
| 198 |
+
tm.assert_extension_array_equal(
|
| 199 |
+
b, pd.array([True, False, None] * 3, dtype="boolean")
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
@pytest.mark.parametrize(
|
| 203 |
+
"other, expected",
|
| 204 |
+
[
|
| 205 |
+
(pd.NA, [None, None, None]),
|
| 206 |
+
(True, [False, True, None]),
|
| 207 |
+
(np.bool_(True), [False, True, None]),
|
| 208 |
+
(np.bool_(False), [True, False, None]),
|
| 209 |
+
],
|
| 210 |
+
)
|
| 211 |
+
def test_kleene_xor_scalar(self, other, expected):
|
| 212 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 213 |
+
result = a ^ other
|
| 214 |
+
expected = pd.array(expected, dtype="boolean")
|
| 215 |
+
tm.assert_extension_array_equal(result, expected)
|
| 216 |
+
|
| 217 |
+
result = other ^ a
|
| 218 |
+
tm.assert_extension_array_equal(result, expected)
|
| 219 |
+
|
| 220 |
+
# ensure we haven't mutated anything inplace
|
| 221 |
+
tm.assert_extension_array_equal(
|
| 222 |
+
a, pd.array([True, False, None], dtype="boolean")
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
@pytest.mark.parametrize("other", [True, False, pd.NA, [True, False, None] * 3])
|
| 226 |
+
def test_no_masked_assumptions(self, other, all_logical_operators):
|
| 227 |
+
# The logical operations should not assume that masked values are False!
|
| 228 |
+
a = pd.arrays.BooleanArray(
|
| 229 |
+
np.array([True, True, True, False, False, False, True, False, True]),
|
| 230 |
+
np.array([False] * 6 + [True, True, True]),
|
| 231 |
+
)
|
| 232 |
+
b = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")
|
| 233 |
+
if isinstance(other, list):
|
| 234 |
+
other = pd.array(other, dtype="boolean")
|
| 235 |
+
|
| 236 |
+
result = getattr(a, all_logical_operators)(other)
|
| 237 |
+
expected = getattr(b, all_logical_operators)(other)
|
| 238 |
+
tm.assert_extension_array_equal(result, expected)
|
| 239 |
+
|
| 240 |
+
if isinstance(other, BooleanArray):
|
| 241 |
+
other._data[other._mask] = True
|
| 242 |
+
a._data[a._mask] = False
|
| 243 |
+
|
| 244 |
+
result = getattr(a, all_logical_operators)(other)
|
| 245 |
+
expected = getattr(b, all_logical_operators)(other)
|
| 246 |
+
tm.assert_extension_array_equal(result, expected)
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
@pytest.mark.parametrize("operation", [kleene_or, kleene_xor, kleene_and])
|
| 250 |
+
def test_error_both_scalar(operation):
|
| 251 |
+
msg = r"Either `left` or `right` need to be a np\.ndarray."
|
| 252 |
+
with pytest.raises(TypeError, match=msg):
|
| 253 |
+
# masks need to be non-None, otherwise it ends up in an infinite recursion
|
| 254 |
+
operation(True, True, np.zeros(1), np.zeros(1))
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_ops.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import pandas._testing as tm
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class TestUnaryOps:
|
| 6 |
+
def test_invert(self):
|
| 7 |
+
a = pd.array([True, False, None], dtype="boolean")
|
| 8 |
+
expected = pd.array([False, True, None], dtype="boolean")
|
| 9 |
+
tm.assert_extension_array_equal(~a, expected)
|
| 10 |
+
|
| 11 |
+
expected = pd.Series(expected, index=["a", "b", "c"], name="name")
|
| 12 |
+
result = ~pd.Series(a, index=["a", "b", "c"], name="name")
|
| 13 |
+
tm.assert_series_equal(result, expected)
|
| 14 |
+
|
| 15 |
+
df = pd.DataFrame({"A": a, "B": [True, False, False]}, index=["a", "b", "c"])
|
| 16 |
+
result = ~df
|
| 17 |
+
expected = pd.DataFrame(
|
| 18 |
+
{"A": expected, "B": [False, True, True]}, index=["a", "b", "c"]
|
| 19 |
+
)
|
| 20 |
+
tm.assert_frame_equal(result, expected)
|
| 21 |
+
|
| 22 |
+
def test_abs(self):
|
| 23 |
+
# matching numpy behavior, abs is the identity function
|
| 24 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
| 25 |
+
result = abs(arr)
|
| 26 |
+
|
| 27 |
+
tm.assert_extension_array_equal(result, arr)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_reduction.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@pytest.fixture
|
| 8 |
+
def data():
|
| 9 |
+
"""Fixture returning boolean array, with valid and missing values."""
|
| 10 |
+
return pd.array(
|
| 11 |
+
[True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False],
|
| 12 |
+
dtype="boolean",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@pytest.mark.parametrize(
|
| 17 |
+
"values, exp_any, exp_all, exp_any_noskip, exp_all_noskip",
|
| 18 |
+
[
|
| 19 |
+
([True, pd.NA], True, True, True, pd.NA),
|
| 20 |
+
([False, pd.NA], False, False, pd.NA, False),
|
| 21 |
+
([pd.NA], False, True, pd.NA, pd.NA),
|
| 22 |
+
([], False, True, False, True),
|
| 23 |
+
# GH-33253: all True / all False values buggy with skipna=False
|
| 24 |
+
([True, True], True, True, True, True),
|
| 25 |
+
([False, False], False, False, False, False),
|
| 26 |
+
],
|
| 27 |
+
)
|
| 28 |
+
def test_any_all(values, exp_any, exp_all, exp_any_noskip, exp_all_noskip):
|
| 29 |
+
# the methods return numpy scalars
|
| 30 |
+
exp_any = pd.NA if exp_any is pd.NA else np.bool_(exp_any)
|
| 31 |
+
exp_all = pd.NA if exp_all is pd.NA else np.bool_(exp_all)
|
| 32 |
+
exp_any_noskip = pd.NA if exp_any_noskip is pd.NA else np.bool_(exp_any_noskip)
|
| 33 |
+
exp_all_noskip = pd.NA if exp_all_noskip is pd.NA else np.bool_(exp_all_noskip)
|
| 34 |
+
|
| 35 |
+
for con in [pd.array, pd.Series]:
|
| 36 |
+
a = con(values, dtype="boolean")
|
| 37 |
+
assert a.any() is exp_any
|
| 38 |
+
assert a.all() is exp_all
|
| 39 |
+
assert a.any(skipna=False) is exp_any_noskip
|
| 40 |
+
assert a.all(skipna=False) is exp_all_noskip
|
| 41 |
+
|
| 42 |
+
assert np.any(a.any()) is exp_any
|
| 43 |
+
assert np.all(a.all()) is exp_all
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@pytest.mark.parametrize("dropna", [True, False])
|
| 47 |
+
def test_reductions_return_types(dropna, data, all_numeric_reductions):
|
| 48 |
+
op = all_numeric_reductions
|
| 49 |
+
s = pd.Series(data)
|
| 50 |
+
if dropna:
|
| 51 |
+
s = s.dropna()
|
| 52 |
+
|
| 53 |
+
if op in ("sum", "prod"):
|
| 54 |
+
assert isinstance(getattr(s, op)(), np.int_)
|
| 55 |
+
elif op == "count":
|
| 56 |
+
# Oddly on the 32 bit build (but not Windows), this is intc (!= intp)
|
| 57 |
+
assert isinstance(getattr(s, op)(), np.integer)
|
| 58 |
+
elif op in ("min", "max"):
|
| 59 |
+
assert isinstance(getattr(s, op)(), np.bool_)
|
| 60 |
+
else:
|
| 61 |
+
# "mean", "std", "var", "median", "kurt", "skew"
|
| 62 |
+
assert isinstance(getattr(s, op)(), np.float64)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_repr.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_repr():
|
| 5 |
+
df = pd.DataFrame({"A": pd.array([True, False, None], dtype="boolean")})
|
| 6 |
+
expected = " A\n0 True\n1 False\n2 <NA>"
|
| 7 |
+
assert repr(df) == expected
|
| 8 |
+
|
| 9 |
+
expected = "0 True\n1 False\n2 <NA>\nName: A, dtype: boolean"
|
| 10 |
+
assert repr(df.A) == expected
|
| 11 |
+
|
| 12 |
+
expected = "<BooleanArray>\n[True, False, <NA>]\nLength: 3, dtype: boolean"
|
| 13 |
+
assert repr(df.A.array) == expected
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (187 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/conftest.cpython-310.pyc
ADDED
|
Binary file (664 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_algos.cpython-310.pyc
ADDED
|
Binary file (2.54 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_analytics.cpython-310.pyc
ADDED
|
Binary file (10.8 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_api.cpython-310.pyc
ADDED
|
Binary file (14.2 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_astype.cpython-310.pyc
ADDED
|
Binary file (3.65 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_constructors.cpython-310.pyc
ADDED
|
Binary file (27 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_dtypes.cpython-310.pyc
ADDED
|
Binary file (4.93 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (12.2 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_missing.cpython-310.pyc
ADDED
|
Binary file (6.43 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_operators.cpython-310.pyc
ADDED
|
Binary file (12.1 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_replace.cpython-310.pyc
ADDED
|
Binary file (2.79 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_repr.cpython-310.pyc
ADDED
|
Binary file (23.8 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_sorting.cpython-310.pyc
ADDED
|
Binary file (3.49 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_subclass.cpython-310.pyc
ADDED
|
Binary file (1.45 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_take.cpython-310.pyc
ADDED
|
Binary file (3.68 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/__pycache__/test_warnings.cpython-310.pyc
ADDED
|
Binary file (1.11 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/conftest.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import Categorical
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
@pytest.fixture(params=[True, False])
|
| 7 |
+
def allow_fill(request):
|
| 8 |
+
"""Boolean 'allow_fill' parameter for Categorical.take"""
|
| 9 |
+
return request.param
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@pytest.fixture
|
| 13 |
+
def factor():
|
| 14 |
+
"""Fixture returning a Categorical object"""
|
| 15 |
+
return Categorical(["a", "b", "b", "a", "a", "c", "c", "c"], ordered=True)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_algos.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@pytest.mark.parametrize("ordered", [True, False])
|
| 9 |
+
@pytest.mark.parametrize("categories", [["b", "a", "c"], ["a", "b", "c", "d"]])
|
| 10 |
+
def test_factorize(categories, ordered):
|
| 11 |
+
cat = pd.Categorical(
|
| 12 |
+
["b", "b", "a", "c", None], categories=categories, ordered=ordered
|
| 13 |
+
)
|
| 14 |
+
codes, uniques = pd.factorize(cat)
|
| 15 |
+
expected_codes = np.array([0, 0, 1, 2, -1], dtype=np.intp)
|
| 16 |
+
expected_uniques = pd.Categorical(
|
| 17 |
+
["b", "a", "c"], categories=categories, ordered=ordered
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
tm.assert_numpy_array_equal(codes, expected_codes)
|
| 21 |
+
tm.assert_categorical_equal(uniques, expected_uniques)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_factorized_sort():
|
| 25 |
+
cat = pd.Categorical(["b", "b", None, "a"])
|
| 26 |
+
codes, uniques = pd.factorize(cat, sort=True)
|
| 27 |
+
expected_codes = np.array([1, 1, -1, 0], dtype=np.intp)
|
| 28 |
+
expected_uniques = pd.Categorical(["a", "b"])
|
| 29 |
+
|
| 30 |
+
tm.assert_numpy_array_equal(codes, expected_codes)
|
| 31 |
+
tm.assert_categorical_equal(uniques, expected_uniques)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def test_factorized_sort_ordered():
|
| 35 |
+
cat = pd.Categorical(
|
| 36 |
+
["b", "b", None, "a"], categories=["c", "b", "a"], ordered=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
codes, uniques = pd.factorize(cat, sort=True)
|
| 40 |
+
expected_codes = np.array([0, 0, -1, 1], dtype=np.intp)
|
| 41 |
+
expected_uniques = pd.Categorical(
|
| 42 |
+
["b", "a"], categories=["c", "b", "a"], ordered=True
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
tm.assert_numpy_array_equal(codes, expected_codes)
|
| 46 |
+
tm.assert_categorical_equal(uniques, expected_uniques)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_isin_cats():
|
| 50 |
+
# GH2003
|
| 51 |
+
cat = pd.Categorical(["a", "b", np.nan])
|
| 52 |
+
|
| 53 |
+
result = cat.isin(["a", np.nan])
|
| 54 |
+
expected = np.array([True, False, True], dtype=bool)
|
| 55 |
+
tm.assert_numpy_array_equal(expected, result)
|
| 56 |
+
|
| 57 |
+
result = cat.isin(["a", "c"])
|
| 58 |
+
expected = np.array([True, False, False], dtype=bool)
|
| 59 |
+
tm.assert_numpy_array_equal(expected, result)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@pytest.mark.parametrize("empty", [[], pd.Series(dtype=object), np.array([])])
|
| 63 |
+
def test_isin_empty(empty):
|
| 64 |
+
s = pd.Categorical(["a", "b"])
|
| 65 |
+
expected = np.array([False, False], dtype=bool)
|
| 66 |
+
|
| 67 |
+
result = s.isin(empty)
|
| 68 |
+
tm.assert_numpy_array_equal(expected, result)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def test_diff():
|
| 72 |
+
ser = pd.Series([1, 2, 3], dtype="category")
|
| 73 |
+
|
| 74 |
+
msg = "Convert to a suitable dtype"
|
| 75 |
+
with pytest.raises(TypeError, match=msg):
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from pandas.compat import PYPY
|
| 8 |
+
|
| 9 |
+
from pandas import (
|
| 10 |
+
Categorical,
|
| 11 |
+
CategoricalDtype,
|
| 12 |
+
Index,
|
| 13 |
+
NaT,
|
| 14 |
+
Series,
|
| 15 |
+
date_range,
|
| 16 |
+
)
|
| 17 |
+
import pandas._testing as tm
|
| 18 |
+
from pandas.api.types import is_scalar
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class TestCategoricalAnalytics:
|
| 22 |
+
@pytest.mark.parametrize("aggregation", ["min", "max"])
|
| 23 |
+
def test_min_max_not_ordered_raises(self, aggregation):
|
| 24 |
+
# unordered cats have no min/max
|
| 25 |
+
cat = Categorical(["a", "b", "c", "d"], ordered=False)
|
| 26 |
+
msg = f"Categorical is not ordered for operation {aggregation}"
|
| 27 |
+
agg_func = getattr(cat, aggregation)
|
| 28 |
+
|
| 29 |
+
with pytest.raises(TypeError, match=msg):
|
| 30 |
+
agg_func()
|
| 31 |
+
|
| 32 |
+
ufunc = np.minimum if aggregation == "min" else np.maximum
|
| 33 |
+
with pytest.raises(TypeError, match=msg):
|
| 34 |
+
ufunc.reduce(cat)
|
| 35 |
+
|
| 36 |
+
def test_min_max_ordered(self, index_or_series_or_array):
|
| 37 |
+
cat = Categorical(["a", "b", "c", "d"], ordered=True)
|
| 38 |
+
obj = index_or_series_or_array(cat)
|
| 39 |
+
_min = obj.min()
|
| 40 |
+
_max = obj.max()
|
| 41 |
+
assert _min == "a"
|
| 42 |
+
assert _max == "d"
|
| 43 |
+
|
| 44 |
+
assert np.minimum.reduce(obj) == "a"
|
| 45 |
+
assert np.maximum.reduce(obj) == "d"
|
| 46 |
+
# TODO: raises if we pass axis=0 (on Index and Categorical, not Series)
|
| 47 |
+
|
| 48 |
+
cat = Categorical(
|
| 49 |
+
["a", "b", "c", "d"], categories=["d", "c", "b", "a"], ordered=True
|
| 50 |
+
)
|
| 51 |
+
obj = index_or_series_or_array(cat)
|
| 52 |
+
_min = obj.min()
|
| 53 |
+
_max = obj.max()
|
| 54 |
+
assert _min == "d"
|
| 55 |
+
assert _max == "a"
|
| 56 |
+
assert np.minimum.reduce(obj) == "d"
|
| 57 |
+
assert np.maximum.reduce(obj) == "a"
|
| 58 |
+
|
| 59 |
+
@pytest.mark.parametrize(
|
| 60 |
+
"categories,expected",
|
| 61 |
+
[
|
| 62 |
+
(list("ABC"), np.NaN),
|
| 63 |
+
([1, 2, 3], np.NaN),
|
| 64 |
+
pytest.param(
|
| 65 |
+
Series(date_range("2020-01-01", periods=3), dtype="category"),
|
| 66 |
+
NaT,
|
| 67 |
+
marks=pytest.mark.xfail(
|
| 68 |
+
reason="https://github.com/pandas-dev/pandas/issues/29962"
|
| 69 |
+
),
|
| 70 |
+
),
|
| 71 |
+
],
|
| 72 |
+
)
|
| 73 |
+
@pytest.mark.parametrize("aggregation", ["min", "max"])
|
| 74 |
+
def test_min_max_ordered_empty(self, categories, expected, aggregation):
|
| 75 |
+
# GH 30227
|
| 76 |
+
cat = Categorical([], categories=categories, ordered=True)
|
| 77 |
+
|
| 78 |
+
agg_func = getattr(cat, aggregation)
|
| 79 |
+
result = agg_func()
|
| 80 |
+
assert result is expected
|
| 81 |
+
|
| 82 |
+
@pytest.mark.parametrize(
|
| 83 |
+
"values, categories",
|
| 84 |
+
[(["a", "b", "c", np.nan], list("cba")), ([1, 2, 3, np.nan], [3, 2, 1])],
|
| 85 |
+
)
|
| 86 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
| 87 |
+
@pytest.mark.parametrize("function", ["min", "max"])
|
| 88 |
+
def test_min_max_with_nan(self, values, categories, function, skipna):
|
| 89 |
+
# GH 25303
|
| 90 |
+
cat = Categorical(values, categories=categories, ordered=True)
|
| 91 |
+
result = getattr(cat, function)(skipna=skipna)
|
| 92 |
+
|
| 93 |
+
if skipna is False:
|
| 94 |
+
assert result is np.nan
|
| 95 |
+
else:
|
| 96 |
+
expected = categories[0] if function == "min" else categories[2]
|
| 97 |
+
assert result == expected
|
| 98 |
+
|
| 99 |
+
@pytest.mark.parametrize("function", ["min", "max"])
|
| 100 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
| 101 |
+
def test_min_max_only_nan(self, function, skipna):
|
| 102 |
+
# https://github.com/pandas-dev/pandas/issues/33450
|
| 103 |
+
cat = Categorical([np.nan], categories=[1, 2], ordered=True)
|
| 104 |
+
result = getattr(cat, function)(skipna=skipna)
|
| 105 |
+
assert result is np.nan
|
| 106 |
+
|
| 107 |
+
@pytest.mark.parametrize("method", ["min", "max"])
|
| 108 |
+
def test_numeric_only_min_max_raises(self, method):
|
| 109 |
+
# GH 25303
|
| 110 |
+
cat = Categorical(
|
| 111 |
+
[np.nan, 1, 2, np.nan], categories=[5, 4, 3, 2, 1], ordered=True
|
| 112 |
+
)
|
| 113 |
+
with pytest.raises(TypeError, match=".* got an unexpected keyword"):
|
| 114 |
+
getattr(cat, method)(numeric_only=True)
|
| 115 |
+
|
| 116 |
+
@pytest.mark.parametrize("method", ["min", "max"])
|
| 117 |
+
def test_numpy_min_max_raises(self, method):
|
| 118 |
+
cat = Categorical(["a", "b", "c", "b"], ordered=False)
|
| 119 |
+
msg = (
|
| 120 |
+
f"Categorical is not ordered for operation {method}\n"
|
| 121 |
+
"you can use .as_ordered() to change the Categorical to an ordered one"
|
| 122 |
+
)
|
| 123 |
+
method = getattr(np, method)
|
| 124 |
+
with pytest.raises(TypeError, match=re.escape(msg)):
|
| 125 |
+
method(cat)
|
| 126 |
+
|
| 127 |
+
@pytest.mark.parametrize("kwarg", ["axis", "out", "keepdims"])
|
| 128 |
+
@pytest.mark.parametrize("method", ["min", "max"])
|
| 129 |
+
def test_numpy_min_max_unsupported_kwargs_raises(self, method, kwarg):
|
| 130 |
+
cat = Categorical(["a", "b", "c", "b"], ordered=True)
|
| 131 |
+
msg = (
|
| 132 |
+
f"the '{kwarg}' parameter is not supported in the pandas implementation "
|
| 133 |
+
f"of {method}"
|
| 134 |
+
)
|
| 135 |
+
if kwarg == "axis":
|
| 136 |
+
msg = r"`axis` must be fewer than the number of dimensions \(1\)"
|
| 137 |
+
kwargs = {kwarg: 42}
|
| 138 |
+
method = getattr(np, method)
|
| 139 |
+
with pytest.raises(ValueError, match=msg):
|
| 140 |
+
method(cat, **kwargs)
|
| 141 |
+
|
| 142 |
+
@pytest.mark.parametrize("method, expected", [("min", "a"), ("max", "c")])
|
| 143 |
+
def test_numpy_min_max_axis_equals_none(self, method, expected):
|
| 144 |
+
cat = Categorical(["a", "b", "c", "b"], ordered=True)
|
| 145 |
+
method = getattr(np, method)
|
| 146 |
+
result = method(cat, axis=None)
|
| 147 |
+
assert result == expected
|
| 148 |
+
|
| 149 |
+
@pytest.mark.parametrize(
|
| 150 |
+
"values,categories,exp_mode",
|
| 151 |
+
[
|
| 152 |
+
([1, 1, 2, 4, 5, 5, 5], [5, 4, 3, 2, 1], [5]),
|
| 153 |
+
([1, 1, 1, 4, 5, 5, 5], [5, 4, 3, 2, 1], [5, 1]),
|
| 154 |
+
([1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [5, 4, 3, 2, 1]),
|
| 155 |
+
([np.nan, np.nan, np.nan, 4, 5], [5, 4, 3, 2, 1], [5, 4]),
|
| 156 |
+
([np.nan, np.nan, np.nan, 4, 5, 4], [5, 4, 3, 2, 1], [4]),
|
| 157 |
+
([np.nan, np.nan, 4, 5, 4], [5, 4, 3, 2, 1], [4]),
|
| 158 |
+
],
|
| 159 |
+
)
|
| 160 |
+
def test_mode(self, values, categories, exp_mode):
|
| 161 |
+
cat = Categorical(values, categories=categories, ordered=True)
|
| 162 |
+
res = Series(cat).mode()._values
|
| 163 |
+
exp = Categorical(exp_mode, categories=categories, ordered=True)
|
| 164 |
+
tm.assert_categorical_equal(res, exp)
|
| 165 |
+
|
| 166 |
+
def test_searchsorted(self, ordered):
|
| 167 |
+
# https://github.com/pandas-dev/pandas/issues/8420
|
| 168 |
+
# https://github.com/pandas-dev/pandas/issues/14522
|
| 169 |
+
|
| 170 |
+
cat = Categorical(
|
| 171 |
+
["cheese", "milk", "apple", "bread", "bread"],
|
| 172 |
+
categories=["cheese", "milk", "apple", "bread"],
|
| 173 |
+
ordered=ordered,
|
| 174 |
+
)
|
| 175 |
+
ser = Series(cat)
|
| 176 |
+
|
| 177 |
+
# Searching for single item argument, side='left' (default)
|
| 178 |
+
res_cat = cat.searchsorted("apple")
|
| 179 |
+
assert res_cat == 2
|
| 180 |
+
assert is_scalar(res_cat)
|
| 181 |
+
|
| 182 |
+
res_ser = ser.searchsorted("apple")
|
| 183 |
+
assert res_ser == 2
|
| 184 |
+
assert is_scalar(res_ser)
|
| 185 |
+
|
| 186 |
+
# Searching for single item array, side='left' (default)
|
| 187 |
+
res_cat = cat.searchsorted(["bread"])
|
| 188 |
+
res_ser = ser.searchsorted(["bread"])
|
| 189 |
+
exp = np.array([3], dtype=np.intp)
|
| 190 |
+
tm.assert_numpy_array_equal(res_cat, exp)
|
| 191 |
+
tm.assert_numpy_array_equal(res_ser, exp)
|
| 192 |
+
|
| 193 |
+
# Searching for several items array, side='right'
|
| 194 |
+
res_cat = cat.searchsorted(["apple", "bread"], side="right")
|
| 195 |
+
res_ser = ser.searchsorted(["apple", "bread"], side="right")
|
| 196 |
+
exp = np.array([3, 5], dtype=np.intp)
|
| 197 |
+
tm.assert_numpy_array_equal(res_cat, exp)
|
| 198 |
+
tm.assert_numpy_array_equal(res_ser, exp)
|
| 199 |
+
|
| 200 |
+
# Searching for a single value that is not from the Categorical
|
| 201 |
+
with pytest.raises(TypeError, match="cucumber"):
|
| 202 |
+
cat.searchsorted("cucumber")
|
| 203 |
+
with pytest.raises(TypeError, match="cucumber"):
|
| 204 |
+
ser.searchsorted("cucumber")
|
| 205 |
+
|
| 206 |
+
# Searching for multiple values one of each is not from the Categorical
|
| 207 |
+
msg = (
|
| 208 |
+
"Cannot setitem on a Categorical with a new category, "
|
| 209 |
+
"set the categories first"
|
| 210 |
+
)
|
| 211 |
+
with pytest.raises(TypeError, match=msg):
|
| 212 |
+
cat.searchsorted(["bread", "cucumber"])
|
| 213 |
+
with pytest.raises(TypeError, match=msg):
|
| 214 |
+
ser.searchsorted(["bread", "cucumber"])
|
| 215 |
+
|
| 216 |
+
def test_unique(self, ordered):
|
| 217 |
+
# GH38140
|
| 218 |
+
dtype = CategoricalDtype(["a", "b", "c"], ordered=ordered)
|
| 219 |
+
|
| 220 |
+
# categories are reordered based on value when ordered=False
|
| 221 |
+
cat = Categorical(["a", "b", "c"], dtype=dtype)
|
| 222 |
+
res = cat.unique()
|
| 223 |
+
tm.assert_categorical_equal(res, cat)
|
| 224 |
+
|
| 225 |
+
cat = Categorical(["a", "b", "a", "a"], dtype=dtype)
|
| 226 |
+
res = cat.unique()
|
| 227 |
+
tm.assert_categorical_equal(res, Categorical(["a", "b"], dtype=dtype))
|
| 228 |
+
|
| 229 |
+
cat = Categorical(["c", "a", "b", "a", "a"], dtype=dtype)
|
| 230 |
+
res = cat.unique()
|
| 231 |
+
exp_cat = Categorical(["c", "a", "b"], dtype=dtype)
|
| 232 |
+
tm.assert_categorical_equal(res, exp_cat)
|
| 233 |
+
|
| 234 |
+
# nan must be removed
|
| 235 |
+
cat = Categorical(["b", np.nan, "b", np.nan, "a"], dtype=dtype)
|
| 236 |
+
res = cat.unique()
|
| 237 |
+
exp_cat = Categorical(["b", np.nan, "a"], dtype=dtype)
|
| 238 |
+
tm.assert_categorical_equal(res, exp_cat)
|
| 239 |
+
|
| 240 |
+
def test_unique_index_series(self, ordered):
|
| 241 |
+
# GH38140
|
| 242 |
+
dtype = CategoricalDtype([3, 2, 1], ordered=ordered)
|
| 243 |
+
|
| 244 |
+
c = Categorical([3, 1, 2, 2, 1], dtype=dtype)
|
| 245 |
+
# Categorical.unique sorts categories by appearance order
|
| 246 |
+
# if ordered=False
|
| 247 |
+
exp = Categorical([3, 1, 2], dtype=dtype)
|
| 248 |
+
tm.assert_categorical_equal(c.unique(), exp)
|
| 249 |
+
|
| 250 |
+
tm.assert_index_equal(Index(c).unique(), Index(exp))
|
| 251 |
+
tm.assert_categorical_equal(Series(c).unique(), exp)
|
| 252 |
+
|
| 253 |
+
c = Categorical([1, 1, 2, 2], dtype=dtype)
|
| 254 |
+
exp = Categorical([1, 2], dtype=dtype)
|
| 255 |
+
tm.assert_categorical_equal(c.unique(), exp)
|
| 256 |
+
tm.assert_index_equal(Index(c).unique(), Index(exp))
|
| 257 |
+
tm.assert_categorical_equal(Series(c).unique(), exp)
|
| 258 |
+
|
| 259 |
+
def test_shift(self):
|
| 260 |
+
# GH 9416
|
| 261 |
+
cat = Categorical(["a", "b", "c", "d", "a"])
|
| 262 |
+
|
| 263 |
+
# shift forward
|
| 264 |
+
sp1 = cat.shift(1)
|
| 265 |
+
xp1 = Categorical([np.nan, "a", "b", "c", "d"])
|
| 266 |
+
tm.assert_categorical_equal(sp1, xp1)
|
| 267 |
+
tm.assert_categorical_equal(cat[:-1], sp1[1:])
|
| 268 |
+
|
| 269 |
+
# shift back
|
| 270 |
+
sn2 = cat.shift(-2)
|
| 271 |
+
xp2 = Categorical(
|
| 272 |
+
["c", "d", "a", np.nan, np.nan], categories=["a", "b", "c", "d"]
|
| 273 |
+
)
|
| 274 |
+
tm.assert_categorical_equal(sn2, xp2)
|
| 275 |
+
tm.assert_categorical_equal(cat[2:], sn2[:-2])
|
| 276 |
+
|
| 277 |
+
# shift by zero
|
| 278 |
+
tm.assert_categorical_equal(cat, cat.shift(0))
|
| 279 |
+
|
| 280 |
+
def test_nbytes(self):
|
| 281 |
+
cat = Categorical([1, 2, 3])
|
| 282 |
+
exp = 3 + 3 * 8 # 3 int8s for values + 3 int64s for categories
|
| 283 |
+
assert cat.nbytes == exp
|
| 284 |
+
|
| 285 |
+
def test_memory_usage(self):
|
| 286 |
+
cat = Categorical([1, 2, 3])
|
| 287 |
+
|
| 288 |
+
# .categories is an index, so we include the hashtable
|
| 289 |
+
assert 0 < cat.nbytes <= cat.memory_usage()
|
| 290 |
+
assert 0 < cat.nbytes <= cat.memory_usage(deep=True)
|
| 291 |
+
|
| 292 |
+
cat = Categorical(["foo", "foo", "bar"])
|
| 293 |
+
assert cat.memory_usage(deep=True) > cat.nbytes
|
| 294 |
+
|
| 295 |
+
if not PYPY:
|
| 296 |
+
# sys.getsizeof will call the .memory_usage with
|
| 297 |
+
# deep=True, and add on some GC overhead
|
| 298 |
+
diff = cat.memory_usage(deep=True) - sys.getsizeof(cat)
|
| 299 |
+
assert abs(diff) < 100
|
| 300 |
+
|
| 301 |
+
def test_map(self):
|
| 302 |
+
c = Categorical(list("ABABC"), categories=list("CBA"), ordered=True)
|
| 303 |
+
result = c.map(lambda x: x.lower())
|
| 304 |
+
exp = Categorical(list("ababc"), categories=list("cba"), ordered=True)
|
| 305 |
+
tm.assert_categorical_equal(result, exp)
|
| 306 |
+
|
| 307 |
+
c = Categorical(list("ABABC"), categories=list("ABC"), ordered=False)
|
| 308 |
+
result = c.map(lambda x: x.lower())
|
| 309 |
+
exp = Categorical(list("ababc"), categories=list("abc"), ordered=False)
|
| 310 |
+
tm.assert_categorical_equal(result, exp)
|
| 311 |
+
|
| 312 |
+
result = c.map(lambda x: 1)
|
| 313 |
+
# GH 12766: Return an index not an array
|
| 314 |
+
tm.assert_index_equal(result, Index(np.array([1] * 5, dtype=np.int64)))
|
| 315 |
+
|
| 316 |
+
@pytest.mark.parametrize("value", [1, "True", [1, 2, 3], 5.0])
|
| 317 |
+
def test_validate_inplace_raises(self, value):
|
| 318 |
+
cat = Categorical(["A", "B", "B", "C", "A"])
|
| 319 |
+
msg = (
|
| 320 |
+
'For argument "inplace" expected type bool, '
|
| 321 |
+
f"received type {type(value).__name__}"
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
with pytest.raises(ValueError, match=msg):
|
| 325 |
+
cat.sort_values(inplace=value)
|
| 326 |
+
|
| 327 |
+
def test_quantile_empty(self):
|
| 328 |
+
# make sure we have correct itemsize on resulting codes
|
| 329 |
+
cat = Categorical(["A", "B"])
|
| 330 |
+
idx = Index([0.0, 0.5])
|
| 331 |
+
result = cat[:0]._quantile(idx, interpolation="linear")
|
| 332 |
+
assert result._codes.dtype == np.int8
|
| 333 |
+
|
| 334 |
+
expected = cat.take([-1, -1], allow_fill=True)
|
| 335 |
+
tm.assert_extension_array_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_api.py
ADDED
|
@@ -0,0 +1,493 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas.compat import PY311
|
| 7 |
+
|
| 8 |
+
from pandas import (
|
| 9 |
+
Categorical,
|
| 10 |
+
CategoricalIndex,
|
| 11 |
+
DataFrame,
|
| 12 |
+
Index,
|
| 13 |
+
Series,
|
| 14 |
+
StringDtype,
|
| 15 |
+
)
|
| 16 |
+
import pandas._testing as tm
|
| 17 |
+
from pandas.core.arrays.categorical import recode_for_categories
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class TestCategoricalAPI:
|
| 21 |
+
def test_ordered_api(self):
|
| 22 |
+
# GH 9347
|
| 23 |
+
cat1 = Categorical(list("acb"), ordered=False)
|
| 24 |
+
tm.assert_index_equal(cat1.categories, Index(["a", "b", "c"]))
|
| 25 |
+
assert not cat1.ordered
|
| 26 |
+
|
| 27 |
+
cat2 = Categorical(list("acb"), categories=list("bca"), ordered=False)
|
| 28 |
+
tm.assert_index_equal(cat2.categories, Index(["b", "c", "a"]))
|
| 29 |
+
assert not cat2.ordered
|
| 30 |
+
|
| 31 |
+
cat3 = Categorical(list("acb"), ordered=True)
|
| 32 |
+
tm.assert_index_equal(cat3.categories, Index(["a", "b", "c"]))
|
| 33 |
+
assert cat3.ordered
|
| 34 |
+
|
| 35 |
+
cat4 = Categorical(list("acb"), categories=list("bca"), ordered=True)
|
| 36 |
+
tm.assert_index_equal(cat4.categories, Index(["b", "c", "a"]))
|
| 37 |
+
assert cat4.ordered
|
| 38 |
+
|
| 39 |
+
def test_set_ordered(self):
|
| 40 |
+
cat = Categorical(["a", "b", "c", "a"], ordered=True)
|
| 41 |
+
cat2 = cat.as_unordered()
|
| 42 |
+
assert not cat2.ordered
|
| 43 |
+
cat2 = cat.as_ordered()
|
| 44 |
+
assert cat2.ordered
|
| 45 |
+
|
| 46 |
+
assert cat2.set_ordered(True).ordered
|
| 47 |
+
assert not cat2.set_ordered(False).ordered
|
| 48 |
+
|
| 49 |
+
# removed in 0.19.0
|
| 50 |
+
msg = (
|
| 51 |
+
"property 'ordered' of 'Categorical' object has no setter"
|
| 52 |
+
if PY311
|
| 53 |
+
else "can't set attribute"
|
| 54 |
+
)
|
| 55 |
+
with pytest.raises(AttributeError, match=msg):
|
| 56 |
+
cat.ordered = True
|
| 57 |
+
with pytest.raises(AttributeError, match=msg):
|
| 58 |
+
cat.ordered = False
|
| 59 |
+
|
| 60 |
+
def test_rename_categories(self):
|
| 61 |
+
cat = Categorical(["a", "b", "c", "a"])
|
| 62 |
+
|
| 63 |
+
# inplace=False: the old one must not be changed
|
| 64 |
+
res = cat.rename_categories([1, 2, 3])
|
| 65 |
+
tm.assert_numpy_array_equal(
|
| 66 |
+
res.__array__(), np.array([1, 2, 3, 1], dtype=np.int64)
|
| 67 |
+
)
|
| 68 |
+
tm.assert_index_equal(res.categories, Index([1, 2, 3]))
|
| 69 |
+
|
| 70 |
+
exp_cat = np.array(["a", "b", "c", "a"], dtype=np.object_)
|
| 71 |
+
tm.assert_numpy_array_equal(cat.__array__(), exp_cat)
|
| 72 |
+
|
| 73 |
+
exp_cat = Index(["a", "b", "c"])
|
| 74 |
+
tm.assert_index_equal(cat.categories, exp_cat)
|
| 75 |
+
|
| 76 |
+
# GH18862 (let rename_categories take callables)
|
| 77 |
+
result = cat.rename_categories(lambda x: x.upper())
|
| 78 |
+
expected = Categorical(["A", "B", "C", "A"])
|
| 79 |
+
tm.assert_categorical_equal(result, expected)
|
| 80 |
+
|
| 81 |
+
@pytest.mark.parametrize("new_categories", [[1, 2, 3, 4], [1, 2]])
|
| 82 |
+
def test_rename_categories_wrong_length_raises(self, new_categories):
|
| 83 |
+
cat = Categorical(["a", "b", "c", "a"])
|
| 84 |
+
msg = (
|
| 85 |
+
"new categories need to have the same number of items as the "
|
| 86 |
+
"old categories!"
|
| 87 |
+
)
|
| 88 |
+
with pytest.raises(ValueError, match=msg):
|
| 89 |
+
cat.rename_categories(new_categories)
|
| 90 |
+
|
| 91 |
+
def test_rename_categories_series(self):
|
| 92 |
+
# https://github.com/pandas-dev/pandas/issues/17981
|
| 93 |
+
c = Categorical(["a", "b"])
|
| 94 |
+
result = c.rename_categories(Series([0, 1], index=["a", "b"]))
|
| 95 |
+
expected = Categorical([0, 1])
|
| 96 |
+
tm.assert_categorical_equal(result, expected)
|
| 97 |
+
|
| 98 |
+
def test_rename_categories_dict(self):
|
| 99 |
+
# GH 17336
|
| 100 |
+
cat = Categorical(["a", "b", "c", "d"])
|
| 101 |
+
res = cat.rename_categories({"a": 4, "b": 3, "c": 2, "d": 1})
|
| 102 |
+
expected = Index([4, 3, 2, 1])
|
| 103 |
+
tm.assert_index_equal(res.categories, expected)
|
| 104 |
+
|
| 105 |
+
# Test for dicts of smaller length
|
| 106 |
+
cat = Categorical(["a", "b", "c", "d"])
|
| 107 |
+
res = cat.rename_categories({"a": 1, "c": 3})
|
| 108 |
+
|
| 109 |
+
expected = Index([1, "b", 3, "d"])
|
| 110 |
+
tm.assert_index_equal(res.categories, expected)
|
| 111 |
+
|
| 112 |
+
# Test for dicts with bigger length
|
| 113 |
+
cat = Categorical(["a", "b", "c", "d"])
|
| 114 |
+
res = cat.rename_categories({"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6})
|
| 115 |
+
expected = Index([1, 2, 3, 4])
|
| 116 |
+
tm.assert_index_equal(res.categories, expected)
|
| 117 |
+
|
| 118 |
+
# Test for dicts with no items from old categories
|
| 119 |
+
cat = Categorical(["a", "b", "c", "d"])
|
| 120 |
+
res = cat.rename_categories({"f": 1, "g": 3})
|
| 121 |
+
|
| 122 |
+
expected = Index(["a", "b", "c", "d"])
|
| 123 |
+
tm.assert_index_equal(res.categories, expected)
|
| 124 |
+
|
| 125 |
+
def test_reorder_categories(self):
|
| 126 |
+
cat = Categorical(["a", "b", "c", "a"], ordered=True)
|
| 127 |
+
old = cat.copy()
|
| 128 |
+
new = Categorical(
|
| 129 |
+
["a", "b", "c", "a"], categories=["c", "b", "a"], ordered=True
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
res = cat.reorder_categories(["c", "b", "a"])
|
| 133 |
+
# cat must be the same as before
|
| 134 |
+
tm.assert_categorical_equal(cat, old)
|
| 135 |
+
# only res is changed
|
| 136 |
+
tm.assert_categorical_equal(res, new)
|
| 137 |
+
|
| 138 |
+
@pytest.mark.parametrize(
|
| 139 |
+
"new_categories",
|
| 140 |
+
[
|
| 141 |
+
["a"], # not all "old" included in "new"
|
| 142 |
+
["a", "b", "d"], # still not all "old" in "new"
|
| 143 |
+
["a", "b", "c", "d"], # all "old" included in "new", but too long
|
| 144 |
+
],
|
| 145 |
+
)
|
| 146 |
+
def test_reorder_categories_raises(self, new_categories):
|
| 147 |
+
cat = Categorical(["a", "b", "c", "a"], ordered=True)
|
| 148 |
+
msg = "items in new_categories are not the same as in old categories"
|
| 149 |
+
with pytest.raises(ValueError, match=msg):
|
| 150 |
+
cat.reorder_categories(new_categories)
|
| 151 |
+
|
| 152 |
+
def test_add_categories(self):
|
| 153 |
+
cat = Categorical(["a", "b", "c", "a"], ordered=True)
|
| 154 |
+
old = cat.copy()
|
| 155 |
+
new = Categorical(
|
| 156 |
+
["a", "b", "c", "a"], categories=["a", "b", "c", "d"], ordered=True
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
res = cat.add_categories("d")
|
| 160 |
+
tm.assert_categorical_equal(cat, old)
|
| 161 |
+
tm.assert_categorical_equal(res, new)
|
| 162 |
+
|
| 163 |
+
res = cat.add_categories(["d"])
|
| 164 |
+
tm.assert_categorical_equal(cat, old)
|
| 165 |
+
tm.assert_categorical_equal(res, new)
|
| 166 |
+
|
| 167 |
+
# GH 9927
|
| 168 |
+
cat = Categorical(list("abc"), ordered=True)
|
| 169 |
+
expected = Categorical(list("abc"), categories=list("abcde"), ordered=True)
|
| 170 |
+
# test with Series, np.array, index, list
|
| 171 |
+
res = cat.add_categories(Series(["d", "e"]))
|
| 172 |
+
tm.assert_categorical_equal(res, expected)
|
| 173 |
+
res = cat.add_categories(np.array(["d", "e"]))
|
| 174 |
+
tm.assert_categorical_equal(res, expected)
|
| 175 |
+
res = cat.add_categories(Index(["d", "e"]))
|
| 176 |
+
tm.assert_categorical_equal(res, expected)
|
| 177 |
+
res = cat.add_categories(["d", "e"])
|
| 178 |
+
tm.assert_categorical_equal(res, expected)
|
| 179 |
+
|
| 180 |
+
def test_add_categories_existing_raises(self):
|
| 181 |
+
# new is in old categories
|
| 182 |
+
cat = Categorical(["a", "b", "c", "d"], ordered=True)
|
| 183 |
+
msg = re.escape("new categories must not include old categories: {'d'}")
|
| 184 |
+
with pytest.raises(ValueError, match=msg):
|
| 185 |
+
cat.add_categories(["d"])
|
| 186 |
+
|
| 187 |
+
def test_add_categories_losing_dtype_information(self):
|
| 188 |
+
# GH#48812
|
| 189 |
+
cat = Categorical(Series([1, 2], dtype="Int64"))
|
| 190 |
+
ser = Series([4], dtype="Int64")
|
| 191 |
+
result = cat.add_categories(ser)
|
| 192 |
+
expected = Categorical(
|
| 193 |
+
Series([1, 2], dtype="Int64"), categories=Series([1, 2, 4], dtype="Int64")
|
| 194 |
+
)
|
| 195 |
+
tm.assert_categorical_equal(result, expected)
|
| 196 |
+
|
| 197 |
+
cat = Categorical(Series(["a", "b", "a"], dtype=StringDtype()))
|
| 198 |
+
ser = Series(["d"], dtype=StringDtype())
|
| 199 |
+
result = cat.add_categories(ser)
|
| 200 |
+
expected = Categorical(
|
| 201 |
+
Series(["a", "b", "a"], dtype=StringDtype()),
|
| 202 |
+
categories=Series(["a", "b", "d"], dtype=StringDtype()),
|
| 203 |
+
)
|
| 204 |
+
tm.assert_categorical_equal(result, expected)
|
| 205 |
+
|
| 206 |
+
def test_set_categories(self):
|
| 207 |
+
cat = Categorical(["a", "b", "c", "a"], ordered=True)
|
| 208 |
+
exp_categories = Index(["c", "b", "a"])
|
| 209 |
+
exp_values = np.array(["a", "b", "c", "a"], dtype=np.object_)
|
| 210 |
+
|
| 211 |
+
cat = cat.set_categories(["c", "b", "a"])
|
| 212 |
+
res = cat.set_categories(["a", "b", "c"])
|
| 213 |
+
# cat must be the same as before
|
| 214 |
+
tm.assert_index_equal(cat.categories, exp_categories)
|
| 215 |
+
tm.assert_numpy_array_equal(cat.__array__(), exp_values)
|
| 216 |
+
# only res is changed
|
| 217 |
+
exp_categories_back = Index(["a", "b", "c"])
|
| 218 |
+
tm.assert_index_equal(res.categories, exp_categories_back)
|
| 219 |
+
tm.assert_numpy_array_equal(res.__array__(), exp_values)
|
| 220 |
+
|
| 221 |
+
# not all "old" included in "new" -> all not included ones are now
|
| 222 |
+
# np.nan
|
| 223 |
+
cat = Categorical(["a", "b", "c", "a"], ordered=True)
|
| 224 |
+
res = cat.set_categories(["a"])
|
| 225 |
+
tm.assert_numpy_array_equal(res.codes, np.array([0, -1, -1, 0], dtype=np.int8))
|
| 226 |
+
|
| 227 |
+
# still not all "old" in "new"
|
| 228 |
+
res = cat.set_categories(["a", "b", "d"])
|
| 229 |
+
tm.assert_numpy_array_equal(res.codes, np.array([0, 1, -1, 0], dtype=np.int8))
|
| 230 |
+
tm.assert_index_equal(res.categories, Index(["a", "b", "d"]))
|
| 231 |
+
|
| 232 |
+
# all "old" included in "new"
|
| 233 |
+
cat = cat.set_categories(["a", "b", "c", "d"])
|
| 234 |
+
exp_categories = Index(["a", "b", "c", "d"])
|
| 235 |
+
tm.assert_index_equal(cat.categories, exp_categories)
|
| 236 |
+
|
| 237 |
+
# internals...
|
| 238 |
+
c = Categorical([1, 2, 3, 4, 1], categories=[1, 2, 3, 4], ordered=True)
|
| 239 |
+
tm.assert_numpy_array_equal(c._codes, np.array([0, 1, 2, 3, 0], dtype=np.int8))
|
| 240 |
+
tm.assert_index_equal(c.categories, Index([1, 2, 3, 4]))
|
| 241 |
+
|
| 242 |
+
exp = np.array([1, 2, 3, 4, 1], dtype=np.int64)
|
| 243 |
+
tm.assert_numpy_array_equal(np.asarray(c), exp)
|
| 244 |
+
|
| 245 |
+
# all "pointers" to '4' must be changed from 3 to 0,...
|
| 246 |
+
c = c.set_categories([4, 3, 2, 1])
|
| 247 |
+
|
| 248 |
+
# positions are changed
|
| 249 |
+
tm.assert_numpy_array_equal(c._codes, np.array([3, 2, 1, 0, 3], dtype=np.int8))
|
| 250 |
+
|
| 251 |
+
# categories are now in new order
|
| 252 |
+
tm.assert_index_equal(c.categories, Index([4, 3, 2, 1]))
|
| 253 |
+
|
| 254 |
+
# output is the same
|
| 255 |
+
exp = np.array([1, 2, 3, 4, 1], dtype=np.int64)
|
| 256 |
+
tm.assert_numpy_array_equal(np.asarray(c), exp)
|
| 257 |
+
assert c.min() == 4
|
| 258 |
+
assert c.max() == 1
|
| 259 |
+
|
| 260 |
+
# set_categories should set the ordering if specified
|
| 261 |
+
c2 = c.set_categories([4, 3, 2, 1], ordered=False)
|
| 262 |
+
assert not c2.ordered
|
| 263 |
+
|
| 264 |
+
tm.assert_numpy_array_equal(np.asarray(c), np.asarray(c2))
|
| 265 |
+
|
| 266 |
+
# set_categories should pass thru the ordering
|
| 267 |
+
c2 = c.set_ordered(False).set_categories([4, 3, 2, 1])
|
| 268 |
+
assert not c2.ordered
|
| 269 |
+
|
| 270 |
+
tm.assert_numpy_array_equal(np.asarray(c), np.asarray(c2))
|
| 271 |
+
|
| 272 |
+
@pytest.mark.parametrize(
|
| 273 |
+
"values, categories, new_categories",
|
| 274 |
+
[
|
| 275 |
+
# No NaNs, same cats, same order
|
| 276 |
+
(["a", "b", "a"], ["a", "b"], ["a", "b"]),
|
| 277 |
+
# No NaNs, same cats, different order
|
| 278 |
+
(["a", "b", "a"], ["a", "b"], ["b", "a"]),
|
| 279 |
+
# Same, unsorted
|
| 280 |
+
(["b", "a", "a"], ["a", "b"], ["a", "b"]),
|
| 281 |
+
# No NaNs, same cats, different order
|
| 282 |
+
(["b", "a", "a"], ["a", "b"], ["b", "a"]),
|
| 283 |
+
# NaNs
|
| 284 |
+
(["a", "b", "c"], ["a", "b"], ["a", "b"]),
|
| 285 |
+
(["a", "b", "c"], ["a", "b"], ["b", "a"]),
|
| 286 |
+
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
|
| 287 |
+
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
|
| 288 |
+
# Introduce NaNs
|
| 289 |
+
(["a", "b", "c"], ["a", "b"], ["a"]),
|
| 290 |
+
(["a", "b", "c"], ["a", "b"], ["b"]),
|
| 291 |
+
(["b", "a", "c"], ["a", "b"], ["a"]),
|
| 292 |
+
(["b", "a", "c"], ["a", "b"], ["a"]),
|
| 293 |
+
# No overlap
|
| 294 |
+
(["a", "b", "c"], ["a", "b"], ["d", "e"]),
|
| 295 |
+
],
|
| 296 |
+
)
|
| 297 |
+
@pytest.mark.parametrize("ordered", [True, False])
|
| 298 |
+
def test_set_categories_many(self, values, categories, new_categories, ordered):
|
| 299 |
+
c = Categorical(values, categories)
|
| 300 |
+
expected = Categorical(values, new_categories, ordered)
|
| 301 |
+
result = c.set_categories(new_categories, ordered=ordered)
|
| 302 |
+
tm.assert_categorical_equal(result, expected)
|
| 303 |
+
|
| 304 |
+
def test_set_categories_rename_less(self):
|
| 305 |
+
# GH 24675
|
| 306 |
+
cat = Categorical(["A", "B"])
|
| 307 |
+
result = cat.set_categories(["A"], rename=True)
|
| 308 |
+
expected = Categorical(["A", np.nan])
|
| 309 |
+
tm.assert_categorical_equal(result, expected)
|
| 310 |
+
|
| 311 |
+
def test_set_categories_private(self):
|
| 312 |
+
cat = Categorical(["a", "b", "c"], categories=["a", "b", "c", "d"])
|
| 313 |
+
cat._set_categories(["a", "c", "d", "e"])
|
| 314 |
+
expected = Categorical(["a", "c", "d"], categories=list("acde"))
|
| 315 |
+
tm.assert_categorical_equal(cat, expected)
|
| 316 |
+
|
| 317 |
+
# fastpath
|
| 318 |
+
cat = Categorical(["a", "b", "c"], categories=["a", "b", "c", "d"])
|
| 319 |
+
cat._set_categories(["a", "c", "d", "e"], fastpath=True)
|
| 320 |
+
expected = Categorical(["a", "c", "d"], categories=list("acde"))
|
| 321 |
+
tm.assert_categorical_equal(cat, expected)
|
| 322 |
+
|
| 323 |
+
def test_remove_categories(self):
|
| 324 |
+
cat = Categorical(["a", "b", "c", "a"], ordered=True)
|
| 325 |
+
old = cat.copy()
|
| 326 |
+
new = Categorical(["a", "b", np.nan, "a"], categories=["a", "b"], ordered=True)
|
| 327 |
+
|
| 328 |
+
res = cat.remove_categories("c")
|
| 329 |
+
tm.assert_categorical_equal(cat, old)
|
| 330 |
+
tm.assert_categorical_equal(res, new)
|
| 331 |
+
|
| 332 |
+
res = cat.remove_categories(["c"])
|
| 333 |
+
tm.assert_categorical_equal(cat, old)
|
| 334 |
+
tm.assert_categorical_equal(res, new)
|
| 335 |
+
|
| 336 |
+
@pytest.mark.parametrize("removals", [["c"], ["c", np.nan], "c", ["c", "c"]])
|
| 337 |
+
def test_remove_categories_raises(self, removals):
|
| 338 |
+
cat = Categorical(["a", "b", "a"])
|
| 339 |
+
message = re.escape("removals must all be in old categories: {'c'}")
|
| 340 |
+
|
| 341 |
+
with pytest.raises(ValueError, match=message):
|
| 342 |
+
cat.remove_categories(removals)
|
| 343 |
+
|
| 344 |
+
def test_remove_unused_categories(self):
|
| 345 |
+
c = Categorical(["a", "b", "c", "d", "a"], categories=["a", "b", "c", "d", "e"])
|
| 346 |
+
exp_categories_all = Index(["a", "b", "c", "d", "e"])
|
| 347 |
+
exp_categories_dropped = Index(["a", "b", "c", "d"])
|
| 348 |
+
|
| 349 |
+
tm.assert_index_equal(c.categories, exp_categories_all)
|
| 350 |
+
|
| 351 |
+
res = c.remove_unused_categories()
|
| 352 |
+
tm.assert_index_equal(res.categories, exp_categories_dropped)
|
| 353 |
+
tm.assert_index_equal(c.categories, exp_categories_all)
|
| 354 |
+
|
| 355 |
+
# with NaN values (GH11599)
|
| 356 |
+
c = Categorical(["a", "b", "c", np.nan], categories=["a", "b", "c", "d", "e"])
|
| 357 |
+
res = c.remove_unused_categories()
|
| 358 |
+
tm.assert_index_equal(res.categories, Index(np.array(["a", "b", "c"])))
|
| 359 |
+
exp_codes = np.array([0, 1, 2, -1], dtype=np.int8)
|
| 360 |
+
tm.assert_numpy_array_equal(res.codes, exp_codes)
|
| 361 |
+
tm.assert_index_equal(c.categories, exp_categories_all)
|
| 362 |
+
|
| 363 |
+
val = ["F", np.nan, "D", "B", "D", "F", np.nan]
|
| 364 |
+
cat = Categorical(values=val, categories=list("ABCDEFG"))
|
| 365 |
+
out = cat.remove_unused_categories()
|
| 366 |
+
tm.assert_index_equal(out.categories, Index(["B", "D", "F"]))
|
| 367 |
+
exp_codes = np.array([2, -1, 1, 0, 1, 2, -1], dtype=np.int8)
|
| 368 |
+
tm.assert_numpy_array_equal(out.codes, exp_codes)
|
| 369 |
+
assert out.tolist() == val
|
| 370 |
+
|
| 371 |
+
alpha = list("abcdefghijklmnopqrstuvwxyz")
|
| 372 |
+
val = np.random.choice(alpha[::2], 10000).astype("object")
|
| 373 |
+
val[np.random.choice(len(val), 100)] = np.nan
|
| 374 |
+
|
| 375 |
+
cat = Categorical(values=val, categories=alpha)
|
| 376 |
+
out = cat.remove_unused_categories()
|
| 377 |
+
assert out.tolist() == val.tolist()
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
class TestCategoricalAPIWithFactor:
|
| 381 |
+
def test_describe(self, factor):
|
| 382 |
+
# string type
|
| 383 |
+
desc = factor.describe()
|
| 384 |
+
assert factor.ordered
|
| 385 |
+
exp_index = CategoricalIndex(
|
| 386 |
+
["a", "b", "c"], name="categories", ordered=factor.ordered
|
| 387 |
+
)
|
| 388 |
+
expected = DataFrame(
|
| 389 |
+
{"counts": [3, 2, 3], "freqs": [3 / 8.0, 2 / 8.0, 3 / 8.0]}, index=exp_index
|
| 390 |
+
)
|
| 391 |
+
tm.assert_frame_equal(desc, expected)
|
| 392 |
+
|
| 393 |
+
# check unused categories
|
| 394 |
+
cat = factor.copy()
|
| 395 |
+
cat = cat.set_categories(["a", "b", "c", "d"])
|
| 396 |
+
desc = cat.describe()
|
| 397 |
+
|
| 398 |
+
exp_index = CategoricalIndex(
|
| 399 |
+
list("abcd"), ordered=factor.ordered, name="categories"
|
| 400 |
+
)
|
| 401 |
+
expected = DataFrame(
|
| 402 |
+
{"counts": [3, 2, 3, 0], "freqs": [3 / 8.0, 2 / 8.0, 3 / 8.0, 0]},
|
| 403 |
+
index=exp_index,
|
| 404 |
+
)
|
| 405 |
+
tm.assert_frame_equal(desc, expected)
|
| 406 |
+
|
| 407 |
+
# check an integer one
|
| 408 |
+
cat = Categorical([1, 2, 3, 1, 2, 3, 3, 2, 1, 1, 1])
|
| 409 |
+
desc = cat.describe()
|
| 410 |
+
exp_index = CategoricalIndex([1, 2, 3], ordered=cat.ordered, name="categories")
|
| 411 |
+
expected = DataFrame(
|
| 412 |
+
{"counts": [5, 3, 3], "freqs": [5 / 11.0, 3 / 11.0, 3 / 11.0]},
|
| 413 |
+
index=exp_index,
|
| 414 |
+
)
|
| 415 |
+
tm.assert_frame_equal(desc, expected)
|
| 416 |
+
|
| 417 |
+
# https://github.com/pandas-dev/pandas/issues/3678
|
| 418 |
+
# describe should work with NaN
|
| 419 |
+
cat = Categorical([np.nan, 1, 2, 2])
|
| 420 |
+
desc = cat.describe()
|
| 421 |
+
expected = DataFrame(
|
| 422 |
+
{"counts": [1, 2, 1], "freqs": [1 / 4.0, 2 / 4.0, 1 / 4.0]},
|
| 423 |
+
index=CategoricalIndex(
|
| 424 |
+
[1, 2, np.nan], categories=[1, 2], name="categories"
|
| 425 |
+
),
|
| 426 |
+
)
|
| 427 |
+
tm.assert_frame_equal(desc, expected)
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
class TestPrivateCategoricalAPI:
|
| 431 |
+
def test_codes_immutable(self):
|
| 432 |
+
# Codes should be read only
|
| 433 |
+
c = Categorical(["a", "b", "c", "a", np.nan])
|
| 434 |
+
exp = np.array([0, 1, 2, 0, -1], dtype="int8")
|
| 435 |
+
tm.assert_numpy_array_equal(c.codes, exp)
|
| 436 |
+
|
| 437 |
+
# Assignments to codes should raise
|
| 438 |
+
msg = (
|
| 439 |
+
"property 'codes' of 'Categorical' object has no setter"
|
| 440 |
+
if PY311
|
| 441 |
+
else "can't set attribute"
|
| 442 |
+
)
|
| 443 |
+
with pytest.raises(AttributeError, match=msg):
|
| 444 |
+
c.codes = np.array([0, 1, 2, 0, 1], dtype="int8")
|
| 445 |
+
|
| 446 |
+
# changes in the codes array should raise
|
| 447 |
+
codes = c.codes
|
| 448 |
+
|
| 449 |
+
with pytest.raises(ValueError, match="assignment destination is read-only"):
|
| 450 |
+
codes[4] = 1
|
| 451 |
+
|
| 452 |
+
# But even after getting the codes, the original array should still be
|
| 453 |
+
# writeable!
|
| 454 |
+
c[4] = "a"
|
| 455 |
+
exp = np.array([0, 1, 2, 0, 0], dtype="int8")
|
| 456 |
+
tm.assert_numpy_array_equal(c.codes, exp)
|
| 457 |
+
c._codes[4] = 2
|
| 458 |
+
exp = np.array([0, 1, 2, 0, 2], dtype="int8")
|
| 459 |
+
tm.assert_numpy_array_equal(c.codes, exp)
|
| 460 |
+
|
| 461 |
+
@pytest.mark.parametrize(
|
| 462 |
+
"codes, old, new, expected",
|
| 463 |
+
[
|
| 464 |
+
([0, 1], ["a", "b"], ["a", "b"], [0, 1]),
|
| 465 |
+
([0, 1], ["b", "a"], ["b", "a"], [0, 1]),
|
| 466 |
+
([0, 1], ["a", "b"], ["b", "a"], [1, 0]),
|
| 467 |
+
([0, 1], ["b", "a"], ["a", "b"], [1, 0]),
|
| 468 |
+
([0, 1, 0, 1], ["a", "b"], ["a", "b", "c"], [0, 1, 0, 1]),
|
| 469 |
+
([0, 1, 2, 2], ["a", "b", "c"], ["a", "b"], [0, 1, -1, -1]),
|
| 470 |
+
([0, 1, -1], ["a", "b", "c"], ["a", "b", "c"], [0, 1, -1]),
|
| 471 |
+
([0, 1, -1], ["a", "b", "c"], ["b"], [-1, 0, -1]),
|
| 472 |
+
([0, 1, -1], ["a", "b", "c"], ["d"], [-1, -1, -1]),
|
| 473 |
+
([0, 1, -1], ["a", "b", "c"], [], [-1, -1, -1]),
|
| 474 |
+
([-1, -1], [], ["a", "b"], [-1, -1]),
|
| 475 |
+
([1, 0], ["b", "a"], ["a", "b"], [0, 1]),
|
| 476 |
+
],
|
| 477 |
+
)
|
| 478 |
+
def test_recode_to_categories(self, codes, old, new, expected):
|
| 479 |
+
codes = np.asanyarray(codes, dtype=np.int8)
|
| 480 |
+
expected = np.asanyarray(expected, dtype=np.int8)
|
| 481 |
+
old = Index(old)
|
| 482 |
+
new = Index(new)
|
| 483 |
+
result = recode_for_categories(codes, old, new)
|
| 484 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 485 |
+
|
| 486 |
+
def test_recode_to_categories_large(self):
|
| 487 |
+
N = 1000
|
| 488 |
+
codes = np.arange(N)
|
| 489 |
+
old = Index(codes)
|
| 490 |
+
expected = np.arange(N - 1, -1, -1, dtype=np.int16)
|
| 491 |
+
new = Index(expected)
|
| 492 |
+
result = recode_for_categories(codes, old, new)
|
| 493 |
+
tm.assert_numpy_array_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_astype.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Categorical,
|
| 6 |
+
CategoricalDtype,
|
| 7 |
+
NaT,
|
| 8 |
+
Timestamp,
|
| 9 |
+
array,
|
| 10 |
+
to_datetime,
|
| 11 |
+
)
|
| 12 |
+
import pandas._testing as tm
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TestAstype:
|
| 16 |
+
def test_astype_str_int_categories_to_nullable_int(self):
|
| 17 |
+
# GH#39616
|
| 18 |
+
dtype = CategoricalDtype([str(i) for i in range(5)])
|
| 19 |
+
codes = np.random.randint(5, size=20)
|
| 20 |
+
arr = Categorical.from_codes(codes, dtype=dtype)
|
| 21 |
+
|
| 22 |
+
res = arr.astype("Int64")
|
| 23 |
+
expected = array(codes, dtype="Int64")
|
| 24 |
+
tm.assert_extension_array_equal(res, expected)
|
| 25 |
+
|
| 26 |
+
def test_astype_str_int_categories_to_nullable_float(self):
|
| 27 |
+
# GH#39616
|
| 28 |
+
dtype = CategoricalDtype([str(i / 2) for i in range(5)])
|
| 29 |
+
codes = np.random.randint(5, size=20)
|
| 30 |
+
arr = Categorical.from_codes(codes, dtype=dtype)
|
| 31 |
+
|
| 32 |
+
res = arr.astype("Float64")
|
| 33 |
+
expected = array(codes, dtype="Float64") / 2
|
| 34 |
+
tm.assert_extension_array_equal(res, expected)
|
| 35 |
+
|
| 36 |
+
@pytest.mark.parametrize("ordered", [True, False])
|
| 37 |
+
def test_astype(self, ordered):
|
| 38 |
+
# string
|
| 39 |
+
cat = Categorical(list("abbaaccc"), ordered=ordered)
|
| 40 |
+
result = cat.astype(object)
|
| 41 |
+
expected = np.array(cat)
|
| 42 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 43 |
+
|
| 44 |
+
msg = r"Cannot cast object dtype to float64"
|
| 45 |
+
with pytest.raises(ValueError, match=msg):
|
| 46 |
+
cat.astype(float)
|
| 47 |
+
|
| 48 |
+
# numeric
|
| 49 |
+
cat = Categorical([0, 1, 2, 2, 1, 0, 1, 0, 2], ordered=ordered)
|
| 50 |
+
result = cat.astype(object)
|
| 51 |
+
expected = np.array(cat, dtype=object)
|
| 52 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 53 |
+
|
| 54 |
+
result = cat.astype(int)
|
| 55 |
+
expected = np.array(cat, dtype="int")
|
| 56 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 57 |
+
|
| 58 |
+
result = cat.astype(float)
|
| 59 |
+
expected = np.array(cat, dtype=float)
|
| 60 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 61 |
+
|
| 62 |
+
@pytest.mark.parametrize("dtype_ordered", [True, False])
|
| 63 |
+
@pytest.mark.parametrize("cat_ordered", [True, False])
|
| 64 |
+
def test_astype_category(self, dtype_ordered, cat_ordered):
|
| 65 |
+
# GH#10696/GH#18593
|
| 66 |
+
data = list("abcaacbab")
|
| 67 |
+
cat = Categorical(data, categories=list("bac"), ordered=cat_ordered)
|
| 68 |
+
|
| 69 |
+
# standard categories
|
| 70 |
+
dtype = CategoricalDtype(ordered=dtype_ordered)
|
| 71 |
+
result = cat.astype(dtype)
|
| 72 |
+
expected = Categorical(data, categories=cat.categories, ordered=dtype_ordered)
|
| 73 |
+
tm.assert_categorical_equal(result, expected)
|
| 74 |
+
|
| 75 |
+
# non-standard categories
|
| 76 |
+
dtype = CategoricalDtype(list("adc"), dtype_ordered)
|
| 77 |
+
result = cat.astype(dtype)
|
| 78 |
+
expected = Categorical(data, dtype=dtype)
|
| 79 |
+
tm.assert_categorical_equal(result, expected)
|
| 80 |
+
|
| 81 |
+
if dtype_ordered is False:
|
| 82 |
+
# dtype='category' can't specify ordered, so only test once
|
| 83 |
+
result = cat.astype("category")
|
| 84 |
+
expected = cat
|
| 85 |
+
tm.assert_categorical_equal(result, expected)
|
| 86 |
+
|
| 87 |
+
def test_astype_object_datetime_categories(self):
|
| 88 |
+
# GH#40754
|
| 89 |
+
cat = Categorical(to_datetime(["2021-03-27", NaT]))
|
| 90 |
+
result = cat.astype(object)
|
| 91 |
+
expected = np.array([Timestamp("2021-03-27 00:00:00"), NaT], dtype="object")
|
| 92 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 93 |
+
|
| 94 |
+
def test_astype_object_timestamp_categories(self):
|
| 95 |
+
# GH#18024
|
| 96 |
+
cat = Categorical([Timestamp("2014-01-01")])
|
| 97 |
+
result = cat.astype(object)
|
| 98 |
+
expected = np.array([Timestamp("2014-01-01 00:00:00")], dtype="object")
|
| 99 |
+
tm.assert_numpy_array_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_constructors.py
ADDED
|
@@ -0,0 +1,759 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import (
|
| 2 |
+
date,
|
| 3 |
+
datetime,
|
| 4 |
+
)
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
from pandas.core.dtypes.common import (
|
| 10 |
+
is_float_dtype,
|
| 11 |
+
is_integer_dtype,
|
| 12 |
+
)
|
| 13 |
+
from pandas.core.dtypes.dtypes import CategoricalDtype
|
| 14 |
+
|
| 15 |
+
import pandas as pd
|
| 16 |
+
from pandas import (
|
| 17 |
+
Categorical,
|
| 18 |
+
CategoricalIndex,
|
| 19 |
+
DatetimeIndex,
|
| 20 |
+
Index,
|
| 21 |
+
Interval,
|
| 22 |
+
IntervalIndex,
|
| 23 |
+
MultiIndex,
|
| 24 |
+
NaT,
|
| 25 |
+
Series,
|
| 26 |
+
Timestamp,
|
| 27 |
+
date_range,
|
| 28 |
+
period_range,
|
| 29 |
+
timedelta_range,
|
| 30 |
+
)
|
| 31 |
+
import pandas._testing as tm
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class TestCategoricalConstructors:
|
| 35 |
+
def test_categorical_from_cat_and_dtype_str_preserve_ordered(self):
|
| 36 |
+
# GH#49309 we should preserve orderedness in `res`
|
| 37 |
+
cat = Categorical([3, 1], categories=[3, 2, 1], ordered=True)
|
| 38 |
+
|
| 39 |
+
res = Categorical(cat, dtype="category")
|
| 40 |
+
assert res.dtype.ordered
|
| 41 |
+
|
| 42 |
+
def test_categorical_disallows_scalar(self):
|
| 43 |
+
# GH#38433
|
| 44 |
+
with pytest.raises(TypeError, match="Categorical input must be list-like"):
|
| 45 |
+
Categorical("A", categories=["A", "B"])
|
| 46 |
+
|
| 47 |
+
def test_categorical_1d_only(self):
|
| 48 |
+
# ndim > 1
|
| 49 |
+
msg = "> 1 ndim Categorical are not supported at this time"
|
| 50 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 51 |
+
Categorical(np.array([list("abcd")]))
|
| 52 |
+
|
| 53 |
+
def test_validate_ordered(self):
|
| 54 |
+
# see gh-14058
|
| 55 |
+
exp_msg = "'ordered' must either be 'True' or 'False'"
|
| 56 |
+
exp_err = TypeError
|
| 57 |
+
|
| 58 |
+
# This should be a boolean.
|
| 59 |
+
ordered = np.array([0, 1, 2])
|
| 60 |
+
|
| 61 |
+
with pytest.raises(exp_err, match=exp_msg):
|
| 62 |
+
Categorical([1, 2, 3], ordered=ordered)
|
| 63 |
+
|
| 64 |
+
with pytest.raises(exp_err, match=exp_msg):
|
| 65 |
+
Categorical.from_codes(
|
| 66 |
+
[0, 0, 1], categories=["a", "b", "c"], ordered=ordered
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
def test_constructor_empty(self):
|
| 70 |
+
# GH 17248
|
| 71 |
+
c = Categorical([])
|
| 72 |
+
expected = Index([])
|
| 73 |
+
tm.assert_index_equal(c.categories, expected)
|
| 74 |
+
|
| 75 |
+
c = Categorical([], categories=[1, 2, 3])
|
| 76 |
+
expected = Index([1, 2, 3], dtype=np.int64)
|
| 77 |
+
tm.assert_index_equal(c.categories, expected)
|
| 78 |
+
|
| 79 |
+
def test_constructor_empty_boolean(self):
|
| 80 |
+
# see gh-22702
|
| 81 |
+
cat = Categorical([], categories=[True, False])
|
| 82 |
+
categories = sorted(cat.categories.tolist())
|
| 83 |
+
assert categories == [False, True]
|
| 84 |
+
|
| 85 |
+
def test_constructor_tuples(self):
|
| 86 |
+
values = np.array([(1,), (1, 2), (1,), (1, 2)], dtype=object)
|
| 87 |
+
result = Categorical(values)
|
| 88 |
+
expected = Index([(1,), (1, 2)], tupleize_cols=False)
|
| 89 |
+
tm.assert_index_equal(result.categories, expected)
|
| 90 |
+
assert result.ordered is False
|
| 91 |
+
|
| 92 |
+
def test_constructor_tuples_datetimes(self):
|
| 93 |
+
# numpy will auto reshape when all of the tuples are the
|
| 94 |
+
# same len, so add an extra one with 2 items and slice it off
|
| 95 |
+
values = np.array(
|
| 96 |
+
[
|
| 97 |
+
(Timestamp("2010-01-01"),),
|
| 98 |
+
(Timestamp("2010-01-02"),),
|
| 99 |
+
(Timestamp("2010-01-01"),),
|
| 100 |
+
(Timestamp("2010-01-02"),),
|
| 101 |
+
("a", "b"),
|
| 102 |
+
],
|
| 103 |
+
dtype=object,
|
| 104 |
+
)[:-1]
|
| 105 |
+
result = Categorical(values)
|
| 106 |
+
expected = Index(
|
| 107 |
+
[(Timestamp("2010-01-01"),), (Timestamp("2010-01-02"),)],
|
| 108 |
+
tupleize_cols=False,
|
| 109 |
+
)
|
| 110 |
+
tm.assert_index_equal(result.categories, expected)
|
| 111 |
+
|
| 112 |
+
def test_constructor_unsortable(self):
|
| 113 |
+
# it works!
|
| 114 |
+
arr = np.array([1, 2, 3, datetime.now()], dtype="O")
|
| 115 |
+
factor = Categorical(arr, ordered=False)
|
| 116 |
+
assert not factor.ordered
|
| 117 |
+
|
| 118 |
+
# this however will raise as cannot be sorted
|
| 119 |
+
msg = (
|
| 120 |
+
"'values' is not ordered, please explicitly specify the "
|
| 121 |
+
"categories order by passing in a categories argument."
|
| 122 |
+
)
|
| 123 |
+
with pytest.raises(TypeError, match=msg):
|
| 124 |
+
Categorical(arr, ordered=True)
|
| 125 |
+
|
| 126 |
+
def test_constructor_interval(self):
|
| 127 |
+
result = Categorical(
|
| 128 |
+
[Interval(1, 2), Interval(2, 3), Interval(3, 6)], ordered=True
|
| 129 |
+
)
|
| 130 |
+
ii = IntervalIndex([Interval(1, 2), Interval(2, 3), Interval(3, 6)])
|
| 131 |
+
exp = Categorical(ii, ordered=True)
|
| 132 |
+
tm.assert_categorical_equal(result, exp)
|
| 133 |
+
tm.assert_index_equal(result.categories, ii)
|
| 134 |
+
|
| 135 |
+
def test_constructor(self):
|
| 136 |
+
exp_arr = np.array(["a", "b", "c", "a", "b", "c"], dtype=np.object_)
|
| 137 |
+
c1 = Categorical(exp_arr)
|
| 138 |
+
tm.assert_numpy_array_equal(c1.__array__(), exp_arr)
|
| 139 |
+
c2 = Categorical(exp_arr, categories=["a", "b", "c"])
|
| 140 |
+
tm.assert_numpy_array_equal(c2.__array__(), exp_arr)
|
| 141 |
+
c2 = Categorical(exp_arr, categories=["c", "b", "a"])
|
| 142 |
+
tm.assert_numpy_array_equal(c2.__array__(), exp_arr)
|
| 143 |
+
|
| 144 |
+
# categories must be unique
|
| 145 |
+
msg = "Categorical categories must be unique"
|
| 146 |
+
with pytest.raises(ValueError, match=msg):
|
| 147 |
+
Categorical([1, 2], [1, 2, 2])
|
| 148 |
+
|
| 149 |
+
with pytest.raises(ValueError, match=msg):
|
| 150 |
+
Categorical(["a", "b"], ["a", "b", "b"])
|
| 151 |
+
|
| 152 |
+
# The default should be unordered
|
| 153 |
+
c1 = Categorical(["a", "b", "c", "a"])
|
| 154 |
+
assert not c1.ordered
|
| 155 |
+
|
| 156 |
+
# Categorical as input
|
| 157 |
+
c1 = Categorical(["a", "b", "c", "a"])
|
| 158 |
+
c2 = Categorical(c1)
|
| 159 |
+
tm.assert_categorical_equal(c1, c2)
|
| 160 |
+
|
| 161 |
+
c1 = Categorical(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
| 162 |
+
c2 = Categorical(c1)
|
| 163 |
+
tm.assert_categorical_equal(c1, c2)
|
| 164 |
+
|
| 165 |
+
c1 = Categorical(["a", "b", "c", "a"], categories=["a", "c", "b"])
|
| 166 |
+
c2 = Categorical(c1)
|
| 167 |
+
tm.assert_categorical_equal(c1, c2)
|
| 168 |
+
|
| 169 |
+
c1 = Categorical(["a", "b", "c", "a"], categories=["a", "c", "b"])
|
| 170 |
+
c2 = Categorical(c1, categories=["a", "b", "c"])
|
| 171 |
+
tm.assert_numpy_array_equal(c1.__array__(), c2.__array__())
|
| 172 |
+
tm.assert_index_equal(c2.categories, Index(["a", "b", "c"]))
|
| 173 |
+
|
| 174 |
+
# Series of dtype category
|
| 175 |
+
c1 = Categorical(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
| 176 |
+
c2 = Categorical(Series(c1))
|
| 177 |
+
tm.assert_categorical_equal(c1, c2)
|
| 178 |
+
|
| 179 |
+
c1 = Categorical(["a", "b", "c", "a"], categories=["a", "c", "b"])
|
| 180 |
+
c2 = Categorical(Series(c1))
|
| 181 |
+
tm.assert_categorical_equal(c1, c2)
|
| 182 |
+
|
| 183 |
+
# Series
|
| 184 |
+
c1 = Categorical(["a", "b", "c", "a"])
|
| 185 |
+
c2 = Categorical(Series(["a", "b", "c", "a"]))
|
| 186 |
+
tm.assert_categorical_equal(c1, c2)
|
| 187 |
+
|
| 188 |
+
c1 = Categorical(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
| 189 |
+
c2 = Categorical(Series(["a", "b", "c", "a"]), categories=["a", "b", "c", "d"])
|
| 190 |
+
tm.assert_categorical_equal(c1, c2)
|
| 191 |
+
|
| 192 |
+
# This should result in integer categories, not float!
|
| 193 |
+
cat = Categorical([1, 2, 3, np.nan], categories=[1, 2, 3])
|
| 194 |
+
assert is_integer_dtype(cat.categories)
|
| 195 |
+
|
| 196 |
+
# https://github.com/pandas-dev/pandas/issues/3678
|
| 197 |
+
cat = Categorical([np.nan, 1, 2, 3])
|
| 198 |
+
assert is_integer_dtype(cat.categories)
|
| 199 |
+
|
| 200 |
+
# this should result in floats
|
| 201 |
+
cat = Categorical([np.nan, 1, 2.0, 3])
|
| 202 |
+
assert is_float_dtype(cat.categories)
|
| 203 |
+
|
| 204 |
+
cat = Categorical([np.nan, 1.0, 2.0, 3.0])
|
| 205 |
+
assert is_float_dtype(cat.categories)
|
| 206 |
+
|
| 207 |
+
# This doesn't work -> this would probably need some kind of "remember
|
| 208 |
+
# the original type" feature to try to cast the array interface result
|
| 209 |
+
# to...
|
| 210 |
+
|
| 211 |
+
# vals = np.asarray(cat[cat.notna()])
|
| 212 |
+
# assert is_integer_dtype(vals)
|
| 213 |
+
|
| 214 |
+
# corner cases
|
| 215 |
+
cat = Categorical([1])
|
| 216 |
+
assert len(cat.categories) == 1
|
| 217 |
+
assert cat.categories[0] == 1
|
| 218 |
+
assert len(cat.codes) == 1
|
| 219 |
+
assert cat.codes[0] == 0
|
| 220 |
+
|
| 221 |
+
cat = Categorical(["a"])
|
| 222 |
+
assert len(cat.categories) == 1
|
| 223 |
+
assert cat.categories[0] == "a"
|
| 224 |
+
assert len(cat.codes) == 1
|
| 225 |
+
assert cat.codes[0] == 0
|
| 226 |
+
|
| 227 |
+
# two arrays
|
| 228 |
+
# - when the first is an integer dtype and the second is not
|
| 229 |
+
# - when the resulting codes are all -1/NaN
|
| 230 |
+
with tm.assert_produces_warning(None):
|
| 231 |
+
Categorical([0, 1, 2, 0, 1, 2], categories=["a", "b", "c"])
|
| 232 |
+
|
| 233 |
+
with tm.assert_produces_warning(None):
|
| 234 |
+
Categorical([0, 1, 2, 0, 1, 2], categories=[3, 4, 5])
|
| 235 |
+
|
| 236 |
+
# the next one are from the old docs
|
| 237 |
+
with tm.assert_produces_warning(None):
|
| 238 |
+
Categorical([0, 1, 2, 0, 1, 2], [1, 2, 3])
|
| 239 |
+
cat = Categorical([1, 2], categories=[1, 2, 3])
|
| 240 |
+
|
| 241 |
+
# this is a legitimate constructor
|
| 242 |
+
with tm.assert_produces_warning(None):
|
| 243 |
+
Categorical(np.array([], dtype="int64"), categories=[3, 2, 1], ordered=True)
|
| 244 |
+
|
| 245 |
+
def test_constructor_with_existing_categories(self):
|
| 246 |
+
# GH25318: constructing with pd.Series used to bogusly skip recoding
|
| 247 |
+
# categories
|
| 248 |
+
c0 = Categorical(["a", "b", "c", "a"])
|
| 249 |
+
c1 = Categorical(["a", "b", "c", "a"], categories=["b", "c"])
|
| 250 |
+
|
| 251 |
+
c2 = Categorical(c0, categories=c1.categories)
|
| 252 |
+
tm.assert_categorical_equal(c1, c2)
|
| 253 |
+
|
| 254 |
+
c3 = Categorical(Series(c0), categories=c1.categories)
|
| 255 |
+
tm.assert_categorical_equal(c1, c3)
|
| 256 |
+
|
| 257 |
+
def test_constructor_not_sequence(self):
|
| 258 |
+
# https://github.com/pandas-dev/pandas/issues/16022
|
| 259 |
+
msg = r"^Parameter 'categories' must be list-like, was"
|
| 260 |
+
with pytest.raises(TypeError, match=msg):
|
| 261 |
+
Categorical(["a", "b"], categories="a")
|
| 262 |
+
|
| 263 |
+
def test_constructor_with_null(self):
|
| 264 |
+
# Cannot have NaN in categories
|
| 265 |
+
msg = "Categorical categories cannot be null"
|
| 266 |
+
with pytest.raises(ValueError, match=msg):
|
| 267 |
+
Categorical([np.nan, "a", "b", "c"], categories=[np.nan, "a", "b", "c"])
|
| 268 |
+
|
| 269 |
+
with pytest.raises(ValueError, match=msg):
|
| 270 |
+
Categorical([None, "a", "b", "c"], categories=[None, "a", "b", "c"])
|
| 271 |
+
|
| 272 |
+
with pytest.raises(ValueError, match=msg):
|
| 273 |
+
Categorical(
|
| 274 |
+
DatetimeIndex(["nat", "20160101"]),
|
| 275 |
+
categories=[NaT, Timestamp("20160101")],
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
def test_constructor_with_index(self):
|
| 279 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 280 |
+
tm.assert_categorical_equal(ci.values, Categorical(ci))
|
| 281 |
+
|
| 282 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 283 |
+
tm.assert_categorical_equal(
|
| 284 |
+
ci.values, Categorical(ci.astype(object), categories=ci.categories)
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
def test_constructor_with_generator(self):
|
| 288 |
+
# This was raising an Error in isna(single_val).any() because isna
|
| 289 |
+
# returned a scalar for a generator
|
| 290 |
+
|
| 291 |
+
exp = Categorical([0, 1, 2])
|
| 292 |
+
cat = Categorical(x for x in [0, 1, 2])
|
| 293 |
+
tm.assert_categorical_equal(cat, exp)
|
| 294 |
+
cat = Categorical(range(3))
|
| 295 |
+
tm.assert_categorical_equal(cat, exp)
|
| 296 |
+
|
| 297 |
+
MultiIndex.from_product([range(5), ["a", "b", "c"]])
|
| 298 |
+
|
| 299 |
+
# check that categories accept generators and sequences
|
| 300 |
+
cat = Categorical([0, 1, 2], categories=(x for x in [0, 1, 2]))
|
| 301 |
+
tm.assert_categorical_equal(cat, exp)
|
| 302 |
+
cat = Categorical([0, 1, 2], categories=range(3))
|
| 303 |
+
tm.assert_categorical_equal(cat, exp)
|
| 304 |
+
|
| 305 |
+
def test_constructor_with_rangeindex(self):
|
| 306 |
+
# RangeIndex is preserved in Categories
|
| 307 |
+
rng = Index(range(3))
|
| 308 |
+
|
| 309 |
+
cat = Categorical(rng)
|
| 310 |
+
tm.assert_index_equal(cat.categories, rng, exact=True)
|
| 311 |
+
|
| 312 |
+
cat = Categorical([1, 2, 0], categories=rng)
|
| 313 |
+
tm.assert_index_equal(cat.categories, rng, exact=True)
|
| 314 |
+
|
| 315 |
+
@pytest.mark.parametrize(
|
| 316 |
+
"dtl",
|
| 317 |
+
[
|
| 318 |
+
date_range("1995-01-01 00:00:00", periods=5, freq="s"),
|
| 319 |
+
date_range("1995-01-01 00:00:00", periods=5, freq="s", tz="US/Eastern"),
|
| 320 |
+
timedelta_range("1 day", periods=5, freq="s"),
|
| 321 |
+
],
|
| 322 |
+
)
|
| 323 |
+
def test_constructor_with_datetimelike(self, dtl):
|
| 324 |
+
# see gh-12077
|
| 325 |
+
# constructor with a datetimelike and NaT
|
| 326 |
+
|
| 327 |
+
s = Series(dtl)
|
| 328 |
+
c = Categorical(s)
|
| 329 |
+
|
| 330 |
+
expected = type(dtl)(s)
|
| 331 |
+
expected._data.freq = None
|
| 332 |
+
|
| 333 |
+
tm.assert_index_equal(c.categories, expected)
|
| 334 |
+
tm.assert_numpy_array_equal(c.codes, np.arange(5, dtype="int8"))
|
| 335 |
+
|
| 336 |
+
# with NaT
|
| 337 |
+
s2 = s.copy()
|
| 338 |
+
s2.iloc[-1] = NaT
|
| 339 |
+
c = Categorical(s2)
|
| 340 |
+
|
| 341 |
+
expected = type(dtl)(s2.dropna())
|
| 342 |
+
expected._data.freq = None
|
| 343 |
+
|
| 344 |
+
tm.assert_index_equal(c.categories, expected)
|
| 345 |
+
|
| 346 |
+
exp = np.array([0, 1, 2, 3, -1], dtype=np.int8)
|
| 347 |
+
tm.assert_numpy_array_equal(c.codes, exp)
|
| 348 |
+
|
| 349 |
+
result = repr(c)
|
| 350 |
+
assert "NaT" in result
|
| 351 |
+
|
| 352 |
+
def test_constructor_from_index_series_datetimetz(self):
|
| 353 |
+
idx = date_range("2015-01-01 10:00", freq="D", periods=3, tz="US/Eastern")
|
| 354 |
+
idx = idx._with_freq(None) # freq not preserved in result.categories
|
| 355 |
+
result = Categorical(idx)
|
| 356 |
+
tm.assert_index_equal(result.categories, idx)
|
| 357 |
+
|
| 358 |
+
result = Categorical(Series(idx))
|
| 359 |
+
tm.assert_index_equal(result.categories, idx)
|
| 360 |
+
|
| 361 |
+
def test_constructor_date_objects(self):
|
| 362 |
+
# we dont cast date objects to timestamps, matching Index constructor
|
| 363 |
+
v = date.today()
|
| 364 |
+
|
| 365 |
+
cat = Categorical([v, v])
|
| 366 |
+
assert cat.categories.dtype == object
|
| 367 |
+
assert type(cat.categories[0]) is date
|
| 368 |
+
|
| 369 |
+
def test_constructor_from_index_series_timedelta(self):
|
| 370 |
+
idx = timedelta_range("1 days", freq="D", periods=3)
|
| 371 |
+
idx = idx._with_freq(None) # freq not preserved in result.categories
|
| 372 |
+
result = Categorical(idx)
|
| 373 |
+
tm.assert_index_equal(result.categories, idx)
|
| 374 |
+
|
| 375 |
+
result = Categorical(Series(idx))
|
| 376 |
+
tm.assert_index_equal(result.categories, idx)
|
| 377 |
+
|
| 378 |
+
def test_constructor_from_index_series_period(self):
|
| 379 |
+
idx = period_range("2015-01-01", freq="D", periods=3)
|
| 380 |
+
result = Categorical(idx)
|
| 381 |
+
tm.assert_index_equal(result.categories, idx)
|
| 382 |
+
|
| 383 |
+
result = Categorical(Series(idx))
|
| 384 |
+
tm.assert_index_equal(result.categories, idx)
|
| 385 |
+
|
| 386 |
+
@pytest.mark.parametrize(
|
| 387 |
+
"values",
|
| 388 |
+
[
|
| 389 |
+
np.array([1.0, 1.2, 1.8, np.nan]),
|
| 390 |
+
np.array([1, 2, 3], dtype="int64"),
|
| 391 |
+
["a", "b", "c", np.nan],
|
| 392 |
+
[pd.Period("2014-01"), pd.Period("2014-02"), NaT],
|
| 393 |
+
[Timestamp("2014-01-01"), Timestamp("2014-01-02"), NaT],
|
| 394 |
+
[
|
| 395 |
+
Timestamp("2014-01-01", tz="US/Eastern"),
|
| 396 |
+
Timestamp("2014-01-02", tz="US/Eastern"),
|
| 397 |
+
NaT,
|
| 398 |
+
],
|
| 399 |
+
],
|
| 400 |
+
)
|
| 401 |
+
def test_constructor_invariant(self, values):
|
| 402 |
+
# GH 14190
|
| 403 |
+
c = Categorical(values)
|
| 404 |
+
c2 = Categorical(c)
|
| 405 |
+
tm.assert_categorical_equal(c, c2)
|
| 406 |
+
|
| 407 |
+
@pytest.mark.parametrize("ordered", [True, False])
|
| 408 |
+
def test_constructor_with_dtype(self, ordered):
|
| 409 |
+
categories = ["b", "a", "c"]
|
| 410 |
+
dtype = CategoricalDtype(categories, ordered=ordered)
|
| 411 |
+
result = Categorical(["a", "b", "a", "c"], dtype=dtype)
|
| 412 |
+
expected = Categorical(
|
| 413 |
+
["a", "b", "a", "c"], categories=categories, ordered=ordered
|
| 414 |
+
)
|
| 415 |
+
tm.assert_categorical_equal(result, expected)
|
| 416 |
+
assert result.ordered is ordered
|
| 417 |
+
|
| 418 |
+
def test_constructor_dtype_and_others_raises(self):
|
| 419 |
+
dtype = CategoricalDtype(["a", "b"], ordered=True)
|
| 420 |
+
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
|
| 421 |
+
with pytest.raises(ValueError, match=msg):
|
| 422 |
+
Categorical(["a", "b"], categories=["a", "b"], dtype=dtype)
|
| 423 |
+
|
| 424 |
+
with pytest.raises(ValueError, match=msg):
|
| 425 |
+
Categorical(["a", "b"], ordered=True, dtype=dtype)
|
| 426 |
+
|
| 427 |
+
with pytest.raises(ValueError, match=msg):
|
| 428 |
+
Categorical(["a", "b"], ordered=False, dtype=dtype)
|
| 429 |
+
|
| 430 |
+
@pytest.mark.parametrize("categories", [None, ["a", "b"], ["a", "c"]])
|
| 431 |
+
@pytest.mark.parametrize("ordered", [True, False])
|
| 432 |
+
def test_constructor_str_category(self, categories, ordered):
|
| 433 |
+
result = Categorical(
|
| 434 |
+
["a", "b"], categories=categories, ordered=ordered, dtype="category"
|
| 435 |
+
)
|
| 436 |
+
expected = Categorical(["a", "b"], categories=categories, ordered=ordered)
|
| 437 |
+
tm.assert_categorical_equal(result, expected)
|
| 438 |
+
|
| 439 |
+
def test_constructor_str_unknown(self):
|
| 440 |
+
with pytest.raises(ValueError, match="Unknown dtype"):
|
| 441 |
+
Categorical([1, 2], dtype="foo")
|
| 442 |
+
|
| 443 |
+
def test_constructor_np_strs(self):
|
| 444 |
+
# GH#31499 Hashtable.map_locations needs to work on np.str_ objects
|
| 445 |
+
cat = Categorical(["1", "0", "1"], [np.str_("0"), np.str_("1")])
|
| 446 |
+
assert all(isinstance(x, np.str_) for x in cat.categories)
|
| 447 |
+
|
| 448 |
+
def test_constructor_from_categorical_with_dtype(self):
|
| 449 |
+
dtype = CategoricalDtype(["a", "b", "c"], ordered=True)
|
| 450 |
+
values = Categorical(["a", "b", "d"])
|
| 451 |
+
result = Categorical(values, dtype=dtype)
|
| 452 |
+
# We use dtype.categories, not values.categories
|
| 453 |
+
expected = Categorical(
|
| 454 |
+
["a", "b", "d"], categories=["a", "b", "c"], ordered=True
|
| 455 |
+
)
|
| 456 |
+
tm.assert_categorical_equal(result, expected)
|
| 457 |
+
|
| 458 |
+
def test_constructor_from_categorical_with_unknown_dtype(self):
|
| 459 |
+
dtype = CategoricalDtype(None, ordered=True)
|
| 460 |
+
values = Categorical(["a", "b", "d"])
|
| 461 |
+
result = Categorical(values, dtype=dtype)
|
| 462 |
+
# We use values.categories, not dtype.categories
|
| 463 |
+
expected = Categorical(
|
| 464 |
+
["a", "b", "d"], categories=["a", "b", "d"], ordered=True
|
| 465 |
+
)
|
| 466 |
+
tm.assert_categorical_equal(result, expected)
|
| 467 |
+
|
| 468 |
+
def test_constructor_from_categorical_string(self):
|
| 469 |
+
values = Categorical(["a", "b", "d"])
|
| 470 |
+
# use categories, ordered
|
| 471 |
+
result = Categorical(
|
| 472 |
+
values, categories=["a", "b", "c"], ordered=True, dtype="category"
|
| 473 |
+
)
|
| 474 |
+
expected = Categorical(
|
| 475 |
+
["a", "b", "d"], categories=["a", "b", "c"], ordered=True
|
| 476 |
+
)
|
| 477 |
+
tm.assert_categorical_equal(result, expected)
|
| 478 |
+
|
| 479 |
+
# No string
|
| 480 |
+
result = Categorical(values, categories=["a", "b", "c"], ordered=True)
|
| 481 |
+
tm.assert_categorical_equal(result, expected)
|
| 482 |
+
|
| 483 |
+
def test_constructor_with_categorical_categories(self):
|
| 484 |
+
# GH17884
|
| 485 |
+
expected = Categorical(["a", "b"], categories=["a", "b", "c"])
|
| 486 |
+
|
| 487 |
+
result = Categorical(["a", "b"], categories=Categorical(["a", "b", "c"]))
|
| 488 |
+
tm.assert_categorical_equal(result, expected)
|
| 489 |
+
|
| 490 |
+
result = Categorical(["a", "b"], categories=CategoricalIndex(["a", "b", "c"]))
|
| 491 |
+
tm.assert_categorical_equal(result, expected)
|
| 492 |
+
|
| 493 |
+
@pytest.mark.parametrize("klass", [lambda x: np.array(x, dtype=object), list])
|
| 494 |
+
def test_construction_with_null(self, klass, nulls_fixture):
|
| 495 |
+
# https://github.com/pandas-dev/pandas/issues/31927
|
| 496 |
+
values = klass(["a", nulls_fixture, "b"])
|
| 497 |
+
result = Categorical(values)
|
| 498 |
+
|
| 499 |
+
dtype = CategoricalDtype(["a", "b"])
|
| 500 |
+
codes = [0, -1, 1]
|
| 501 |
+
expected = Categorical.from_codes(codes=codes, dtype=dtype)
|
| 502 |
+
|
| 503 |
+
tm.assert_categorical_equal(result, expected)
|
| 504 |
+
|
| 505 |
+
def test_from_codes_nullable_int_categories(self, any_numeric_ea_dtype):
|
| 506 |
+
# GH#39649
|
| 507 |
+
cats = pd.array(range(5), dtype=any_numeric_ea_dtype)
|
| 508 |
+
codes = np.random.randint(5, size=3)
|
| 509 |
+
dtype = CategoricalDtype(cats)
|
| 510 |
+
arr = Categorical.from_codes(codes, dtype=dtype)
|
| 511 |
+
assert arr.categories.dtype == cats.dtype
|
| 512 |
+
tm.assert_index_equal(arr.categories, Index(cats))
|
| 513 |
+
|
| 514 |
+
def test_from_codes_empty(self):
|
| 515 |
+
cat = ["a", "b", "c"]
|
| 516 |
+
result = Categorical.from_codes([], categories=cat)
|
| 517 |
+
expected = Categorical([], categories=cat)
|
| 518 |
+
|
| 519 |
+
tm.assert_categorical_equal(result, expected)
|
| 520 |
+
|
| 521 |
+
def test_from_codes_too_few_categories(self):
|
| 522 |
+
dtype = CategoricalDtype(categories=[1, 2])
|
| 523 |
+
msg = "codes need to be between "
|
| 524 |
+
with pytest.raises(ValueError, match=msg):
|
| 525 |
+
Categorical.from_codes([1, 2], categories=dtype.categories)
|
| 526 |
+
with pytest.raises(ValueError, match=msg):
|
| 527 |
+
Categorical.from_codes([1, 2], dtype=dtype)
|
| 528 |
+
|
| 529 |
+
def test_from_codes_non_int_codes(self):
|
| 530 |
+
dtype = CategoricalDtype(categories=[1, 2])
|
| 531 |
+
msg = "codes need to be array-like integers"
|
| 532 |
+
with pytest.raises(ValueError, match=msg):
|
| 533 |
+
Categorical.from_codes(["a"], categories=dtype.categories)
|
| 534 |
+
with pytest.raises(ValueError, match=msg):
|
| 535 |
+
Categorical.from_codes(["a"], dtype=dtype)
|
| 536 |
+
|
| 537 |
+
def test_from_codes_non_unique_categories(self):
|
| 538 |
+
with pytest.raises(ValueError, match="Categorical categories must be unique"):
|
| 539 |
+
Categorical.from_codes([0, 1, 2], categories=["a", "a", "b"])
|
| 540 |
+
|
| 541 |
+
def test_from_codes_nan_cat_included(self):
|
| 542 |
+
with pytest.raises(ValueError, match="Categorical categories cannot be null"):
|
| 543 |
+
Categorical.from_codes([0, 1, 2], categories=["a", "b", np.nan])
|
| 544 |
+
|
| 545 |
+
def test_from_codes_too_negative(self):
|
| 546 |
+
dtype = CategoricalDtype(categories=["a", "b", "c"])
|
| 547 |
+
msg = r"codes need to be between -1 and len\(categories\)-1"
|
| 548 |
+
with pytest.raises(ValueError, match=msg):
|
| 549 |
+
Categorical.from_codes([-2, 1, 2], categories=dtype.categories)
|
| 550 |
+
with pytest.raises(ValueError, match=msg):
|
| 551 |
+
Categorical.from_codes([-2, 1, 2], dtype=dtype)
|
| 552 |
+
|
| 553 |
+
def test_from_codes(self):
|
| 554 |
+
dtype = CategoricalDtype(categories=["a", "b", "c"])
|
| 555 |
+
exp = Categorical(["a", "b", "c"], ordered=False)
|
| 556 |
+
res = Categorical.from_codes([0, 1, 2], categories=dtype.categories)
|
| 557 |
+
tm.assert_categorical_equal(exp, res)
|
| 558 |
+
|
| 559 |
+
res = Categorical.from_codes([0, 1, 2], dtype=dtype)
|
| 560 |
+
tm.assert_categorical_equal(exp, res)
|
| 561 |
+
|
| 562 |
+
@pytest.mark.parametrize("klass", [Categorical, CategoricalIndex])
|
| 563 |
+
def test_from_codes_with_categorical_categories(self, klass):
|
| 564 |
+
# GH17884
|
| 565 |
+
expected = Categorical(["a", "b"], categories=["a", "b", "c"])
|
| 566 |
+
|
| 567 |
+
result = Categorical.from_codes([0, 1], categories=klass(["a", "b", "c"]))
|
| 568 |
+
tm.assert_categorical_equal(result, expected)
|
| 569 |
+
|
| 570 |
+
@pytest.mark.parametrize("klass", [Categorical, CategoricalIndex])
|
| 571 |
+
def test_from_codes_with_non_unique_categorical_categories(self, klass):
|
| 572 |
+
with pytest.raises(ValueError, match="Categorical categories must be unique"):
|
| 573 |
+
Categorical.from_codes([0, 1], klass(["a", "b", "a"]))
|
| 574 |
+
|
| 575 |
+
def test_from_codes_with_nan_code(self):
|
| 576 |
+
# GH21767
|
| 577 |
+
codes = [1, 2, np.nan]
|
| 578 |
+
dtype = CategoricalDtype(categories=["a", "b", "c"])
|
| 579 |
+
with pytest.raises(ValueError, match="codes need to be array-like integers"):
|
| 580 |
+
Categorical.from_codes(codes, categories=dtype.categories)
|
| 581 |
+
with pytest.raises(ValueError, match="codes need to be array-like integers"):
|
| 582 |
+
Categorical.from_codes(codes, dtype=dtype)
|
| 583 |
+
|
| 584 |
+
@pytest.mark.parametrize("codes", [[1.0, 2.0, 0], [1.1, 2.0, 0]])
|
| 585 |
+
def test_from_codes_with_float(self, codes):
|
| 586 |
+
# GH21767
|
| 587 |
+
# float codes should raise even if values are equal to integers
|
| 588 |
+
dtype = CategoricalDtype(categories=["a", "b", "c"])
|
| 589 |
+
|
| 590 |
+
msg = "codes need to be array-like integers"
|
| 591 |
+
with pytest.raises(ValueError, match=msg):
|
| 592 |
+
Categorical.from_codes(codes, dtype.categories)
|
| 593 |
+
with pytest.raises(ValueError, match=msg):
|
| 594 |
+
Categorical.from_codes(codes, dtype=dtype)
|
| 595 |
+
|
| 596 |
+
def test_from_codes_with_dtype_raises(self):
|
| 597 |
+
msg = "Cannot specify"
|
| 598 |
+
with pytest.raises(ValueError, match=msg):
|
| 599 |
+
Categorical.from_codes(
|
| 600 |
+
[0, 1], categories=["a", "b"], dtype=CategoricalDtype(["a", "b"])
|
| 601 |
+
)
|
| 602 |
+
|
| 603 |
+
with pytest.raises(ValueError, match=msg):
|
| 604 |
+
Categorical.from_codes(
|
| 605 |
+
[0, 1], ordered=True, dtype=CategoricalDtype(["a", "b"])
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
def test_from_codes_neither(self):
|
| 609 |
+
msg = "Both were None"
|
| 610 |
+
with pytest.raises(ValueError, match=msg):
|
| 611 |
+
Categorical.from_codes([0, 1])
|
| 612 |
+
|
| 613 |
+
def test_from_codes_with_nullable_int(self):
|
| 614 |
+
codes = pd.array([0, 1], dtype="Int64")
|
| 615 |
+
categories = ["a", "b"]
|
| 616 |
+
|
| 617 |
+
result = Categorical.from_codes(codes, categories=categories)
|
| 618 |
+
expected = Categorical.from_codes(codes.to_numpy(int), categories=categories)
|
| 619 |
+
|
| 620 |
+
tm.assert_categorical_equal(result, expected)
|
| 621 |
+
|
| 622 |
+
def test_from_codes_with_nullable_int_na_raises(self):
|
| 623 |
+
codes = pd.array([0, None], dtype="Int64")
|
| 624 |
+
categories = ["a", "b"]
|
| 625 |
+
|
| 626 |
+
msg = "codes cannot contain NA values"
|
| 627 |
+
with pytest.raises(ValueError, match=msg):
|
| 628 |
+
Categorical.from_codes(codes, categories=categories)
|
| 629 |
+
|
| 630 |
+
@pytest.mark.parametrize("dtype", [None, "category"])
|
| 631 |
+
def test_from_inferred_categories(self, dtype):
|
| 632 |
+
cats = ["a", "b"]
|
| 633 |
+
codes = np.array([0, 0, 1, 1], dtype="i8")
|
| 634 |
+
result = Categorical._from_inferred_categories(cats, codes, dtype)
|
| 635 |
+
expected = Categorical.from_codes(codes, cats)
|
| 636 |
+
tm.assert_categorical_equal(result, expected)
|
| 637 |
+
|
| 638 |
+
@pytest.mark.parametrize("dtype", [None, "category"])
|
| 639 |
+
def test_from_inferred_categories_sorts(self, dtype):
|
| 640 |
+
cats = ["b", "a"]
|
| 641 |
+
codes = np.array([0, 1, 1, 1], dtype="i8")
|
| 642 |
+
result = Categorical._from_inferred_categories(cats, codes, dtype)
|
| 643 |
+
expected = Categorical.from_codes([1, 0, 0, 0], ["a", "b"])
|
| 644 |
+
tm.assert_categorical_equal(result, expected)
|
| 645 |
+
|
| 646 |
+
def test_from_inferred_categories_dtype(self):
|
| 647 |
+
cats = ["a", "b", "d"]
|
| 648 |
+
codes = np.array([0, 1, 0, 2], dtype="i8")
|
| 649 |
+
dtype = CategoricalDtype(["c", "b", "a"], ordered=True)
|
| 650 |
+
result = Categorical._from_inferred_categories(cats, codes, dtype)
|
| 651 |
+
expected = Categorical(
|
| 652 |
+
["a", "b", "a", "d"], categories=["c", "b", "a"], ordered=True
|
| 653 |
+
)
|
| 654 |
+
tm.assert_categorical_equal(result, expected)
|
| 655 |
+
|
| 656 |
+
def test_from_inferred_categories_coerces(self):
|
| 657 |
+
cats = ["1", "2", "bad"]
|
| 658 |
+
codes = np.array([0, 0, 1, 2], dtype="i8")
|
| 659 |
+
dtype = CategoricalDtype([1, 2])
|
| 660 |
+
result = Categorical._from_inferred_categories(cats, codes, dtype)
|
| 661 |
+
expected = Categorical([1, 1, 2, np.nan])
|
| 662 |
+
tm.assert_categorical_equal(result, expected)
|
| 663 |
+
|
| 664 |
+
@pytest.mark.parametrize("ordered", [None, True, False])
|
| 665 |
+
def test_construction_with_ordered(self, ordered):
|
| 666 |
+
# GH 9347, 9190
|
| 667 |
+
cat = Categorical([0, 1, 2], ordered=ordered)
|
| 668 |
+
assert cat.ordered == bool(ordered)
|
| 669 |
+
|
| 670 |
+
def test_constructor_imaginary(self):
|
| 671 |
+
values = [1, 2, 3 + 1j]
|
| 672 |
+
c1 = Categorical(values)
|
| 673 |
+
tm.assert_index_equal(c1.categories, Index(values))
|
| 674 |
+
tm.assert_numpy_array_equal(np.array(c1), np.array(values))
|
| 675 |
+
|
| 676 |
+
def test_constructor_string_and_tuples(self):
|
| 677 |
+
# GH 21416
|
| 678 |
+
c = Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
|
| 679 |
+
expected_index = Index([("a", "b"), ("b", "a"), "c"])
|
| 680 |
+
assert c.categories.equals(expected_index)
|
| 681 |
+
|
| 682 |
+
def test_interval(self):
|
| 683 |
+
idx = pd.interval_range(0, 10, periods=10)
|
| 684 |
+
cat = Categorical(idx, categories=idx)
|
| 685 |
+
expected_codes = np.arange(10, dtype="int8")
|
| 686 |
+
tm.assert_numpy_array_equal(cat.codes, expected_codes)
|
| 687 |
+
tm.assert_index_equal(cat.categories, idx)
|
| 688 |
+
|
| 689 |
+
# infer categories
|
| 690 |
+
cat = Categorical(idx)
|
| 691 |
+
tm.assert_numpy_array_equal(cat.codes, expected_codes)
|
| 692 |
+
tm.assert_index_equal(cat.categories, idx)
|
| 693 |
+
|
| 694 |
+
# list values
|
| 695 |
+
cat = Categorical(list(idx))
|
| 696 |
+
tm.assert_numpy_array_equal(cat.codes, expected_codes)
|
| 697 |
+
tm.assert_index_equal(cat.categories, idx)
|
| 698 |
+
|
| 699 |
+
# list values, categories
|
| 700 |
+
cat = Categorical(list(idx), categories=list(idx))
|
| 701 |
+
tm.assert_numpy_array_equal(cat.codes, expected_codes)
|
| 702 |
+
tm.assert_index_equal(cat.categories, idx)
|
| 703 |
+
|
| 704 |
+
# shuffled
|
| 705 |
+
values = idx.take([1, 2, 0])
|
| 706 |
+
cat = Categorical(values, categories=idx)
|
| 707 |
+
tm.assert_numpy_array_equal(cat.codes, np.array([1, 2, 0], dtype="int8"))
|
| 708 |
+
tm.assert_index_equal(cat.categories, idx)
|
| 709 |
+
|
| 710 |
+
# extra
|
| 711 |
+
values = pd.interval_range(8, 11, periods=3)
|
| 712 |
+
cat = Categorical(values, categories=idx)
|
| 713 |
+
expected_codes = np.array([8, 9, -1], dtype="int8")
|
| 714 |
+
tm.assert_numpy_array_equal(cat.codes, expected_codes)
|
| 715 |
+
tm.assert_index_equal(cat.categories, idx)
|
| 716 |
+
|
| 717 |
+
# overlapping
|
| 718 |
+
idx = IntervalIndex([Interval(0, 2), Interval(0, 1)])
|
| 719 |
+
cat = Categorical(idx, categories=idx)
|
| 720 |
+
expected_codes = np.array([0, 1], dtype="int8")
|
| 721 |
+
tm.assert_numpy_array_equal(cat.codes, expected_codes)
|
| 722 |
+
tm.assert_index_equal(cat.categories, idx)
|
| 723 |
+
|
| 724 |
+
def test_categorical_extension_array_nullable(self, nulls_fixture):
|
| 725 |
+
# GH:
|
| 726 |
+
arr = pd.arrays.StringArray._from_sequence([nulls_fixture] * 2)
|
| 727 |
+
result = Categorical(arr)
|
| 728 |
+
assert arr.dtype == result.categories.dtype
|
| 729 |
+
expected = Categorical(Series([pd.NA, pd.NA], dtype=arr.dtype))
|
| 730 |
+
tm.assert_categorical_equal(result, expected)
|
| 731 |
+
|
| 732 |
+
def test_from_sequence_copy(self):
|
| 733 |
+
cat = Categorical(np.arange(5).repeat(2))
|
| 734 |
+
result = Categorical._from_sequence(cat, dtype=None, copy=False)
|
| 735 |
+
|
| 736 |
+
# more generally, we'd be OK with a view
|
| 737 |
+
assert result._codes is cat._codes
|
| 738 |
+
|
| 739 |
+
result = Categorical._from_sequence(cat, dtype=None, copy=True)
|
| 740 |
+
|
| 741 |
+
assert not tm.shares_memory(result, cat)
|
| 742 |
+
|
| 743 |
+
def test_constructor_datetime64_non_nano(self):
|
| 744 |
+
categories = np.arange(10).view("M8[D]")
|
| 745 |
+
values = categories[::2].copy()
|
| 746 |
+
|
| 747 |
+
cat = Categorical(values, categories=categories)
|
| 748 |
+
assert (cat == values).all()
|
| 749 |
+
|
| 750 |
+
def test_constructor_preserves_freq(self):
|
| 751 |
+
# GH33830 freq retention in categorical
|
| 752 |
+
dti = date_range("2016-01-01", periods=5)
|
| 753 |
+
|
| 754 |
+
expected = dti.freq
|
| 755 |
+
|
| 756 |
+
cat = Categorical(dti)
|
| 757 |
+
result = cat.categories.freq
|
| 758 |
+
|
| 759 |
+
assert expected == result
|
videochat2/lib/python3.10/site-packages/pandas/tests/arrays/categorical/test_dtypes.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas.core.dtypes.dtypes import CategoricalDtype
|
| 5 |
+
|
| 6 |
+
from pandas import (
|
| 7 |
+
Categorical,
|
| 8 |
+
CategoricalIndex,
|
| 9 |
+
Index,
|
| 10 |
+
IntervalIndex,
|
| 11 |
+
Series,
|
| 12 |
+
Timestamp,
|
| 13 |
+
)
|
| 14 |
+
import pandas._testing as tm
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class TestCategoricalDtypes:
|
| 18 |
+
def test_categories_match_up_to_permutation(self):
|
| 19 |
+
# test dtype comparisons between cats
|
| 20 |
+
|
| 21 |
+
c1 = Categorical(list("aabca"), categories=list("abc"), ordered=False)
|
| 22 |
+
c2 = Categorical(list("aabca"), categories=list("cab"), ordered=False)
|
| 23 |
+
c3 = Categorical(list("aabca"), categories=list("cab"), ordered=True)
|
| 24 |
+
assert c1._categories_match_up_to_permutation(c1)
|
| 25 |
+
assert c2._categories_match_up_to_permutation(c2)
|
| 26 |
+
assert c3._categories_match_up_to_permutation(c3)
|
| 27 |
+
assert c1._categories_match_up_to_permutation(c2)
|
| 28 |
+
assert not c1._categories_match_up_to_permutation(c3)
|
| 29 |
+
assert not c1._categories_match_up_to_permutation(Index(list("aabca")))
|
| 30 |
+
assert not c1._categories_match_up_to_permutation(c1.astype(object))
|
| 31 |
+
assert c1._categories_match_up_to_permutation(CategoricalIndex(c1))
|
| 32 |
+
assert c1._categories_match_up_to_permutation(
|
| 33 |
+
CategoricalIndex(c1, categories=list("cab"))
|
| 34 |
+
)
|
| 35 |
+
assert not c1._categories_match_up_to_permutation(
|
| 36 |
+
CategoricalIndex(c1, ordered=True)
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# GH 16659
|
| 40 |
+
s1 = Series(c1)
|
| 41 |
+
s2 = Series(c2)
|
| 42 |
+
s3 = Series(c3)
|
| 43 |
+
assert c1._categories_match_up_to_permutation(s1)
|
| 44 |
+
assert c2._categories_match_up_to_permutation(s2)
|
| 45 |
+
assert c3._categories_match_up_to_permutation(s3)
|
| 46 |
+
assert c1._categories_match_up_to_permutation(s2)
|
| 47 |
+
assert not c1._categories_match_up_to_permutation(s3)
|
| 48 |
+
assert not c1._categories_match_up_to_permutation(s1.astype(object))
|
| 49 |
+
|
| 50 |
+
def test_set_dtype_same(self):
|
| 51 |
+
c = Categorical(["a", "b", "c"])
|
| 52 |
+
result = c._set_dtype(CategoricalDtype(["a", "b", "c"]))
|
| 53 |
+
tm.assert_categorical_equal(result, c)
|
| 54 |
+
|
| 55 |
+
def test_set_dtype_new_categories(self):
|
| 56 |
+
c = Categorical(["a", "b", "c"])
|
| 57 |
+
result = c._set_dtype(CategoricalDtype(list("abcd")))
|
| 58 |
+
tm.assert_numpy_array_equal(result.codes, c.codes)
|
| 59 |
+
tm.assert_index_equal(result.dtype.categories, Index(list("abcd")))
|
| 60 |
+
|
| 61 |
+
@pytest.mark.parametrize(
|
| 62 |
+
"values, categories, new_categories",
|
| 63 |
+
[
|
| 64 |
+
# No NaNs, same cats, same order
|
| 65 |
+
(["a", "b", "a"], ["a", "b"], ["a", "b"]),
|
| 66 |
+
# No NaNs, same cats, different order
|
| 67 |
+
(["a", "b", "a"], ["a", "b"], ["b", "a"]),
|
| 68 |
+
# Same, unsorted
|
| 69 |
+
(["b", "a", "a"], ["a", "b"], ["a", "b"]),
|
| 70 |
+
# No NaNs, same cats, different order
|
| 71 |
+
(["b", "a", "a"], ["a", "b"], ["b", "a"]),
|
| 72 |
+
# NaNs
|
| 73 |
+
(["a", "b", "c"], ["a", "b"], ["a", "b"]),
|
| 74 |
+
(["a", "b", "c"], ["a", "b"], ["b", "a"]),
|
| 75 |
+
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
|
| 76 |
+
(["b", "a", "c"], ["a", "b"], ["a", "b"]),
|
| 77 |
+
# Introduce NaNs
|
| 78 |
+
(["a", "b", "c"], ["a", "b"], ["a"]),
|
| 79 |
+
(["a", "b", "c"], ["a", "b"], ["b"]),
|
| 80 |
+
(["b", "a", "c"], ["a", "b"], ["a"]),
|
| 81 |
+
(["b", "a", "c"], ["a", "b"], ["a"]),
|
| 82 |
+
# No overlap
|
| 83 |
+
(["a", "b", "c"], ["a", "b"], ["d", "e"]),
|
| 84 |
+
],
|
| 85 |
+
)
|
| 86 |
+
@pytest.mark.parametrize("ordered", [True, False])
|
| 87 |
+
def test_set_dtype_many(self, values, categories, new_categories, ordered):
|
| 88 |
+
c = Categorical(values, categories)
|
| 89 |
+
expected = Categorical(values, new_categories, ordered)
|
| 90 |
+
result = c._set_dtype(expected.dtype)
|
| 91 |
+
tm.assert_categorical_equal(result, expected)
|
| 92 |
+
|
| 93 |
+
def test_set_dtype_no_overlap(self):
|
| 94 |
+
c = Categorical(["a", "b", "c"], ["d", "e"])
|
| 95 |
+
result = c._set_dtype(CategoricalDtype(["a", "b"]))
|
| 96 |
+
expected = Categorical([None, None, None], categories=["a", "b"])
|
| 97 |
+
tm.assert_categorical_equal(result, expected)
|
| 98 |
+
|
| 99 |
+
def test_codes_dtypes(self):
|
| 100 |
+
# GH 8453
|
| 101 |
+
result = Categorical(["foo", "bar", "baz"])
|
| 102 |
+
assert result.codes.dtype == "int8"
|
| 103 |
+
|
| 104 |
+
result = Categorical([f"foo{i:05d}" for i in range(400)])
|
| 105 |
+
assert result.codes.dtype == "int16"
|
| 106 |
+
|
| 107 |
+
result = Categorical([f"foo{i:05d}" for i in range(40000)])
|
| 108 |
+
assert result.codes.dtype == "int32"
|
| 109 |
+
|
| 110 |
+
# adding cats
|
| 111 |
+
result = Categorical(["foo", "bar", "baz"])
|
| 112 |
+
assert result.codes.dtype == "int8"
|
| 113 |
+
result = result.add_categories([f"foo{i:05d}" for i in range(400)])
|
| 114 |
+
assert result.codes.dtype == "int16"
|
| 115 |
+
|
| 116 |
+
# removing cats
|
| 117 |
+
result = result.remove_categories([f"foo{i:05d}" for i in range(300)])
|
| 118 |
+
assert result.codes.dtype == "int8"
|
| 119 |
+
|
| 120 |
+
def test_iter_python_types(self):
|
| 121 |
+
# GH-19909
|
| 122 |
+
cat = Categorical([1, 2])
|
| 123 |
+
assert isinstance(list(cat)[0], int)
|
| 124 |
+
assert isinstance(cat.tolist()[0], int)
|
| 125 |
+
|
| 126 |
+
def test_iter_python_types_datetime(self):
|
| 127 |
+
cat = Categorical([Timestamp("2017-01-01"), Timestamp("2017-01-02")])
|
| 128 |
+
assert isinstance(list(cat)[0], Timestamp)
|
| 129 |
+
assert isinstance(cat.tolist()[0], Timestamp)
|
| 130 |
+
|
| 131 |
+
def test_interval_index_category(self):
|
| 132 |
+
# GH 38316
|
| 133 |
+
index = IntervalIndex.from_breaks(np.arange(3, dtype="uint64"))
|
| 134 |
+
|
| 135 |
+
result = CategoricalIndex(index).dtype.categories
|
| 136 |
+
expected = IntervalIndex.from_arrays(
|
| 137 |
+
[0, 1], [1, 2], dtype="interval[uint64, right]"
|
| 138 |
+
)
|
| 139 |
+
tm.assert_index_equal(result, expected)
|