Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/masked_shared.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_array.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_datetimelike.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_datetimes.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_ndarray_backed.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_period.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_timedeltas.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_comparison.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_construction.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_function.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_ops.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_reduction.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_astype.py +53 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_construction.py +325 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_function.py +126 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_logical.py +254 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_reduction.py +62 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/test_repr.py +13 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/test_constructors.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/test_cumulative.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/test_reductions.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/test_constructors.py +284 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/test_cumulative.py +44 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/test_reductions.py +183 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/conftest.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_comparison.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_concat.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_construction.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_dtypes.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_function.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_indexing.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_reduction.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_repr.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/conftest.py +68 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_arithmetic.py +385 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_comparison.py +39 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_concat.py +69 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_construction.py +245 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_dtypes.py +294 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_function.py +203 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_indexing.py +19 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_reduction.py +125 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/test_repr.py +67 -0
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (192 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/masked_shared.cpython-310.pyc
ADDED
Binary file (4.34 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_array.cpython-310.pyc
ADDED
Binary file (9.32 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_datetimelike.cpython-310.pyc
ADDED
Binary file (35.5 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_datetimes.cpython-310.pyc
ADDED
Binary file (24.8 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_ndarray_backed.cpython-310.pyc
ADDED
Binary file (2.3 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_period.cpython-310.pyc
ADDED
Binary file (5.72 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/__pycache__/test_timedeltas.cpython-310.pyc
ADDED
Binary file (10.3 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_comparison.cpython-310.pyc
ADDED
Binary file (2.3 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_construction.cpython-310.pyc
ADDED
Binary file (8.3 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_function.cpython-310.pyc
ADDED
Binary file (3.39 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_ops.cpython-310.pyc
ADDED
Binary file (1.18 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/boolean/__pycache__/test_reduction.cpython-310.pyc
ADDED
Binary file (1.75 kB). View file
|
|
llmeval-env/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)
|
llmeval-env/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"))
|
llmeval-env/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)
|
llmeval-env/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))
|
llmeval-env/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)
|
llmeval-env/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
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (202 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (10.9 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/test_cumulative.cpython-310.pyc
ADDED
Binary file (1.41 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/__pycache__/test_reductions.cpython-310.pyc
ADDED
Binary file (5.09 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/test_constructors.py
ADDED
@@ -0,0 +1,284 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas._libs import iNaT
|
5 |
+
|
6 |
+
from pandas.core.dtypes.dtypes import DatetimeTZDtype
|
7 |
+
|
8 |
+
import pandas as pd
|
9 |
+
import pandas._testing as tm
|
10 |
+
from pandas.core.arrays import DatetimeArray
|
11 |
+
|
12 |
+
|
13 |
+
class TestDatetimeArrayConstructor:
|
14 |
+
def test_from_sequence_invalid_type(self):
|
15 |
+
mi = pd.MultiIndex.from_product([np.arange(5), np.arange(5)])
|
16 |
+
with pytest.raises(TypeError, match="Cannot create a DatetimeArray"):
|
17 |
+
DatetimeArray._from_sequence(mi, dtype="M8[ns]")
|
18 |
+
|
19 |
+
def test_only_1dim_accepted(self):
|
20 |
+
arr = np.array([0, 1, 2, 3], dtype="M8[h]").astype("M8[ns]")
|
21 |
+
|
22 |
+
depr_msg = "DatetimeArray.__init__ is deprecated"
|
23 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
24 |
+
with pytest.raises(ValueError, match="Only 1-dimensional"):
|
25 |
+
# 3-dim, we allow 2D to sneak in for ops purposes GH#29853
|
26 |
+
DatetimeArray(arr.reshape(2, 2, 1))
|
27 |
+
|
28 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
29 |
+
with pytest.raises(ValueError, match="Only 1-dimensional"):
|
30 |
+
# 0-dim
|
31 |
+
DatetimeArray(arr[[0]].squeeze())
|
32 |
+
|
33 |
+
def test_freq_validation(self):
|
34 |
+
# GH#24623 check that invalid instances cannot be created with the
|
35 |
+
# public constructor
|
36 |
+
arr = np.arange(5, dtype=np.int64) * 3600 * 10**9
|
37 |
+
|
38 |
+
msg = (
|
39 |
+
"Inferred frequency h from passed values does not "
|
40 |
+
"conform to passed frequency W-SUN"
|
41 |
+
)
|
42 |
+
depr_msg = "DatetimeArray.__init__ is deprecated"
|
43 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
44 |
+
with pytest.raises(ValueError, match=msg):
|
45 |
+
DatetimeArray(arr, freq="W")
|
46 |
+
|
47 |
+
@pytest.mark.parametrize(
|
48 |
+
"meth",
|
49 |
+
[
|
50 |
+
DatetimeArray._from_sequence,
|
51 |
+
pd.to_datetime,
|
52 |
+
pd.DatetimeIndex,
|
53 |
+
],
|
54 |
+
)
|
55 |
+
def test_mixing_naive_tzaware_raises(self, meth):
|
56 |
+
# GH#24569
|
57 |
+
arr = np.array([pd.Timestamp("2000"), pd.Timestamp("2000", tz="CET")])
|
58 |
+
|
59 |
+
msg = (
|
60 |
+
"Cannot mix tz-aware with tz-naive values|"
|
61 |
+
"Tz-aware datetime.datetime cannot be converted "
|
62 |
+
"to datetime64 unless utc=True"
|
63 |
+
)
|
64 |
+
|
65 |
+
for obj in [arr, arr[::-1]]:
|
66 |
+
# check that we raise regardless of whether naive is found
|
67 |
+
# before aware or vice-versa
|
68 |
+
with pytest.raises(ValueError, match=msg):
|
69 |
+
meth(obj)
|
70 |
+
|
71 |
+
def test_from_pandas_array(self):
|
72 |
+
arr = pd.array(np.arange(5, dtype=np.int64)) * 3600 * 10**9
|
73 |
+
|
74 |
+
result = DatetimeArray._from_sequence(arr, dtype="M8[ns]")._with_freq("infer")
|
75 |
+
|
76 |
+
expected = pd.date_range("1970-01-01", periods=5, freq="h")._data
|
77 |
+
tm.assert_datetime_array_equal(result, expected)
|
78 |
+
|
79 |
+
def test_mismatched_timezone_raises(self):
|
80 |
+
depr_msg = "DatetimeArray.__init__ is deprecated"
|
81 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
82 |
+
arr = DatetimeArray(
|
83 |
+
np.array(["2000-01-01T06:00:00"], dtype="M8[ns]"),
|
84 |
+
dtype=DatetimeTZDtype(tz="US/Central"),
|
85 |
+
)
|
86 |
+
dtype = DatetimeTZDtype(tz="US/Eastern")
|
87 |
+
msg = r"dtype=datetime64\[ns.*\] does not match data dtype datetime64\[ns.*\]"
|
88 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
89 |
+
with pytest.raises(TypeError, match=msg):
|
90 |
+
DatetimeArray(arr, dtype=dtype)
|
91 |
+
|
92 |
+
# also with mismatched tzawareness
|
93 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
94 |
+
with pytest.raises(TypeError, match=msg):
|
95 |
+
DatetimeArray(arr, dtype=np.dtype("M8[ns]"))
|
96 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
97 |
+
with pytest.raises(TypeError, match=msg):
|
98 |
+
DatetimeArray(arr.tz_localize(None), dtype=arr.dtype)
|
99 |
+
|
100 |
+
def test_non_array_raises(self):
|
101 |
+
depr_msg = "DatetimeArray.__init__ is deprecated"
|
102 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
103 |
+
with pytest.raises(ValueError, match="list"):
|
104 |
+
DatetimeArray([1, 2, 3])
|
105 |
+
|
106 |
+
def test_bool_dtype_raises(self):
|
107 |
+
arr = np.array([1, 2, 3], dtype="bool")
|
108 |
+
|
109 |
+
depr_msg = "DatetimeArray.__init__ is deprecated"
|
110 |
+
msg = "Unexpected value for 'dtype': 'bool'. Must be"
|
111 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
112 |
+
with pytest.raises(ValueError, match=msg):
|
113 |
+
DatetimeArray(arr)
|
114 |
+
|
115 |
+
msg = r"dtype bool cannot be converted to datetime64\[ns\]"
|
116 |
+
with pytest.raises(TypeError, match=msg):
|
117 |
+
DatetimeArray._from_sequence(arr, dtype="M8[ns]")
|
118 |
+
|
119 |
+
with pytest.raises(TypeError, match=msg):
|
120 |
+
pd.DatetimeIndex(arr)
|
121 |
+
|
122 |
+
with pytest.raises(TypeError, match=msg):
|
123 |
+
pd.to_datetime(arr)
|
124 |
+
|
125 |
+
def test_incorrect_dtype_raises(self):
|
126 |
+
depr_msg = "DatetimeArray.__init__ is deprecated"
|
127 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
128 |
+
with pytest.raises(ValueError, match="Unexpected value for 'dtype'."):
|
129 |
+
DatetimeArray(np.array([1, 2, 3], dtype="i8"), dtype="category")
|
130 |
+
|
131 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
132 |
+
with pytest.raises(ValueError, match="Unexpected value for 'dtype'."):
|
133 |
+
DatetimeArray(np.array([1, 2, 3], dtype="i8"), dtype="m8[s]")
|
134 |
+
|
135 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
136 |
+
with pytest.raises(ValueError, match="Unexpected value for 'dtype'."):
|
137 |
+
DatetimeArray(np.array([1, 2, 3], dtype="i8"), dtype="M8[D]")
|
138 |
+
|
139 |
+
def test_mismatched_values_dtype_units(self):
|
140 |
+
arr = np.array([1, 2, 3], dtype="M8[s]")
|
141 |
+
dtype = np.dtype("M8[ns]")
|
142 |
+
msg = "Values resolution does not match dtype."
|
143 |
+
depr_msg = "DatetimeArray.__init__ is deprecated"
|
144 |
+
|
145 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
146 |
+
with pytest.raises(ValueError, match=msg):
|
147 |
+
DatetimeArray(arr, dtype=dtype)
|
148 |
+
|
149 |
+
dtype2 = DatetimeTZDtype(tz="UTC", unit="ns")
|
150 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
151 |
+
with pytest.raises(ValueError, match=msg):
|
152 |
+
DatetimeArray(arr, dtype=dtype2)
|
153 |
+
|
154 |
+
def test_freq_infer_raises(self):
|
155 |
+
depr_msg = "DatetimeArray.__init__ is deprecated"
|
156 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
157 |
+
with pytest.raises(ValueError, match="Frequency inference"):
|
158 |
+
DatetimeArray(np.array([1, 2, 3], dtype="i8"), freq="infer")
|
159 |
+
|
160 |
+
def test_copy(self):
|
161 |
+
data = np.array([1, 2, 3], dtype="M8[ns]")
|
162 |
+
arr = DatetimeArray._from_sequence(data, copy=False)
|
163 |
+
assert arr._ndarray is data
|
164 |
+
|
165 |
+
arr = DatetimeArray._from_sequence(data, copy=True)
|
166 |
+
assert arr._ndarray is not data
|
167 |
+
|
168 |
+
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
|
169 |
+
def test_numpy_datetime_unit(self, unit):
|
170 |
+
data = np.array([1, 2, 3], dtype=f"M8[{unit}]")
|
171 |
+
arr = DatetimeArray._from_sequence(data)
|
172 |
+
assert arr.unit == unit
|
173 |
+
assert arr[0].unit == unit
|
174 |
+
|
175 |
+
|
176 |
+
class TestSequenceToDT64NS:
|
177 |
+
def test_tz_dtype_mismatch_raises(self):
|
178 |
+
arr = DatetimeArray._from_sequence(
|
179 |
+
["2000"], dtype=DatetimeTZDtype(tz="US/Central")
|
180 |
+
)
|
181 |
+
with pytest.raises(TypeError, match="data is already tz-aware"):
|
182 |
+
DatetimeArray._from_sequence(arr, dtype=DatetimeTZDtype(tz="UTC"))
|
183 |
+
|
184 |
+
def test_tz_dtype_matches(self):
|
185 |
+
dtype = DatetimeTZDtype(tz="US/Central")
|
186 |
+
arr = DatetimeArray._from_sequence(["2000"], dtype=dtype)
|
187 |
+
result = DatetimeArray._from_sequence(arr, dtype=dtype)
|
188 |
+
tm.assert_equal(arr, result)
|
189 |
+
|
190 |
+
@pytest.mark.parametrize("order", ["F", "C"])
|
191 |
+
def test_2d(self, order):
|
192 |
+
dti = pd.date_range("2016-01-01", periods=6, tz="US/Pacific")
|
193 |
+
arr = np.array(dti, dtype=object).reshape(3, 2)
|
194 |
+
if order == "F":
|
195 |
+
arr = arr.T
|
196 |
+
|
197 |
+
res = DatetimeArray._from_sequence(arr, dtype=dti.dtype)
|
198 |
+
expected = DatetimeArray._from_sequence(arr.ravel(), dtype=dti.dtype).reshape(
|
199 |
+
arr.shape
|
200 |
+
)
|
201 |
+
tm.assert_datetime_array_equal(res, expected)
|
202 |
+
|
203 |
+
|
204 |
+
# ----------------------------------------------------------------------------
|
205 |
+
# Arrow interaction
|
206 |
+
|
207 |
+
|
208 |
+
EXTREME_VALUES = [0, 123456789, None, iNaT, 2**63 - 1, -(2**63) + 1]
|
209 |
+
FINE_TO_COARSE_SAFE = [123_000_000_000, None, -123_000_000_000]
|
210 |
+
COARSE_TO_FINE_SAFE = [123, None, -123]
|
211 |
+
|
212 |
+
|
213 |
+
@pytest.mark.parametrize(
|
214 |
+
("pa_unit", "pd_unit", "pa_tz", "pd_tz", "data"),
|
215 |
+
[
|
216 |
+
("s", "s", "UTC", "UTC", EXTREME_VALUES),
|
217 |
+
("ms", "ms", "UTC", "Europe/Berlin", EXTREME_VALUES),
|
218 |
+
("us", "us", "US/Eastern", "UTC", EXTREME_VALUES),
|
219 |
+
("ns", "ns", "US/Central", "Asia/Kolkata", EXTREME_VALUES),
|
220 |
+
("ns", "s", "UTC", "UTC", FINE_TO_COARSE_SAFE),
|
221 |
+
("us", "ms", "UTC", "Europe/Berlin", FINE_TO_COARSE_SAFE),
|
222 |
+
("ms", "us", "US/Eastern", "UTC", COARSE_TO_FINE_SAFE),
|
223 |
+
("s", "ns", "US/Central", "Asia/Kolkata", COARSE_TO_FINE_SAFE),
|
224 |
+
],
|
225 |
+
)
|
226 |
+
def test_from_arrow_with_different_units_and_timezones_with(
|
227 |
+
pa_unit, pd_unit, pa_tz, pd_tz, data
|
228 |
+
):
|
229 |
+
pa = pytest.importorskip("pyarrow")
|
230 |
+
|
231 |
+
pa_type = pa.timestamp(pa_unit, tz=pa_tz)
|
232 |
+
arr = pa.array(data, type=pa_type)
|
233 |
+
dtype = DatetimeTZDtype(unit=pd_unit, tz=pd_tz)
|
234 |
+
|
235 |
+
result = dtype.__from_arrow__(arr)
|
236 |
+
expected = DatetimeArray._from_sequence(data, dtype=f"M8[{pa_unit}, UTC]").astype(
|
237 |
+
dtype, copy=False
|
238 |
+
)
|
239 |
+
tm.assert_extension_array_equal(result, expected)
|
240 |
+
|
241 |
+
result = dtype.__from_arrow__(pa.chunked_array([arr]))
|
242 |
+
tm.assert_extension_array_equal(result, expected)
|
243 |
+
|
244 |
+
|
245 |
+
@pytest.mark.parametrize(
|
246 |
+
("unit", "tz"),
|
247 |
+
[
|
248 |
+
("s", "UTC"),
|
249 |
+
("ms", "Europe/Berlin"),
|
250 |
+
("us", "US/Eastern"),
|
251 |
+
("ns", "Asia/Kolkata"),
|
252 |
+
("ns", "UTC"),
|
253 |
+
],
|
254 |
+
)
|
255 |
+
def test_from_arrow_from_empty(unit, tz):
|
256 |
+
pa = pytest.importorskip("pyarrow")
|
257 |
+
|
258 |
+
data = []
|
259 |
+
arr = pa.array(data)
|
260 |
+
dtype = DatetimeTZDtype(unit=unit, tz=tz)
|
261 |
+
|
262 |
+
result = dtype.__from_arrow__(arr)
|
263 |
+
expected = DatetimeArray._from_sequence(np.array(data, dtype=f"datetime64[{unit}]"))
|
264 |
+
expected = expected.tz_localize(tz=tz)
|
265 |
+
tm.assert_extension_array_equal(result, expected)
|
266 |
+
|
267 |
+
result = dtype.__from_arrow__(pa.chunked_array([arr]))
|
268 |
+
tm.assert_extension_array_equal(result, expected)
|
269 |
+
|
270 |
+
|
271 |
+
def test_from_arrow_from_integers():
|
272 |
+
pa = pytest.importorskip("pyarrow")
|
273 |
+
|
274 |
+
data = [0, 123456789, None, 2**63 - 1, iNaT, -123456789]
|
275 |
+
arr = pa.array(data)
|
276 |
+
dtype = DatetimeTZDtype(unit="ns", tz="UTC")
|
277 |
+
|
278 |
+
result = dtype.__from_arrow__(arr)
|
279 |
+
expected = DatetimeArray._from_sequence(np.array(data, dtype="datetime64[ns]"))
|
280 |
+
expected = expected.tz_localize("UTC")
|
281 |
+
tm.assert_extension_array_equal(result, expected)
|
282 |
+
|
283 |
+
result = dtype.__from_arrow__(pa.chunked_array([arr]))
|
284 |
+
tm.assert_extension_array_equal(result, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/test_cumulative.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import pandas._testing as tm
|
4 |
+
from pandas.core.arrays import DatetimeArray
|
5 |
+
|
6 |
+
|
7 |
+
class TestAccumulator:
|
8 |
+
def test_accumulators_freq(self):
|
9 |
+
# GH#50297
|
10 |
+
arr = DatetimeArray._from_sequence(
|
11 |
+
[
|
12 |
+
"2000-01-01",
|
13 |
+
"2000-01-02",
|
14 |
+
"2000-01-03",
|
15 |
+
],
|
16 |
+
dtype="M8[ns]",
|
17 |
+
)._with_freq("infer")
|
18 |
+
result = arr._accumulate("cummin")
|
19 |
+
expected = DatetimeArray._from_sequence(["2000-01-01"] * 3, dtype="M8[ns]")
|
20 |
+
tm.assert_datetime_array_equal(result, expected)
|
21 |
+
|
22 |
+
result = arr._accumulate("cummax")
|
23 |
+
expected = DatetimeArray._from_sequence(
|
24 |
+
[
|
25 |
+
"2000-01-01",
|
26 |
+
"2000-01-02",
|
27 |
+
"2000-01-03",
|
28 |
+
],
|
29 |
+
dtype="M8[ns]",
|
30 |
+
)
|
31 |
+
tm.assert_datetime_array_equal(result, expected)
|
32 |
+
|
33 |
+
@pytest.mark.parametrize("func", ["cumsum", "cumprod"])
|
34 |
+
def test_accumulators_disallowed(self, func):
|
35 |
+
# GH#50297
|
36 |
+
arr = DatetimeArray._from_sequence(
|
37 |
+
[
|
38 |
+
"2000-01-01",
|
39 |
+
"2000-01-02",
|
40 |
+
],
|
41 |
+
dtype="M8[ns]",
|
42 |
+
)._with_freq("infer")
|
43 |
+
with pytest.raises(TypeError, match=f"Accumulation {func}"):
|
44 |
+
arr._accumulate(func)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/datetimes/test_reductions.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas.core.dtypes.dtypes import DatetimeTZDtype
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
from pandas import NaT
|
8 |
+
import pandas._testing as tm
|
9 |
+
from pandas.core.arrays import DatetimeArray
|
10 |
+
|
11 |
+
|
12 |
+
class TestReductions:
|
13 |
+
@pytest.fixture(params=["s", "ms", "us", "ns"])
|
14 |
+
def unit(self, request):
|
15 |
+
return request.param
|
16 |
+
|
17 |
+
@pytest.fixture
|
18 |
+
def arr1d(self, tz_naive_fixture):
|
19 |
+
"""Fixture returning DatetimeArray with parametrized timezones"""
|
20 |
+
tz = tz_naive_fixture
|
21 |
+
dtype = DatetimeTZDtype(tz=tz) if tz is not None else np.dtype("M8[ns]")
|
22 |
+
arr = DatetimeArray._from_sequence(
|
23 |
+
[
|
24 |
+
"2000-01-03",
|
25 |
+
"2000-01-03",
|
26 |
+
"NaT",
|
27 |
+
"2000-01-02",
|
28 |
+
"2000-01-05",
|
29 |
+
"2000-01-04",
|
30 |
+
],
|
31 |
+
dtype=dtype,
|
32 |
+
)
|
33 |
+
return arr
|
34 |
+
|
35 |
+
def test_min_max(self, arr1d, unit):
|
36 |
+
arr = arr1d
|
37 |
+
arr = arr.as_unit(unit)
|
38 |
+
tz = arr.tz
|
39 |
+
|
40 |
+
result = arr.min()
|
41 |
+
expected = pd.Timestamp("2000-01-02", tz=tz).as_unit(unit)
|
42 |
+
assert result == expected
|
43 |
+
assert result.unit == expected.unit
|
44 |
+
|
45 |
+
result = arr.max()
|
46 |
+
expected = pd.Timestamp("2000-01-05", tz=tz).as_unit(unit)
|
47 |
+
assert result == expected
|
48 |
+
assert result.unit == expected.unit
|
49 |
+
|
50 |
+
result = arr.min(skipna=False)
|
51 |
+
assert result is NaT
|
52 |
+
|
53 |
+
result = arr.max(skipna=False)
|
54 |
+
assert result is NaT
|
55 |
+
|
56 |
+
@pytest.mark.parametrize("tz", [None, "US/Central"])
|
57 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
58 |
+
def test_min_max_empty(self, skipna, tz):
|
59 |
+
dtype = DatetimeTZDtype(tz=tz) if tz is not None else np.dtype("M8[ns]")
|
60 |
+
arr = DatetimeArray._from_sequence([], dtype=dtype)
|
61 |
+
result = arr.min(skipna=skipna)
|
62 |
+
assert result is NaT
|
63 |
+
|
64 |
+
result = arr.max(skipna=skipna)
|
65 |
+
assert result is NaT
|
66 |
+
|
67 |
+
@pytest.mark.parametrize("tz", [None, "US/Central"])
|
68 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
69 |
+
def test_median_empty(self, skipna, tz):
|
70 |
+
dtype = DatetimeTZDtype(tz=tz) if tz is not None else np.dtype("M8[ns]")
|
71 |
+
arr = DatetimeArray._from_sequence([], dtype=dtype)
|
72 |
+
result = arr.median(skipna=skipna)
|
73 |
+
assert result is NaT
|
74 |
+
|
75 |
+
arr = arr.reshape(0, 3)
|
76 |
+
result = arr.median(axis=0, skipna=skipna)
|
77 |
+
expected = type(arr)._from_sequence([NaT, NaT, NaT], dtype=arr.dtype)
|
78 |
+
tm.assert_equal(result, expected)
|
79 |
+
|
80 |
+
result = arr.median(axis=1, skipna=skipna)
|
81 |
+
expected = type(arr)._from_sequence([], dtype=arr.dtype)
|
82 |
+
tm.assert_equal(result, expected)
|
83 |
+
|
84 |
+
def test_median(self, arr1d):
|
85 |
+
arr = arr1d
|
86 |
+
|
87 |
+
result = arr.median()
|
88 |
+
assert result == arr[0]
|
89 |
+
result = arr.median(skipna=False)
|
90 |
+
assert result is NaT
|
91 |
+
|
92 |
+
result = arr.dropna().median(skipna=False)
|
93 |
+
assert result == arr[0]
|
94 |
+
|
95 |
+
result = arr.median(axis=0)
|
96 |
+
assert result == arr[0]
|
97 |
+
|
98 |
+
def test_median_axis(self, arr1d):
|
99 |
+
arr = arr1d
|
100 |
+
assert arr.median(axis=0) == arr.median()
|
101 |
+
assert arr.median(axis=0, skipna=False) is NaT
|
102 |
+
|
103 |
+
msg = r"abs\(axis\) must be less than ndim"
|
104 |
+
with pytest.raises(ValueError, match=msg):
|
105 |
+
arr.median(axis=1)
|
106 |
+
|
107 |
+
@pytest.mark.filterwarnings("ignore:All-NaN slice encountered:RuntimeWarning")
|
108 |
+
def test_median_2d(self, arr1d):
|
109 |
+
arr = arr1d.reshape(1, -1)
|
110 |
+
|
111 |
+
# axis = None
|
112 |
+
assert arr.median() == arr1d.median()
|
113 |
+
assert arr.median(skipna=False) is NaT
|
114 |
+
|
115 |
+
# axis = 0
|
116 |
+
result = arr.median(axis=0)
|
117 |
+
expected = arr1d
|
118 |
+
tm.assert_equal(result, expected)
|
119 |
+
|
120 |
+
# Since column 3 is all-NaT, we get NaT there with or without skipna
|
121 |
+
result = arr.median(axis=0, skipna=False)
|
122 |
+
expected = arr1d
|
123 |
+
tm.assert_equal(result, expected)
|
124 |
+
|
125 |
+
# axis = 1
|
126 |
+
result = arr.median(axis=1)
|
127 |
+
expected = type(arr)._from_sequence([arr1d.median()], dtype=arr.dtype)
|
128 |
+
tm.assert_equal(result, expected)
|
129 |
+
|
130 |
+
result = arr.median(axis=1, skipna=False)
|
131 |
+
expected = type(arr)._from_sequence([NaT], dtype=arr.dtype)
|
132 |
+
tm.assert_equal(result, expected)
|
133 |
+
|
134 |
+
def test_mean(self, arr1d):
|
135 |
+
arr = arr1d
|
136 |
+
|
137 |
+
# manually verified result
|
138 |
+
expected = arr[0] + 0.4 * pd.Timedelta(days=1)
|
139 |
+
|
140 |
+
result = arr.mean()
|
141 |
+
assert result == expected
|
142 |
+
result = arr.mean(skipna=False)
|
143 |
+
assert result is NaT
|
144 |
+
|
145 |
+
result = arr.dropna().mean(skipna=False)
|
146 |
+
assert result == expected
|
147 |
+
|
148 |
+
result = arr.mean(axis=0)
|
149 |
+
assert result == expected
|
150 |
+
|
151 |
+
def test_mean_2d(self):
|
152 |
+
dti = pd.date_range("2016-01-01", periods=6, tz="US/Pacific")
|
153 |
+
dta = dti._data.reshape(3, 2)
|
154 |
+
|
155 |
+
result = dta.mean(axis=0)
|
156 |
+
expected = dta[1]
|
157 |
+
tm.assert_datetime_array_equal(result, expected)
|
158 |
+
|
159 |
+
result = dta.mean(axis=1)
|
160 |
+
expected = dta[:, 0] + pd.Timedelta(hours=12)
|
161 |
+
tm.assert_datetime_array_equal(result, expected)
|
162 |
+
|
163 |
+
result = dta.mean(axis=None)
|
164 |
+
expected = dti.mean()
|
165 |
+
assert result == expected
|
166 |
+
|
167 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
168 |
+
def test_mean_empty(self, arr1d, skipna):
|
169 |
+
arr = arr1d[:0]
|
170 |
+
|
171 |
+
assert arr.mean(skipna=skipna) is NaT
|
172 |
+
|
173 |
+
arr2d = arr.reshape(0, 3)
|
174 |
+
result = arr2d.mean(axis=0, skipna=skipna)
|
175 |
+
expected = DatetimeArray._from_sequence([NaT, NaT, NaT], dtype=arr.dtype)
|
176 |
+
tm.assert_datetime_array_equal(result, expected)
|
177 |
+
|
178 |
+
result = arr2d.mean(axis=1, skipna=skipna)
|
179 |
+
expected = arr # i.e. 1D, empty
|
180 |
+
tm.assert_datetime_array_equal(result, expected)
|
181 |
+
|
182 |
+
result = arr2d.mean(axis=None, skipna=skipna)
|
183 |
+
assert result is NaT
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (200 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/conftest.cpython-310.pyc
ADDED
Binary file (1.8 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
Binary file (9.39 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_comparison.cpython-310.pyc
ADDED
Binary file (1.49 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_concat.cpython-310.pyc
ADDED
Binary file (1.85 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_construction.cpython-310.pyc
ADDED
Binary file (6.81 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_dtypes.cpython-310.pyc
ADDED
Binary file (7.19 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_function.cpython-310.pyc
ADDED
Binary file (5.9 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (809 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_reduction.cpython-310.pyc
ADDED
Binary file (2.96 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/arrays/integer/__pycache__/test_repr.cpython-310.pyc
ADDED
Binary file (1.94 kB). View file
|
|
llmeval-env/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
|
llmeval-env/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
|
llmeval-env/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
|
llmeval-env/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)
|
llmeval-env/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)
|
llmeval-env/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)
|
llmeval-env/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)
|
llmeval-env/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)
|
llmeval-env/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)
|
llmeval-env/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
|