Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/test_arrow_compat.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/test_function.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/test_indexing.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_arithmetic.py +248 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_arrow_compat.py +209 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_function.py +74 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_indexing.py +60 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/test_arrow_compat.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/test_astype.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/test_constructors.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/test_reductions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/test_arrow_compat.py +130 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/test_astype.py +67 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/test_constructors.py +156 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/test_reductions.py +42 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_array.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_astype.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_combine_concat.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_constructors.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_libsparse.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_arithmetics.py +514 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_astype.py +133 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_combine_concat.py +62 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_unary.py +79 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/conftest.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_api.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_apply.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_cython_aggregations.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_dtypes.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_ewm.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_expanding.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_groupby.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_numba.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_online.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_rolling.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_rolling_functions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_rolling_quantile.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_timeseries_window.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_win_type.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/conftest.py +146 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/moments/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/window/moments/__pycache__/__init__.cpython-310.pyc +0 -0
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (191 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
Binary file (6.48 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/test_arrow_compat.cpython-310.pyc
ADDED
Binary file (6.35 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/test_function.cpython-310.pyc
ADDED
Binary file (2.53 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (2.07 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_arithmetic.py
ADDED
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
from typing import Any
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import pytest
|
7 |
+
|
8 |
+
import pandas as pd
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
# integer dtypes
|
12 |
+
arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_INT_EA_DTYPES]
|
13 |
+
scalars: list[Any] = [2] * len(arrays)
|
14 |
+
# floating dtypes
|
15 |
+
arrays += [pd.array([0.1, 0.2, 0.3, None], dtype=dtype) for dtype in tm.FLOAT_EA_DTYPES]
|
16 |
+
scalars += [0.2, 0.2]
|
17 |
+
# boolean
|
18 |
+
arrays += [pd.array([True, False, True, None], dtype="boolean")]
|
19 |
+
scalars += [False]
|
20 |
+
|
21 |
+
|
22 |
+
@pytest.fixture(params=zip(arrays, scalars), ids=[a.dtype.name for a in arrays])
|
23 |
+
def data(request):
|
24 |
+
"""Fixture returning parametrized (array, scalar) tuple.
|
25 |
+
|
26 |
+
Used to test equivalence of scalars, numpy arrays with array ops, and the
|
27 |
+
equivalence of DataFrame and Series ops.
|
28 |
+
"""
|
29 |
+
return request.param
|
30 |
+
|
31 |
+
|
32 |
+
def check_skip(data, op_name):
|
33 |
+
if isinstance(data.dtype, pd.BooleanDtype) and "sub" in op_name:
|
34 |
+
pytest.skip("subtract not implemented for boolean")
|
35 |
+
|
36 |
+
|
37 |
+
def is_bool_not_implemented(data, op_name):
|
38 |
+
# match non-masked behavior
|
39 |
+
return data.dtype.kind == "b" and op_name.strip("_").lstrip("r") in [
|
40 |
+
"pow",
|
41 |
+
"truediv",
|
42 |
+
"floordiv",
|
43 |
+
]
|
44 |
+
|
45 |
+
|
46 |
+
# Test equivalence of scalars, numpy arrays with array ops
|
47 |
+
# -----------------------------------------------------------------------------
|
48 |
+
|
49 |
+
|
50 |
+
def test_array_scalar_like_equivalence(data, all_arithmetic_operators):
|
51 |
+
data, scalar = data
|
52 |
+
op = tm.get_op_from_name(all_arithmetic_operators)
|
53 |
+
check_skip(data, all_arithmetic_operators)
|
54 |
+
|
55 |
+
scalar_array = pd.array([scalar] * len(data), dtype=data.dtype)
|
56 |
+
|
57 |
+
# TODO also add len-1 array (np.array([scalar], dtype=data.dtype.numpy_dtype))
|
58 |
+
for scalar in [scalar, data.dtype.type(scalar)]:
|
59 |
+
if is_bool_not_implemented(data, all_arithmetic_operators):
|
60 |
+
msg = "operator '.*' not implemented for bool dtypes"
|
61 |
+
with pytest.raises(NotImplementedError, match=msg):
|
62 |
+
op(data, scalar)
|
63 |
+
with pytest.raises(NotImplementedError, match=msg):
|
64 |
+
op(data, scalar_array)
|
65 |
+
else:
|
66 |
+
result = op(data, scalar)
|
67 |
+
expected = op(data, scalar_array)
|
68 |
+
tm.assert_extension_array_equal(result, expected)
|
69 |
+
|
70 |
+
|
71 |
+
def test_array_NA(data, all_arithmetic_operators):
|
72 |
+
data, _ = data
|
73 |
+
op = tm.get_op_from_name(all_arithmetic_operators)
|
74 |
+
check_skip(data, all_arithmetic_operators)
|
75 |
+
|
76 |
+
scalar = pd.NA
|
77 |
+
scalar_array = pd.array([pd.NA] * len(data), dtype=data.dtype)
|
78 |
+
|
79 |
+
mask = data._mask.copy()
|
80 |
+
|
81 |
+
if is_bool_not_implemented(data, all_arithmetic_operators):
|
82 |
+
msg = "operator '.*' not implemented for bool dtypes"
|
83 |
+
with pytest.raises(NotImplementedError, match=msg):
|
84 |
+
op(data, scalar)
|
85 |
+
# GH#45421 check op doesn't alter data._mask inplace
|
86 |
+
tm.assert_numpy_array_equal(mask, data._mask)
|
87 |
+
return
|
88 |
+
|
89 |
+
result = op(data, scalar)
|
90 |
+
# GH#45421 check op doesn't alter data._mask inplace
|
91 |
+
tm.assert_numpy_array_equal(mask, data._mask)
|
92 |
+
|
93 |
+
expected = op(data, scalar_array)
|
94 |
+
tm.assert_numpy_array_equal(mask, data._mask)
|
95 |
+
|
96 |
+
tm.assert_extension_array_equal(result, expected)
|
97 |
+
|
98 |
+
|
99 |
+
def test_numpy_array_equivalence(data, all_arithmetic_operators):
|
100 |
+
data, scalar = data
|
101 |
+
op = tm.get_op_from_name(all_arithmetic_operators)
|
102 |
+
check_skip(data, all_arithmetic_operators)
|
103 |
+
|
104 |
+
numpy_array = np.array([scalar] * len(data), dtype=data.dtype.numpy_dtype)
|
105 |
+
pd_array = pd.array(numpy_array, dtype=data.dtype)
|
106 |
+
|
107 |
+
if is_bool_not_implemented(data, all_arithmetic_operators):
|
108 |
+
msg = "operator '.*' not implemented for bool dtypes"
|
109 |
+
with pytest.raises(NotImplementedError, match=msg):
|
110 |
+
op(data, numpy_array)
|
111 |
+
with pytest.raises(NotImplementedError, match=msg):
|
112 |
+
op(data, pd_array)
|
113 |
+
return
|
114 |
+
|
115 |
+
result = op(data, numpy_array)
|
116 |
+
expected = op(data, pd_array)
|
117 |
+
tm.assert_extension_array_equal(result, expected)
|
118 |
+
|
119 |
+
|
120 |
+
# Test equivalence with Series and DataFrame ops
|
121 |
+
# -----------------------------------------------------------------------------
|
122 |
+
|
123 |
+
|
124 |
+
def test_frame(data, all_arithmetic_operators):
|
125 |
+
data, scalar = data
|
126 |
+
op = tm.get_op_from_name(all_arithmetic_operators)
|
127 |
+
check_skip(data, all_arithmetic_operators)
|
128 |
+
|
129 |
+
# DataFrame with scalar
|
130 |
+
df = pd.DataFrame({"A": data})
|
131 |
+
|
132 |
+
if is_bool_not_implemented(data, all_arithmetic_operators):
|
133 |
+
msg = "operator '.*' not implemented for bool dtypes"
|
134 |
+
with pytest.raises(NotImplementedError, match=msg):
|
135 |
+
op(df, scalar)
|
136 |
+
with pytest.raises(NotImplementedError, match=msg):
|
137 |
+
op(data, scalar)
|
138 |
+
return
|
139 |
+
|
140 |
+
result = op(df, scalar)
|
141 |
+
expected = pd.DataFrame({"A": op(data, scalar)})
|
142 |
+
tm.assert_frame_equal(result, expected)
|
143 |
+
|
144 |
+
|
145 |
+
def test_series(data, all_arithmetic_operators):
|
146 |
+
data, scalar = data
|
147 |
+
op = tm.get_op_from_name(all_arithmetic_operators)
|
148 |
+
check_skip(data, all_arithmetic_operators)
|
149 |
+
|
150 |
+
ser = pd.Series(data)
|
151 |
+
|
152 |
+
others = [
|
153 |
+
scalar,
|
154 |
+
np.array([scalar] * len(data), dtype=data.dtype.numpy_dtype),
|
155 |
+
pd.array([scalar] * len(data), dtype=data.dtype),
|
156 |
+
pd.Series([scalar] * len(data), dtype=data.dtype),
|
157 |
+
]
|
158 |
+
|
159 |
+
for other in others:
|
160 |
+
if is_bool_not_implemented(data, all_arithmetic_operators):
|
161 |
+
msg = "operator '.*' not implemented for bool dtypes"
|
162 |
+
with pytest.raises(NotImplementedError, match=msg):
|
163 |
+
op(ser, other)
|
164 |
+
|
165 |
+
else:
|
166 |
+
result = op(ser, other)
|
167 |
+
expected = pd.Series(op(data, other))
|
168 |
+
tm.assert_series_equal(result, expected)
|
169 |
+
|
170 |
+
|
171 |
+
# Test generic characteristics / errors
|
172 |
+
# -----------------------------------------------------------------------------
|
173 |
+
|
174 |
+
|
175 |
+
def test_error_invalid_object(data, all_arithmetic_operators):
|
176 |
+
data, _ = data
|
177 |
+
|
178 |
+
op = all_arithmetic_operators
|
179 |
+
opa = getattr(data, op)
|
180 |
+
|
181 |
+
# 2d -> return NotImplemented
|
182 |
+
result = opa(pd.DataFrame({"A": data}))
|
183 |
+
assert result is NotImplemented
|
184 |
+
|
185 |
+
msg = r"can only perform ops with 1-d structures"
|
186 |
+
with pytest.raises(NotImplementedError, match=msg):
|
187 |
+
opa(np.arange(len(data)).reshape(-1, len(data)))
|
188 |
+
|
189 |
+
|
190 |
+
def test_error_len_mismatch(data, all_arithmetic_operators):
|
191 |
+
# operating with a list-like with non-matching length raises
|
192 |
+
data, scalar = data
|
193 |
+
op = tm.get_op_from_name(all_arithmetic_operators)
|
194 |
+
|
195 |
+
other = [scalar] * (len(data) - 1)
|
196 |
+
|
197 |
+
err = ValueError
|
198 |
+
msg = "|".join(
|
199 |
+
[
|
200 |
+
r"operands could not be broadcast together with shapes \(3,\) \(4,\)",
|
201 |
+
r"operands could not be broadcast together with shapes \(4,\) \(3,\)",
|
202 |
+
]
|
203 |
+
)
|
204 |
+
if data.dtype.kind == "b" and all_arithmetic_operators.strip("_") in [
|
205 |
+
"sub",
|
206 |
+
"rsub",
|
207 |
+
]:
|
208 |
+
err = TypeError
|
209 |
+
msg = (
|
210 |
+
r"numpy boolean subtract, the `\-` operator, is not supported, use "
|
211 |
+
r"the bitwise_xor, the `\^` operator, or the logical_xor function instead"
|
212 |
+
)
|
213 |
+
elif is_bool_not_implemented(data, all_arithmetic_operators):
|
214 |
+
msg = "operator '.*' not implemented for bool dtypes"
|
215 |
+
err = NotImplementedError
|
216 |
+
|
217 |
+
for other in [other, np.array(other)]:
|
218 |
+
with pytest.raises(err, match=msg):
|
219 |
+
op(data, other)
|
220 |
+
|
221 |
+
s = pd.Series(data)
|
222 |
+
with pytest.raises(err, match=msg):
|
223 |
+
op(s, other)
|
224 |
+
|
225 |
+
|
226 |
+
@pytest.mark.parametrize("op", ["__neg__", "__abs__", "__invert__"])
|
227 |
+
def test_unary_op_does_not_propagate_mask(data, op):
|
228 |
+
# https://github.com/pandas-dev/pandas/issues/39943
|
229 |
+
data, _ = data
|
230 |
+
ser = pd.Series(data)
|
231 |
+
|
232 |
+
if op == "__invert__" and data.dtype.kind == "f":
|
233 |
+
# we follow numpy in raising
|
234 |
+
msg = "ufunc 'invert' not supported for the input types"
|
235 |
+
with pytest.raises(TypeError, match=msg):
|
236 |
+
getattr(ser, op)()
|
237 |
+
with pytest.raises(TypeError, match=msg):
|
238 |
+
getattr(data, op)()
|
239 |
+
with pytest.raises(TypeError, match=msg):
|
240 |
+
# Check that this is still the numpy behavior
|
241 |
+
getattr(data._data, op)()
|
242 |
+
|
243 |
+
return
|
244 |
+
|
245 |
+
result = getattr(ser, op)()
|
246 |
+
expected = result.copy(deep=True)
|
247 |
+
ser[0] = None
|
248 |
+
tm.assert_series_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_arrow_compat.py
ADDED
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
import pandas._testing as tm
|
6 |
+
|
7 |
+
pytestmark = pytest.mark.filterwarnings(
|
8 |
+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
|
9 |
+
)
|
10 |
+
|
11 |
+
pa = pytest.importorskip("pyarrow")
|
12 |
+
|
13 |
+
from pandas.core.arrays.arrow._arrow_utils import pyarrow_array_to_numpy_and_mask
|
14 |
+
|
15 |
+
arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_INT_EA_DTYPES]
|
16 |
+
arrays += [pd.array([0.1, 0.2, 0.3, None], dtype=dtype) for dtype in tm.FLOAT_EA_DTYPES]
|
17 |
+
arrays += [pd.array([True, False, True, None], dtype="boolean")]
|
18 |
+
|
19 |
+
|
20 |
+
@pytest.fixture(params=arrays, ids=[a.dtype.name for a in arrays])
|
21 |
+
def data(request):
|
22 |
+
"""
|
23 |
+
Fixture returning parametrized array from given dtype, including integer,
|
24 |
+
float and boolean
|
25 |
+
"""
|
26 |
+
return request.param
|
27 |
+
|
28 |
+
|
29 |
+
def test_arrow_array(data):
|
30 |
+
arr = pa.array(data)
|
31 |
+
expected = pa.array(
|
32 |
+
data.to_numpy(object, na_value=None),
|
33 |
+
type=pa.from_numpy_dtype(data.dtype.numpy_dtype),
|
34 |
+
)
|
35 |
+
assert arr.equals(expected)
|
36 |
+
|
37 |
+
|
38 |
+
def test_arrow_roundtrip(data):
|
39 |
+
df = pd.DataFrame({"a": data})
|
40 |
+
table = pa.table(df)
|
41 |
+
assert table.field("a").type == str(data.dtype.numpy_dtype)
|
42 |
+
|
43 |
+
result = table.to_pandas()
|
44 |
+
assert result["a"].dtype == data.dtype
|
45 |
+
tm.assert_frame_equal(result, df)
|
46 |
+
|
47 |
+
|
48 |
+
def test_dataframe_from_arrow_types_mapper():
|
49 |
+
def types_mapper(arrow_type):
|
50 |
+
if pa.types.is_boolean(arrow_type):
|
51 |
+
return pd.BooleanDtype()
|
52 |
+
elif pa.types.is_integer(arrow_type):
|
53 |
+
return pd.Int64Dtype()
|
54 |
+
|
55 |
+
bools_array = pa.array([True, None, False], type=pa.bool_())
|
56 |
+
ints_array = pa.array([1, None, 2], type=pa.int64())
|
57 |
+
small_ints_array = pa.array([-1, 0, 7], type=pa.int8())
|
58 |
+
record_batch = pa.RecordBatch.from_arrays(
|
59 |
+
[bools_array, ints_array, small_ints_array], ["bools", "ints", "small_ints"]
|
60 |
+
)
|
61 |
+
result = record_batch.to_pandas(types_mapper=types_mapper)
|
62 |
+
bools = pd.Series([True, None, False], dtype="boolean")
|
63 |
+
ints = pd.Series([1, None, 2], dtype="Int64")
|
64 |
+
small_ints = pd.Series([-1, 0, 7], dtype="Int64")
|
65 |
+
expected = pd.DataFrame({"bools": bools, "ints": ints, "small_ints": small_ints})
|
66 |
+
tm.assert_frame_equal(result, expected)
|
67 |
+
|
68 |
+
|
69 |
+
def test_arrow_load_from_zero_chunks(data):
|
70 |
+
# GH-41040
|
71 |
+
|
72 |
+
df = pd.DataFrame({"a": data[0:0]})
|
73 |
+
table = pa.table(df)
|
74 |
+
assert table.field("a").type == str(data.dtype.numpy_dtype)
|
75 |
+
table = pa.table(
|
76 |
+
[pa.chunked_array([], type=table.field("a").type)], schema=table.schema
|
77 |
+
)
|
78 |
+
result = table.to_pandas()
|
79 |
+
assert result["a"].dtype == data.dtype
|
80 |
+
tm.assert_frame_equal(result, df)
|
81 |
+
|
82 |
+
|
83 |
+
def test_arrow_from_arrow_uint():
|
84 |
+
# https://github.com/pandas-dev/pandas/issues/31896
|
85 |
+
# possible mismatch in types
|
86 |
+
|
87 |
+
dtype = pd.UInt32Dtype()
|
88 |
+
result = dtype.__from_arrow__(pa.array([1, 2, 3, 4, None], type="int64"))
|
89 |
+
expected = pd.array([1, 2, 3, 4, None], dtype="UInt32")
|
90 |
+
|
91 |
+
tm.assert_extension_array_equal(result, expected)
|
92 |
+
|
93 |
+
|
94 |
+
def test_arrow_sliced(data):
|
95 |
+
# https://github.com/pandas-dev/pandas/issues/38525
|
96 |
+
|
97 |
+
df = pd.DataFrame({"a": data})
|
98 |
+
table = pa.table(df)
|
99 |
+
result = table.slice(2, None).to_pandas()
|
100 |
+
expected = df.iloc[2:].reset_index(drop=True)
|
101 |
+
tm.assert_frame_equal(result, expected)
|
102 |
+
|
103 |
+
# no missing values
|
104 |
+
df2 = df.fillna(data[0])
|
105 |
+
table = pa.table(df2)
|
106 |
+
result = table.slice(2, None).to_pandas()
|
107 |
+
expected = df2.iloc[2:].reset_index(drop=True)
|
108 |
+
tm.assert_frame_equal(result, expected)
|
109 |
+
|
110 |
+
|
111 |
+
@pytest.fixture
|
112 |
+
def np_dtype_to_arrays(any_real_numpy_dtype):
|
113 |
+
"""
|
114 |
+
Fixture returning actual and expected dtype, pandas and numpy arrays and
|
115 |
+
mask from a given numpy dtype
|
116 |
+
"""
|
117 |
+
np_dtype = np.dtype(any_real_numpy_dtype)
|
118 |
+
pa_type = pa.from_numpy_dtype(np_dtype)
|
119 |
+
|
120 |
+
# None ensures the creation of a bitmask buffer.
|
121 |
+
pa_array = pa.array([0, 1, 2, None], type=pa_type)
|
122 |
+
# Since masked Arrow buffer slots are not required to contain a specific
|
123 |
+
# value, assert only the first three values of the created np.array
|
124 |
+
np_expected = np.array([0, 1, 2], dtype=np_dtype)
|
125 |
+
mask_expected = np.array([True, True, True, False])
|
126 |
+
return np_dtype, pa_array, np_expected, mask_expected
|
127 |
+
|
128 |
+
|
129 |
+
def test_pyarrow_array_to_numpy_and_mask(np_dtype_to_arrays):
|
130 |
+
"""
|
131 |
+
Test conversion from pyarrow array to numpy array.
|
132 |
+
|
133 |
+
Modifies the pyarrow buffer to contain padding and offset, which are
|
134 |
+
considered valid buffers by pyarrow.
|
135 |
+
|
136 |
+
Also tests empty pyarrow arrays with non empty buffers.
|
137 |
+
See https://github.com/pandas-dev/pandas/issues/40896
|
138 |
+
"""
|
139 |
+
np_dtype, pa_array, np_expected, mask_expected = np_dtype_to_arrays
|
140 |
+
data, mask = pyarrow_array_to_numpy_and_mask(pa_array, np_dtype)
|
141 |
+
tm.assert_numpy_array_equal(data[:3], np_expected)
|
142 |
+
tm.assert_numpy_array_equal(mask, mask_expected)
|
143 |
+
|
144 |
+
mask_buffer = pa_array.buffers()[0]
|
145 |
+
data_buffer = pa_array.buffers()[1]
|
146 |
+
data_buffer_bytes = pa_array.buffers()[1].to_pybytes()
|
147 |
+
|
148 |
+
# Add trailing padding to the buffer.
|
149 |
+
data_buffer_trail = pa.py_buffer(data_buffer_bytes + b"\x00")
|
150 |
+
pa_array_trail = pa.Array.from_buffers(
|
151 |
+
type=pa_array.type,
|
152 |
+
length=len(pa_array),
|
153 |
+
buffers=[mask_buffer, data_buffer_trail],
|
154 |
+
offset=pa_array.offset,
|
155 |
+
)
|
156 |
+
pa_array_trail.validate()
|
157 |
+
data, mask = pyarrow_array_to_numpy_and_mask(pa_array_trail, np_dtype)
|
158 |
+
tm.assert_numpy_array_equal(data[:3], np_expected)
|
159 |
+
tm.assert_numpy_array_equal(mask, mask_expected)
|
160 |
+
|
161 |
+
# Add offset to the buffer.
|
162 |
+
offset = b"\x00" * (pa_array.type.bit_width // 8)
|
163 |
+
data_buffer_offset = pa.py_buffer(offset + data_buffer_bytes)
|
164 |
+
mask_buffer_offset = pa.py_buffer(b"\x0E")
|
165 |
+
pa_array_offset = pa.Array.from_buffers(
|
166 |
+
type=pa_array.type,
|
167 |
+
length=len(pa_array),
|
168 |
+
buffers=[mask_buffer_offset, data_buffer_offset],
|
169 |
+
offset=pa_array.offset + 1,
|
170 |
+
)
|
171 |
+
pa_array_offset.validate()
|
172 |
+
data, mask = pyarrow_array_to_numpy_and_mask(pa_array_offset, np_dtype)
|
173 |
+
tm.assert_numpy_array_equal(data[:3], np_expected)
|
174 |
+
tm.assert_numpy_array_equal(mask, mask_expected)
|
175 |
+
|
176 |
+
# Empty array
|
177 |
+
np_expected_empty = np.array([], dtype=np_dtype)
|
178 |
+
mask_expected_empty = np.array([], dtype=np.bool_)
|
179 |
+
|
180 |
+
pa_array_offset = pa.Array.from_buffers(
|
181 |
+
type=pa_array.type,
|
182 |
+
length=0,
|
183 |
+
buffers=[mask_buffer, data_buffer],
|
184 |
+
offset=pa_array.offset,
|
185 |
+
)
|
186 |
+
pa_array_offset.validate()
|
187 |
+
data, mask = pyarrow_array_to_numpy_and_mask(pa_array_offset, np_dtype)
|
188 |
+
tm.assert_numpy_array_equal(data[:3], np_expected_empty)
|
189 |
+
tm.assert_numpy_array_equal(mask, mask_expected_empty)
|
190 |
+
|
191 |
+
|
192 |
+
@pytest.mark.parametrize(
|
193 |
+
"arr", [pa.nulls(10), pa.chunked_array([pa.nulls(4), pa.nulls(6)])]
|
194 |
+
)
|
195 |
+
def test_from_arrow_null(data, arr):
|
196 |
+
res = data.dtype.__from_arrow__(arr)
|
197 |
+
assert res.isna().all()
|
198 |
+
assert len(res) == 10
|
199 |
+
|
200 |
+
|
201 |
+
def test_from_arrow_type_error(data):
|
202 |
+
# ensure that __from_arrow__ returns a TypeError when getting a wrong
|
203 |
+
# array type
|
204 |
+
|
205 |
+
arr = pa.array(data).cast("string")
|
206 |
+
with pytest.raises(TypeError, match=None):
|
207 |
+
# we don't test the exact error message, only the fact that it raises
|
208 |
+
# a TypeError is relevant
|
209 |
+
data.dtype.__from_arrow__(arr)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_function.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas.core.dtypes.common import is_integer_dtype
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
import pandas._testing as tm
|
8 |
+
from pandas.core.arrays import BaseMaskedArray
|
9 |
+
|
10 |
+
arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_INT_EA_DTYPES]
|
11 |
+
arrays += [
|
12 |
+
pd.array([0.141, -0.268, 5.895, None], dtype=dtype) for dtype in tm.FLOAT_EA_DTYPES
|
13 |
+
]
|
14 |
+
|
15 |
+
|
16 |
+
@pytest.fixture(params=arrays, ids=[a.dtype.name for a in arrays])
|
17 |
+
def data(request):
|
18 |
+
"""
|
19 |
+
Fixture returning parametrized 'data' array with different integer and
|
20 |
+
floating point types
|
21 |
+
"""
|
22 |
+
return request.param
|
23 |
+
|
24 |
+
|
25 |
+
@pytest.fixture()
|
26 |
+
def numpy_dtype(data):
|
27 |
+
"""
|
28 |
+
Fixture returning numpy dtype from 'data' input array.
|
29 |
+
"""
|
30 |
+
# For integer dtype, the numpy conversion must be done to float
|
31 |
+
if is_integer_dtype(data):
|
32 |
+
numpy_dtype = float
|
33 |
+
else:
|
34 |
+
numpy_dtype = data.dtype.type
|
35 |
+
return numpy_dtype
|
36 |
+
|
37 |
+
|
38 |
+
def test_round(data, numpy_dtype):
|
39 |
+
# No arguments
|
40 |
+
result = data.round()
|
41 |
+
expected = pd.array(
|
42 |
+
np.round(data.to_numpy(dtype=numpy_dtype, na_value=None)), dtype=data.dtype
|
43 |
+
)
|
44 |
+
tm.assert_extension_array_equal(result, expected)
|
45 |
+
|
46 |
+
# Decimals argument
|
47 |
+
result = data.round(decimals=2)
|
48 |
+
expected = pd.array(
|
49 |
+
np.round(data.to_numpy(dtype=numpy_dtype, na_value=None), decimals=2),
|
50 |
+
dtype=data.dtype,
|
51 |
+
)
|
52 |
+
tm.assert_extension_array_equal(result, expected)
|
53 |
+
|
54 |
+
|
55 |
+
def test_tolist(data):
|
56 |
+
result = data.tolist()
|
57 |
+
expected = list(data)
|
58 |
+
tm.assert_equal(result, expected)
|
59 |
+
|
60 |
+
|
61 |
+
def test_to_numpy():
|
62 |
+
# GH#56991
|
63 |
+
|
64 |
+
class MyStringArray(BaseMaskedArray):
|
65 |
+
dtype = pd.StringDtype()
|
66 |
+
_dtype_cls = pd.StringDtype
|
67 |
+
_internal_fill_value = pd.NA
|
68 |
+
|
69 |
+
arr = MyStringArray(
|
70 |
+
values=np.array(["a", "b", "c"]), mask=np.array([False, True, False])
|
71 |
+
)
|
72 |
+
result = arr.to_numpy()
|
73 |
+
expected = np.array(["a", pd.NA, "c"])
|
74 |
+
tm.assert_numpy_array_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_indexing.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
|
9 |
+
class TestSetitemValidation:
|
10 |
+
def _check_setitem_invalid(self, arr, invalid):
|
11 |
+
msg = f"Invalid value '{str(invalid)}' for dtype {arr.dtype}"
|
12 |
+
msg = re.escape(msg)
|
13 |
+
with pytest.raises(TypeError, match=msg):
|
14 |
+
arr[0] = invalid
|
15 |
+
|
16 |
+
with pytest.raises(TypeError, match=msg):
|
17 |
+
arr[:] = invalid
|
18 |
+
|
19 |
+
with pytest.raises(TypeError, match=msg):
|
20 |
+
arr[[0]] = invalid
|
21 |
+
|
22 |
+
# FIXME: don't leave commented-out
|
23 |
+
# with pytest.raises(TypeError):
|
24 |
+
# arr[[0]] = [invalid]
|
25 |
+
|
26 |
+
# with pytest.raises(TypeError):
|
27 |
+
# arr[[0]] = np.array([invalid], dtype=object)
|
28 |
+
|
29 |
+
# Series non-coercion, behavior subject to change
|
30 |
+
ser = pd.Series(arr)
|
31 |
+
with pytest.raises(TypeError, match=msg):
|
32 |
+
ser[0] = invalid
|
33 |
+
# TODO: so, so many other variants of this...
|
34 |
+
|
35 |
+
_invalid_scalars = [
|
36 |
+
1 + 2j,
|
37 |
+
"True",
|
38 |
+
"1",
|
39 |
+
"1.0",
|
40 |
+
pd.NaT,
|
41 |
+
np.datetime64("NaT"),
|
42 |
+
np.timedelta64("NaT"),
|
43 |
+
]
|
44 |
+
|
45 |
+
@pytest.mark.parametrize(
|
46 |
+
"invalid", _invalid_scalars + [1, 1.0, np.int64(1), np.float64(1)]
|
47 |
+
)
|
48 |
+
def test_setitem_validation_scalar_bool(self, invalid):
|
49 |
+
arr = pd.array([True, False, None], dtype="boolean")
|
50 |
+
self._check_setitem_invalid(arr, invalid)
|
51 |
+
|
52 |
+
@pytest.mark.parametrize("invalid", _invalid_scalars + [True, 1.5, np.float64(1.5)])
|
53 |
+
def test_setitem_validation_scalar_int(self, invalid, any_int_ea_dtype):
|
54 |
+
arr = pd.array([1, 2, None], dtype=any_int_ea_dtype)
|
55 |
+
self._check_setitem_invalid(arr, invalid)
|
56 |
+
|
57 |
+
@pytest.mark.parametrize("invalid", _invalid_scalars + [True])
|
58 |
+
def test_setitem_validation_scalar_float(self, invalid, float_ea_dtype):
|
59 |
+
arr = pd.array([1, 2, None], dtype=float_ea_dtype)
|
60 |
+
self._check_setitem_invalid(arr, invalid)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (191 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/test_arrow_compat.cpython-310.pyc
ADDED
Binary file (3.68 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (2.34 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (5.37 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/__pycache__/test_reductions.cpython-310.pyc
ADDED
Binary file (1.22 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/test_arrow_compat.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas.compat.pyarrow import pa_version_under10p1
|
4 |
+
|
5 |
+
from pandas.core.dtypes.dtypes import PeriodDtype
|
6 |
+
|
7 |
+
import pandas as pd
|
8 |
+
import pandas._testing as tm
|
9 |
+
from pandas.core.arrays import (
|
10 |
+
PeriodArray,
|
11 |
+
period_array,
|
12 |
+
)
|
13 |
+
|
14 |
+
pytestmark = pytest.mark.filterwarnings(
|
15 |
+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
|
16 |
+
)
|
17 |
+
|
18 |
+
|
19 |
+
pa = pytest.importorskip("pyarrow")
|
20 |
+
|
21 |
+
|
22 |
+
def test_arrow_extension_type():
|
23 |
+
from pandas.core.arrays.arrow.extension_types import ArrowPeriodType
|
24 |
+
|
25 |
+
p1 = ArrowPeriodType("D")
|
26 |
+
p2 = ArrowPeriodType("D")
|
27 |
+
p3 = ArrowPeriodType("M")
|
28 |
+
|
29 |
+
assert p1.freq == "D"
|
30 |
+
assert p1 == p2
|
31 |
+
assert p1 != p3
|
32 |
+
assert hash(p1) == hash(p2)
|
33 |
+
assert hash(p1) != hash(p3)
|
34 |
+
|
35 |
+
|
36 |
+
@pytest.mark.xfail(not pa_version_under10p1, reason="Wrong behavior with pyarrow 10")
|
37 |
+
@pytest.mark.parametrize(
|
38 |
+
"data, freq",
|
39 |
+
[
|
40 |
+
(pd.date_range("2017", periods=3), "D"),
|
41 |
+
(pd.date_range("2017", periods=3, freq="YE"), "Y-DEC"),
|
42 |
+
],
|
43 |
+
)
|
44 |
+
def test_arrow_array(data, freq):
|
45 |
+
from pandas.core.arrays.arrow.extension_types import ArrowPeriodType
|
46 |
+
|
47 |
+
periods = period_array(data, freq=freq)
|
48 |
+
result = pa.array(periods)
|
49 |
+
assert isinstance(result.type, ArrowPeriodType)
|
50 |
+
assert result.type.freq == freq
|
51 |
+
expected = pa.array(periods.asi8, type="int64")
|
52 |
+
assert result.storage.equals(expected)
|
53 |
+
|
54 |
+
# convert to its storage type
|
55 |
+
result = pa.array(periods, type=pa.int64())
|
56 |
+
assert result.equals(expected)
|
57 |
+
|
58 |
+
# unsupported conversions
|
59 |
+
msg = "Not supported to convert PeriodArray to 'double' type"
|
60 |
+
with pytest.raises(TypeError, match=msg):
|
61 |
+
pa.array(periods, type="float64")
|
62 |
+
|
63 |
+
with pytest.raises(TypeError, match="different 'freq'"):
|
64 |
+
pa.array(periods, type=ArrowPeriodType("T"))
|
65 |
+
|
66 |
+
|
67 |
+
def test_arrow_array_missing():
|
68 |
+
from pandas.core.arrays.arrow.extension_types import ArrowPeriodType
|
69 |
+
|
70 |
+
arr = PeriodArray([1, 2, 3], dtype="period[D]")
|
71 |
+
arr[1] = pd.NaT
|
72 |
+
|
73 |
+
result = pa.array(arr)
|
74 |
+
assert isinstance(result.type, ArrowPeriodType)
|
75 |
+
assert result.type.freq == "D"
|
76 |
+
expected = pa.array([1, None, 3], type="int64")
|
77 |
+
assert result.storage.equals(expected)
|
78 |
+
|
79 |
+
|
80 |
+
def test_arrow_table_roundtrip():
|
81 |
+
from pandas.core.arrays.arrow.extension_types import ArrowPeriodType
|
82 |
+
|
83 |
+
arr = PeriodArray([1, 2, 3], dtype="period[D]")
|
84 |
+
arr[1] = pd.NaT
|
85 |
+
df = pd.DataFrame({"a": arr})
|
86 |
+
|
87 |
+
table = pa.table(df)
|
88 |
+
assert isinstance(table.field("a").type, ArrowPeriodType)
|
89 |
+
result = table.to_pandas()
|
90 |
+
assert isinstance(result["a"].dtype, PeriodDtype)
|
91 |
+
tm.assert_frame_equal(result, df)
|
92 |
+
|
93 |
+
table2 = pa.concat_tables([table, table])
|
94 |
+
result = table2.to_pandas()
|
95 |
+
expected = pd.concat([df, df], ignore_index=True)
|
96 |
+
tm.assert_frame_equal(result, expected)
|
97 |
+
|
98 |
+
|
99 |
+
def test_arrow_load_from_zero_chunks():
|
100 |
+
# GH-41040
|
101 |
+
|
102 |
+
from pandas.core.arrays.arrow.extension_types import ArrowPeriodType
|
103 |
+
|
104 |
+
arr = PeriodArray([], dtype="period[D]")
|
105 |
+
df = pd.DataFrame({"a": arr})
|
106 |
+
|
107 |
+
table = pa.table(df)
|
108 |
+
assert isinstance(table.field("a").type, ArrowPeriodType)
|
109 |
+
table = pa.table(
|
110 |
+
[pa.chunked_array([], type=table.column(0).type)], schema=table.schema
|
111 |
+
)
|
112 |
+
|
113 |
+
result = table.to_pandas()
|
114 |
+
assert isinstance(result["a"].dtype, PeriodDtype)
|
115 |
+
tm.assert_frame_equal(result, df)
|
116 |
+
|
117 |
+
|
118 |
+
def test_arrow_table_roundtrip_without_metadata():
|
119 |
+
arr = PeriodArray([1, 2, 3], dtype="period[h]")
|
120 |
+
arr[1] = pd.NaT
|
121 |
+
df = pd.DataFrame({"a": arr})
|
122 |
+
|
123 |
+
table = pa.table(df)
|
124 |
+
# remove the metadata
|
125 |
+
table = table.replace_schema_metadata()
|
126 |
+
assert table.schema.metadata is None
|
127 |
+
|
128 |
+
result = table.to_pandas()
|
129 |
+
assert isinstance(result["a"].dtype, PeriodDtype)
|
130 |
+
tm.assert_frame_equal(result, df)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/test_astype.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas.core.dtypes.dtypes import PeriodDtype
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
import pandas._testing as tm
|
8 |
+
from pandas.core.arrays import period_array
|
9 |
+
|
10 |
+
|
11 |
+
@pytest.mark.parametrize("dtype", [int, np.int32, np.int64, "uint32", "uint64"])
|
12 |
+
def test_astype_int(dtype):
|
13 |
+
# We choose to ignore the sign and size of integers for
|
14 |
+
# Period/Datetime/Timedelta astype
|
15 |
+
arr = period_array(["2000", "2001", None], freq="D")
|
16 |
+
|
17 |
+
if np.dtype(dtype) != np.int64:
|
18 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
19 |
+
arr.astype(dtype)
|
20 |
+
return
|
21 |
+
|
22 |
+
result = arr.astype(dtype)
|
23 |
+
expected = arr._ndarray.view("i8")
|
24 |
+
tm.assert_numpy_array_equal(result, expected)
|
25 |
+
|
26 |
+
|
27 |
+
def test_astype_copies():
|
28 |
+
arr = period_array(["2000", "2001", None], freq="D")
|
29 |
+
result = arr.astype(np.int64, copy=False)
|
30 |
+
|
31 |
+
# Add the `.base`, since we now use `.asi8` which returns a view.
|
32 |
+
# We could maybe override it in PeriodArray to return ._ndarray directly.
|
33 |
+
assert result.base is arr._ndarray
|
34 |
+
|
35 |
+
result = arr.astype(np.int64, copy=True)
|
36 |
+
assert result is not arr._ndarray
|
37 |
+
tm.assert_numpy_array_equal(result, arr._ndarray.view("i8"))
|
38 |
+
|
39 |
+
|
40 |
+
def test_astype_categorical():
|
41 |
+
arr = period_array(["2000", "2001", "2001", None], freq="D")
|
42 |
+
result = arr.astype("category")
|
43 |
+
categories = pd.PeriodIndex(["2000", "2001"], freq="D")
|
44 |
+
expected = pd.Categorical.from_codes([0, 1, 1, -1], categories=categories)
|
45 |
+
tm.assert_categorical_equal(result, expected)
|
46 |
+
|
47 |
+
|
48 |
+
def test_astype_period():
|
49 |
+
arr = period_array(["2000", "2001", None], freq="D")
|
50 |
+
result = arr.astype(PeriodDtype("M"))
|
51 |
+
expected = period_array(["2000", "2001", None], freq="M")
|
52 |
+
tm.assert_period_array_equal(result, expected)
|
53 |
+
|
54 |
+
|
55 |
+
@pytest.mark.parametrize("dtype", ["datetime64[ns]", "timedelta64[ns]"])
|
56 |
+
def test_astype_datetime(dtype):
|
57 |
+
arr = period_array(["2000", "2001", None], freq="D")
|
58 |
+
# slice off the [ns] so that the regex matches.
|
59 |
+
if dtype == "timedelta64[ns]":
|
60 |
+
with pytest.raises(TypeError, match=dtype[:-4]):
|
61 |
+
arr.astype(dtype)
|
62 |
+
|
63 |
+
else:
|
64 |
+
# GH#45038 allow period->dt64 because we allow dt64->period
|
65 |
+
result = arr.astype(dtype)
|
66 |
+
expected = pd.DatetimeIndex(["2000", "2001", pd.NaT], dtype=dtype)._data
|
67 |
+
tm.assert_datetime_array_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/test_constructors.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas._libs.tslibs import iNaT
|
5 |
+
from pandas._libs.tslibs.offsets import MonthEnd
|
6 |
+
from pandas._libs.tslibs.period import IncompatibleFrequency
|
7 |
+
|
8 |
+
import pandas as pd
|
9 |
+
import pandas._testing as tm
|
10 |
+
from pandas.core.arrays import (
|
11 |
+
PeriodArray,
|
12 |
+
period_array,
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
@pytest.mark.parametrize(
|
17 |
+
"data, freq, expected",
|
18 |
+
[
|
19 |
+
([pd.Period("2017", "D")], None, [17167]),
|
20 |
+
([pd.Period("2017", "D")], "D", [17167]),
|
21 |
+
([2017], "D", [17167]),
|
22 |
+
(["2017"], "D", [17167]),
|
23 |
+
([pd.Period("2017", "D")], pd.tseries.offsets.Day(), [17167]),
|
24 |
+
([pd.Period("2017", "D"), None], None, [17167, iNaT]),
|
25 |
+
(pd.Series(pd.date_range("2017", periods=3)), None, [17167, 17168, 17169]),
|
26 |
+
(pd.date_range("2017", periods=3), None, [17167, 17168, 17169]),
|
27 |
+
(pd.period_range("2017", periods=4, freq="Q"), None, [188, 189, 190, 191]),
|
28 |
+
],
|
29 |
+
)
|
30 |
+
def test_period_array_ok(data, freq, expected):
|
31 |
+
result = period_array(data, freq=freq).asi8
|
32 |
+
expected = np.asarray(expected, dtype=np.int64)
|
33 |
+
tm.assert_numpy_array_equal(result, expected)
|
34 |
+
|
35 |
+
|
36 |
+
def test_period_array_readonly_object():
|
37 |
+
# https://github.com/pandas-dev/pandas/issues/25403
|
38 |
+
pa = period_array([pd.Period("2019-01-01")])
|
39 |
+
arr = np.asarray(pa, dtype="object")
|
40 |
+
arr.setflags(write=False)
|
41 |
+
|
42 |
+
result = period_array(arr)
|
43 |
+
tm.assert_period_array_equal(result, pa)
|
44 |
+
|
45 |
+
result = pd.Series(arr)
|
46 |
+
tm.assert_series_equal(result, pd.Series(pa))
|
47 |
+
|
48 |
+
result = pd.DataFrame({"A": arr})
|
49 |
+
tm.assert_frame_equal(result, pd.DataFrame({"A": pa}))
|
50 |
+
|
51 |
+
|
52 |
+
def test_from_datetime64_freq_changes():
|
53 |
+
# https://github.com/pandas-dev/pandas/issues/23438
|
54 |
+
arr = pd.date_range("2017", periods=3, freq="D")
|
55 |
+
result = PeriodArray._from_datetime64(arr, freq="M")
|
56 |
+
expected = period_array(["2017-01-01", "2017-01-01", "2017-01-01"], freq="M")
|
57 |
+
tm.assert_period_array_equal(result, expected)
|
58 |
+
|
59 |
+
|
60 |
+
@pytest.mark.parametrize("freq", ["2M", MonthEnd(2)])
|
61 |
+
def test_from_datetime64_freq_2M(freq):
|
62 |
+
arr = np.array(
|
63 |
+
["2020-01-01T00:00:00", "2020-01-02T00:00:00"], dtype="datetime64[ns]"
|
64 |
+
)
|
65 |
+
result = PeriodArray._from_datetime64(arr, freq)
|
66 |
+
expected = period_array(["2020-01", "2020-01"], freq=freq)
|
67 |
+
tm.assert_period_array_equal(result, expected)
|
68 |
+
|
69 |
+
|
70 |
+
@pytest.mark.parametrize(
|
71 |
+
"data, freq, msg",
|
72 |
+
[
|
73 |
+
(
|
74 |
+
[pd.Period("2017", "D"), pd.Period("2017", "Y")],
|
75 |
+
None,
|
76 |
+
"Input has different freq",
|
77 |
+
),
|
78 |
+
([pd.Period("2017", "D")], "Y", "Input has different freq"),
|
79 |
+
],
|
80 |
+
)
|
81 |
+
def test_period_array_raises(data, freq, msg):
|
82 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
83 |
+
period_array(data, freq)
|
84 |
+
|
85 |
+
|
86 |
+
def test_period_array_non_period_series_raies():
|
87 |
+
ser = pd.Series([1, 2, 3])
|
88 |
+
with pytest.raises(TypeError, match="dtype"):
|
89 |
+
PeriodArray(ser, dtype="period[D]")
|
90 |
+
|
91 |
+
|
92 |
+
def test_period_array_freq_mismatch():
|
93 |
+
arr = period_array(["2000", "2001"], freq="D")
|
94 |
+
with pytest.raises(IncompatibleFrequency, match="freq"):
|
95 |
+
PeriodArray(arr, dtype="period[M]")
|
96 |
+
|
97 |
+
dtype = pd.PeriodDtype(pd.tseries.offsets.MonthEnd())
|
98 |
+
with pytest.raises(IncompatibleFrequency, match="freq"):
|
99 |
+
PeriodArray(arr, dtype=dtype)
|
100 |
+
|
101 |
+
|
102 |
+
def test_from_sequence_disallows_i8():
|
103 |
+
arr = period_array(["2000", "2001"], freq="D")
|
104 |
+
|
105 |
+
msg = str(arr[0].ordinal)
|
106 |
+
with pytest.raises(TypeError, match=msg):
|
107 |
+
PeriodArray._from_sequence(arr.asi8, dtype=arr.dtype)
|
108 |
+
|
109 |
+
with pytest.raises(TypeError, match=msg):
|
110 |
+
PeriodArray._from_sequence(list(arr.asi8), dtype=arr.dtype)
|
111 |
+
|
112 |
+
|
113 |
+
def test_from_td64nat_sequence_raises():
|
114 |
+
# GH#44507
|
115 |
+
td = pd.NaT.to_numpy("m8[ns]")
|
116 |
+
|
117 |
+
dtype = pd.period_range("2005-01-01", periods=3, freq="D").dtype
|
118 |
+
|
119 |
+
arr = np.array([None], dtype=object)
|
120 |
+
arr[0] = td
|
121 |
+
|
122 |
+
msg = "Value must be Period, string, integer, or datetime"
|
123 |
+
with pytest.raises(ValueError, match=msg):
|
124 |
+
PeriodArray._from_sequence(arr, dtype=dtype)
|
125 |
+
|
126 |
+
with pytest.raises(ValueError, match=msg):
|
127 |
+
pd.PeriodIndex(arr, dtype=dtype)
|
128 |
+
with pytest.raises(ValueError, match=msg):
|
129 |
+
pd.Index(arr, dtype=dtype)
|
130 |
+
with pytest.raises(ValueError, match=msg):
|
131 |
+
pd.array(arr, dtype=dtype)
|
132 |
+
with pytest.raises(ValueError, match=msg):
|
133 |
+
pd.Series(arr, dtype=dtype)
|
134 |
+
with pytest.raises(ValueError, match=msg):
|
135 |
+
pd.DataFrame(arr, dtype=dtype)
|
136 |
+
|
137 |
+
|
138 |
+
def test_freq_deprecated():
|
139 |
+
# GH#52462
|
140 |
+
data = np.arange(5).astype(np.int64)
|
141 |
+
msg = "The 'freq' keyword in the PeriodArray constructor is deprecated"
|
142 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
143 |
+
res = PeriodArray(data, freq="M")
|
144 |
+
|
145 |
+
expected = PeriodArray(data, dtype="period[M]")
|
146 |
+
tm.assert_equal(res, expected)
|
147 |
+
|
148 |
+
|
149 |
+
def test_period_array_from_datetime64():
|
150 |
+
arr = np.array(
|
151 |
+
["2020-01-01T00:00:00", "2020-02-02T00:00:00"], dtype="datetime64[ns]"
|
152 |
+
)
|
153 |
+
result = PeriodArray._from_datetime64(arr, freq=MonthEnd(2))
|
154 |
+
|
155 |
+
expected = period_array(["2020-01-01", "2020-02-01"], freq=MonthEnd(2))
|
156 |
+
tm.assert_period_array_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/period/test_reductions.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import pandas as pd
|
4 |
+
from pandas.core.arrays import period_array
|
5 |
+
|
6 |
+
|
7 |
+
class TestReductions:
|
8 |
+
def test_min_max(self):
|
9 |
+
arr = period_array(
|
10 |
+
[
|
11 |
+
"2000-01-03",
|
12 |
+
"2000-01-03",
|
13 |
+
"NaT",
|
14 |
+
"2000-01-02",
|
15 |
+
"2000-01-05",
|
16 |
+
"2000-01-04",
|
17 |
+
],
|
18 |
+
freq="D",
|
19 |
+
)
|
20 |
+
|
21 |
+
result = arr.min()
|
22 |
+
expected = pd.Period("2000-01-02", freq="D")
|
23 |
+
assert result == expected
|
24 |
+
|
25 |
+
result = arr.max()
|
26 |
+
expected = pd.Period("2000-01-05", freq="D")
|
27 |
+
assert result == expected
|
28 |
+
|
29 |
+
result = arr.min(skipna=False)
|
30 |
+
assert result is pd.NaT
|
31 |
+
|
32 |
+
result = arr.max(skipna=False)
|
33 |
+
assert result is pd.NaT
|
34 |
+
|
35 |
+
@pytest.mark.parametrize("skipna", [True, False])
|
36 |
+
def test_min_max_empty(self, skipna):
|
37 |
+
arr = period_array([], freq="D")
|
38 |
+
result = arr.min(skipna=skipna)
|
39 |
+
assert result is pd.NaT
|
40 |
+
|
41 |
+
result = arr.max(skipna=skipna)
|
42 |
+
assert result is pd.NaT
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_array.cpython-310.pyc
ADDED
Binary file (14.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (3.96 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_combine_concat.cpython-310.pyc
ADDED
Binary file (2.19 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (10.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/__pycache__/test_libsparse.cpython-310.pyc
ADDED
Binary file (13.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_arithmetics.py
ADDED
@@ -0,0 +1,514 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import operator
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
from pandas import SparseDtype
|
8 |
+
import pandas._testing as tm
|
9 |
+
from pandas.core.arrays.sparse import SparseArray
|
10 |
+
|
11 |
+
|
12 |
+
@pytest.fixture(params=["integer", "block"])
|
13 |
+
def kind(request):
|
14 |
+
"""kind kwarg to pass to SparseArray"""
|
15 |
+
return request.param
|
16 |
+
|
17 |
+
|
18 |
+
@pytest.fixture(params=[True, False])
|
19 |
+
def mix(request):
|
20 |
+
"""
|
21 |
+
Fixture returning True or False, determining whether to operate
|
22 |
+
op(sparse, dense) instead of op(sparse, sparse)
|
23 |
+
"""
|
24 |
+
return request.param
|
25 |
+
|
26 |
+
|
27 |
+
class TestSparseArrayArithmetics:
|
28 |
+
def _assert(self, a, b):
|
29 |
+
# We have to use tm.assert_sp_array_equal. See GH #45126
|
30 |
+
tm.assert_numpy_array_equal(a, b)
|
31 |
+
|
32 |
+
def _check_numeric_ops(self, a, b, a_dense, b_dense, mix: bool, op):
|
33 |
+
# Check that arithmetic behavior matches non-Sparse Series arithmetic
|
34 |
+
|
35 |
+
if isinstance(a_dense, np.ndarray):
|
36 |
+
expected = op(pd.Series(a_dense), b_dense).values
|
37 |
+
elif isinstance(b_dense, np.ndarray):
|
38 |
+
expected = op(a_dense, pd.Series(b_dense)).values
|
39 |
+
else:
|
40 |
+
raise NotImplementedError
|
41 |
+
|
42 |
+
with np.errstate(invalid="ignore", divide="ignore"):
|
43 |
+
if mix:
|
44 |
+
result = op(a, b_dense).to_dense()
|
45 |
+
else:
|
46 |
+
result = op(a, b).to_dense()
|
47 |
+
|
48 |
+
self._assert(result, expected)
|
49 |
+
|
50 |
+
def _check_bool_result(self, res):
|
51 |
+
assert isinstance(res, SparseArray)
|
52 |
+
assert isinstance(res.dtype, SparseDtype)
|
53 |
+
assert res.dtype.subtype == np.bool_
|
54 |
+
assert isinstance(res.fill_value, bool)
|
55 |
+
|
56 |
+
def _check_comparison_ops(self, a, b, a_dense, b_dense):
|
57 |
+
with np.errstate(invalid="ignore"):
|
58 |
+
# Unfortunately, trying to wrap the computation of each expected
|
59 |
+
# value is with np.errstate() is too tedious.
|
60 |
+
#
|
61 |
+
# sparse & sparse
|
62 |
+
self._check_bool_result(a == b)
|
63 |
+
self._assert((a == b).to_dense(), a_dense == b_dense)
|
64 |
+
|
65 |
+
self._check_bool_result(a != b)
|
66 |
+
self._assert((a != b).to_dense(), a_dense != b_dense)
|
67 |
+
|
68 |
+
self._check_bool_result(a >= b)
|
69 |
+
self._assert((a >= b).to_dense(), a_dense >= b_dense)
|
70 |
+
|
71 |
+
self._check_bool_result(a <= b)
|
72 |
+
self._assert((a <= b).to_dense(), a_dense <= b_dense)
|
73 |
+
|
74 |
+
self._check_bool_result(a > b)
|
75 |
+
self._assert((a > b).to_dense(), a_dense > b_dense)
|
76 |
+
|
77 |
+
self._check_bool_result(a < b)
|
78 |
+
self._assert((a < b).to_dense(), a_dense < b_dense)
|
79 |
+
|
80 |
+
# sparse & dense
|
81 |
+
self._check_bool_result(a == b_dense)
|
82 |
+
self._assert((a == b_dense).to_dense(), a_dense == b_dense)
|
83 |
+
|
84 |
+
self._check_bool_result(a != b_dense)
|
85 |
+
self._assert((a != b_dense).to_dense(), a_dense != b_dense)
|
86 |
+
|
87 |
+
self._check_bool_result(a >= b_dense)
|
88 |
+
self._assert((a >= b_dense).to_dense(), a_dense >= b_dense)
|
89 |
+
|
90 |
+
self._check_bool_result(a <= b_dense)
|
91 |
+
self._assert((a <= b_dense).to_dense(), a_dense <= b_dense)
|
92 |
+
|
93 |
+
self._check_bool_result(a > b_dense)
|
94 |
+
self._assert((a > b_dense).to_dense(), a_dense > b_dense)
|
95 |
+
|
96 |
+
self._check_bool_result(a < b_dense)
|
97 |
+
self._assert((a < b_dense).to_dense(), a_dense < b_dense)
|
98 |
+
|
99 |
+
def _check_logical_ops(self, a, b, a_dense, b_dense):
|
100 |
+
# sparse & sparse
|
101 |
+
self._check_bool_result(a & b)
|
102 |
+
self._assert((a & b).to_dense(), a_dense & b_dense)
|
103 |
+
|
104 |
+
self._check_bool_result(a | b)
|
105 |
+
self._assert((a | b).to_dense(), a_dense | b_dense)
|
106 |
+
# sparse & dense
|
107 |
+
self._check_bool_result(a & b_dense)
|
108 |
+
self._assert((a & b_dense).to_dense(), a_dense & b_dense)
|
109 |
+
|
110 |
+
self._check_bool_result(a | b_dense)
|
111 |
+
self._assert((a | b_dense).to_dense(), a_dense | b_dense)
|
112 |
+
|
113 |
+
@pytest.mark.parametrize("scalar", [0, 1, 3])
|
114 |
+
@pytest.mark.parametrize("fill_value", [None, 0, 2])
|
115 |
+
def test_float_scalar(
|
116 |
+
self, kind, mix, all_arithmetic_functions, fill_value, scalar, request
|
117 |
+
):
|
118 |
+
op = all_arithmetic_functions
|
119 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
120 |
+
a = SparseArray(values, kind=kind, fill_value=fill_value)
|
121 |
+
self._check_numeric_ops(a, scalar, values, scalar, mix, op)
|
122 |
+
|
123 |
+
def test_float_scalar_comparison(self, kind):
|
124 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
125 |
+
|
126 |
+
a = SparseArray(values, kind=kind)
|
127 |
+
self._check_comparison_ops(a, 1, values, 1)
|
128 |
+
self._check_comparison_ops(a, 0, values, 0)
|
129 |
+
self._check_comparison_ops(a, 3, values, 3)
|
130 |
+
|
131 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
132 |
+
self._check_comparison_ops(a, 1, values, 1)
|
133 |
+
self._check_comparison_ops(a, 0, values, 0)
|
134 |
+
self._check_comparison_ops(a, 3, values, 3)
|
135 |
+
|
136 |
+
a = SparseArray(values, kind=kind, fill_value=2)
|
137 |
+
self._check_comparison_ops(a, 1, values, 1)
|
138 |
+
self._check_comparison_ops(a, 0, values, 0)
|
139 |
+
self._check_comparison_ops(a, 3, values, 3)
|
140 |
+
|
141 |
+
def test_float_same_index_without_nans(self, kind, mix, all_arithmetic_functions):
|
142 |
+
# when sp_index are the same
|
143 |
+
op = all_arithmetic_functions
|
144 |
+
|
145 |
+
values = np.array([0.0, 1.0, 2.0, 6.0, 0.0, 0.0, 1.0, 2.0, 1.0, 0.0])
|
146 |
+
rvalues = np.array([0.0, 2.0, 3.0, 4.0, 0.0, 0.0, 1.0, 3.0, 2.0, 0.0])
|
147 |
+
|
148 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
149 |
+
b = SparseArray(rvalues, kind=kind, fill_value=0)
|
150 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
151 |
+
|
152 |
+
def test_float_same_index_with_nans(
|
153 |
+
self, kind, mix, all_arithmetic_functions, request
|
154 |
+
):
|
155 |
+
# when sp_index are the same
|
156 |
+
op = all_arithmetic_functions
|
157 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
158 |
+
rvalues = np.array([np.nan, 2, 3, 4, np.nan, 0, 1, 3, 2, np.nan])
|
159 |
+
|
160 |
+
a = SparseArray(values, kind=kind)
|
161 |
+
b = SparseArray(rvalues, kind=kind)
|
162 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
163 |
+
|
164 |
+
def test_float_same_index_comparison(self, kind):
|
165 |
+
# when sp_index are the same
|
166 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
167 |
+
rvalues = np.array([np.nan, 2, 3, 4, np.nan, 0, 1, 3, 2, np.nan])
|
168 |
+
|
169 |
+
a = SparseArray(values, kind=kind)
|
170 |
+
b = SparseArray(rvalues, kind=kind)
|
171 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
172 |
+
|
173 |
+
values = np.array([0.0, 1.0, 2.0, 6.0, 0.0, 0.0, 1.0, 2.0, 1.0, 0.0])
|
174 |
+
rvalues = np.array([0.0, 2.0, 3.0, 4.0, 0.0, 0.0, 1.0, 3.0, 2.0, 0.0])
|
175 |
+
|
176 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
177 |
+
b = SparseArray(rvalues, kind=kind, fill_value=0)
|
178 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
179 |
+
|
180 |
+
def test_float_array(self, kind, mix, all_arithmetic_functions):
|
181 |
+
op = all_arithmetic_functions
|
182 |
+
|
183 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
184 |
+
rvalues = np.array([2, np.nan, 2, 3, np.nan, 0, 1, 5, 2, np.nan])
|
185 |
+
|
186 |
+
a = SparseArray(values, kind=kind)
|
187 |
+
b = SparseArray(rvalues, kind=kind)
|
188 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
189 |
+
self._check_numeric_ops(a, b * 0, values, rvalues * 0, mix, op)
|
190 |
+
|
191 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
192 |
+
b = SparseArray(rvalues, kind=kind)
|
193 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
194 |
+
|
195 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
196 |
+
b = SparseArray(rvalues, kind=kind, fill_value=0)
|
197 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
198 |
+
|
199 |
+
a = SparseArray(values, kind=kind, fill_value=1)
|
200 |
+
b = SparseArray(rvalues, kind=kind, fill_value=2)
|
201 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
202 |
+
|
203 |
+
def test_float_array_different_kind(self, mix, all_arithmetic_functions):
|
204 |
+
op = all_arithmetic_functions
|
205 |
+
|
206 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
207 |
+
rvalues = np.array([2, np.nan, 2, 3, np.nan, 0, 1, 5, 2, np.nan])
|
208 |
+
|
209 |
+
a = SparseArray(values, kind="integer")
|
210 |
+
b = SparseArray(rvalues, kind="block")
|
211 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
212 |
+
self._check_numeric_ops(a, b * 0, values, rvalues * 0, mix, op)
|
213 |
+
|
214 |
+
a = SparseArray(values, kind="integer", fill_value=0)
|
215 |
+
b = SparseArray(rvalues, kind="block")
|
216 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
217 |
+
|
218 |
+
a = SparseArray(values, kind="integer", fill_value=0)
|
219 |
+
b = SparseArray(rvalues, kind="block", fill_value=0)
|
220 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
221 |
+
|
222 |
+
a = SparseArray(values, kind="integer", fill_value=1)
|
223 |
+
b = SparseArray(rvalues, kind="block", fill_value=2)
|
224 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
225 |
+
|
226 |
+
def test_float_array_comparison(self, kind):
|
227 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
228 |
+
rvalues = np.array([2, np.nan, 2, 3, np.nan, 0, 1, 5, 2, np.nan])
|
229 |
+
|
230 |
+
a = SparseArray(values, kind=kind)
|
231 |
+
b = SparseArray(rvalues, kind=kind)
|
232 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
233 |
+
self._check_comparison_ops(a, b * 0, values, rvalues * 0)
|
234 |
+
|
235 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
236 |
+
b = SparseArray(rvalues, kind=kind)
|
237 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
238 |
+
|
239 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
240 |
+
b = SparseArray(rvalues, kind=kind, fill_value=0)
|
241 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
242 |
+
|
243 |
+
a = SparseArray(values, kind=kind, fill_value=1)
|
244 |
+
b = SparseArray(rvalues, kind=kind, fill_value=2)
|
245 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
246 |
+
|
247 |
+
def test_int_array(self, kind, mix, all_arithmetic_functions):
|
248 |
+
op = all_arithmetic_functions
|
249 |
+
|
250 |
+
# have to specify dtype explicitly until fixing GH 667
|
251 |
+
dtype = np.int64
|
252 |
+
|
253 |
+
values = np.array([0, 1, 2, 0, 0, 0, 1, 2, 1, 0], dtype=dtype)
|
254 |
+
rvalues = np.array([2, 0, 2, 3, 0, 0, 1, 5, 2, 0], dtype=dtype)
|
255 |
+
|
256 |
+
a = SparseArray(values, dtype=dtype, kind=kind)
|
257 |
+
assert a.dtype == SparseDtype(dtype)
|
258 |
+
b = SparseArray(rvalues, dtype=dtype, kind=kind)
|
259 |
+
assert b.dtype == SparseDtype(dtype)
|
260 |
+
|
261 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
262 |
+
self._check_numeric_ops(a, b * 0, values, rvalues * 0, mix, op)
|
263 |
+
|
264 |
+
a = SparseArray(values, fill_value=0, dtype=dtype, kind=kind)
|
265 |
+
assert a.dtype == SparseDtype(dtype)
|
266 |
+
b = SparseArray(rvalues, dtype=dtype, kind=kind)
|
267 |
+
assert b.dtype == SparseDtype(dtype)
|
268 |
+
|
269 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
270 |
+
|
271 |
+
a = SparseArray(values, fill_value=0, dtype=dtype, kind=kind)
|
272 |
+
assert a.dtype == SparseDtype(dtype)
|
273 |
+
b = SparseArray(rvalues, fill_value=0, dtype=dtype, kind=kind)
|
274 |
+
assert b.dtype == SparseDtype(dtype)
|
275 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
276 |
+
|
277 |
+
a = SparseArray(values, fill_value=1, dtype=dtype, kind=kind)
|
278 |
+
assert a.dtype == SparseDtype(dtype, fill_value=1)
|
279 |
+
b = SparseArray(rvalues, fill_value=2, dtype=dtype, kind=kind)
|
280 |
+
assert b.dtype == SparseDtype(dtype, fill_value=2)
|
281 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
282 |
+
|
283 |
+
def test_int_array_comparison(self, kind):
|
284 |
+
dtype = "int64"
|
285 |
+
# int32 NI ATM
|
286 |
+
|
287 |
+
values = np.array([0, 1, 2, 0, 0, 0, 1, 2, 1, 0], dtype=dtype)
|
288 |
+
rvalues = np.array([2, 0, 2, 3, 0, 0, 1, 5, 2, 0], dtype=dtype)
|
289 |
+
|
290 |
+
a = SparseArray(values, dtype=dtype, kind=kind)
|
291 |
+
b = SparseArray(rvalues, dtype=dtype, kind=kind)
|
292 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
293 |
+
self._check_comparison_ops(a, b * 0, values, rvalues * 0)
|
294 |
+
|
295 |
+
a = SparseArray(values, dtype=dtype, kind=kind, fill_value=0)
|
296 |
+
b = SparseArray(rvalues, dtype=dtype, kind=kind)
|
297 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
298 |
+
|
299 |
+
a = SparseArray(values, dtype=dtype, kind=kind, fill_value=0)
|
300 |
+
b = SparseArray(rvalues, dtype=dtype, kind=kind, fill_value=0)
|
301 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
302 |
+
|
303 |
+
a = SparseArray(values, dtype=dtype, kind=kind, fill_value=1)
|
304 |
+
b = SparseArray(rvalues, dtype=dtype, kind=kind, fill_value=2)
|
305 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
306 |
+
|
307 |
+
@pytest.mark.parametrize("fill_value", [True, False, np.nan])
|
308 |
+
def test_bool_same_index(self, kind, fill_value):
|
309 |
+
# GH 14000
|
310 |
+
# when sp_index are the same
|
311 |
+
values = np.array([True, False, True, True], dtype=np.bool_)
|
312 |
+
rvalues = np.array([True, False, True, True], dtype=np.bool_)
|
313 |
+
|
314 |
+
a = SparseArray(values, kind=kind, dtype=np.bool_, fill_value=fill_value)
|
315 |
+
b = SparseArray(rvalues, kind=kind, dtype=np.bool_, fill_value=fill_value)
|
316 |
+
self._check_logical_ops(a, b, values, rvalues)
|
317 |
+
|
318 |
+
@pytest.mark.parametrize("fill_value", [True, False, np.nan])
|
319 |
+
def test_bool_array_logical(self, kind, fill_value):
|
320 |
+
# GH 14000
|
321 |
+
# when sp_index are the same
|
322 |
+
values = np.array([True, False, True, False, True, True], dtype=np.bool_)
|
323 |
+
rvalues = np.array([True, False, False, True, False, True], dtype=np.bool_)
|
324 |
+
|
325 |
+
a = SparseArray(values, kind=kind, dtype=np.bool_, fill_value=fill_value)
|
326 |
+
b = SparseArray(rvalues, kind=kind, dtype=np.bool_, fill_value=fill_value)
|
327 |
+
self._check_logical_ops(a, b, values, rvalues)
|
328 |
+
|
329 |
+
def test_mixed_array_float_int(self, kind, mix, all_arithmetic_functions, request):
|
330 |
+
op = all_arithmetic_functions
|
331 |
+
rdtype = "int64"
|
332 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
333 |
+
rvalues = np.array([2, 0, 2, 3, 0, 0, 1, 5, 2, 0], dtype=rdtype)
|
334 |
+
|
335 |
+
a = SparseArray(values, kind=kind)
|
336 |
+
b = SparseArray(rvalues, kind=kind)
|
337 |
+
assert b.dtype == SparseDtype(rdtype)
|
338 |
+
|
339 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
340 |
+
self._check_numeric_ops(a, b * 0, values, rvalues * 0, mix, op)
|
341 |
+
|
342 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
343 |
+
b = SparseArray(rvalues, kind=kind)
|
344 |
+
assert b.dtype == SparseDtype(rdtype)
|
345 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
346 |
+
|
347 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
348 |
+
b = SparseArray(rvalues, kind=kind, fill_value=0)
|
349 |
+
assert b.dtype == SparseDtype(rdtype)
|
350 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
351 |
+
|
352 |
+
a = SparseArray(values, kind=kind, fill_value=1)
|
353 |
+
b = SparseArray(rvalues, kind=kind, fill_value=2)
|
354 |
+
assert b.dtype == SparseDtype(rdtype, fill_value=2)
|
355 |
+
self._check_numeric_ops(a, b, values, rvalues, mix, op)
|
356 |
+
|
357 |
+
def test_mixed_array_comparison(self, kind):
|
358 |
+
rdtype = "int64"
|
359 |
+
# int32 NI ATM
|
360 |
+
|
361 |
+
values = np.array([np.nan, 1, 2, 0, np.nan, 0, 1, 2, 1, np.nan])
|
362 |
+
rvalues = np.array([2, 0, 2, 3, 0, 0, 1, 5, 2, 0], dtype=rdtype)
|
363 |
+
|
364 |
+
a = SparseArray(values, kind=kind)
|
365 |
+
b = SparseArray(rvalues, kind=kind)
|
366 |
+
assert b.dtype == SparseDtype(rdtype)
|
367 |
+
|
368 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
369 |
+
self._check_comparison_ops(a, b * 0, values, rvalues * 0)
|
370 |
+
|
371 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
372 |
+
b = SparseArray(rvalues, kind=kind)
|
373 |
+
assert b.dtype == SparseDtype(rdtype)
|
374 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
375 |
+
|
376 |
+
a = SparseArray(values, kind=kind, fill_value=0)
|
377 |
+
b = SparseArray(rvalues, kind=kind, fill_value=0)
|
378 |
+
assert b.dtype == SparseDtype(rdtype)
|
379 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
380 |
+
|
381 |
+
a = SparseArray(values, kind=kind, fill_value=1)
|
382 |
+
b = SparseArray(rvalues, kind=kind, fill_value=2)
|
383 |
+
assert b.dtype == SparseDtype(rdtype, fill_value=2)
|
384 |
+
self._check_comparison_ops(a, b, values, rvalues)
|
385 |
+
|
386 |
+
def test_xor(self):
|
387 |
+
s = SparseArray([True, True, False, False])
|
388 |
+
t = SparseArray([True, False, True, False])
|
389 |
+
result = s ^ t
|
390 |
+
sp_index = pd.core.arrays.sparse.IntIndex(4, np.array([0, 1, 2], dtype="int32"))
|
391 |
+
expected = SparseArray([False, True, True], sparse_index=sp_index)
|
392 |
+
tm.assert_sp_array_equal(result, expected)
|
393 |
+
|
394 |
+
|
395 |
+
@pytest.mark.parametrize("op", [operator.eq, operator.add])
|
396 |
+
def test_with_list(op):
|
397 |
+
arr = SparseArray([0, 1], fill_value=0)
|
398 |
+
result = op(arr, [0, 1])
|
399 |
+
expected = op(arr, SparseArray([0, 1]))
|
400 |
+
tm.assert_sp_array_equal(result, expected)
|
401 |
+
|
402 |
+
|
403 |
+
def test_with_dataframe():
|
404 |
+
# GH#27910
|
405 |
+
arr = SparseArray([0, 1], fill_value=0)
|
406 |
+
df = pd.DataFrame([[1, 2], [3, 4]])
|
407 |
+
result = arr.__add__(df)
|
408 |
+
assert result is NotImplemented
|
409 |
+
|
410 |
+
|
411 |
+
def test_with_zerodim_ndarray():
|
412 |
+
# GH#27910
|
413 |
+
arr = SparseArray([0, 1], fill_value=0)
|
414 |
+
|
415 |
+
result = arr * np.array(2)
|
416 |
+
expected = arr * 2
|
417 |
+
tm.assert_sp_array_equal(result, expected)
|
418 |
+
|
419 |
+
|
420 |
+
@pytest.mark.parametrize("ufunc", [np.abs, np.exp])
|
421 |
+
@pytest.mark.parametrize(
|
422 |
+
"arr", [SparseArray([0, 0, -1, 1]), SparseArray([None, None, -1, 1])]
|
423 |
+
)
|
424 |
+
def test_ufuncs(ufunc, arr):
|
425 |
+
result = ufunc(arr)
|
426 |
+
fill_value = ufunc(arr.fill_value)
|
427 |
+
expected = SparseArray(ufunc(np.asarray(arr)), fill_value=fill_value)
|
428 |
+
tm.assert_sp_array_equal(result, expected)
|
429 |
+
|
430 |
+
|
431 |
+
@pytest.mark.parametrize(
|
432 |
+
"a, b",
|
433 |
+
[
|
434 |
+
(SparseArray([0, 0, 0]), np.array([0, 1, 2])),
|
435 |
+
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
|
436 |
+
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
|
437 |
+
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
|
438 |
+
(SparseArray([0, 0, 0], fill_value=1), np.array([0, 1, 2])),
|
439 |
+
],
|
440 |
+
)
|
441 |
+
@pytest.mark.parametrize("ufunc", [np.add, np.greater])
|
442 |
+
def test_binary_ufuncs(ufunc, a, b):
|
443 |
+
# can't say anything about fill value here.
|
444 |
+
result = ufunc(a, b)
|
445 |
+
expected = ufunc(np.asarray(a), np.asarray(b))
|
446 |
+
assert isinstance(result, SparseArray)
|
447 |
+
tm.assert_numpy_array_equal(np.asarray(result), expected)
|
448 |
+
|
449 |
+
|
450 |
+
def test_ndarray_inplace():
|
451 |
+
sparray = SparseArray([0, 2, 0, 0])
|
452 |
+
ndarray = np.array([0, 1, 2, 3])
|
453 |
+
ndarray += sparray
|
454 |
+
expected = np.array([0, 3, 2, 3])
|
455 |
+
tm.assert_numpy_array_equal(ndarray, expected)
|
456 |
+
|
457 |
+
|
458 |
+
def test_sparray_inplace():
|
459 |
+
sparray = SparseArray([0, 2, 0, 0])
|
460 |
+
ndarray = np.array([0, 1, 2, 3])
|
461 |
+
sparray += ndarray
|
462 |
+
expected = SparseArray([0, 3, 2, 3], fill_value=0)
|
463 |
+
tm.assert_sp_array_equal(sparray, expected)
|
464 |
+
|
465 |
+
|
466 |
+
@pytest.mark.parametrize("cons", [list, np.array, SparseArray])
|
467 |
+
def test_mismatched_length_cmp_op(cons):
|
468 |
+
left = SparseArray([True, True])
|
469 |
+
right = cons([True, True, True])
|
470 |
+
with pytest.raises(ValueError, match="operands have mismatched length"):
|
471 |
+
left & right
|
472 |
+
|
473 |
+
|
474 |
+
@pytest.mark.parametrize("op", ["add", "sub", "mul", "truediv", "floordiv", "pow"])
|
475 |
+
@pytest.mark.parametrize("fill_value", [np.nan, 3])
|
476 |
+
def test_binary_operators(op, fill_value):
|
477 |
+
op = getattr(operator, op)
|
478 |
+
data1 = np.random.default_rng(2).standard_normal(20)
|
479 |
+
data2 = np.random.default_rng(2).standard_normal(20)
|
480 |
+
|
481 |
+
data1[::2] = fill_value
|
482 |
+
data2[::3] = fill_value
|
483 |
+
|
484 |
+
first = SparseArray(data1, fill_value=fill_value)
|
485 |
+
second = SparseArray(data2, fill_value=fill_value)
|
486 |
+
|
487 |
+
with np.errstate(all="ignore"):
|
488 |
+
res = op(first, second)
|
489 |
+
exp = SparseArray(
|
490 |
+
op(first.to_dense(), second.to_dense()), fill_value=first.fill_value
|
491 |
+
)
|
492 |
+
assert isinstance(res, SparseArray)
|
493 |
+
tm.assert_almost_equal(res.to_dense(), exp.to_dense())
|
494 |
+
|
495 |
+
res2 = op(first, second.to_dense())
|
496 |
+
assert isinstance(res2, SparseArray)
|
497 |
+
tm.assert_sp_array_equal(res, res2)
|
498 |
+
|
499 |
+
res3 = op(first.to_dense(), second)
|
500 |
+
assert isinstance(res3, SparseArray)
|
501 |
+
tm.assert_sp_array_equal(res, res3)
|
502 |
+
|
503 |
+
res4 = op(first, 4)
|
504 |
+
assert isinstance(res4, SparseArray)
|
505 |
+
|
506 |
+
# Ignore this if the actual op raises (e.g. pow).
|
507 |
+
try:
|
508 |
+
exp = op(first.to_dense(), 4)
|
509 |
+
exp_fv = op(first.fill_value, 4)
|
510 |
+
except ValueError:
|
511 |
+
pass
|
512 |
+
else:
|
513 |
+
tm.assert_almost_equal(res4.fill_value, exp_fv)
|
514 |
+
tm.assert_almost_equal(res4.to_dense(), exp)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_astype.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas._libs.sparse import IntIndex
|
5 |
+
|
6 |
+
from pandas import (
|
7 |
+
SparseDtype,
|
8 |
+
Timestamp,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
from pandas.core.arrays.sparse import SparseArray
|
12 |
+
|
13 |
+
|
14 |
+
class TestAstype:
|
15 |
+
def test_astype(self):
|
16 |
+
# float -> float
|
17 |
+
arr = SparseArray([None, None, 0, 2])
|
18 |
+
result = arr.astype("Sparse[float32]")
|
19 |
+
expected = SparseArray([None, None, 0, 2], dtype=np.dtype("float32"))
|
20 |
+
tm.assert_sp_array_equal(result, expected)
|
21 |
+
|
22 |
+
dtype = SparseDtype("float64", fill_value=0)
|
23 |
+
result = arr.astype(dtype)
|
24 |
+
expected = SparseArray._simple_new(
|
25 |
+
np.array([0.0, 2.0], dtype=dtype.subtype), IntIndex(4, [2, 3]), dtype
|
26 |
+
)
|
27 |
+
tm.assert_sp_array_equal(result, expected)
|
28 |
+
|
29 |
+
dtype = SparseDtype("int64", 0)
|
30 |
+
result = arr.astype(dtype)
|
31 |
+
expected = SparseArray._simple_new(
|
32 |
+
np.array([0, 2], dtype=np.int64), IntIndex(4, [2, 3]), dtype
|
33 |
+
)
|
34 |
+
tm.assert_sp_array_equal(result, expected)
|
35 |
+
|
36 |
+
arr = SparseArray([0, np.nan, 0, 1], fill_value=0)
|
37 |
+
with pytest.raises(ValueError, match="NA"):
|
38 |
+
arr.astype("Sparse[i8]")
|
39 |
+
|
40 |
+
def test_astype_bool(self):
|
41 |
+
a = SparseArray([1, 0, 0, 1], dtype=SparseDtype(int, 0))
|
42 |
+
result = a.astype(bool)
|
43 |
+
expected = np.array([1, 0, 0, 1], dtype=bool)
|
44 |
+
tm.assert_numpy_array_equal(result, expected)
|
45 |
+
|
46 |
+
# update fill value
|
47 |
+
result = a.astype(SparseDtype(bool, False))
|
48 |
+
expected = SparseArray(
|
49 |
+
[True, False, False, True], dtype=SparseDtype(bool, False)
|
50 |
+
)
|
51 |
+
tm.assert_sp_array_equal(result, expected)
|
52 |
+
|
53 |
+
def test_astype_all(self, any_real_numpy_dtype):
|
54 |
+
vals = np.array([1, 2, 3])
|
55 |
+
arr = SparseArray(vals, fill_value=1)
|
56 |
+
typ = np.dtype(any_real_numpy_dtype)
|
57 |
+
res = arr.astype(typ)
|
58 |
+
tm.assert_numpy_array_equal(res, vals.astype(any_real_numpy_dtype))
|
59 |
+
|
60 |
+
@pytest.mark.parametrize(
|
61 |
+
"arr, dtype, expected",
|
62 |
+
[
|
63 |
+
(
|
64 |
+
SparseArray([0, 1]),
|
65 |
+
"float",
|
66 |
+
SparseArray([0.0, 1.0], dtype=SparseDtype(float, 0.0)),
|
67 |
+
),
|
68 |
+
(SparseArray([0, 1]), bool, SparseArray([False, True])),
|
69 |
+
(
|
70 |
+
SparseArray([0, 1], fill_value=1),
|
71 |
+
bool,
|
72 |
+
SparseArray([False, True], dtype=SparseDtype(bool, True)),
|
73 |
+
),
|
74 |
+
pytest.param(
|
75 |
+
SparseArray([0, 1]),
|
76 |
+
"datetime64[ns]",
|
77 |
+
SparseArray(
|
78 |
+
np.array([0, 1], dtype="datetime64[ns]"),
|
79 |
+
dtype=SparseDtype("datetime64[ns]", Timestamp("1970")),
|
80 |
+
),
|
81 |
+
),
|
82 |
+
(
|
83 |
+
SparseArray([0, 1, 10]),
|
84 |
+
str,
|
85 |
+
SparseArray(["0", "1", "10"], dtype=SparseDtype(str, "0")),
|
86 |
+
),
|
87 |
+
(SparseArray(["10", "20"]), float, SparseArray([10.0, 20.0])),
|
88 |
+
(
|
89 |
+
SparseArray([0, 1, 0]),
|
90 |
+
object,
|
91 |
+
SparseArray([0, 1, 0], dtype=SparseDtype(object, 0)),
|
92 |
+
),
|
93 |
+
],
|
94 |
+
)
|
95 |
+
def test_astype_more(self, arr, dtype, expected):
|
96 |
+
result = arr.astype(arr.dtype.update_dtype(dtype))
|
97 |
+
tm.assert_sp_array_equal(result, expected)
|
98 |
+
|
99 |
+
def test_astype_nan_raises(self):
|
100 |
+
arr = SparseArray([1.0, np.nan])
|
101 |
+
with pytest.raises(ValueError, match="Cannot convert non-finite"):
|
102 |
+
arr.astype(int)
|
103 |
+
|
104 |
+
def test_astype_copy_false(self):
|
105 |
+
# GH#34456 bug caused by using .view instead of .astype in astype_nansafe
|
106 |
+
arr = SparseArray([1, 2, 3])
|
107 |
+
|
108 |
+
dtype = SparseDtype(float, 0)
|
109 |
+
|
110 |
+
result = arr.astype(dtype, copy=False)
|
111 |
+
expected = SparseArray([1.0, 2.0, 3.0], fill_value=0.0)
|
112 |
+
tm.assert_sp_array_equal(result, expected)
|
113 |
+
|
114 |
+
def test_astype_dt64_to_int64(self):
|
115 |
+
# GH#49631 match non-sparse behavior
|
116 |
+
values = np.array(["NaT", "2016-01-02", "2016-01-03"], dtype="M8[ns]")
|
117 |
+
|
118 |
+
arr = SparseArray(values)
|
119 |
+
result = arr.astype("int64")
|
120 |
+
expected = values.astype("int64")
|
121 |
+
tm.assert_numpy_array_equal(result, expected)
|
122 |
+
|
123 |
+
# we should also be able to cast to equivalent Sparse[int64]
|
124 |
+
dtype_int64 = SparseDtype("int64", np.iinfo(np.int64).min)
|
125 |
+
result2 = arr.astype(dtype_int64)
|
126 |
+
tm.assert_numpy_array_equal(result2.to_numpy(), expected)
|
127 |
+
|
128 |
+
# GH#50087 we should match the non-sparse behavior regardless of
|
129 |
+
# if we have a fill_value other than NaT
|
130 |
+
dtype = SparseDtype("datetime64[ns]", values[1])
|
131 |
+
arr3 = SparseArray(values, dtype=dtype)
|
132 |
+
result3 = arr3.astype("int64")
|
133 |
+
tm.assert_numpy_array_equal(result3, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_combine_concat.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.sparse import SparseArray
|
7 |
+
|
8 |
+
|
9 |
+
class TestSparseArrayConcat:
|
10 |
+
@pytest.mark.parametrize("kind", ["integer", "block"])
|
11 |
+
def test_basic(self, kind):
|
12 |
+
a = SparseArray([1, 0, 0, 2], kind=kind)
|
13 |
+
b = SparseArray([1, 0, 2, 2], kind=kind)
|
14 |
+
|
15 |
+
result = SparseArray._concat_same_type([a, b])
|
16 |
+
# Can't make any assertions about the sparse index itself
|
17 |
+
# since we aren't don't merge sparse blocs across arrays
|
18 |
+
# in to_concat
|
19 |
+
expected = np.array([1, 2, 1, 2, 2], dtype="int64")
|
20 |
+
tm.assert_numpy_array_equal(result.sp_values, expected)
|
21 |
+
assert result.kind == kind
|
22 |
+
|
23 |
+
@pytest.mark.parametrize("kind", ["integer", "block"])
|
24 |
+
def test_uses_first_kind(self, kind):
|
25 |
+
other = "integer" if kind == "block" else "block"
|
26 |
+
a = SparseArray([1, 0, 0, 2], kind=kind)
|
27 |
+
b = SparseArray([1, 0, 2, 2], kind=other)
|
28 |
+
|
29 |
+
result = SparseArray._concat_same_type([a, b])
|
30 |
+
expected = np.array([1, 2, 1, 2, 2], dtype="int64")
|
31 |
+
tm.assert_numpy_array_equal(result.sp_values, expected)
|
32 |
+
assert result.kind == kind
|
33 |
+
|
34 |
+
|
35 |
+
@pytest.mark.parametrize(
|
36 |
+
"other, expected_dtype",
|
37 |
+
[
|
38 |
+
# compatible dtype -> preserve sparse
|
39 |
+
(pd.Series([3, 4, 5], dtype="int64"), pd.SparseDtype("int64", 0)),
|
40 |
+
# (pd.Series([3, 4, 5], dtype="Int64"), pd.SparseDtype("int64", 0)),
|
41 |
+
# incompatible dtype -> Sparse[common dtype]
|
42 |
+
(pd.Series([1.5, 2.5, 3.5], dtype="float64"), pd.SparseDtype("float64", 0)),
|
43 |
+
# incompatible dtype -> Sparse[object] dtype
|
44 |
+
(pd.Series(["a", "b", "c"], dtype=object), pd.SparseDtype(object, 0)),
|
45 |
+
# categorical with compatible categories -> dtype of the categories
|
46 |
+
(pd.Series([3, 4, 5], dtype="category"), np.dtype("int64")),
|
47 |
+
(pd.Series([1.5, 2.5, 3.5], dtype="category"), np.dtype("float64")),
|
48 |
+
# categorical with incompatible categories -> object dtype
|
49 |
+
(pd.Series(["a", "b", "c"], dtype="category"), np.dtype(object)),
|
50 |
+
],
|
51 |
+
)
|
52 |
+
def test_concat_with_non_sparse(other, expected_dtype):
|
53 |
+
# https://github.com/pandas-dev/pandas/issues/34336
|
54 |
+
s_sparse = pd.Series([1, 0, 2], dtype=pd.SparseDtype("int64", 0))
|
55 |
+
|
56 |
+
result = pd.concat([s_sparse, other], ignore_index=True)
|
57 |
+
expected = pd.Series(list(s_sparse) + list(other)).astype(expected_dtype)
|
58 |
+
tm.assert_series_equal(result, expected)
|
59 |
+
|
60 |
+
result = pd.concat([other, s_sparse], ignore_index=True)
|
61 |
+
expected = pd.Series(list(other) + list(s_sparse)).astype(expected_dtype)
|
62 |
+
tm.assert_series_equal(result, expected)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/arrays/sparse/test_unary.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.arrays import SparseArray
|
9 |
+
|
10 |
+
|
11 |
+
@pytest.mark.filterwarnings("ignore:invalid value encountered in cast:RuntimeWarning")
|
12 |
+
@pytest.mark.parametrize("fill_value", [0, np.nan])
|
13 |
+
@pytest.mark.parametrize("op", [operator.pos, operator.neg])
|
14 |
+
def test_unary_op(op, fill_value):
|
15 |
+
arr = np.array([0, 1, np.nan, 2])
|
16 |
+
sparray = SparseArray(arr, fill_value=fill_value)
|
17 |
+
result = op(sparray)
|
18 |
+
expected = SparseArray(op(arr), fill_value=op(fill_value))
|
19 |
+
tm.assert_sp_array_equal(result, expected)
|
20 |
+
|
21 |
+
|
22 |
+
@pytest.mark.parametrize("fill_value", [True, False])
|
23 |
+
def test_invert(fill_value):
|
24 |
+
arr = np.array([True, False, False, True])
|
25 |
+
sparray = SparseArray(arr, fill_value=fill_value)
|
26 |
+
result = ~sparray
|
27 |
+
expected = SparseArray(~arr, fill_value=not fill_value)
|
28 |
+
tm.assert_sp_array_equal(result, expected)
|
29 |
+
|
30 |
+
result = ~pd.Series(sparray)
|
31 |
+
expected = pd.Series(expected)
|
32 |
+
tm.assert_series_equal(result, expected)
|
33 |
+
|
34 |
+
result = ~pd.DataFrame({"A": sparray})
|
35 |
+
expected = pd.DataFrame({"A": expected})
|
36 |
+
tm.assert_frame_equal(result, expected)
|
37 |
+
|
38 |
+
|
39 |
+
class TestUnaryMethods:
|
40 |
+
@pytest.mark.filterwarnings(
|
41 |
+
"ignore:invalid value encountered in cast:RuntimeWarning"
|
42 |
+
)
|
43 |
+
def test_neg_operator(self):
|
44 |
+
arr = SparseArray([-1, -2, np.nan, 3], fill_value=np.nan, dtype=np.int8)
|
45 |
+
res = -arr
|
46 |
+
exp = SparseArray([1, 2, np.nan, -3], fill_value=np.nan, dtype=np.int8)
|
47 |
+
tm.assert_sp_array_equal(exp, res)
|
48 |
+
|
49 |
+
arr = SparseArray([-1, -2, 1, 3], fill_value=-1, dtype=np.int8)
|
50 |
+
res = -arr
|
51 |
+
exp = SparseArray([1, 2, -1, -3], fill_value=1, dtype=np.int8)
|
52 |
+
tm.assert_sp_array_equal(exp, res)
|
53 |
+
|
54 |
+
@pytest.mark.filterwarnings(
|
55 |
+
"ignore:invalid value encountered in cast:RuntimeWarning"
|
56 |
+
)
|
57 |
+
def test_abs_operator(self):
|
58 |
+
arr = SparseArray([-1, -2, np.nan, 3], fill_value=np.nan, dtype=np.int8)
|
59 |
+
res = abs(arr)
|
60 |
+
exp = SparseArray([1, 2, np.nan, 3], fill_value=np.nan, dtype=np.int8)
|
61 |
+
tm.assert_sp_array_equal(exp, res)
|
62 |
+
|
63 |
+
arr = SparseArray([-1, -2, 1, 3], fill_value=-1, dtype=np.int8)
|
64 |
+
res = abs(arr)
|
65 |
+
exp = SparseArray([1, 2, 1, 3], fill_value=1, dtype=np.int8)
|
66 |
+
tm.assert_sp_array_equal(exp, res)
|
67 |
+
|
68 |
+
def test_invert_operator(self):
|
69 |
+
arr = SparseArray([False, True, False, True], fill_value=False, dtype=np.bool_)
|
70 |
+
exp = SparseArray(
|
71 |
+
np.invert([False, True, False, True]), fill_value=True, dtype=np.bool_
|
72 |
+
)
|
73 |
+
res = ~arr
|
74 |
+
tm.assert_sp_array_equal(exp, res)
|
75 |
+
|
76 |
+
arr = SparseArray([0, 1, 0, 2, 3, 0], fill_value=0, dtype=np.int32)
|
77 |
+
res = ~arr
|
78 |
+
exp = SparseArray([-1, -2, -1, -3, -4, -1], fill_value=-1, dtype=np.int32)
|
79 |
+
tm.assert_sp_array_equal(exp, res)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (184 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/conftest.cpython-310.pyc
ADDED
Binary file (3.46 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_api.cpython-310.pyc
ADDED
Binary file (11.7 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_apply.cpython-310.pyc
ADDED
Binary file (11 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_cython_aggregations.cpython-310.pyc
ADDED
Binary file (3.51 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_dtypes.cpython-310.pyc
ADDED
Binary file (4.06 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_ewm.cpython-310.pyc
ADDED
Binary file (18.7 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_expanding.cpython-310.pyc
ADDED
Binary file (21.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_groupby.cpython-310.pyc
ADDED
Binary file (38.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_numba.cpython-310.pyc
ADDED
Binary file (14.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_online.cpython-310.pyc
ADDED
Binary file (3.15 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_rolling.cpython-310.pyc
ADDED
Binary file (48.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_rolling_functions.cpython-310.pyc
ADDED
Binary file (17.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_rolling_quantile.cpython-310.pyc
ADDED
Binary file (5.51 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_timeseries_window.cpython-310.pyc
ADDED
Binary file (18.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/__pycache__/test_win_type.cpython-310.pyc
ADDED
Binary file (14.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/conftest.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import (
|
2 |
+
datetime,
|
3 |
+
timedelta,
|
4 |
+
)
|
5 |
+
|
6 |
+
import numpy as np
|
7 |
+
import pytest
|
8 |
+
|
9 |
+
import pandas.util._test_decorators as td
|
10 |
+
|
11 |
+
from pandas import (
|
12 |
+
DataFrame,
|
13 |
+
Series,
|
14 |
+
bdate_range,
|
15 |
+
)
|
16 |
+
|
17 |
+
|
18 |
+
@pytest.fixture(params=[True, False])
|
19 |
+
def raw(request):
|
20 |
+
"""raw keyword argument for rolling.apply"""
|
21 |
+
return request.param
|
22 |
+
|
23 |
+
|
24 |
+
@pytest.fixture(
|
25 |
+
params=[
|
26 |
+
"sum",
|
27 |
+
"mean",
|
28 |
+
"median",
|
29 |
+
"max",
|
30 |
+
"min",
|
31 |
+
"var",
|
32 |
+
"std",
|
33 |
+
"kurt",
|
34 |
+
"skew",
|
35 |
+
"count",
|
36 |
+
"sem",
|
37 |
+
]
|
38 |
+
)
|
39 |
+
def arithmetic_win_operators(request):
|
40 |
+
return request.param
|
41 |
+
|
42 |
+
|
43 |
+
@pytest.fixture(params=[True, False])
|
44 |
+
def center(request):
|
45 |
+
return request.param
|
46 |
+
|
47 |
+
|
48 |
+
@pytest.fixture(params=[None, 1])
|
49 |
+
def min_periods(request):
|
50 |
+
return request.param
|
51 |
+
|
52 |
+
|
53 |
+
@pytest.fixture(params=[True, False])
|
54 |
+
def parallel(request):
|
55 |
+
"""parallel keyword argument for numba.jit"""
|
56 |
+
return request.param
|
57 |
+
|
58 |
+
|
59 |
+
# Can parameterize nogil & nopython over True | False, but limiting per
|
60 |
+
# https://github.com/pandas-dev/pandas/pull/41971#issuecomment-860607472
|
61 |
+
|
62 |
+
|
63 |
+
@pytest.fixture(params=[False])
|
64 |
+
def nogil(request):
|
65 |
+
"""nogil keyword argument for numba.jit"""
|
66 |
+
return request.param
|
67 |
+
|
68 |
+
|
69 |
+
@pytest.fixture(params=[True])
|
70 |
+
def nopython(request):
|
71 |
+
"""nopython keyword argument for numba.jit"""
|
72 |
+
return request.param
|
73 |
+
|
74 |
+
|
75 |
+
@pytest.fixture(params=[True, False])
|
76 |
+
def adjust(request):
|
77 |
+
"""adjust keyword argument for ewm"""
|
78 |
+
return request.param
|
79 |
+
|
80 |
+
|
81 |
+
@pytest.fixture(params=[True, False])
|
82 |
+
def ignore_na(request):
|
83 |
+
"""ignore_na keyword argument for ewm"""
|
84 |
+
return request.param
|
85 |
+
|
86 |
+
|
87 |
+
@pytest.fixture(params=[True, False])
|
88 |
+
def numeric_only(request):
|
89 |
+
"""numeric_only keyword argument"""
|
90 |
+
return request.param
|
91 |
+
|
92 |
+
|
93 |
+
@pytest.fixture(
|
94 |
+
params=[
|
95 |
+
pytest.param("numba", marks=[td.skip_if_no("numba"), pytest.mark.single_cpu]),
|
96 |
+
"cython",
|
97 |
+
]
|
98 |
+
)
|
99 |
+
def engine(request):
|
100 |
+
"""engine keyword argument for rolling.apply"""
|
101 |
+
return request.param
|
102 |
+
|
103 |
+
|
104 |
+
@pytest.fixture(
|
105 |
+
params=[
|
106 |
+
pytest.param(
|
107 |
+
("numba", True), marks=[td.skip_if_no("numba"), pytest.mark.single_cpu]
|
108 |
+
),
|
109 |
+
("cython", True),
|
110 |
+
("cython", False),
|
111 |
+
]
|
112 |
+
)
|
113 |
+
def engine_and_raw(request):
|
114 |
+
"""engine and raw keyword arguments for rolling.apply"""
|
115 |
+
return request.param
|
116 |
+
|
117 |
+
|
118 |
+
@pytest.fixture(params=["1 day", timedelta(days=1), np.timedelta64(1, "D")])
|
119 |
+
def halflife_with_times(request):
|
120 |
+
"""Halflife argument for EWM when times is specified."""
|
121 |
+
return request.param
|
122 |
+
|
123 |
+
|
124 |
+
@pytest.fixture
|
125 |
+
def series():
|
126 |
+
"""Make mocked series as fixture."""
|
127 |
+
arr = np.random.default_rng(2).standard_normal(100)
|
128 |
+
locs = np.arange(20, 40)
|
129 |
+
arr[locs] = np.nan
|
130 |
+
series = Series(arr, index=bdate_range(datetime(2009, 1, 1), periods=100))
|
131 |
+
return series
|
132 |
+
|
133 |
+
|
134 |
+
@pytest.fixture
|
135 |
+
def frame():
|
136 |
+
"""Make mocked frame as fixture."""
|
137 |
+
return DataFrame(
|
138 |
+
np.random.default_rng(2).standard_normal((100, 10)),
|
139 |
+
index=bdate_range(datetime(2009, 1, 1), periods=100),
|
140 |
+
)
|
141 |
+
|
142 |
+
|
143 |
+
@pytest.fixture(params=[None, 1, 2, 5, 10])
|
144 |
+
def step(request):
|
145 |
+
"""step keyword argument for rolling window operations."""
|
146 |
+
return request.param
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/moments/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/window/moments/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (192 Bytes). View file
|
|