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
- env-llmeval/lib/python3.10/site-packages/pandas/_libs/algos.cpython-310-x86_64-linux-gnu.so +3 -0
- env-llmeval/lib/python3.10/site-packages/pandas/_libs/groupby.cpython-310-x86_64-linux-gnu.so +3 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_reduction.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_arithmetic.py +139 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_astype.py +53 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_comparison.py +60 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_construction.py +325 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_function.py +126 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_indexing.py +13 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_logical.py +254 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_ops.py +27 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_reduction.py +62 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_repr.py +13 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/conftest.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_comparison.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_concat.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_construction.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_dtypes.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_function.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_indexing.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_reduction.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_repr.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/conftest.py +68 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_arithmetic.py +385 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_comparison.py +39 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_concat.py +69 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_construction.py +245 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_dtypes.py +294 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_function.py +203 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_indexing.py +19 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_reduction.py +125 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_repr.py +67 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_astype.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_formats.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_interval.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_interval_pyarrow.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_overlaps.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_astype.py +28 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_formats.py +13 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_interval.py +231 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_interval_pyarrow.py +160 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_overlaps.py +93 -0
.gitattributes
CHANGED
@@ -173,3 +173,5 @@ env-llmeval/lib/python3.10/site-packages/pandas/_libs/join.cpython-310-x86_64-li
|
|
173 |
env-llmeval/lib/python3.10/site-packages/pandas/_libs/hashtable.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
174 |
env-llmeval/lib/python3.10/site-packages/pandas/_libs/interval.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
175 |
env-llmeval/lib/python3.10/site-packages/pandas/_libs/tslibs/offsets.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
173 |
env-llmeval/lib/python3.10/site-packages/pandas/_libs/hashtable.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
174 |
env-llmeval/lib/python3.10/site-packages/pandas/_libs/interval.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
175 |
env-llmeval/lib/python3.10/site-packages/pandas/_libs/tslibs/offsets.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
176 |
+
env-llmeval/lib/python3.10/site-packages/pandas/_libs/algos.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
177 |
+
env-llmeval/lib/python3.10/site-packages/pandas/_libs/groupby.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
env-llmeval/lib/python3.10/site-packages/pandas/_libs/algos.cpython-310-x86_64-linux-gnu.so
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6c90dd62bc6525e39bc76c8df9c7162cfc2cb1b371d00ff925cdfe7cd63ffdd5
|
3 |
+
size 2194056
|
env-llmeval/lib/python3.10/site-packages/pandas/_libs/groupby.cpython-310-x86_64-linux-gnu.so
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:724930f75dc3e6a3f7ae745ed008c7e3d5080a902a35d1843364531122789450
|
3 |
+
size 2622024
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_reduction.cpython-310.pyc
ADDED
Binary file (1.74 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_arithmetic.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, using_infer_string):
|
94 |
+
# invalid ops
|
95 |
+
|
96 |
+
if using_infer_string:
|
97 |
+
import pyarrow as pa
|
98 |
+
|
99 |
+
err = (TypeError, pa.lib.ArrowNotImplementedError, NotImplementedError)
|
100 |
+
else:
|
101 |
+
err = TypeError
|
102 |
+
|
103 |
+
op = all_arithmetic_operators
|
104 |
+
s = pd.Series(data)
|
105 |
+
ops = getattr(s, op)
|
106 |
+
|
107 |
+
# invalid scalars
|
108 |
+
msg = (
|
109 |
+
"did not contain a loop with signature matching types|"
|
110 |
+
"BooleanArray cannot perform the operation|"
|
111 |
+
"not supported for the input types, and the inputs could not be safely coerced "
|
112 |
+
"to any supported types according to the casting rule ''safe''"
|
113 |
+
)
|
114 |
+
with pytest.raises(TypeError, match=msg):
|
115 |
+
ops("foo")
|
116 |
+
msg = "|".join(
|
117 |
+
[
|
118 |
+
r"unsupported operand type\(s\) for",
|
119 |
+
"Concatenation operation is not implemented for NumPy arrays",
|
120 |
+
"has no kernel",
|
121 |
+
]
|
122 |
+
)
|
123 |
+
with pytest.raises(err, match=msg):
|
124 |
+
ops(pd.Timestamp("20180101"))
|
125 |
+
|
126 |
+
# invalid array-likes
|
127 |
+
if op not in ("__mul__", "__rmul__"):
|
128 |
+
# TODO(extension) numpy's mul with object array sees booleans as numbers
|
129 |
+
msg = "|".join(
|
130 |
+
[
|
131 |
+
r"unsupported operand type\(s\) for",
|
132 |
+
"can only concatenate str",
|
133 |
+
"not all arguments converted during string formatting",
|
134 |
+
"has no kernel",
|
135 |
+
"not implemented",
|
136 |
+
]
|
137 |
+
)
|
138 |
+
with pytest.raises(err, match=msg):
|
139 |
+
ops(pd.Series("foo", index=s.index))
|
env-llmeval/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)
|
env-llmeval/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 |
+
)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_construction.py
ADDED
@@ -0,0 +1,325 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="bool")
|
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 |
+
dtype="boolean",
|
247 |
+
)
|
248 |
+
expected = BooleanArray(
|
249 |
+
np.array([True, False, True, True, False, False, False]),
|
250 |
+
np.array([False, False, False, False, False, False, True]),
|
251 |
+
)
|
252 |
+
|
253 |
+
tm.assert_extension_array_equal(result, expected)
|
254 |
+
|
255 |
+
|
256 |
+
def test_to_boolean_array_from_strings_invalid_string():
|
257 |
+
with pytest.raises(ValueError, match="cannot be cast"):
|
258 |
+
BooleanArray._from_sequence_of_strings(["donkey"], dtype="boolean")
|
259 |
+
|
260 |
+
|
261 |
+
@pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
|
262 |
+
def test_to_numpy(box):
|
263 |
+
con = pd.Series if box else pd.array
|
264 |
+
# default (with or without missing values) -> object dtype
|
265 |
+
arr = con([True, False, True], dtype="boolean")
|
266 |
+
result = arr.to_numpy()
|
267 |
+
expected = np.array([True, False, True], dtype="bool")
|
268 |
+
tm.assert_numpy_array_equal(result, expected)
|
269 |
+
|
270 |
+
arr = con([True, False, None], dtype="boolean")
|
271 |
+
result = arr.to_numpy()
|
272 |
+
expected = np.array([True, False, pd.NA], dtype="object")
|
273 |
+
tm.assert_numpy_array_equal(result, expected)
|
274 |
+
|
275 |
+
arr = con([True, False, None], dtype="boolean")
|
276 |
+
result = arr.to_numpy(dtype="str")
|
277 |
+
expected = np.array([True, False, pd.NA], dtype=f"{tm.ENDIAN}U5")
|
278 |
+
tm.assert_numpy_array_equal(result, expected)
|
279 |
+
|
280 |
+
# no missing values -> can convert to bool, otherwise raises
|
281 |
+
arr = con([True, False, True], dtype="boolean")
|
282 |
+
result = arr.to_numpy(dtype="bool")
|
283 |
+
expected = np.array([True, False, True], dtype="bool")
|
284 |
+
tm.assert_numpy_array_equal(result, expected)
|
285 |
+
|
286 |
+
arr = con([True, False, None], dtype="boolean")
|
287 |
+
with pytest.raises(ValueError, match="cannot convert to 'bool'-dtype"):
|
288 |
+
result = arr.to_numpy(dtype="bool")
|
289 |
+
|
290 |
+
# specify dtype and na_value
|
291 |
+
arr = con([True, False, None], dtype="boolean")
|
292 |
+
result = arr.to_numpy(dtype=object, na_value=None)
|
293 |
+
expected = np.array([True, False, None], dtype="object")
|
294 |
+
tm.assert_numpy_array_equal(result, expected)
|
295 |
+
|
296 |
+
result = arr.to_numpy(dtype=bool, na_value=False)
|
297 |
+
expected = np.array([True, False, False], dtype="bool")
|
298 |
+
tm.assert_numpy_array_equal(result, expected)
|
299 |
+
|
300 |
+
result = arr.to_numpy(dtype="int64", na_value=-99)
|
301 |
+
expected = np.array([1, 0, -99], dtype="int64")
|
302 |
+
tm.assert_numpy_array_equal(result, expected)
|
303 |
+
|
304 |
+
result = arr.to_numpy(dtype="float64", na_value=np.nan)
|
305 |
+
expected = np.array([1, 0, np.nan], dtype="float64")
|
306 |
+
tm.assert_numpy_array_equal(result, expected)
|
307 |
+
|
308 |
+
# converting to int or float without specifying na_value raises
|
309 |
+
with pytest.raises(ValueError, match="cannot convert to 'int64'-dtype"):
|
310 |
+
arr.to_numpy(dtype="int64")
|
311 |
+
|
312 |
+
|
313 |
+
def test_to_numpy_copy():
|
314 |
+
# to_numpy can be zero-copy if no missing values
|
315 |
+
arr = pd.array([True, False, True], dtype="boolean")
|
316 |
+
result = arr.to_numpy(dtype=bool)
|
317 |
+
result[0] = False
|
318 |
+
tm.assert_extension_array_equal(
|
319 |
+
arr, pd.array([False, False, True], dtype="boolean")
|
320 |
+
)
|
321 |
+
|
322 |
+
arr = pd.array([True, False, True], dtype="boolean")
|
323 |
+
result = arr.to_numpy(dtype=bool, copy=True)
|
324 |
+
result[0] = False
|
325 |
+
tm.assert_extension_array_equal(arr, pd.array([True, False, True], dtype="boolean"))
|
env-llmeval/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)
|
env-llmeval/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)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_logical.py
ADDED
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from pandas.arrays import BooleanArray
|
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))
|
env-llmeval/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)
|
env-llmeval/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)
|
env-llmeval/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
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (194 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (192 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/conftest.cpython-310.pyc
ADDED
Binary file (1.79 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
Binary file (9.38 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_comparison.cpython-310.pyc
ADDED
Binary file (1.49 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_concat.cpython-310.pyc
ADDED
Binary file (1.84 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_construction.cpython-310.pyc
ADDED
Binary file (6.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_dtypes.cpython-310.pyc
ADDED
Binary file (7.18 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_function.cpython-310.pyc
ADDED
Binary file (5.89 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (801 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_reduction.cpython-310.pyc
ADDED
Binary file (2.95 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_repr.cpython-310.pyc
ADDED
Binary file (1.93 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/conftest.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
from pandas.core.arrays.integer import (
|
6 |
+
Int8Dtype,
|
7 |
+
Int16Dtype,
|
8 |
+
Int32Dtype,
|
9 |
+
Int64Dtype,
|
10 |
+
UInt8Dtype,
|
11 |
+
UInt16Dtype,
|
12 |
+
UInt32Dtype,
|
13 |
+
UInt64Dtype,
|
14 |
+
)
|
15 |
+
|
16 |
+
|
17 |
+
@pytest.fixture(
|
18 |
+
params=[
|
19 |
+
Int8Dtype,
|
20 |
+
Int16Dtype,
|
21 |
+
Int32Dtype,
|
22 |
+
Int64Dtype,
|
23 |
+
UInt8Dtype,
|
24 |
+
UInt16Dtype,
|
25 |
+
UInt32Dtype,
|
26 |
+
UInt64Dtype,
|
27 |
+
]
|
28 |
+
)
|
29 |
+
def dtype(request):
|
30 |
+
"""Parametrized fixture returning integer 'dtype'"""
|
31 |
+
return request.param()
|
32 |
+
|
33 |
+
|
34 |
+
@pytest.fixture
|
35 |
+
def data(dtype):
|
36 |
+
"""
|
37 |
+
Fixture returning 'data' array with valid and missing values according to
|
38 |
+
parametrized integer 'dtype'.
|
39 |
+
|
40 |
+
Used to test dtype conversion with and without missing values.
|
41 |
+
"""
|
42 |
+
return pd.array(
|
43 |
+
list(range(8)) + [np.nan] + list(range(10, 98)) + [np.nan] + [99, 100],
|
44 |
+
dtype=dtype,
|
45 |
+
)
|
46 |
+
|
47 |
+
|
48 |
+
@pytest.fixture
|
49 |
+
def data_missing(dtype):
|
50 |
+
"""
|
51 |
+
Fixture returning array with exactly one NaN and one valid integer,
|
52 |
+
according to parametrized integer 'dtype'.
|
53 |
+
|
54 |
+
Used to test dtype conversion with and without missing values.
|
55 |
+
"""
|
56 |
+
return pd.array([np.nan, 1], dtype=dtype)
|
57 |
+
|
58 |
+
|
59 |
+
@pytest.fixture(params=["data", "data_missing"])
|
60 |
+
def all_data(request, data, data_missing):
|
61 |
+
"""Parametrized fixture returning 'data' or 'data_missing' integer arrays.
|
62 |
+
|
63 |
+
Used to test dtype conversion with and without missing values.
|
64 |
+
"""
|
65 |
+
if request.param == "data":
|
66 |
+
return data
|
67 |
+
elif request.param == "data_missing":
|
68 |
+
return data_missing
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_arithmetic.py
ADDED
@@ -0,0 +1,385 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from pandas.core import ops
|
9 |
+
from pandas.core.arrays import FloatingArray
|
10 |
+
|
11 |
+
# Basic test for the arithmetic array ops
|
12 |
+
# -----------------------------------------------------------------------------
|
13 |
+
|
14 |
+
|
15 |
+
@pytest.mark.parametrize(
|
16 |
+
"opname, exp",
|
17 |
+
[("add", [1, 3, None, None, 9]), ("mul", [0, 2, None, None, 20])],
|
18 |
+
ids=["add", "mul"],
|
19 |
+
)
|
20 |
+
def test_add_mul(dtype, opname, exp):
|
21 |
+
a = pd.array([0, 1, None, 3, 4], dtype=dtype)
|
22 |
+
b = pd.array([1, 2, 3, None, 5], dtype=dtype)
|
23 |
+
|
24 |
+
# array / array
|
25 |
+
expected = pd.array(exp, dtype=dtype)
|
26 |
+
|
27 |
+
op = getattr(operator, opname)
|
28 |
+
result = op(a, b)
|
29 |
+
tm.assert_extension_array_equal(result, expected)
|
30 |
+
|
31 |
+
op = getattr(ops, "r" + opname)
|
32 |
+
result = op(a, b)
|
33 |
+
tm.assert_extension_array_equal(result, expected)
|
34 |
+
|
35 |
+
|
36 |
+
def test_sub(dtype):
|
37 |
+
a = pd.array([1, 2, 3, None, 5], dtype=dtype)
|
38 |
+
b = pd.array([0, 1, None, 3, 4], dtype=dtype)
|
39 |
+
|
40 |
+
result = a - b
|
41 |
+
expected = pd.array([1, 1, None, None, 1], dtype=dtype)
|
42 |
+
tm.assert_extension_array_equal(result, expected)
|
43 |
+
|
44 |
+
|
45 |
+
def test_div(dtype):
|
46 |
+
a = pd.array([1, 2, 3, None, 5], dtype=dtype)
|
47 |
+
b = pd.array([0, 1, None, 3, 4], dtype=dtype)
|
48 |
+
|
49 |
+
result = a / b
|
50 |
+
expected = pd.array([np.inf, 2, None, None, 1.25], dtype="Float64")
|
51 |
+
tm.assert_extension_array_equal(result, expected)
|
52 |
+
|
53 |
+
|
54 |
+
@pytest.mark.parametrize("zero, negative", [(0, False), (0.0, False), (-0.0, True)])
|
55 |
+
def test_divide_by_zero(zero, negative):
|
56 |
+
# https://github.com/pandas-dev/pandas/issues/27398, GH#22793
|
57 |
+
a = pd.array([0, 1, -1, None], dtype="Int64")
|
58 |
+
result = a / zero
|
59 |
+
expected = FloatingArray(
|
60 |
+
np.array([np.nan, np.inf, -np.inf, 1], dtype="float64"),
|
61 |
+
np.array([False, False, False, True]),
|
62 |
+
)
|
63 |
+
if negative:
|
64 |
+
expected *= -1
|
65 |
+
tm.assert_extension_array_equal(result, expected)
|
66 |
+
|
67 |
+
|
68 |
+
def test_floordiv(dtype):
|
69 |
+
a = pd.array([1, 2, 3, None, 5], dtype=dtype)
|
70 |
+
b = pd.array([0, 1, None, 3, 4], dtype=dtype)
|
71 |
+
|
72 |
+
result = a // b
|
73 |
+
# Series op sets 1//0 to np.inf, which IntegerArray does not do (yet)
|
74 |
+
expected = pd.array([0, 2, None, None, 1], dtype=dtype)
|
75 |
+
tm.assert_extension_array_equal(result, expected)
|
76 |
+
|
77 |
+
|
78 |
+
def test_floordiv_by_int_zero_no_mask(any_int_ea_dtype):
|
79 |
+
# GH 48223: Aligns with non-masked floordiv
|
80 |
+
# but differs from numpy
|
81 |
+
# https://github.com/pandas-dev/pandas/issues/30188#issuecomment-564452740
|
82 |
+
ser = pd.Series([0, 1], dtype=any_int_ea_dtype)
|
83 |
+
result = 1 // ser
|
84 |
+
expected = pd.Series([np.inf, 1.0], dtype="Float64")
|
85 |
+
tm.assert_series_equal(result, expected)
|
86 |
+
|
87 |
+
ser_non_nullable = ser.astype(ser.dtype.numpy_dtype)
|
88 |
+
result = 1 // ser_non_nullable
|
89 |
+
expected = expected.astype(np.float64)
|
90 |
+
tm.assert_series_equal(result, expected)
|
91 |
+
|
92 |
+
|
93 |
+
def test_mod(dtype):
|
94 |
+
a = pd.array([1, 2, 3, None, 5], dtype=dtype)
|
95 |
+
b = pd.array([0, 1, None, 3, 4], dtype=dtype)
|
96 |
+
|
97 |
+
result = a % b
|
98 |
+
expected = pd.array([0, 0, None, None, 1], dtype=dtype)
|
99 |
+
tm.assert_extension_array_equal(result, expected)
|
100 |
+
|
101 |
+
|
102 |
+
def test_pow_scalar():
|
103 |
+
a = pd.array([-1, 0, 1, None, 2], dtype="Int64")
|
104 |
+
result = a**0
|
105 |
+
expected = pd.array([1, 1, 1, 1, 1], dtype="Int64")
|
106 |
+
tm.assert_extension_array_equal(result, expected)
|
107 |
+
|
108 |
+
result = a**1
|
109 |
+
expected = pd.array([-1, 0, 1, None, 2], dtype="Int64")
|
110 |
+
tm.assert_extension_array_equal(result, expected)
|
111 |
+
|
112 |
+
result = a**pd.NA
|
113 |
+
expected = pd.array([None, None, 1, None, None], dtype="Int64")
|
114 |
+
tm.assert_extension_array_equal(result, expected)
|
115 |
+
|
116 |
+
result = a**np.nan
|
117 |
+
expected = FloatingArray(
|
118 |
+
np.array([np.nan, np.nan, 1, np.nan, np.nan], dtype="float64"),
|
119 |
+
np.array([False, False, False, True, False]),
|
120 |
+
)
|
121 |
+
tm.assert_extension_array_equal(result, expected)
|
122 |
+
|
123 |
+
# reversed
|
124 |
+
a = a[1:] # Can't raise integers to negative powers.
|
125 |
+
|
126 |
+
result = 0**a
|
127 |
+
expected = pd.array([1, 0, None, 0], dtype="Int64")
|
128 |
+
tm.assert_extension_array_equal(result, expected)
|
129 |
+
|
130 |
+
result = 1**a
|
131 |
+
expected = pd.array([1, 1, 1, 1], dtype="Int64")
|
132 |
+
tm.assert_extension_array_equal(result, expected)
|
133 |
+
|
134 |
+
result = pd.NA**a
|
135 |
+
expected = pd.array([1, None, None, None], dtype="Int64")
|
136 |
+
tm.assert_extension_array_equal(result, expected)
|
137 |
+
|
138 |
+
result = np.nan**a
|
139 |
+
expected = FloatingArray(
|
140 |
+
np.array([1, np.nan, np.nan, np.nan], dtype="float64"),
|
141 |
+
np.array([False, False, True, False]),
|
142 |
+
)
|
143 |
+
tm.assert_extension_array_equal(result, expected)
|
144 |
+
|
145 |
+
|
146 |
+
def test_pow_array():
|
147 |
+
a = pd.array([0, 0, 0, 1, 1, 1, None, None, None])
|
148 |
+
b = pd.array([0, 1, None, 0, 1, None, 0, 1, None])
|
149 |
+
result = a**b
|
150 |
+
expected = pd.array([1, 0, None, 1, 1, 1, 1, None, None])
|
151 |
+
tm.assert_extension_array_equal(result, expected)
|
152 |
+
|
153 |
+
|
154 |
+
def test_rpow_one_to_na():
|
155 |
+
# https://github.com/pandas-dev/pandas/issues/22022
|
156 |
+
# https://github.com/pandas-dev/pandas/issues/29997
|
157 |
+
arr = pd.array([np.nan, np.nan], dtype="Int64")
|
158 |
+
result = np.array([1.0, 2.0]) ** arr
|
159 |
+
expected = pd.array([1.0, np.nan], dtype="Float64")
|
160 |
+
tm.assert_extension_array_equal(result, expected)
|
161 |
+
|
162 |
+
|
163 |
+
@pytest.mark.parametrize("other", [0, 0.5])
|
164 |
+
def test_numpy_zero_dim_ndarray(other):
|
165 |
+
arr = pd.array([1, None, 2])
|
166 |
+
result = arr + np.array(other)
|
167 |
+
expected = arr + other
|
168 |
+
tm.assert_equal(result, expected)
|
169 |
+
|
170 |
+
|
171 |
+
# Test generic characteristics / errors
|
172 |
+
# -----------------------------------------------------------------------------
|
173 |
+
|
174 |
+
|
175 |
+
def test_error_invalid_values(data, all_arithmetic_operators, using_infer_string):
|
176 |
+
op = all_arithmetic_operators
|
177 |
+
s = pd.Series(data)
|
178 |
+
ops = getattr(s, op)
|
179 |
+
|
180 |
+
if using_infer_string:
|
181 |
+
import pyarrow as pa
|
182 |
+
|
183 |
+
errs = (TypeError, pa.lib.ArrowNotImplementedError, NotImplementedError)
|
184 |
+
else:
|
185 |
+
errs = TypeError
|
186 |
+
|
187 |
+
# invalid scalars
|
188 |
+
msg = "|".join(
|
189 |
+
[
|
190 |
+
r"can only perform ops with numeric values",
|
191 |
+
r"IntegerArray cannot perform the operation mod",
|
192 |
+
r"unsupported operand type",
|
193 |
+
r"can only concatenate str \(not \"int\"\) to str",
|
194 |
+
"not all arguments converted during string",
|
195 |
+
"ufunc '.*' not supported for the input types, and the inputs could not",
|
196 |
+
"ufunc '.*' did not contain a loop with signature matching types",
|
197 |
+
"Addition/subtraction of integers and integer-arrays with Timestamp",
|
198 |
+
"has no kernel",
|
199 |
+
"not implemented",
|
200 |
+
"The 'out' kwarg is necessary. Use numpy.strings.multiply without it.",
|
201 |
+
]
|
202 |
+
)
|
203 |
+
with pytest.raises(errs, match=msg):
|
204 |
+
ops("foo")
|
205 |
+
with pytest.raises(errs, match=msg):
|
206 |
+
ops(pd.Timestamp("20180101"))
|
207 |
+
|
208 |
+
# invalid array-likes
|
209 |
+
str_ser = pd.Series("foo", index=s.index)
|
210 |
+
# with pytest.raises(TypeError, match=msg):
|
211 |
+
if (
|
212 |
+
all_arithmetic_operators
|
213 |
+
in [
|
214 |
+
"__mul__",
|
215 |
+
"__rmul__",
|
216 |
+
]
|
217 |
+
and not using_infer_string
|
218 |
+
): # (data[~data.isna()] >= 0).all():
|
219 |
+
res = ops(str_ser)
|
220 |
+
expected = pd.Series(["foo" * x for x in data], index=s.index)
|
221 |
+
expected = expected.fillna(np.nan)
|
222 |
+
# TODO: doing this fillna to keep tests passing as we make
|
223 |
+
# assert_almost_equal stricter, but the expected with pd.NA seems
|
224 |
+
# more-correct than np.nan here.
|
225 |
+
tm.assert_series_equal(res, expected)
|
226 |
+
else:
|
227 |
+
with pytest.raises(errs, match=msg):
|
228 |
+
ops(str_ser)
|
229 |
+
|
230 |
+
msg = "|".join(
|
231 |
+
[
|
232 |
+
"can only perform ops with numeric values",
|
233 |
+
"cannot perform .* with this index type: DatetimeArray",
|
234 |
+
"Addition/subtraction of integers and integer-arrays "
|
235 |
+
"with DatetimeArray is no longer supported. *",
|
236 |
+
"unsupported operand type",
|
237 |
+
r"can only concatenate str \(not \"int\"\) to str",
|
238 |
+
"not all arguments converted during string",
|
239 |
+
"cannot subtract DatetimeArray from ndarray",
|
240 |
+
"has no kernel",
|
241 |
+
"not implemented",
|
242 |
+
]
|
243 |
+
)
|
244 |
+
with pytest.raises(errs, match=msg):
|
245 |
+
ops(pd.Series(pd.date_range("20180101", periods=len(s))))
|
246 |
+
|
247 |
+
|
248 |
+
# Various
|
249 |
+
# -----------------------------------------------------------------------------
|
250 |
+
|
251 |
+
|
252 |
+
# TODO test unsigned overflow
|
253 |
+
|
254 |
+
|
255 |
+
def test_arith_coerce_scalar(data, all_arithmetic_operators):
|
256 |
+
op = tm.get_op_from_name(all_arithmetic_operators)
|
257 |
+
s = pd.Series(data)
|
258 |
+
other = 0.01
|
259 |
+
|
260 |
+
result = op(s, other)
|
261 |
+
expected = op(s.astype(float), other)
|
262 |
+
expected = expected.astype("Float64")
|
263 |
+
|
264 |
+
# rmod results in NaN that wasn't NA in original nullable Series -> unmask it
|
265 |
+
if all_arithmetic_operators == "__rmod__":
|
266 |
+
mask = (s == 0).fillna(False).to_numpy(bool)
|
267 |
+
expected.array._mask[mask] = False
|
268 |
+
|
269 |
+
tm.assert_series_equal(result, expected)
|
270 |
+
|
271 |
+
|
272 |
+
@pytest.mark.parametrize("other", [1.0, np.array(1.0)])
|
273 |
+
def test_arithmetic_conversion(all_arithmetic_operators, other):
|
274 |
+
# if we have a float operand we should have a float result
|
275 |
+
# if that is equal to an integer
|
276 |
+
op = tm.get_op_from_name(all_arithmetic_operators)
|
277 |
+
|
278 |
+
s = pd.Series([1, 2, 3], dtype="Int64")
|
279 |
+
result = op(s, other)
|
280 |
+
assert result.dtype == "Float64"
|
281 |
+
|
282 |
+
|
283 |
+
def test_cross_type_arithmetic():
|
284 |
+
df = pd.DataFrame(
|
285 |
+
{
|
286 |
+
"A": pd.Series([1, 2, np.nan], dtype="Int64"),
|
287 |
+
"B": pd.Series([1, np.nan, 3], dtype="UInt8"),
|
288 |
+
"C": [1, 2, 3],
|
289 |
+
}
|
290 |
+
)
|
291 |
+
|
292 |
+
result = df.A + df.C
|
293 |
+
expected = pd.Series([2, 4, np.nan], dtype="Int64")
|
294 |
+
tm.assert_series_equal(result, expected)
|
295 |
+
|
296 |
+
result = (df.A + df.C) * 3 == 12
|
297 |
+
expected = pd.Series([False, True, None], dtype="boolean")
|
298 |
+
tm.assert_series_equal(result, expected)
|
299 |
+
|
300 |
+
result = df.A + df.B
|
301 |
+
expected = pd.Series([2, np.nan, np.nan], dtype="Int64")
|
302 |
+
tm.assert_series_equal(result, expected)
|
303 |
+
|
304 |
+
|
305 |
+
@pytest.mark.parametrize("op", ["mean"])
|
306 |
+
def test_reduce_to_float(op):
|
307 |
+
# some reduce ops always return float, even if the result
|
308 |
+
# is a rounded number
|
309 |
+
df = pd.DataFrame(
|
310 |
+
{
|
311 |
+
"A": ["a", "b", "b"],
|
312 |
+
"B": [1, None, 3],
|
313 |
+
"C": pd.array([1, None, 3], dtype="Int64"),
|
314 |
+
}
|
315 |
+
)
|
316 |
+
|
317 |
+
# op
|
318 |
+
result = getattr(df.C, op)()
|
319 |
+
assert isinstance(result, float)
|
320 |
+
|
321 |
+
# groupby
|
322 |
+
result = getattr(df.groupby("A"), op)()
|
323 |
+
|
324 |
+
expected = pd.DataFrame(
|
325 |
+
{"B": np.array([1.0, 3.0]), "C": pd.array([1, 3], dtype="Float64")},
|
326 |
+
index=pd.Index(["a", "b"], name="A"),
|
327 |
+
)
|
328 |
+
tm.assert_frame_equal(result, expected)
|
329 |
+
|
330 |
+
|
331 |
+
@pytest.mark.parametrize(
|
332 |
+
"source, neg_target, abs_target",
|
333 |
+
[
|
334 |
+
([1, 2, 3], [-1, -2, -3], [1, 2, 3]),
|
335 |
+
([1, 2, None], [-1, -2, None], [1, 2, None]),
|
336 |
+
([-1, 0, 1], [1, 0, -1], [1, 0, 1]),
|
337 |
+
],
|
338 |
+
)
|
339 |
+
def test_unary_int_operators(any_signed_int_ea_dtype, source, neg_target, abs_target):
|
340 |
+
dtype = any_signed_int_ea_dtype
|
341 |
+
arr = pd.array(source, dtype=dtype)
|
342 |
+
neg_result, pos_result, abs_result = -arr, +arr, abs(arr)
|
343 |
+
neg_target = pd.array(neg_target, dtype=dtype)
|
344 |
+
abs_target = pd.array(abs_target, dtype=dtype)
|
345 |
+
|
346 |
+
tm.assert_extension_array_equal(neg_result, neg_target)
|
347 |
+
tm.assert_extension_array_equal(pos_result, arr)
|
348 |
+
assert not tm.shares_memory(pos_result, arr)
|
349 |
+
tm.assert_extension_array_equal(abs_result, abs_target)
|
350 |
+
|
351 |
+
|
352 |
+
def test_values_multiplying_large_series_by_NA():
|
353 |
+
# GH#33701
|
354 |
+
|
355 |
+
result = pd.NA * pd.Series(np.zeros(10001))
|
356 |
+
expected = pd.Series([pd.NA] * 10001)
|
357 |
+
|
358 |
+
tm.assert_series_equal(result, expected)
|
359 |
+
|
360 |
+
|
361 |
+
def test_bitwise(dtype):
|
362 |
+
left = pd.array([1, None, 3, 4], dtype=dtype)
|
363 |
+
right = pd.array([None, 3, 5, 4], dtype=dtype)
|
364 |
+
|
365 |
+
result = left | right
|
366 |
+
expected = pd.array([None, None, 3 | 5, 4 | 4], dtype=dtype)
|
367 |
+
tm.assert_extension_array_equal(result, expected)
|
368 |
+
|
369 |
+
result = left & right
|
370 |
+
expected = pd.array([None, None, 3 & 5, 4 & 4], dtype=dtype)
|
371 |
+
tm.assert_extension_array_equal(result, expected)
|
372 |
+
|
373 |
+
result = left ^ right
|
374 |
+
expected = pd.array([None, None, 3 ^ 5, 4 ^ 4], dtype=dtype)
|
375 |
+
tm.assert_extension_array_equal(result, expected)
|
376 |
+
|
377 |
+
# TODO: desired behavior when operating with boolean? defer?
|
378 |
+
|
379 |
+
floats = right.astype("Float64")
|
380 |
+
with pytest.raises(TypeError, match="unsupported operand type"):
|
381 |
+
left | floats
|
382 |
+
with pytest.raises(TypeError, match="unsupported operand type"):
|
383 |
+
left & floats
|
384 |
+
with pytest.raises(TypeError, match="unsupported operand type"):
|
385 |
+
left ^ floats
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_comparison.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import pandas as pd
|
4 |
+
import pandas._testing as tm
|
5 |
+
from pandas.tests.arrays.masked_shared import (
|
6 |
+
ComparisonOps,
|
7 |
+
NumericOps,
|
8 |
+
)
|
9 |
+
|
10 |
+
|
11 |
+
class TestComparisonOps(NumericOps, ComparisonOps):
|
12 |
+
@pytest.mark.parametrize("other", [True, False, pd.NA, -1, 0, 1])
|
13 |
+
def test_scalar(self, other, comparison_op, dtype):
|
14 |
+
ComparisonOps.test_scalar(self, other, comparison_op, dtype)
|
15 |
+
|
16 |
+
def test_compare_to_int(self, dtype, comparison_op):
|
17 |
+
# GH 28930
|
18 |
+
op_name = f"__{comparison_op.__name__}__"
|
19 |
+
s1 = pd.Series([1, None, 3], dtype=dtype)
|
20 |
+
s2 = pd.Series([1, None, 3], dtype="float")
|
21 |
+
|
22 |
+
method = getattr(s1, op_name)
|
23 |
+
result = method(2)
|
24 |
+
|
25 |
+
method = getattr(s2, op_name)
|
26 |
+
expected = method(2).astype("boolean")
|
27 |
+
expected[s2.isna()] = pd.NA
|
28 |
+
|
29 |
+
tm.assert_series_equal(result, expected)
|
30 |
+
|
31 |
+
|
32 |
+
def test_equals():
|
33 |
+
# GH-30652
|
34 |
+
# equals is generally tested in /tests/extension/base/methods, but this
|
35 |
+
# specifically tests that two arrays of the same class but different dtype
|
36 |
+
# do not evaluate equal
|
37 |
+
a1 = pd.array([1, 2, None], dtype="Int64")
|
38 |
+
a2 = pd.array([1, 2, None], dtype="Int32")
|
39 |
+
assert a1.equals(a2) is False
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_concat.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"to_concat_dtypes, result_dtype",
|
10 |
+
[
|
11 |
+
(["Int64", "Int64"], "Int64"),
|
12 |
+
(["UInt64", "UInt64"], "UInt64"),
|
13 |
+
(["Int8", "Int8"], "Int8"),
|
14 |
+
(["Int8", "Int16"], "Int16"),
|
15 |
+
(["UInt8", "Int8"], "Int16"),
|
16 |
+
(["Int32", "UInt32"], "Int64"),
|
17 |
+
(["Int64", "UInt64"], "Float64"),
|
18 |
+
(["Int64", "boolean"], "object"),
|
19 |
+
(["UInt8", "boolean"], "object"),
|
20 |
+
],
|
21 |
+
)
|
22 |
+
def test_concat_series(to_concat_dtypes, result_dtype):
|
23 |
+
# we expect the same dtypes as we would get with non-masked inputs,
|
24 |
+
# just masked where available.
|
25 |
+
|
26 |
+
result = pd.concat([pd.Series([0, 1, pd.NA], dtype=t) for t in to_concat_dtypes])
|
27 |
+
expected = pd.concat([pd.Series([0, 1, pd.NA], dtype=object)] * 2).astype(
|
28 |
+
result_dtype
|
29 |
+
)
|
30 |
+
tm.assert_series_equal(result, expected)
|
31 |
+
|
32 |
+
# order doesn't matter for result
|
33 |
+
result = pd.concat(
|
34 |
+
[pd.Series([0, 1, pd.NA], dtype=t) for t in to_concat_dtypes[::-1]]
|
35 |
+
)
|
36 |
+
expected = pd.concat([pd.Series([0, 1, pd.NA], dtype=object)] * 2).astype(
|
37 |
+
result_dtype
|
38 |
+
)
|
39 |
+
tm.assert_series_equal(result, expected)
|
40 |
+
|
41 |
+
|
42 |
+
@pytest.mark.parametrize(
|
43 |
+
"to_concat_dtypes, result_dtype",
|
44 |
+
[
|
45 |
+
(["Int64", "int64"], "Int64"),
|
46 |
+
(["UInt64", "uint64"], "UInt64"),
|
47 |
+
(["Int8", "int8"], "Int8"),
|
48 |
+
(["Int8", "int16"], "Int16"),
|
49 |
+
(["UInt8", "int8"], "Int16"),
|
50 |
+
(["Int32", "uint32"], "Int64"),
|
51 |
+
(["Int64", "uint64"], "Float64"),
|
52 |
+
(["Int64", "bool"], "object"),
|
53 |
+
(["UInt8", "bool"], "object"),
|
54 |
+
],
|
55 |
+
)
|
56 |
+
def test_concat_series_with_numpy(to_concat_dtypes, result_dtype):
|
57 |
+
# we expect the same dtypes as we would get with non-masked inputs,
|
58 |
+
# just masked where available.
|
59 |
+
|
60 |
+
s1 = pd.Series([0, 1, pd.NA], dtype=to_concat_dtypes[0])
|
61 |
+
s2 = pd.Series(np.array([0, 1], dtype=to_concat_dtypes[1]))
|
62 |
+
result = pd.concat([s1, s2], ignore_index=True)
|
63 |
+
expected = pd.Series([0, 1, pd.NA, 0, 1], dtype=object).astype(result_dtype)
|
64 |
+
tm.assert_series_equal(result, expected)
|
65 |
+
|
66 |
+
# order doesn't matter for result
|
67 |
+
result = pd.concat([s2, s1], ignore_index=True)
|
68 |
+
expected = pd.Series([0, 1, 0, 1, pd.NA], dtype=object).astype(result_dtype)
|
69 |
+
tm.assert_series_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_construction.py
ADDED
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
import pandas._testing as tm
|
6 |
+
from pandas.api.types import is_integer
|
7 |
+
from pandas.core.arrays import IntegerArray
|
8 |
+
from pandas.core.arrays.integer import (
|
9 |
+
Int8Dtype,
|
10 |
+
Int32Dtype,
|
11 |
+
Int64Dtype,
|
12 |
+
)
|
13 |
+
|
14 |
+
|
15 |
+
@pytest.fixture(params=[pd.array, IntegerArray._from_sequence])
|
16 |
+
def constructor(request):
|
17 |
+
"""Fixture returning parametrized IntegerArray from given sequence.
|
18 |
+
|
19 |
+
Used to test dtype conversions.
|
20 |
+
"""
|
21 |
+
return request.param
|
22 |
+
|
23 |
+
|
24 |
+
def test_uses_pandas_na():
|
25 |
+
a = pd.array([1, None], dtype=Int64Dtype())
|
26 |
+
assert a[1] is pd.NA
|
27 |
+
|
28 |
+
|
29 |
+
def test_from_dtype_from_float(data):
|
30 |
+
# construct from our dtype & string dtype
|
31 |
+
dtype = data.dtype
|
32 |
+
|
33 |
+
# from float
|
34 |
+
expected = pd.Series(data)
|
35 |
+
result = pd.Series(data.to_numpy(na_value=np.nan, dtype="float"), dtype=str(dtype))
|
36 |
+
tm.assert_series_equal(result, expected)
|
37 |
+
|
38 |
+
# from int / list
|
39 |
+
expected = pd.Series(data)
|
40 |
+
result = pd.Series(np.array(data).tolist(), dtype=str(dtype))
|
41 |
+
tm.assert_series_equal(result, expected)
|
42 |
+
|
43 |
+
# from int / array
|
44 |
+
expected = pd.Series(data).dropna().reset_index(drop=True)
|
45 |
+
dropped = np.array(data.dropna()).astype(np.dtype(dtype.type))
|
46 |
+
result = pd.Series(dropped, dtype=str(dtype))
|
47 |
+
tm.assert_series_equal(result, expected)
|
48 |
+
|
49 |
+
|
50 |
+
def test_conversions(data_missing):
|
51 |
+
# astype to object series
|
52 |
+
df = pd.DataFrame({"A": data_missing})
|
53 |
+
result = df["A"].astype("object")
|
54 |
+
expected = pd.Series(np.array([pd.NA, 1], dtype=object), name="A")
|
55 |
+
tm.assert_series_equal(result, expected)
|
56 |
+
|
57 |
+
# convert to object ndarray
|
58 |
+
# we assert that we are exactly equal
|
59 |
+
# including type conversions of scalars
|
60 |
+
result = df["A"].astype("object").values
|
61 |
+
expected = np.array([pd.NA, 1], dtype=object)
|
62 |
+
tm.assert_numpy_array_equal(result, expected)
|
63 |
+
|
64 |
+
for r, e in zip(result, expected):
|
65 |
+
if pd.isnull(r):
|
66 |
+
assert pd.isnull(e)
|
67 |
+
elif is_integer(r):
|
68 |
+
assert r == e
|
69 |
+
assert is_integer(e)
|
70 |
+
else:
|
71 |
+
assert r == e
|
72 |
+
assert type(r) == type(e)
|
73 |
+
|
74 |
+
|
75 |
+
def test_integer_array_constructor():
|
76 |
+
values = np.array([1, 2, 3, 4], dtype="int64")
|
77 |
+
mask = np.array([False, False, False, True], dtype="bool")
|
78 |
+
|
79 |
+
result = IntegerArray(values, mask)
|
80 |
+
expected = pd.array([1, 2, 3, np.nan], dtype="Int64")
|
81 |
+
tm.assert_extension_array_equal(result, expected)
|
82 |
+
|
83 |
+
msg = r".* should be .* numpy array. Use the 'pd.array' function instead"
|
84 |
+
with pytest.raises(TypeError, match=msg):
|
85 |
+
IntegerArray(values.tolist(), mask)
|
86 |
+
|
87 |
+
with pytest.raises(TypeError, match=msg):
|
88 |
+
IntegerArray(values, mask.tolist())
|
89 |
+
|
90 |
+
with pytest.raises(TypeError, match=msg):
|
91 |
+
IntegerArray(values.astype(float), mask)
|
92 |
+
msg = r"__init__\(\) missing 1 required positional argument: 'mask'"
|
93 |
+
with pytest.raises(TypeError, match=msg):
|
94 |
+
IntegerArray(values)
|
95 |
+
|
96 |
+
|
97 |
+
def test_integer_array_constructor_copy():
|
98 |
+
values = np.array([1, 2, 3, 4], dtype="int64")
|
99 |
+
mask = np.array([False, False, False, True], dtype="bool")
|
100 |
+
|
101 |
+
result = IntegerArray(values, mask)
|
102 |
+
assert result._data is values
|
103 |
+
assert result._mask is mask
|
104 |
+
|
105 |
+
result = IntegerArray(values, mask, copy=True)
|
106 |
+
assert result._data is not values
|
107 |
+
assert result._mask is not mask
|
108 |
+
|
109 |
+
|
110 |
+
@pytest.mark.parametrize(
|
111 |
+
"a, b",
|
112 |
+
[
|
113 |
+
([1, None], [1, np.nan]),
|
114 |
+
([None], [np.nan]),
|
115 |
+
([None, np.nan], [np.nan, np.nan]),
|
116 |
+
([np.nan, np.nan], [np.nan, np.nan]),
|
117 |
+
],
|
118 |
+
)
|
119 |
+
def test_to_integer_array_none_is_nan(a, b):
|
120 |
+
result = pd.array(a, dtype="Int64")
|
121 |
+
expected = pd.array(b, dtype="Int64")
|
122 |
+
tm.assert_extension_array_equal(result, expected)
|
123 |
+
|
124 |
+
|
125 |
+
@pytest.mark.parametrize(
|
126 |
+
"values",
|
127 |
+
[
|
128 |
+
["foo", "bar"],
|
129 |
+
"foo",
|
130 |
+
1,
|
131 |
+
1.0,
|
132 |
+
pd.date_range("20130101", periods=2),
|
133 |
+
np.array(["foo"]),
|
134 |
+
[[1, 2], [3, 4]],
|
135 |
+
[np.nan, {"a": 1}],
|
136 |
+
],
|
137 |
+
)
|
138 |
+
def test_to_integer_array_error(values):
|
139 |
+
# error in converting existing arrays to IntegerArrays
|
140 |
+
msg = "|".join(
|
141 |
+
[
|
142 |
+
r"cannot be converted to IntegerDtype",
|
143 |
+
r"invalid literal for int\(\) with base 10:",
|
144 |
+
r"values must be a 1D list-like",
|
145 |
+
r"Cannot pass scalar",
|
146 |
+
r"int\(\) argument must be a string",
|
147 |
+
]
|
148 |
+
)
|
149 |
+
with pytest.raises((ValueError, TypeError), match=msg):
|
150 |
+
pd.array(values, dtype="Int64")
|
151 |
+
|
152 |
+
with pytest.raises((ValueError, TypeError), match=msg):
|
153 |
+
IntegerArray._from_sequence(values)
|
154 |
+
|
155 |
+
|
156 |
+
def test_to_integer_array_inferred_dtype(constructor):
|
157 |
+
# if values has dtype -> respect it
|
158 |
+
result = constructor(np.array([1, 2], dtype="int8"))
|
159 |
+
assert result.dtype == Int8Dtype()
|
160 |
+
result = constructor(np.array([1, 2], dtype="int32"))
|
161 |
+
assert result.dtype == Int32Dtype()
|
162 |
+
|
163 |
+
# if values have no dtype -> always int64
|
164 |
+
result = constructor([1, 2])
|
165 |
+
assert result.dtype == Int64Dtype()
|
166 |
+
|
167 |
+
|
168 |
+
def test_to_integer_array_dtype_keyword(constructor):
|
169 |
+
result = constructor([1, 2], dtype="Int8")
|
170 |
+
assert result.dtype == Int8Dtype()
|
171 |
+
|
172 |
+
# if values has dtype -> override it
|
173 |
+
result = constructor(np.array([1, 2], dtype="int8"), dtype="Int32")
|
174 |
+
assert result.dtype == Int32Dtype()
|
175 |
+
|
176 |
+
|
177 |
+
def test_to_integer_array_float():
|
178 |
+
result = IntegerArray._from_sequence([1.0, 2.0], dtype="Int64")
|
179 |
+
expected = pd.array([1, 2], dtype="Int64")
|
180 |
+
tm.assert_extension_array_equal(result, expected)
|
181 |
+
|
182 |
+
with pytest.raises(TypeError, match="cannot safely cast non-equivalent"):
|
183 |
+
IntegerArray._from_sequence([1.5, 2.0], dtype="Int64")
|
184 |
+
|
185 |
+
# for float dtypes, the itemsize is not preserved
|
186 |
+
result = IntegerArray._from_sequence(
|
187 |
+
np.array([1.0, 2.0], dtype="float32"), dtype="Int64"
|
188 |
+
)
|
189 |
+
assert result.dtype == Int64Dtype()
|
190 |
+
|
191 |
+
|
192 |
+
def test_to_integer_array_str():
|
193 |
+
result = IntegerArray._from_sequence(["1", "2", None], dtype="Int64")
|
194 |
+
expected = pd.array([1, 2, np.nan], dtype="Int64")
|
195 |
+
tm.assert_extension_array_equal(result, expected)
|
196 |
+
|
197 |
+
with pytest.raises(
|
198 |
+
ValueError, match=r"invalid literal for int\(\) with base 10: .*"
|
199 |
+
):
|
200 |
+
IntegerArray._from_sequence(["1", "2", ""], dtype="Int64")
|
201 |
+
|
202 |
+
with pytest.raises(
|
203 |
+
ValueError, match=r"invalid literal for int\(\) with base 10: .*"
|
204 |
+
):
|
205 |
+
IntegerArray._from_sequence(["1.5", "2.0"], dtype="Int64")
|
206 |
+
|
207 |
+
|
208 |
+
@pytest.mark.parametrize(
|
209 |
+
"bool_values, int_values, target_dtype, expected_dtype",
|
210 |
+
[
|
211 |
+
([False, True], [0, 1], Int64Dtype(), Int64Dtype()),
|
212 |
+
([False, True], [0, 1], "Int64", Int64Dtype()),
|
213 |
+
([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype()),
|
214 |
+
],
|
215 |
+
)
|
216 |
+
def test_to_integer_array_bool(
|
217 |
+
constructor, bool_values, int_values, target_dtype, expected_dtype
|
218 |
+
):
|
219 |
+
result = constructor(bool_values, dtype=target_dtype)
|
220 |
+
assert result.dtype == expected_dtype
|
221 |
+
expected = pd.array(int_values, dtype=target_dtype)
|
222 |
+
tm.assert_extension_array_equal(result, expected)
|
223 |
+
|
224 |
+
|
225 |
+
@pytest.mark.parametrize(
|
226 |
+
"values, to_dtype, result_dtype",
|
227 |
+
[
|
228 |
+
(np.array([1], dtype="int64"), None, Int64Dtype),
|
229 |
+
(np.array([1, np.nan]), None, Int64Dtype),
|
230 |
+
(np.array([1, np.nan]), "int8", Int8Dtype),
|
231 |
+
],
|
232 |
+
)
|
233 |
+
def test_to_integer_array(values, to_dtype, result_dtype):
|
234 |
+
# convert existing arrays to IntegerArrays
|
235 |
+
result = IntegerArray._from_sequence(values, dtype=to_dtype)
|
236 |
+
assert result.dtype == result_dtype()
|
237 |
+
expected = pd.array(values, dtype=result_dtype())
|
238 |
+
tm.assert_extension_array_equal(result, expected)
|
239 |
+
|
240 |
+
|
241 |
+
def test_integer_array_from_boolean():
|
242 |
+
# GH31104
|
243 |
+
expected = pd.array(np.array([True, False]), dtype="Int64")
|
244 |
+
result = pd.array(np.array([True, False], dtype=object), dtype="Int64")
|
245 |
+
tm.assert_extension_array_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_dtypes.py
ADDED
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas.core.dtypes.generic import ABCIndex
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
import pandas._testing as tm
|
8 |
+
from pandas.core.arrays.integer import (
|
9 |
+
Int8Dtype,
|
10 |
+
UInt32Dtype,
|
11 |
+
)
|
12 |
+
|
13 |
+
|
14 |
+
def test_dtypes(dtype):
|
15 |
+
# smoke tests on auto dtype construction
|
16 |
+
|
17 |
+
if dtype.is_signed_integer:
|
18 |
+
assert np.dtype(dtype.type).kind == "i"
|
19 |
+
else:
|
20 |
+
assert np.dtype(dtype.type).kind == "u"
|
21 |
+
assert dtype.name is not None
|
22 |
+
|
23 |
+
|
24 |
+
@pytest.mark.parametrize("op", ["sum", "min", "max", "prod"])
|
25 |
+
def test_preserve_dtypes(op):
|
26 |
+
# for ops that enable (mean would actually work here
|
27 |
+
# but generally it is a float return value)
|
28 |
+
df = pd.DataFrame(
|
29 |
+
{
|
30 |
+
"A": ["a", "b", "b"],
|
31 |
+
"B": [1, None, 3],
|
32 |
+
"C": pd.array([1, None, 3], dtype="Int64"),
|
33 |
+
}
|
34 |
+
)
|
35 |
+
|
36 |
+
# op
|
37 |
+
result = getattr(df.C, op)()
|
38 |
+
if op in {"sum", "prod", "min", "max"}:
|
39 |
+
assert isinstance(result, np.int64)
|
40 |
+
else:
|
41 |
+
assert isinstance(result, int)
|
42 |
+
|
43 |
+
# groupby
|
44 |
+
result = getattr(df.groupby("A"), op)()
|
45 |
+
|
46 |
+
expected = pd.DataFrame(
|
47 |
+
{"B": np.array([1.0, 3.0]), "C": pd.array([1, 3], dtype="Int64")},
|
48 |
+
index=pd.Index(["a", "b"], name="A"),
|
49 |
+
)
|
50 |
+
tm.assert_frame_equal(result, expected)
|
51 |
+
|
52 |
+
|
53 |
+
def test_astype_nansafe():
|
54 |
+
# see gh-22343
|
55 |
+
arr = pd.array([np.nan, 1, 2], dtype="Int8")
|
56 |
+
msg = "cannot convert NA to integer"
|
57 |
+
|
58 |
+
with pytest.raises(ValueError, match=msg):
|
59 |
+
arr.astype("uint32")
|
60 |
+
|
61 |
+
|
62 |
+
@pytest.mark.parametrize("dropna", [True, False])
|
63 |
+
def test_construct_index(all_data, dropna):
|
64 |
+
# ensure that we do not coerce to different Index dtype or non-index
|
65 |
+
|
66 |
+
all_data = all_data[:10]
|
67 |
+
if dropna:
|
68 |
+
other = np.array(all_data[~all_data.isna()])
|
69 |
+
else:
|
70 |
+
other = all_data
|
71 |
+
|
72 |
+
result = pd.Index(pd.array(other, dtype=all_data.dtype))
|
73 |
+
expected = pd.Index(other, dtype=all_data.dtype)
|
74 |
+
assert all_data.dtype == expected.dtype # dont coerce to object
|
75 |
+
|
76 |
+
tm.assert_index_equal(result, expected)
|
77 |
+
|
78 |
+
|
79 |
+
@pytest.mark.parametrize("dropna", [True, False])
|
80 |
+
def test_astype_index(all_data, dropna):
|
81 |
+
# as an int/uint index to Index
|
82 |
+
|
83 |
+
all_data = all_data[:10]
|
84 |
+
if dropna:
|
85 |
+
other = all_data[~all_data.isna()]
|
86 |
+
else:
|
87 |
+
other = all_data
|
88 |
+
|
89 |
+
dtype = all_data.dtype
|
90 |
+
idx = pd.Index(np.array(other))
|
91 |
+
assert isinstance(idx, ABCIndex)
|
92 |
+
|
93 |
+
result = idx.astype(dtype)
|
94 |
+
expected = idx.astype(object).astype(dtype)
|
95 |
+
tm.assert_index_equal(result, expected)
|
96 |
+
|
97 |
+
|
98 |
+
def test_astype(all_data):
|
99 |
+
all_data = all_data[:10]
|
100 |
+
|
101 |
+
ints = all_data[~all_data.isna()]
|
102 |
+
mixed = all_data
|
103 |
+
dtype = Int8Dtype()
|
104 |
+
|
105 |
+
# coerce to same type - ints
|
106 |
+
s = pd.Series(ints)
|
107 |
+
result = s.astype(all_data.dtype)
|
108 |
+
expected = pd.Series(ints)
|
109 |
+
tm.assert_series_equal(result, expected)
|
110 |
+
|
111 |
+
# coerce to same other - ints
|
112 |
+
s = pd.Series(ints)
|
113 |
+
result = s.astype(dtype)
|
114 |
+
expected = pd.Series(ints, dtype=dtype)
|
115 |
+
tm.assert_series_equal(result, expected)
|
116 |
+
|
117 |
+
# coerce to same numpy_dtype - ints
|
118 |
+
s = pd.Series(ints)
|
119 |
+
result = s.astype(all_data.dtype.numpy_dtype)
|
120 |
+
expected = pd.Series(ints._data.astype(all_data.dtype.numpy_dtype))
|
121 |
+
tm.assert_series_equal(result, expected)
|
122 |
+
|
123 |
+
# coerce to same type - mixed
|
124 |
+
s = pd.Series(mixed)
|
125 |
+
result = s.astype(all_data.dtype)
|
126 |
+
expected = pd.Series(mixed)
|
127 |
+
tm.assert_series_equal(result, expected)
|
128 |
+
|
129 |
+
# coerce to same other - mixed
|
130 |
+
s = pd.Series(mixed)
|
131 |
+
result = s.astype(dtype)
|
132 |
+
expected = pd.Series(mixed, dtype=dtype)
|
133 |
+
tm.assert_series_equal(result, expected)
|
134 |
+
|
135 |
+
# coerce to same numpy_dtype - mixed
|
136 |
+
s = pd.Series(mixed)
|
137 |
+
msg = "cannot convert NA to integer"
|
138 |
+
with pytest.raises(ValueError, match=msg):
|
139 |
+
s.astype(all_data.dtype.numpy_dtype)
|
140 |
+
|
141 |
+
# coerce to object
|
142 |
+
s = pd.Series(mixed)
|
143 |
+
result = s.astype("object")
|
144 |
+
expected = pd.Series(np.asarray(mixed, dtype=object))
|
145 |
+
tm.assert_series_equal(result, expected)
|
146 |
+
|
147 |
+
|
148 |
+
def test_astype_copy():
|
149 |
+
arr = pd.array([1, 2, 3, None], dtype="Int64")
|
150 |
+
orig = pd.array([1, 2, 3, None], dtype="Int64")
|
151 |
+
|
152 |
+
# copy=True -> ensure both data and mask are actual copies
|
153 |
+
result = arr.astype("Int64", copy=True)
|
154 |
+
assert result is not arr
|
155 |
+
assert not tm.shares_memory(result, arr)
|
156 |
+
result[0] = 10
|
157 |
+
tm.assert_extension_array_equal(arr, orig)
|
158 |
+
result[0] = pd.NA
|
159 |
+
tm.assert_extension_array_equal(arr, orig)
|
160 |
+
|
161 |
+
# copy=False
|
162 |
+
result = arr.astype("Int64", copy=False)
|
163 |
+
assert result is arr
|
164 |
+
assert np.shares_memory(result._data, arr._data)
|
165 |
+
assert np.shares_memory(result._mask, arr._mask)
|
166 |
+
result[0] = 10
|
167 |
+
assert arr[0] == 10
|
168 |
+
result[0] = pd.NA
|
169 |
+
assert arr[0] is pd.NA
|
170 |
+
|
171 |
+
# astype to different dtype -> always needs a copy -> even with copy=False
|
172 |
+
# we need to ensure that also the mask is actually copied
|
173 |
+
arr = pd.array([1, 2, 3, None], dtype="Int64")
|
174 |
+
orig = pd.array([1, 2, 3, None], dtype="Int64")
|
175 |
+
|
176 |
+
result = arr.astype("Int32", copy=False)
|
177 |
+
assert not tm.shares_memory(result, arr)
|
178 |
+
result[0] = 10
|
179 |
+
tm.assert_extension_array_equal(arr, orig)
|
180 |
+
result[0] = pd.NA
|
181 |
+
tm.assert_extension_array_equal(arr, orig)
|
182 |
+
|
183 |
+
|
184 |
+
def test_astype_to_larger_numpy():
|
185 |
+
a = pd.array([1, 2], dtype="Int32")
|
186 |
+
result = a.astype("int64")
|
187 |
+
expected = np.array([1, 2], dtype="int64")
|
188 |
+
tm.assert_numpy_array_equal(result, expected)
|
189 |
+
|
190 |
+
a = pd.array([1, 2], dtype="UInt32")
|
191 |
+
result = a.astype("uint64")
|
192 |
+
expected = np.array([1, 2], dtype="uint64")
|
193 |
+
tm.assert_numpy_array_equal(result, expected)
|
194 |
+
|
195 |
+
|
196 |
+
@pytest.mark.parametrize("dtype", [Int8Dtype(), "Int8", UInt32Dtype(), "UInt32"])
|
197 |
+
def test_astype_specific_casting(dtype):
|
198 |
+
s = pd.Series([1, 2, 3], dtype="Int64")
|
199 |
+
result = s.astype(dtype)
|
200 |
+
expected = pd.Series([1, 2, 3], dtype=dtype)
|
201 |
+
tm.assert_series_equal(result, expected)
|
202 |
+
|
203 |
+
s = pd.Series([1, 2, 3, None], dtype="Int64")
|
204 |
+
result = s.astype(dtype)
|
205 |
+
expected = pd.Series([1, 2, 3, None], dtype=dtype)
|
206 |
+
tm.assert_series_equal(result, expected)
|
207 |
+
|
208 |
+
|
209 |
+
def test_astype_floating():
|
210 |
+
arr = pd.array([1, 2, None], dtype="Int64")
|
211 |
+
result = arr.astype("Float64")
|
212 |
+
expected = pd.array([1.0, 2.0, None], dtype="Float64")
|
213 |
+
tm.assert_extension_array_equal(result, expected)
|
214 |
+
|
215 |
+
|
216 |
+
def test_astype_dt64():
|
217 |
+
# GH#32435
|
218 |
+
arr = pd.array([1, 2, 3, pd.NA]) * 10**9
|
219 |
+
|
220 |
+
result = arr.astype("datetime64[ns]")
|
221 |
+
|
222 |
+
expected = np.array([1, 2, 3, "NaT"], dtype="M8[s]").astype("M8[ns]")
|
223 |
+
tm.assert_numpy_array_equal(result, expected)
|
224 |
+
|
225 |
+
|
226 |
+
def test_construct_cast_invalid(dtype):
|
227 |
+
msg = "cannot safely"
|
228 |
+
arr = [1.2, 2.3, 3.7]
|
229 |
+
with pytest.raises(TypeError, match=msg):
|
230 |
+
pd.array(arr, dtype=dtype)
|
231 |
+
|
232 |
+
with pytest.raises(TypeError, match=msg):
|
233 |
+
pd.Series(arr).astype(dtype)
|
234 |
+
|
235 |
+
arr = [1.2, 2.3, 3.7, np.nan]
|
236 |
+
with pytest.raises(TypeError, match=msg):
|
237 |
+
pd.array(arr, dtype=dtype)
|
238 |
+
|
239 |
+
with pytest.raises(TypeError, match=msg):
|
240 |
+
pd.Series(arr).astype(dtype)
|
241 |
+
|
242 |
+
|
243 |
+
@pytest.mark.parametrize("in_series", [True, False])
|
244 |
+
def test_to_numpy_na_nan(in_series):
|
245 |
+
a = pd.array([0, 1, None], dtype="Int64")
|
246 |
+
if in_series:
|
247 |
+
a = pd.Series(a)
|
248 |
+
|
249 |
+
result = a.to_numpy(dtype="float64", na_value=np.nan)
|
250 |
+
expected = np.array([0.0, 1.0, np.nan], dtype="float64")
|
251 |
+
tm.assert_numpy_array_equal(result, expected)
|
252 |
+
|
253 |
+
result = a.to_numpy(dtype="int64", na_value=-1)
|
254 |
+
expected = np.array([0, 1, -1], dtype="int64")
|
255 |
+
tm.assert_numpy_array_equal(result, expected)
|
256 |
+
|
257 |
+
result = a.to_numpy(dtype="bool", na_value=False)
|
258 |
+
expected = np.array([False, True, False], dtype="bool")
|
259 |
+
tm.assert_numpy_array_equal(result, expected)
|
260 |
+
|
261 |
+
|
262 |
+
@pytest.mark.parametrize("in_series", [True, False])
|
263 |
+
@pytest.mark.parametrize("dtype", ["int32", "int64", "bool"])
|
264 |
+
def test_to_numpy_dtype(dtype, in_series):
|
265 |
+
a = pd.array([0, 1], dtype="Int64")
|
266 |
+
if in_series:
|
267 |
+
a = pd.Series(a)
|
268 |
+
|
269 |
+
result = a.to_numpy(dtype=dtype)
|
270 |
+
expected = np.array([0, 1], dtype=dtype)
|
271 |
+
tm.assert_numpy_array_equal(result, expected)
|
272 |
+
|
273 |
+
|
274 |
+
@pytest.mark.parametrize("dtype", ["int64", "bool"])
|
275 |
+
def test_to_numpy_na_raises(dtype):
|
276 |
+
a = pd.array([0, 1, None], dtype="Int64")
|
277 |
+
with pytest.raises(ValueError, match=dtype):
|
278 |
+
a.to_numpy(dtype=dtype)
|
279 |
+
|
280 |
+
|
281 |
+
def test_astype_str():
|
282 |
+
a = pd.array([1, 2, None], dtype="Int64")
|
283 |
+
expected = np.array(["1", "2", "<NA>"], dtype=f"{tm.ENDIAN}U21")
|
284 |
+
|
285 |
+
tm.assert_numpy_array_equal(a.astype(str), expected)
|
286 |
+
tm.assert_numpy_array_equal(a.astype("str"), expected)
|
287 |
+
|
288 |
+
|
289 |
+
def test_astype_boolean():
|
290 |
+
# https://github.com/pandas-dev/pandas/issues/31102
|
291 |
+
a = pd.array([1, 0, -1, 2, None], dtype="Int64")
|
292 |
+
result = a.astype("boolean")
|
293 |
+
expected = pd.array([True, False, True, True, None], dtype="boolean")
|
294 |
+
tm.assert_extension_array_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_function.py
ADDED
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
import pandas._testing as tm
|
6 |
+
from pandas.core.arrays import FloatingArray
|
7 |
+
|
8 |
+
|
9 |
+
@pytest.mark.parametrize("ufunc", [np.abs, np.sign])
|
10 |
+
# np.sign emits a warning with nans, <https://github.com/numpy/numpy/issues/15127>
|
11 |
+
@pytest.mark.filterwarnings("ignore:invalid value encountered in sign:RuntimeWarning")
|
12 |
+
def test_ufuncs_single_int(ufunc):
|
13 |
+
a = pd.array([1, 2, -3, np.nan])
|
14 |
+
result = ufunc(a)
|
15 |
+
expected = pd.array(ufunc(a.astype(float)), dtype="Int64")
|
16 |
+
tm.assert_extension_array_equal(result, expected)
|
17 |
+
|
18 |
+
s = pd.Series(a)
|
19 |
+
result = ufunc(s)
|
20 |
+
expected = pd.Series(pd.array(ufunc(a.astype(float)), dtype="Int64"))
|
21 |
+
tm.assert_series_equal(result, expected)
|
22 |
+
|
23 |
+
|
24 |
+
@pytest.mark.parametrize("ufunc", [np.log, np.exp, np.sin, np.cos, np.sqrt])
|
25 |
+
def test_ufuncs_single_float(ufunc):
|
26 |
+
a = pd.array([1, 2, -3, np.nan])
|
27 |
+
with np.errstate(invalid="ignore"):
|
28 |
+
result = ufunc(a)
|
29 |
+
expected = FloatingArray(ufunc(a.astype(float)), mask=a._mask)
|
30 |
+
tm.assert_extension_array_equal(result, expected)
|
31 |
+
|
32 |
+
s = pd.Series(a)
|
33 |
+
with np.errstate(invalid="ignore"):
|
34 |
+
result = ufunc(s)
|
35 |
+
expected = pd.Series(expected)
|
36 |
+
tm.assert_series_equal(result, expected)
|
37 |
+
|
38 |
+
|
39 |
+
@pytest.mark.parametrize("ufunc", [np.add, np.subtract])
|
40 |
+
def test_ufuncs_binary_int(ufunc):
|
41 |
+
# two IntegerArrays
|
42 |
+
a = pd.array([1, 2, -3, np.nan])
|
43 |
+
result = ufunc(a, a)
|
44 |
+
expected = pd.array(ufunc(a.astype(float), a.astype(float)), dtype="Int64")
|
45 |
+
tm.assert_extension_array_equal(result, expected)
|
46 |
+
|
47 |
+
# IntegerArray with numpy array
|
48 |
+
arr = np.array([1, 2, 3, 4])
|
49 |
+
result = ufunc(a, arr)
|
50 |
+
expected = pd.array(ufunc(a.astype(float), arr), dtype="Int64")
|
51 |
+
tm.assert_extension_array_equal(result, expected)
|
52 |
+
|
53 |
+
result = ufunc(arr, a)
|
54 |
+
expected = pd.array(ufunc(arr, a.astype(float)), dtype="Int64")
|
55 |
+
tm.assert_extension_array_equal(result, expected)
|
56 |
+
|
57 |
+
# IntegerArray with scalar
|
58 |
+
result = ufunc(a, 1)
|
59 |
+
expected = pd.array(ufunc(a.astype(float), 1), dtype="Int64")
|
60 |
+
tm.assert_extension_array_equal(result, expected)
|
61 |
+
|
62 |
+
result = ufunc(1, a)
|
63 |
+
expected = pd.array(ufunc(1, a.astype(float)), dtype="Int64")
|
64 |
+
tm.assert_extension_array_equal(result, expected)
|
65 |
+
|
66 |
+
|
67 |
+
def test_ufunc_binary_output():
|
68 |
+
a = pd.array([1, 2, np.nan])
|
69 |
+
result = np.modf(a)
|
70 |
+
expected = np.modf(a.to_numpy(na_value=np.nan, dtype="float"))
|
71 |
+
expected = (pd.array(expected[0]), pd.array(expected[1]))
|
72 |
+
|
73 |
+
assert isinstance(result, tuple)
|
74 |
+
assert len(result) == 2
|
75 |
+
|
76 |
+
for x, y in zip(result, expected):
|
77 |
+
tm.assert_extension_array_equal(x, y)
|
78 |
+
|
79 |
+
|
80 |
+
@pytest.mark.parametrize("values", [[0, 1], [0, None]])
|
81 |
+
def test_ufunc_reduce_raises(values):
|
82 |
+
arr = pd.array(values)
|
83 |
+
|
84 |
+
res = np.add.reduce(arr)
|
85 |
+
expected = arr.sum(skipna=False)
|
86 |
+
tm.assert_almost_equal(res, expected)
|
87 |
+
|
88 |
+
|
89 |
+
@pytest.mark.parametrize(
|
90 |
+
"pandasmethname, kwargs",
|
91 |
+
[
|
92 |
+
("var", {"ddof": 0}),
|
93 |
+
("var", {"ddof": 1}),
|
94 |
+
("std", {"ddof": 0}),
|
95 |
+
("std", {"ddof": 1}),
|
96 |
+
("kurtosis", {}),
|
97 |
+
("skew", {}),
|
98 |
+
("sem", {}),
|
99 |
+
],
|
100 |
+
)
|
101 |
+
def test_stat_method(pandasmethname, kwargs):
|
102 |
+
s = pd.Series(data=[1, 2, 3, 4, 5, 6, np.nan, np.nan], dtype="Int64")
|
103 |
+
pandasmeth = getattr(s, pandasmethname)
|
104 |
+
result = pandasmeth(**kwargs)
|
105 |
+
s2 = pd.Series(data=[1, 2, 3, 4, 5, 6], dtype="Int64")
|
106 |
+
pandasmeth = getattr(s2, pandasmethname)
|
107 |
+
expected = pandasmeth(**kwargs)
|
108 |
+
assert expected == result
|
109 |
+
|
110 |
+
|
111 |
+
def test_value_counts_na():
|
112 |
+
arr = pd.array([1, 2, 1, pd.NA], dtype="Int64")
|
113 |
+
result = arr.value_counts(dropna=False)
|
114 |
+
ex_index = pd.Index([1, 2, pd.NA], dtype="Int64")
|
115 |
+
assert ex_index.dtype == "Int64"
|
116 |
+
expected = pd.Series([2, 1, 1], index=ex_index, dtype="Int64", name="count")
|
117 |
+
tm.assert_series_equal(result, expected)
|
118 |
+
|
119 |
+
result = arr.value_counts(dropna=True)
|
120 |
+
expected = pd.Series([2, 1], index=arr[:2], dtype="Int64", name="count")
|
121 |
+
assert expected.index.dtype == arr.dtype
|
122 |
+
tm.assert_series_equal(result, expected)
|
123 |
+
|
124 |
+
|
125 |
+
def test_value_counts_empty():
|
126 |
+
# https://github.com/pandas-dev/pandas/issues/33317
|
127 |
+
ser = pd.Series([], dtype="Int64")
|
128 |
+
result = ser.value_counts()
|
129 |
+
idx = pd.Index([], dtype=ser.dtype)
|
130 |
+
assert idx.dtype == ser.dtype
|
131 |
+
expected = pd.Series([], index=idx, dtype="Int64", name="count")
|
132 |
+
tm.assert_series_equal(result, expected)
|
133 |
+
|
134 |
+
|
135 |
+
def test_value_counts_with_normalize():
|
136 |
+
# GH 33172
|
137 |
+
ser = pd.Series([1, 2, 1, pd.NA], dtype="Int64")
|
138 |
+
result = ser.value_counts(normalize=True)
|
139 |
+
expected = pd.Series([2, 1], index=ser[:2], dtype="Float64", name="proportion") / 3
|
140 |
+
assert expected.index.dtype == ser.dtype
|
141 |
+
tm.assert_series_equal(result, expected)
|
142 |
+
|
143 |
+
|
144 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
145 |
+
@pytest.mark.parametrize("min_count", [0, 4])
|
146 |
+
def test_integer_array_sum(skipna, min_count, any_int_ea_dtype):
|
147 |
+
dtype = any_int_ea_dtype
|
148 |
+
arr = pd.array([1, 2, 3, None], dtype=dtype)
|
149 |
+
result = arr.sum(skipna=skipna, min_count=min_count)
|
150 |
+
if skipna and min_count == 0:
|
151 |
+
assert result == 6
|
152 |
+
else:
|
153 |
+
assert result is pd.NA
|
154 |
+
|
155 |
+
|
156 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
157 |
+
@pytest.mark.parametrize("method", ["min", "max"])
|
158 |
+
def test_integer_array_min_max(skipna, method, any_int_ea_dtype):
|
159 |
+
dtype = any_int_ea_dtype
|
160 |
+
arr = pd.array([0, 1, None], dtype=dtype)
|
161 |
+
func = getattr(arr, method)
|
162 |
+
result = func(skipna=skipna)
|
163 |
+
if skipna:
|
164 |
+
assert result == (0 if method == "min" else 1)
|
165 |
+
else:
|
166 |
+
assert result is pd.NA
|
167 |
+
|
168 |
+
|
169 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
170 |
+
@pytest.mark.parametrize("min_count", [0, 9])
|
171 |
+
def test_integer_array_prod(skipna, min_count, any_int_ea_dtype):
|
172 |
+
dtype = any_int_ea_dtype
|
173 |
+
arr = pd.array([1, 2, None], dtype=dtype)
|
174 |
+
result = arr.prod(skipna=skipna, min_count=min_count)
|
175 |
+
if skipna and min_count == 0:
|
176 |
+
assert result == 2
|
177 |
+
else:
|
178 |
+
assert result is pd.NA
|
179 |
+
|
180 |
+
|
181 |
+
@pytest.mark.parametrize(
|
182 |
+
"values, expected", [([1, 2, 3], 6), ([1, 2, 3, None], 6), ([None], 0)]
|
183 |
+
)
|
184 |
+
def test_integer_array_numpy_sum(values, expected):
|
185 |
+
arr = pd.array(values, dtype="Int64")
|
186 |
+
result = np.sum(arr)
|
187 |
+
assert result == expected
|
188 |
+
|
189 |
+
|
190 |
+
@pytest.mark.parametrize("op", ["sum", "prod", "min", "max"])
|
191 |
+
def test_dataframe_reductions(op):
|
192 |
+
# https://github.com/pandas-dev/pandas/pull/32867
|
193 |
+
# ensure the integers are not cast to float during reductions
|
194 |
+
df = pd.DataFrame({"a": pd.array([1, 2], dtype="Int64")})
|
195 |
+
result = df.max()
|
196 |
+
assert isinstance(result["a"], np.int64)
|
197 |
+
|
198 |
+
|
199 |
+
# TODO(jreback) - these need testing / are broken
|
200 |
+
|
201 |
+
# shift
|
202 |
+
|
203 |
+
# set_index (destroys type)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_indexing.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import pandas._testing as tm
|
3 |
+
|
4 |
+
|
5 |
+
def test_array_setitem_nullable_boolean_mask():
|
6 |
+
# GH 31446
|
7 |
+
ser = pd.Series([1, 2], dtype="Int64")
|
8 |
+
result = ser.where(ser > 1)
|
9 |
+
expected = pd.Series([pd.NA, 2], dtype="Int64")
|
10 |
+
tm.assert_series_equal(result, expected)
|
11 |
+
|
12 |
+
|
13 |
+
def test_array_setitem():
|
14 |
+
# GH 31446
|
15 |
+
arr = pd.Series([1, 2], dtype="Int64").array
|
16 |
+
arr[arr > 1] = 1
|
17 |
+
|
18 |
+
expected = pd.array([1, 1], dtype="Int64")
|
19 |
+
tm.assert_extension_array_equal(arr, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_reduction.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
from pandas import (
|
6 |
+
DataFrame,
|
7 |
+
Series,
|
8 |
+
array,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
@pytest.mark.parametrize(
|
14 |
+
"op, expected",
|
15 |
+
[
|
16 |
+
["sum", np.int64(3)],
|
17 |
+
["prod", np.int64(2)],
|
18 |
+
["min", np.int64(1)],
|
19 |
+
["max", np.int64(2)],
|
20 |
+
["mean", np.float64(1.5)],
|
21 |
+
["median", np.float64(1.5)],
|
22 |
+
["var", np.float64(0.5)],
|
23 |
+
["std", np.float64(0.5**0.5)],
|
24 |
+
["skew", pd.NA],
|
25 |
+
["kurt", pd.NA],
|
26 |
+
["any", True],
|
27 |
+
["all", True],
|
28 |
+
],
|
29 |
+
)
|
30 |
+
def test_series_reductions(op, expected):
|
31 |
+
ser = Series([1, 2], dtype="Int64")
|
32 |
+
result = getattr(ser, op)()
|
33 |
+
tm.assert_equal(result, expected)
|
34 |
+
|
35 |
+
|
36 |
+
@pytest.mark.parametrize(
|
37 |
+
"op, expected",
|
38 |
+
[
|
39 |
+
["sum", Series([3], index=["a"], dtype="Int64")],
|
40 |
+
["prod", Series([2], index=["a"], dtype="Int64")],
|
41 |
+
["min", Series([1], index=["a"], dtype="Int64")],
|
42 |
+
["max", Series([2], index=["a"], dtype="Int64")],
|
43 |
+
["mean", Series([1.5], index=["a"], dtype="Float64")],
|
44 |
+
["median", Series([1.5], index=["a"], dtype="Float64")],
|
45 |
+
["var", Series([0.5], index=["a"], dtype="Float64")],
|
46 |
+
["std", Series([0.5**0.5], index=["a"], dtype="Float64")],
|
47 |
+
["skew", Series([pd.NA], index=["a"], dtype="Float64")],
|
48 |
+
["kurt", Series([pd.NA], index=["a"], dtype="Float64")],
|
49 |
+
["any", Series([True], index=["a"], dtype="boolean")],
|
50 |
+
["all", Series([True], index=["a"], dtype="boolean")],
|
51 |
+
],
|
52 |
+
)
|
53 |
+
def test_dataframe_reductions(op, expected):
|
54 |
+
df = DataFrame({"a": array([1, 2], dtype="Int64")})
|
55 |
+
result = getattr(df, op)()
|
56 |
+
tm.assert_series_equal(result, expected)
|
57 |
+
|
58 |
+
|
59 |
+
@pytest.mark.parametrize(
|
60 |
+
"op, expected",
|
61 |
+
[
|
62 |
+
["sum", array([1, 3], dtype="Int64")],
|
63 |
+
["prod", array([1, 3], dtype="Int64")],
|
64 |
+
["min", array([1, 3], dtype="Int64")],
|
65 |
+
["max", array([1, 3], dtype="Int64")],
|
66 |
+
["mean", array([1, 3], dtype="Float64")],
|
67 |
+
["median", array([1, 3], dtype="Float64")],
|
68 |
+
["var", array([pd.NA], dtype="Float64")],
|
69 |
+
["std", array([pd.NA], dtype="Float64")],
|
70 |
+
["skew", array([pd.NA], dtype="Float64")],
|
71 |
+
["any", array([True, True], dtype="boolean")],
|
72 |
+
["all", array([True, True], dtype="boolean")],
|
73 |
+
],
|
74 |
+
)
|
75 |
+
def test_groupby_reductions(op, expected):
|
76 |
+
df = DataFrame(
|
77 |
+
{
|
78 |
+
"A": ["a", "b", "b"],
|
79 |
+
"B": array([1, None, 3], dtype="Int64"),
|
80 |
+
}
|
81 |
+
)
|
82 |
+
result = getattr(df.groupby("A"), op)()
|
83 |
+
expected = DataFrame(expected, index=pd.Index(["a", "b"], name="A"), columns=["B"])
|
84 |
+
|
85 |
+
tm.assert_frame_equal(result, expected)
|
86 |
+
|
87 |
+
|
88 |
+
@pytest.mark.parametrize(
|
89 |
+
"op, expected",
|
90 |
+
[
|
91 |
+
["sum", Series([4, 4], index=["B", "C"], dtype="Float64")],
|
92 |
+
["prod", Series([3, 3], index=["B", "C"], dtype="Float64")],
|
93 |
+
["min", Series([1, 1], index=["B", "C"], dtype="Float64")],
|
94 |
+
["max", Series([3, 3], index=["B", "C"], dtype="Float64")],
|
95 |
+
["mean", Series([2, 2], index=["B", "C"], dtype="Float64")],
|
96 |
+
["median", Series([2, 2], index=["B", "C"], dtype="Float64")],
|
97 |
+
["var", Series([2, 2], index=["B", "C"], dtype="Float64")],
|
98 |
+
["std", Series([2**0.5, 2**0.5], index=["B", "C"], dtype="Float64")],
|
99 |
+
["skew", Series([pd.NA, pd.NA], index=["B", "C"], dtype="Float64")],
|
100 |
+
["kurt", Series([pd.NA, pd.NA], index=["B", "C"], dtype="Float64")],
|
101 |
+
["any", Series([True, True, True], index=["A", "B", "C"], dtype="boolean")],
|
102 |
+
["all", Series([True, True, True], index=["A", "B", "C"], dtype="boolean")],
|
103 |
+
],
|
104 |
+
)
|
105 |
+
def test_mixed_reductions(op, expected, using_infer_string):
|
106 |
+
if op in ["any", "all"] and using_infer_string:
|
107 |
+
expected = expected.astype("bool")
|
108 |
+
df = DataFrame(
|
109 |
+
{
|
110 |
+
"A": ["a", "b", "b"],
|
111 |
+
"B": [1, None, 3],
|
112 |
+
"C": array([1, None, 3], dtype="Int64"),
|
113 |
+
}
|
114 |
+
)
|
115 |
+
|
116 |
+
# series
|
117 |
+
result = getattr(df.C, op)()
|
118 |
+
tm.assert_equal(result, expected["C"])
|
119 |
+
|
120 |
+
# frame
|
121 |
+
if op in ["any", "all"]:
|
122 |
+
result = getattr(df, op)()
|
123 |
+
else:
|
124 |
+
result = getattr(df, op)(numeric_only=True)
|
125 |
+
tm.assert_series_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_repr.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
from pandas.core.arrays.integer import (
|
6 |
+
Int8Dtype,
|
7 |
+
Int16Dtype,
|
8 |
+
Int32Dtype,
|
9 |
+
Int64Dtype,
|
10 |
+
UInt8Dtype,
|
11 |
+
UInt16Dtype,
|
12 |
+
UInt32Dtype,
|
13 |
+
UInt64Dtype,
|
14 |
+
)
|
15 |
+
|
16 |
+
|
17 |
+
def test_dtypes(dtype):
|
18 |
+
# smoke tests on auto dtype construction
|
19 |
+
|
20 |
+
if dtype.is_signed_integer:
|
21 |
+
assert np.dtype(dtype.type).kind == "i"
|
22 |
+
else:
|
23 |
+
assert np.dtype(dtype.type).kind == "u"
|
24 |
+
assert dtype.name is not None
|
25 |
+
|
26 |
+
|
27 |
+
@pytest.mark.parametrize(
|
28 |
+
"dtype, expected",
|
29 |
+
[
|
30 |
+
(Int8Dtype(), "Int8Dtype()"),
|
31 |
+
(Int16Dtype(), "Int16Dtype()"),
|
32 |
+
(Int32Dtype(), "Int32Dtype()"),
|
33 |
+
(Int64Dtype(), "Int64Dtype()"),
|
34 |
+
(UInt8Dtype(), "UInt8Dtype()"),
|
35 |
+
(UInt16Dtype(), "UInt16Dtype()"),
|
36 |
+
(UInt32Dtype(), "UInt32Dtype()"),
|
37 |
+
(UInt64Dtype(), "UInt64Dtype()"),
|
38 |
+
],
|
39 |
+
)
|
40 |
+
def test_repr_dtype(dtype, expected):
|
41 |
+
assert repr(dtype) == expected
|
42 |
+
|
43 |
+
|
44 |
+
def test_repr_array():
|
45 |
+
result = repr(pd.array([1, None, 3]))
|
46 |
+
expected = "<IntegerArray>\n[1, <NA>, 3]\nLength: 3, dtype: Int64"
|
47 |
+
assert result == expected
|
48 |
+
|
49 |
+
|
50 |
+
def test_repr_array_long():
|
51 |
+
data = pd.array([1, 2, None] * 1000)
|
52 |
+
expected = (
|
53 |
+
"<IntegerArray>\n"
|
54 |
+
"[ 1, 2, <NA>, 1, 2, <NA>, 1, 2, <NA>, 1,\n"
|
55 |
+
" ...\n"
|
56 |
+
" <NA>, 1, 2, <NA>, 1, 2, <NA>, 1, 2, <NA>]\n"
|
57 |
+
"Length: 3000, dtype: Int64"
|
58 |
+
)
|
59 |
+
result = repr(data)
|
60 |
+
assert result == expected
|
61 |
+
|
62 |
+
|
63 |
+
def test_frame_repr(data_missing):
|
64 |
+
df = pd.DataFrame({"A": data_missing})
|
65 |
+
result = repr(df)
|
66 |
+
expected = " A\n0 <NA>\n1 1"
|
67 |
+
assert result == expected
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (193 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (1.06 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_formats.cpython-310.pyc
ADDED
Binary file (543 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_interval.cpython-310.pyc
ADDED
Binary file (7.53 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_interval_pyarrow.cpython-310.pyc
ADDED
Binary file (4.41 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/__pycache__/test_overlaps.cpython-310.pyc
ADDED
Binary file (3.42 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_astype.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
Categorical,
|
5 |
+
CategoricalDtype,
|
6 |
+
Index,
|
7 |
+
IntervalIndex,
|
8 |
+
)
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
|
12 |
+
class TestAstype:
|
13 |
+
@pytest.mark.parametrize("ordered", [True, False])
|
14 |
+
def test_astype_categorical_retains_ordered(self, ordered):
|
15 |
+
index = IntervalIndex.from_breaks(range(5))
|
16 |
+
arr = index._data
|
17 |
+
|
18 |
+
dtype = CategoricalDtype(None, ordered=ordered)
|
19 |
+
|
20 |
+
expected = Categorical(list(arr), ordered=ordered)
|
21 |
+
result = arr.astype(dtype)
|
22 |
+
assert result.ordered is ordered
|
23 |
+
tm.assert_categorical_equal(result, expected)
|
24 |
+
|
25 |
+
# test IntervalIndex.astype while we're at it.
|
26 |
+
result = index.astype(dtype)
|
27 |
+
expected = Index(expected)
|
28 |
+
tm.assert_index_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_formats.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pandas.core.arrays import IntervalArray
|
2 |
+
|
3 |
+
|
4 |
+
def test_repr():
|
5 |
+
# GH#25022
|
6 |
+
arr = IntervalArray.from_tuples([(0, 1), (1, 2)])
|
7 |
+
result = repr(arr)
|
8 |
+
expected = (
|
9 |
+
"<IntervalArray>\n"
|
10 |
+
"[(0, 1], (1, 2]]\n"
|
11 |
+
"Length: 2, dtype: interval[int64, right]"
|
12 |
+
)
|
13 |
+
assert result == expected
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_interval.py
ADDED
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
from pandas import (
|
6 |
+
Index,
|
7 |
+
Interval,
|
8 |
+
IntervalIndex,
|
9 |
+
Timedelta,
|
10 |
+
Timestamp,
|
11 |
+
date_range,
|
12 |
+
timedelta_range,
|
13 |
+
)
|
14 |
+
import pandas._testing as tm
|
15 |
+
from pandas.core.arrays import IntervalArray
|
16 |
+
|
17 |
+
|
18 |
+
@pytest.fixture(
|
19 |
+
params=[
|
20 |
+
(Index([0, 2, 4]), Index([1, 3, 5])),
|
21 |
+
(Index([0.0, 1.0, 2.0]), Index([1.0, 2.0, 3.0])),
|
22 |
+
(timedelta_range("0 days", periods=3), timedelta_range("1 day", periods=3)),
|
23 |
+
(date_range("20170101", periods=3), date_range("20170102", periods=3)),
|
24 |
+
(
|
25 |
+
date_range("20170101", periods=3, tz="US/Eastern"),
|
26 |
+
date_range("20170102", periods=3, tz="US/Eastern"),
|
27 |
+
),
|
28 |
+
],
|
29 |
+
ids=lambda x: str(x[0].dtype),
|
30 |
+
)
|
31 |
+
def left_right_dtypes(request):
|
32 |
+
"""
|
33 |
+
Fixture for building an IntervalArray from various dtypes
|
34 |
+
"""
|
35 |
+
return request.param
|
36 |
+
|
37 |
+
|
38 |
+
class TestAttributes:
|
39 |
+
@pytest.mark.parametrize(
|
40 |
+
"left, right",
|
41 |
+
[
|
42 |
+
(0, 1),
|
43 |
+
(Timedelta("0 days"), Timedelta("1 day")),
|
44 |
+
(Timestamp("2018-01-01"), Timestamp("2018-01-02")),
|
45 |
+
(
|
46 |
+
Timestamp("2018-01-01", tz="US/Eastern"),
|
47 |
+
Timestamp("2018-01-02", tz="US/Eastern"),
|
48 |
+
),
|
49 |
+
],
|
50 |
+
)
|
51 |
+
@pytest.mark.parametrize("constructor", [IntervalArray, IntervalIndex])
|
52 |
+
def test_is_empty(self, constructor, left, right, closed):
|
53 |
+
# GH27219
|
54 |
+
tuples = [(left, left), (left, right), np.nan]
|
55 |
+
expected = np.array([closed != "both", False, False])
|
56 |
+
result = constructor.from_tuples(tuples, closed=closed).is_empty
|
57 |
+
tm.assert_numpy_array_equal(result, expected)
|
58 |
+
|
59 |
+
|
60 |
+
class TestMethods:
|
61 |
+
@pytest.mark.parametrize("new_closed", ["left", "right", "both", "neither"])
|
62 |
+
def test_set_closed(self, closed, new_closed):
|
63 |
+
# GH 21670
|
64 |
+
array = IntervalArray.from_breaks(range(10), closed=closed)
|
65 |
+
result = array.set_closed(new_closed)
|
66 |
+
expected = IntervalArray.from_breaks(range(10), closed=new_closed)
|
67 |
+
tm.assert_extension_array_equal(result, expected)
|
68 |
+
|
69 |
+
@pytest.mark.parametrize(
|
70 |
+
"other",
|
71 |
+
[
|
72 |
+
Interval(0, 1, closed="right"),
|
73 |
+
IntervalArray.from_breaks([1, 2, 3, 4], closed="right"),
|
74 |
+
],
|
75 |
+
)
|
76 |
+
def test_where_raises(self, other):
|
77 |
+
# GH#45768 The IntervalArray methods raises; the Series method coerces
|
78 |
+
ser = pd.Series(IntervalArray.from_breaks([1, 2, 3, 4], closed="left"))
|
79 |
+
mask = np.array([True, False, True])
|
80 |
+
match = "'value.closed' is 'right', expected 'left'."
|
81 |
+
with pytest.raises(ValueError, match=match):
|
82 |
+
ser.array._where(mask, other)
|
83 |
+
|
84 |
+
res = ser.where(mask, other=other)
|
85 |
+
expected = ser.astype(object).where(mask, other)
|
86 |
+
tm.assert_series_equal(res, expected)
|
87 |
+
|
88 |
+
def test_shift(self):
|
89 |
+
# https://github.com/pandas-dev/pandas/issues/31495, GH#22428, GH#31502
|
90 |
+
a = IntervalArray.from_breaks([1, 2, 3])
|
91 |
+
result = a.shift()
|
92 |
+
# int -> float
|
93 |
+
expected = IntervalArray.from_tuples([(np.nan, np.nan), (1.0, 2.0)])
|
94 |
+
tm.assert_interval_array_equal(result, expected)
|
95 |
+
|
96 |
+
msg = "can only insert Interval objects and NA into an IntervalArray"
|
97 |
+
with pytest.raises(TypeError, match=msg):
|
98 |
+
a.shift(1, fill_value=pd.NaT)
|
99 |
+
|
100 |
+
def test_shift_datetime(self):
|
101 |
+
# GH#31502, GH#31504
|
102 |
+
a = IntervalArray.from_breaks(date_range("2000", periods=4))
|
103 |
+
result = a.shift(2)
|
104 |
+
expected = a.take([-1, -1, 0], allow_fill=True)
|
105 |
+
tm.assert_interval_array_equal(result, expected)
|
106 |
+
|
107 |
+
result = a.shift(-1)
|
108 |
+
expected = a.take([1, 2, -1], allow_fill=True)
|
109 |
+
tm.assert_interval_array_equal(result, expected)
|
110 |
+
|
111 |
+
msg = "can only insert Interval objects and NA into an IntervalArray"
|
112 |
+
with pytest.raises(TypeError, match=msg):
|
113 |
+
a.shift(1, fill_value=np.timedelta64("NaT", "ns"))
|
114 |
+
|
115 |
+
|
116 |
+
class TestSetitem:
|
117 |
+
def test_set_na(self, left_right_dtypes):
|
118 |
+
left, right = left_right_dtypes
|
119 |
+
left = left.copy(deep=True)
|
120 |
+
right = right.copy(deep=True)
|
121 |
+
result = IntervalArray.from_arrays(left, right)
|
122 |
+
|
123 |
+
if result.dtype.subtype.kind not in ["m", "M"]:
|
124 |
+
msg = "'value' should be an interval type, got <.*NaTType'> instead."
|
125 |
+
with pytest.raises(TypeError, match=msg):
|
126 |
+
result[0] = pd.NaT
|
127 |
+
if result.dtype.subtype.kind in ["i", "u"]:
|
128 |
+
msg = "Cannot set float NaN to integer-backed IntervalArray"
|
129 |
+
# GH#45484 TypeError, not ValueError, matches what we get with
|
130 |
+
# non-NA un-holdable value.
|
131 |
+
with pytest.raises(TypeError, match=msg):
|
132 |
+
result[0] = np.nan
|
133 |
+
return
|
134 |
+
|
135 |
+
result[0] = np.nan
|
136 |
+
|
137 |
+
expected_left = Index([left._na_value] + list(left[1:]))
|
138 |
+
expected_right = Index([right._na_value] + list(right[1:]))
|
139 |
+
expected = IntervalArray.from_arrays(expected_left, expected_right)
|
140 |
+
|
141 |
+
tm.assert_extension_array_equal(result, expected)
|
142 |
+
|
143 |
+
def test_setitem_mismatched_closed(self):
|
144 |
+
arr = IntervalArray.from_breaks(range(4))
|
145 |
+
orig = arr.copy()
|
146 |
+
other = arr.set_closed("both")
|
147 |
+
|
148 |
+
msg = "'value.closed' is 'both', expected 'right'"
|
149 |
+
with pytest.raises(ValueError, match=msg):
|
150 |
+
arr[0] = other[0]
|
151 |
+
with pytest.raises(ValueError, match=msg):
|
152 |
+
arr[:1] = other[:1]
|
153 |
+
with pytest.raises(ValueError, match=msg):
|
154 |
+
arr[:0] = other[:0]
|
155 |
+
with pytest.raises(ValueError, match=msg):
|
156 |
+
arr[:] = other[::-1]
|
157 |
+
with pytest.raises(ValueError, match=msg):
|
158 |
+
arr[:] = list(other[::-1])
|
159 |
+
with pytest.raises(ValueError, match=msg):
|
160 |
+
arr[:] = other[::-1].astype(object)
|
161 |
+
with pytest.raises(ValueError, match=msg):
|
162 |
+
arr[:] = other[::-1].astype("category")
|
163 |
+
|
164 |
+
# empty list should be no-op
|
165 |
+
arr[:0] = []
|
166 |
+
tm.assert_interval_array_equal(arr, orig)
|
167 |
+
|
168 |
+
|
169 |
+
class TestReductions:
|
170 |
+
def test_min_max_invalid_axis(self, left_right_dtypes):
|
171 |
+
left, right = left_right_dtypes
|
172 |
+
left = left.copy(deep=True)
|
173 |
+
right = right.copy(deep=True)
|
174 |
+
arr = IntervalArray.from_arrays(left, right)
|
175 |
+
|
176 |
+
msg = "`axis` must be fewer than the number of dimensions"
|
177 |
+
for axis in [-2, 1]:
|
178 |
+
with pytest.raises(ValueError, match=msg):
|
179 |
+
arr.min(axis=axis)
|
180 |
+
with pytest.raises(ValueError, match=msg):
|
181 |
+
arr.max(axis=axis)
|
182 |
+
|
183 |
+
msg = "'>=' not supported between"
|
184 |
+
with pytest.raises(TypeError, match=msg):
|
185 |
+
arr.min(axis="foo")
|
186 |
+
with pytest.raises(TypeError, match=msg):
|
187 |
+
arr.max(axis="foo")
|
188 |
+
|
189 |
+
def test_min_max(self, left_right_dtypes, index_or_series_or_array):
|
190 |
+
# GH#44746
|
191 |
+
left, right = left_right_dtypes
|
192 |
+
left = left.copy(deep=True)
|
193 |
+
right = right.copy(deep=True)
|
194 |
+
arr = IntervalArray.from_arrays(left, right)
|
195 |
+
|
196 |
+
# The expected results below are only valid if monotonic
|
197 |
+
assert left.is_monotonic_increasing
|
198 |
+
assert Index(arr).is_monotonic_increasing
|
199 |
+
|
200 |
+
MIN = arr[0]
|
201 |
+
MAX = arr[-1]
|
202 |
+
|
203 |
+
indexer = np.arange(len(arr))
|
204 |
+
np.random.default_rng(2).shuffle(indexer)
|
205 |
+
arr = arr.take(indexer)
|
206 |
+
|
207 |
+
arr_na = arr.insert(2, np.nan)
|
208 |
+
|
209 |
+
arr = index_or_series_or_array(arr)
|
210 |
+
arr_na = index_or_series_or_array(arr_na)
|
211 |
+
|
212 |
+
for skipna in [True, False]:
|
213 |
+
res = arr.min(skipna=skipna)
|
214 |
+
assert res == MIN
|
215 |
+
assert type(res) == type(MIN)
|
216 |
+
|
217 |
+
res = arr.max(skipna=skipna)
|
218 |
+
assert res == MAX
|
219 |
+
assert type(res) == type(MAX)
|
220 |
+
|
221 |
+
res = arr_na.min(skipna=False)
|
222 |
+
assert np.isnan(res)
|
223 |
+
res = arr_na.max(skipna=False)
|
224 |
+
assert np.isnan(res)
|
225 |
+
|
226 |
+
res = arr_na.min(skipna=True)
|
227 |
+
assert res == MIN
|
228 |
+
assert type(res) == type(MIN)
|
229 |
+
res = arr_na.max(skipna=True)
|
230 |
+
assert res == MAX
|
231 |
+
assert type(res) == type(MAX)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_interval_pyarrow.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
import pandas._testing as tm
|
6 |
+
from pandas.core.arrays import IntervalArray
|
7 |
+
|
8 |
+
|
9 |
+
def test_arrow_extension_type():
|
10 |
+
pa = pytest.importorskip("pyarrow")
|
11 |
+
|
12 |
+
from pandas.core.arrays.arrow.extension_types import ArrowIntervalType
|
13 |
+
|
14 |
+
p1 = ArrowIntervalType(pa.int64(), "left")
|
15 |
+
p2 = ArrowIntervalType(pa.int64(), "left")
|
16 |
+
p3 = ArrowIntervalType(pa.int64(), "right")
|
17 |
+
|
18 |
+
assert p1.closed == "left"
|
19 |
+
assert p1 == p2
|
20 |
+
assert p1 != p3
|
21 |
+
assert hash(p1) == hash(p2)
|
22 |
+
assert hash(p1) != hash(p3)
|
23 |
+
|
24 |
+
|
25 |
+
def test_arrow_array():
|
26 |
+
pa = pytest.importorskip("pyarrow")
|
27 |
+
|
28 |
+
from pandas.core.arrays.arrow.extension_types import ArrowIntervalType
|
29 |
+
|
30 |
+
intervals = pd.interval_range(1, 5, freq=1).array
|
31 |
+
|
32 |
+
result = pa.array(intervals)
|
33 |
+
assert isinstance(result.type, ArrowIntervalType)
|
34 |
+
assert result.type.closed == intervals.closed
|
35 |
+
assert result.type.subtype == pa.int64()
|
36 |
+
assert result.storage.field("left").equals(pa.array([1, 2, 3, 4], type="int64"))
|
37 |
+
assert result.storage.field("right").equals(pa.array([2, 3, 4, 5], type="int64"))
|
38 |
+
|
39 |
+
expected = pa.array([{"left": i, "right": i + 1} for i in range(1, 5)])
|
40 |
+
assert result.storage.equals(expected)
|
41 |
+
|
42 |
+
# convert to its storage type
|
43 |
+
result = pa.array(intervals, type=expected.type)
|
44 |
+
assert result.equals(expected)
|
45 |
+
|
46 |
+
# unsupported conversions
|
47 |
+
with pytest.raises(TypeError, match="Not supported to convert IntervalArray"):
|
48 |
+
pa.array(intervals, type="float64")
|
49 |
+
|
50 |
+
with pytest.raises(TypeError, match="Not supported to convert IntervalArray"):
|
51 |
+
pa.array(intervals, type=ArrowIntervalType(pa.float64(), "left"))
|
52 |
+
|
53 |
+
|
54 |
+
def test_arrow_array_missing():
|
55 |
+
pa = pytest.importorskip("pyarrow")
|
56 |
+
|
57 |
+
from pandas.core.arrays.arrow.extension_types import ArrowIntervalType
|
58 |
+
|
59 |
+
arr = IntervalArray.from_breaks([0.0, 1.0, 2.0, 3.0])
|
60 |
+
arr[1] = None
|
61 |
+
|
62 |
+
result = pa.array(arr)
|
63 |
+
assert isinstance(result.type, ArrowIntervalType)
|
64 |
+
assert result.type.closed == arr.closed
|
65 |
+
assert result.type.subtype == pa.float64()
|
66 |
+
|
67 |
+
# fields have missing values (not NaN)
|
68 |
+
left = pa.array([0.0, None, 2.0], type="float64")
|
69 |
+
right = pa.array([1.0, None, 3.0], type="float64")
|
70 |
+
assert result.storage.field("left").equals(left)
|
71 |
+
assert result.storage.field("right").equals(right)
|
72 |
+
|
73 |
+
# structarray itself also has missing values on the array level
|
74 |
+
vals = [
|
75 |
+
{"left": 0.0, "right": 1.0},
|
76 |
+
{"left": None, "right": None},
|
77 |
+
{"left": 2.0, "right": 3.0},
|
78 |
+
]
|
79 |
+
expected = pa.StructArray.from_pandas(vals, mask=np.array([False, True, False]))
|
80 |
+
assert result.storage.equals(expected)
|
81 |
+
|
82 |
+
|
83 |
+
@pytest.mark.filterwarnings(
|
84 |
+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
|
85 |
+
)
|
86 |
+
@pytest.mark.parametrize(
|
87 |
+
"breaks",
|
88 |
+
[[0.0, 1.0, 2.0, 3.0], pd.date_range("2017", periods=4, freq="D")],
|
89 |
+
ids=["float", "datetime64[ns]"],
|
90 |
+
)
|
91 |
+
def test_arrow_table_roundtrip(breaks):
|
92 |
+
pa = pytest.importorskip("pyarrow")
|
93 |
+
|
94 |
+
from pandas.core.arrays.arrow.extension_types import ArrowIntervalType
|
95 |
+
|
96 |
+
arr = IntervalArray.from_breaks(breaks)
|
97 |
+
arr[1] = None
|
98 |
+
df = pd.DataFrame({"a": arr})
|
99 |
+
|
100 |
+
table = pa.table(df)
|
101 |
+
assert isinstance(table.field("a").type, ArrowIntervalType)
|
102 |
+
result = table.to_pandas()
|
103 |
+
assert isinstance(result["a"].dtype, pd.IntervalDtype)
|
104 |
+
tm.assert_frame_equal(result, df)
|
105 |
+
|
106 |
+
table2 = pa.concat_tables([table, table])
|
107 |
+
result = table2.to_pandas()
|
108 |
+
expected = pd.concat([df, df], ignore_index=True)
|
109 |
+
tm.assert_frame_equal(result, expected)
|
110 |
+
|
111 |
+
# GH#41040
|
112 |
+
table = pa.table(
|
113 |
+
[pa.chunked_array([], type=table.column(0).type)], schema=table.schema
|
114 |
+
)
|
115 |
+
result = table.to_pandas()
|
116 |
+
tm.assert_frame_equal(result, expected[0:0])
|
117 |
+
|
118 |
+
|
119 |
+
@pytest.mark.filterwarnings(
|
120 |
+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
|
121 |
+
)
|
122 |
+
@pytest.mark.parametrize(
|
123 |
+
"breaks",
|
124 |
+
[[0.0, 1.0, 2.0, 3.0], pd.date_range("2017", periods=4, freq="D")],
|
125 |
+
ids=["float", "datetime64[ns]"],
|
126 |
+
)
|
127 |
+
def test_arrow_table_roundtrip_without_metadata(breaks):
|
128 |
+
pa = pytest.importorskip("pyarrow")
|
129 |
+
|
130 |
+
arr = IntervalArray.from_breaks(breaks)
|
131 |
+
arr[1] = None
|
132 |
+
df = pd.DataFrame({"a": arr})
|
133 |
+
|
134 |
+
table = pa.table(df)
|
135 |
+
# remove the metadata
|
136 |
+
table = table.replace_schema_metadata()
|
137 |
+
assert table.schema.metadata is None
|
138 |
+
|
139 |
+
result = table.to_pandas()
|
140 |
+
assert isinstance(result["a"].dtype, pd.IntervalDtype)
|
141 |
+
tm.assert_frame_equal(result, df)
|
142 |
+
|
143 |
+
|
144 |
+
def test_from_arrow_from_raw_struct_array():
|
145 |
+
# in case pyarrow lost the Interval extension type (eg on parquet roundtrip
|
146 |
+
# with datetime64[ns] subtype, see GH-45881), still allow conversion
|
147 |
+
# from arrow to IntervalArray
|
148 |
+
pa = pytest.importorskip("pyarrow")
|
149 |
+
|
150 |
+
arr = pa.array([{"left": 0, "right": 1}, {"left": 1, "right": 2}])
|
151 |
+
dtype = pd.IntervalDtype(np.dtype("int64"), closed="neither")
|
152 |
+
|
153 |
+
result = dtype.__from_arrow__(arr)
|
154 |
+
expected = IntervalArray.from_breaks(
|
155 |
+
np.array([0, 1, 2], dtype="int64"), closed="neither"
|
156 |
+
)
|
157 |
+
tm.assert_extension_array_equal(result, expected)
|
158 |
+
|
159 |
+
result = dtype.__from_arrow__(pa.chunked_array([arr]))
|
160 |
+
tm.assert_extension_array_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/interval/test_overlaps.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Tests for Interval-Interval operations, such as overlaps, contains, etc."""
|
2 |
+
import numpy as np
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
from pandas import (
|
6 |
+
Interval,
|
7 |
+
IntervalIndex,
|
8 |
+
Timedelta,
|
9 |
+
Timestamp,
|
10 |
+
)
|
11 |
+
import pandas._testing as tm
|
12 |
+
from pandas.core.arrays import IntervalArray
|
13 |
+
|
14 |
+
|
15 |
+
@pytest.fixture(params=[IntervalArray, IntervalIndex])
|
16 |
+
def constructor(request):
|
17 |
+
"""
|
18 |
+
Fixture for testing both interval container classes.
|
19 |
+
"""
|
20 |
+
return request.param
|
21 |
+
|
22 |
+
|
23 |
+
@pytest.fixture(
|
24 |
+
params=[
|
25 |
+
(Timedelta("0 days"), Timedelta("1 day")),
|
26 |
+
(Timestamp("2018-01-01"), Timedelta("1 day")),
|
27 |
+
(0, 1),
|
28 |
+
],
|
29 |
+
ids=lambda x: type(x[0]).__name__,
|
30 |
+
)
|
31 |
+
def start_shift(request):
|
32 |
+
"""
|
33 |
+
Fixture for generating intervals of different types from a start value
|
34 |
+
and a shift value that can be added to start to generate an endpoint.
|
35 |
+
"""
|
36 |
+
return request.param
|
37 |
+
|
38 |
+
|
39 |
+
class TestOverlaps:
|
40 |
+
def test_overlaps_interval(self, constructor, start_shift, closed, other_closed):
|
41 |
+
start, shift = start_shift
|
42 |
+
interval = Interval(start, start + 3 * shift, other_closed)
|
43 |
+
|
44 |
+
# intervals: identical, nested, spanning, partial, adjacent, disjoint
|
45 |
+
tuples = [
|
46 |
+
(start, start + 3 * shift),
|
47 |
+
(start + shift, start + 2 * shift),
|
48 |
+
(start - shift, start + 4 * shift),
|
49 |
+
(start + 2 * shift, start + 4 * shift),
|
50 |
+
(start + 3 * shift, start + 4 * shift),
|
51 |
+
(start + 4 * shift, start + 5 * shift),
|
52 |
+
]
|
53 |
+
interval_container = constructor.from_tuples(tuples, closed)
|
54 |
+
|
55 |
+
adjacent = interval.closed_right and interval_container.closed_left
|
56 |
+
expected = np.array([True, True, True, True, adjacent, False])
|
57 |
+
result = interval_container.overlaps(interval)
|
58 |
+
tm.assert_numpy_array_equal(result, expected)
|
59 |
+
|
60 |
+
@pytest.mark.parametrize("other_constructor", [IntervalArray, IntervalIndex])
|
61 |
+
def test_overlaps_interval_container(self, constructor, other_constructor):
|
62 |
+
# TODO: modify this test when implemented
|
63 |
+
interval_container = constructor.from_breaks(range(5))
|
64 |
+
other_container = other_constructor.from_breaks(range(5))
|
65 |
+
with pytest.raises(NotImplementedError, match="^$"):
|
66 |
+
interval_container.overlaps(other_container)
|
67 |
+
|
68 |
+
def test_overlaps_na(self, constructor, start_shift):
|
69 |
+
"""NA values are marked as False"""
|
70 |
+
start, shift = start_shift
|
71 |
+
interval = Interval(start, start + shift)
|
72 |
+
|
73 |
+
tuples = [
|
74 |
+
(start, start + shift),
|
75 |
+
np.nan,
|
76 |
+
(start + 2 * shift, start + 3 * shift),
|
77 |
+
]
|
78 |
+
interval_container = constructor.from_tuples(tuples)
|
79 |
+
|
80 |
+
expected = np.array([True, False, False])
|
81 |
+
result = interval_container.overlaps(interval)
|
82 |
+
tm.assert_numpy_array_equal(result, expected)
|
83 |
+
|
84 |
+
@pytest.mark.parametrize(
|
85 |
+
"other",
|
86 |
+
[10, True, "foo", Timedelta("1 day"), Timestamp("2018-01-01")],
|
87 |
+
ids=lambda x: type(x).__name__,
|
88 |
+
)
|
89 |
+
def test_overlaps_invalid_type(self, constructor, other):
|
90 |
+
interval_container = constructor.from_breaks(range(5))
|
91 |
+
msg = f"`other` must be Interval-like, got {type(other).__name__}"
|
92 |
+
with pytest.raises(TypeError, match=msg):
|
93 |
+
interval_container.overlaps(other)
|