QPromaQ commited on
Commit
eae446e
·
verified ·
1 Parent(s): aa75b59

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/conftest.cpython-312.pyc +0 -0
  2. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_arithmetic.cpython-312.pyc +0 -0
  3. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_comparison.cpython-312.pyc +0 -0
  4. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_construction.cpython-312.pyc +0 -0
  5. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_contains.cpython-312.pyc +0 -0
  6. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_function.cpython-312.pyc +0 -0
  7. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_repr.cpython-312.pyc +0 -0
  8. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_to_numpy.cpython-312.pyc +0 -0
  9. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_arithmetic.py +240 -0
  10. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_concat.py +20 -0
  11. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_contains.py +12 -0
  12. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_function.py +194 -0
  13. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_repr.py +47 -0
  14. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_to_numpy.py +132 -0
  15. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/__init__.py +0 -0
  16. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/__pycache__/__init__.cpython-312.pyc +0 -0
  17. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/__pycache__/test_indexing.cpython-312.pyc +0 -0
  18. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/__pycache__/test_numpy.cpython-312.pyc +0 -0
  19. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/test_indexing.py +41 -0
  20. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/test_numpy.py +351 -0
  21. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__init__.py +0 -0
  22. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/__init__.cpython-312.pyc +0 -0
  23. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_accessor.cpython-312.pyc +0 -0
  24. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_arithmetics.cpython-312.pyc +0 -0
  25. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_array.cpython-312.pyc +0 -0
  26. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_astype.cpython-312.pyc +0 -0
  27. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_combine_concat.cpython-312.pyc +0 -0
  28. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_constructors.cpython-312.pyc +0 -0
  29. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_dtype.cpython-312.pyc +0 -0
  30. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_indexing.cpython-312.pyc +0 -0
  31. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_libsparse.cpython-312.pyc +0 -0
  32. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_reductions.cpython-312.pyc +0 -0
  33. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_unary.cpython-312.pyc +0 -0
  34. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_accessor.py +253 -0
  35. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_arithmetics.py +514 -0
  36. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_array.py +511 -0
  37. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_astype.py +133 -0
  38. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_combine_concat.py +62 -0
  39. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_constructors.py +285 -0
  40. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_dtype.py +224 -0
  41. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_indexing.py +302 -0
  42. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_libsparse.py +551 -0
  43. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_reductions.py +306 -0
  44. .venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_unary.py +79 -0
  45. .venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__init__.py +0 -0
  46. .venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/__init__.cpython-312.pyc +0 -0
  47. .venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/common.cpython-312.pyc +0 -0
  48. .venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/test_constructors.cpython-312.pyc +0 -0
  49. .venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/test_conversion.cpython-312.pyc +0 -0
  50. .venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/test_fillna.cpython-312.pyc +0 -0
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/conftest.cpython-312.pyc ADDED
Binary file (2.2 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_arithmetic.cpython-312.pyc ADDED
Binary file (12 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_comparison.cpython-312.pyc ADDED
Binary file (3.63 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_construction.cpython-312.pyc ADDED
Binary file (11.3 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_contains.cpython-312.pyc ADDED
Binary file (744 Bytes). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_function.cpython-312.pyc ADDED
Binary file (12.2 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_repr.cpython-312.pyc ADDED
Binary file (2.2 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/__pycache__/test_to_numpy.cpython-312.pyc ADDED
Binary file (8.3 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_arithmetic.py ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 FloatingArray
9
+
10
+ # Basic test for the arithmetic array ops
11
+ # -----------------------------------------------------------------------------
12
+
13
+
14
+ @pytest.mark.parametrize(
15
+ "opname, exp",
16
+ [
17
+ ("add", [1.1, 2.2, None, None, 5.5]),
18
+ ("mul", [0.1, 0.4, None, None, 2.5]),
19
+ ("sub", [0.9, 1.8, None, None, 4.5]),
20
+ ("truediv", [10.0, 10.0, None, None, 10.0]),
21
+ ("floordiv", [9.0, 9.0, None, None, 10.0]),
22
+ ("mod", [0.1, 0.2, None, None, 0.0]),
23
+ ],
24
+ ids=["add", "mul", "sub", "div", "floordiv", "mod"],
25
+ )
26
+ def test_array_op(dtype, opname, exp):
27
+ a = pd.array([1.0, 2.0, None, 4.0, 5.0], dtype=dtype)
28
+ b = pd.array([0.1, 0.2, 0.3, None, 0.5], dtype=dtype)
29
+
30
+ op = getattr(operator, opname)
31
+
32
+ result = op(a, b)
33
+ expected = pd.array(exp, dtype=dtype)
34
+ tm.assert_extension_array_equal(result, expected)
35
+
36
+
37
+ @pytest.mark.parametrize("zero, negative", [(0, False), (0.0, False), (-0.0, True)])
38
+ def test_divide_by_zero(dtype, zero, negative):
39
+ # TODO pending NA/NaN discussion
40
+ # https://github.com/pandas-dev/pandas/issues/32265/
41
+ a = pd.array([0, 1, -1, None], dtype=dtype)
42
+ result = a / zero
43
+ expected = FloatingArray(
44
+ np.array([np.nan, np.inf, -np.inf, np.nan], dtype=dtype.numpy_dtype),
45
+ np.array([False, False, False, True]),
46
+ )
47
+ if negative:
48
+ expected *= -1
49
+ tm.assert_extension_array_equal(result, expected)
50
+
51
+
52
+ def test_pow_scalar(dtype):
53
+ a = pd.array([-1, 0, 1, None, 2], dtype=dtype)
54
+ result = a**0
55
+ expected = pd.array([1, 1, 1, 1, 1], dtype=dtype)
56
+ tm.assert_extension_array_equal(result, expected)
57
+
58
+ result = a**1
59
+ expected = pd.array([-1, 0, 1, None, 2], dtype=dtype)
60
+ tm.assert_extension_array_equal(result, expected)
61
+
62
+ result = a**pd.NA
63
+ expected = pd.array([None, None, 1, None, None], dtype=dtype)
64
+ tm.assert_extension_array_equal(result, expected)
65
+
66
+ result = a**np.nan
67
+ # TODO np.nan should be converted to pd.NA / missing before operation?
68
+ expected = FloatingArray(
69
+ np.array([np.nan, np.nan, 1, np.nan, np.nan], dtype=dtype.numpy_dtype),
70
+ mask=a._mask,
71
+ )
72
+ tm.assert_extension_array_equal(result, expected)
73
+
74
+ # reversed
75
+ a = a[1:] # Can't raise integers to negative powers.
76
+
77
+ result = 0**a
78
+ expected = pd.array([1, 0, None, 0], dtype=dtype)
79
+ tm.assert_extension_array_equal(result, expected)
80
+
81
+ result = 1**a
82
+ expected = pd.array([1, 1, 1, 1], dtype=dtype)
83
+ tm.assert_extension_array_equal(result, expected)
84
+
85
+ result = pd.NA**a
86
+ expected = pd.array([1, None, None, None], dtype=dtype)
87
+ tm.assert_extension_array_equal(result, expected)
88
+
89
+ result = np.nan**a
90
+ expected = FloatingArray(
91
+ np.array([1, np.nan, np.nan, np.nan], dtype=dtype.numpy_dtype), mask=a._mask
92
+ )
93
+ tm.assert_extension_array_equal(result, expected)
94
+
95
+
96
+ def test_pow_array(dtype):
97
+ a = pd.array([0, 0, 0, 1, 1, 1, None, None, None], dtype=dtype)
98
+ b = pd.array([0, 1, None, 0, 1, None, 0, 1, None], dtype=dtype)
99
+ result = a**b
100
+ expected = pd.array([1, 0, None, 1, 1, 1, 1, None, None], dtype=dtype)
101
+ tm.assert_extension_array_equal(result, expected)
102
+
103
+
104
+ def test_rpow_one_to_na():
105
+ # https://github.com/pandas-dev/pandas/issues/22022
106
+ # https://github.com/pandas-dev/pandas/issues/29997
107
+ arr = pd.array([np.nan, np.nan], dtype="Float64")
108
+ result = np.array([1.0, 2.0]) ** arr
109
+ expected = pd.array([1.0, np.nan], dtype="Float64")
110
+ tm.assert_extension_array_equal(result, expected)
111
+
112
+
113
+ @pytest.mark.parametrize("other", [0, 0.5])
114
+ def test_arith_zero_dim_ndarray(other):
115
+ arr = pd.array([1, None, 2], dtype="Float64")
116
+ result = arr + np.array(other)
117
+ expected = arr + other
118
+ tm.assert_equal(result, expected)
119
+
120
+
121
+ # Test generic characteristics / errors
122
+ # -----------------------------------------------------------------------------
123
+
124
+
125
+ def test_error_invalid_values(data, all_arithmetic_operators):
126
+ op = all_arithmetic_operators
127
+ s = pd.Series(data)
128
+ ops = getattr(s, op)
129
+
130
+ # invalid scalars
131
+ msg = "|".join(
132
+ [
133
+ r"can only perform ops with numeric values",
134
+ r"FloatingArray cannot perform the operation mod",
135
+ "unsupported operand type",
136
+ "not all arguments converted during string formatting",
137
+ "can't multiply sequence by non-int of type 'float'",
138
+ "ufunc 'subtract' cannot use operands with types dtype",
139
+ r"can only concatenate str \(not \"float\"\) to str",
140
+ "ufunc '.*' not supported for the input types, and the inputs could not",
141
+ "ufunc '.*' did not contain a loop with signature matching types",
142
+ "Concatenation operation is not implemented for NumPy arrays",
143
+ "has no kernel",
144
+ "not implemented",
145
+ "not supported for dtype",
146
+ "Can only string multiply by an integer",
147
+ ]
148
+ )
149
+ with pytest.raises(TypeError, match=msg):
150
+ ops("foo")
151
+ with pytest.raises(TypeError, match=msg):
152
+ ops(pd.Timestamp("20180101"))
153
+
154
+ # invalid array-likes
155
+ with pytest.raises(TypeError, match=msg):
156
+ ops(pd.Series("foo", index=s.index))
157
+
158
+ msg = "|".join(
159
+ [
160
+ "can only perform ops with numeric values",
161
+ "cannot perform .* with this index type: DatetimeArray",
162
+ "Addition/subtraction of integers and integer-arrays "
163
+ "with DatetimeArray is no longer supported. *",
164
+ "unsupported operand type",
165
+ "not all arguments converted during string formatting",
166
+ "can't multiply sequence by non-int of type 'float'",
167
+ "ufunc 'subtract' cannot use operands with types dtype",
168
+ (
169
+ "ufunc 'add' cannot use operands with types "
170
+ rf"dtype\('{tm.ENDIAN}M8\[ns\]'\)"
171
+ ),
172
+ r"ufunc 'add' cannot use operands with types dtype\('float\d{2}'\)",
173
+ "cannot subtract DatetimeArray from ndarray",
174
+ "has no kernel",
175
+ "not implemented",
176
+ "not supported for dtype",
177
+ ]
178
+ )
179
+ with pytest.raises(TypeError, match=msg):
180
+ ops(pd.Series(pd.date_range("20180101", periods=len(s))))
181
+
182
+
183
+ # Various
184
+ # -----------------------------------------------------------------------------
185
+
186
+
187
+ def test_cross_type_arithmetic():
188
+ df = pd.DataFrame(
189
+ {
190
+ "A": pd.array([1, 2, np.nan], dtype="Float64"),
191
+ "B": pd.array([1, np.nan, 3], dtype="Float32"),
192
+ "C": np.array([1, 2, 3], dtype="float64"),
193
+ }
194
+ )
195
+
196
+ result = df.A + df.C
197
+ expected = pd.Series([2, 4, np.nan], dtype="Float64")
198
+ tm.assert_series_equal(result, expected)
199
+
200
+ result = (df.A + df.C) * 3 == 12
201
+ expected = pd.Series([False, True, None], dtype="boolean")
202
+ tm.assert_series_equal(result, expected)
203
+
204
+ result = df.A + df.B
205
+ expected = pd.Series([2, np.nan, np.nan], dtype="Float64")
206
+ tm.assert_series_equal(result, expected)
207
+
208
+
209
+ @pytest.mark.parametrize(
210
+ "source, neg_target, abs_target",
211
+ [
212
+ ([1.1, 2.2, 3.3], [-1.1, -2.2, -3.3], [1.1, 2.2, 3.3]),
213
+ ([1.1, 2.2, None], [-1.1, -2.2, None], [1.1, 2.2, None]),
214
+ ([-1.1, 0.0, 1.1], [1.1, 0.0, -1.1], [1.1, 0.0, 1.1]),
215
+ ],
216
+ )
217
+ def test_unary_float_operators(float_ea_dtype, source, neg_target, abs_target):
218
+ # GH38794
219
+ dtype = float_ea_dtype
220
+ arr = pd.array(source, dtype=dtype)
221
+ neg_result, pos_result, abs_result = -arr, +arr, abs(arr)
222
+ neg_target = pd.array(neg_target, dtype=dtype)
223
+ abs_target = pd.array(abs_target, dtype=dtype)
224
+
225
+ tm.assert_extension_array_equal(neg_result, neg_target)
226
+ tm.assert_extension_array_equal(pos_result, arr)
227
+ assert not tm.shares_memory(pos_result, arr)
228
+ tm.assert_extension_array_equal(abs_result, abs_target)
229
+
230
+
231
+ def test_bitwise(dtype):
232
+ left = pd.array([1, None, 3, 4], dtype=dtype)
233
+ right = pd.array([None, 3, 5, 4], dtype=dtype)
234
+
235
+ with pytest.raises(TypeError, match="unsupported operand type"):
236
+ left | right
237
+ with pytest.raises(TypeError, match="unsupported operand type"):
238
+ left & right
239
+ with pytest.raises(TypeError, match="unsupported operand type"):
240
+ left ^ right
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_concat.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import pandas as pd
4
+ import pandas._testing as tm
5
+
6
+
7
+ @pytest.mark.parametrize(
8
+ "to_concat_dtypes, result_dtype",
9
+ [
10
+ (["Float64", "Float64"], "Float64"),
11
+ (["Float32", "Float64"], "Float64"),
12
+ (["Float32", "Float32"], "Float32"),
13
+ ],
14
+ )
15
+ def test_concat_series(to_concat_dtypes, result_dtype):
16
+ result = pd.concat([pd.Series([1, 2, pd.NA], dtype=t) for t in to_concat_dtypes])
17
+ expected = pd.concat([pd.Series([1, 2, pd.NA], dtype=object)] * 2).astype(
18
+ result_dtype
19
+ )
20
+ tm.assert_series_equal(result, expected)
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_contains.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ import pandas as pd
4
+
5
+
6
+ def test_contains_nan():
7
+ # GH#52840
8
+ arr = pd.array(range(5)) / 0
9
+
10
+ assert np.isnan(arr._data[0])
11
+ assert not arr.isna()[0]
12
+ assert np.nan in arr
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_function.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pytest
3
+
4
+ from pandas.compat import IS64
5
+
6
+ import pandas as pd
7
+ import pandas._testing as tm
8
+
9
+
10
+ @pytest.mark.parametrize("ufunc", [np.abs, np.sign])
11
+ # np.sign emits a warning with nans, <https://github.com/numpy/numpy/issues/15127>
12
+ @pytest.mark.filterwarnings("ignore:invalid value encountered in sign:RuntimeWarning")
13
+ def test_ufuncs_single(ufunc):
14
+ a = pd.array([1, 2, -3, np.nan], dtype="Float64")
15
+ result = ufunc(a)
16
+ expected = pd.array(ufunc(a.astype(float)), dtype="Float64")
17
+ tm.assert_extension_array_equal(result, expected)
18
+
19
+ s = pd.Series(a)
20
+ result = ufunc(s)
21
+ expected = pd.Series(expected)
22
+ tm.assert_series_equal(result, expected)
23
+
24
+
25
+ @pytest.mark.parametrize("ufunc", [np.log, np.exp, np.sin, np.cos, np.sqrt])
26
+ def test_ufuncs_single_float(ufunc):
27
+ a = pd.array([1.0, 0.2, 3.0, np.nan], dtype="Float64")
28
+ with np.errstate(invalid="ignore"):
29
+ result = ufunc(a)
30
+ expected = pd.array(ufunc(a.astype(float)), dtype="Float64")
31
+ tm.assert_extension_array_equal(result, expected)
32
+
33
+ s = pd.Series(a)
34
+ with np.errstate(invalid="ignore"):
35
+ result = ufunc(s)
36
+ expected = pd.Series(ufunc(s.astype(float)), dtype="Float64")
37
+ tm.assert_series_equal(result, expected)
38
+
39
+
40
+ @pytest.mark.parametrize("ufunc", [np.add, np.subtract])
41
+ def test_ufuncs_binary_float(ufunc):
42
+ # two FloatingArrays
43
+ a = pd.array([1, 0.2, -3, np.nan], dtype="Float64")
44
+ result = ufunc(a, a)
45
+ expected = pd.array(ufunc(a.astype(float), a.astype(float)), dtype="Float64")
46
+ tm.assert_extension_array_equal(result, expected)
47
+
48
+ # FloatingArray with numpy array
49
+ arr = np.array([1, 2, 3, 4])
50
+ result = ufunc(a, arr)
51
+ expected = pd.array(ufunc(a.astype(float), arr), dtype="Float64")
52
+ tm.assert_extension_array_equal(result, expected)
53
+
54
+ result = ufunc(arr, a)
55
+ expected = pd.array(ufunc(arr, a.astype(float)), dtype="Float64")
56
+ tm.assert_extension_array_equal(result, expected)
57
+
58
+ # FloatingArray with scalar
59
+ result = ufunc(a, 1)
60
+ expected = pd.array(ufunc(a.astype(float), 1), dtype="Float64")
61
+ tm.assert_extension_array_equal(result, expected)
62
+
63
+ result = ufunc(1, a)
64
+ expected = pd.array(ufunc(1, a.astype(float)), dtype="Float64")
65
+ tm.assert_extension_array_equal(result, expected)
66
+
67
+
68
+ @pytest.mark.parametrize("values", [[0, 1], [0, None]])
69
+ def test_ufunc_reduce_raises(values):
70
+ arr = pd.array(values, dtype="Float64")
71
+
72
+ res = np.add.reduce(arr)
73
+ expected = arr.sum(skipna=False)
74
+ tm.assert_almost_equal(res, expected)
75
+
76
+
77
+ @pytest.mark.skipif(not IS64, reason="GH 36579: fail on 32-bit system")
78
+ @pytest.mark.parametrize(
79
+ "pandasmethname, kwargs",
80
+ [
81
+ ("var", {"ddof": 0}),
82
+ ("var", {"ddof": 1}),
83
+ ("std", {"ddof": 0}),
84
+ ("std", {"ddof": 1}),
85
+ ("kurtosis", {}),
86
+ ("skew", {}),
87
+ ("sem", {}),
88
+ ],
89
+ )
90
+ def test_stat_method(pandasmethname, kwargs):
91
+ s = pd.Series(data=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, np.nan, np.nan], dtype="Float64")
92
+ pandasmeth = getattr(s, pandasmethname)
93
+ result = pandasmeth(**kwargs)
94
+ s2 = pd.Series(data=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6], dtype="float64")
95
+ pandasmeth = getattr(s2, pandasmethname)
96
+ expected = pandasmeth(**kwargs)
97
+ assert expected == result
98
+
99
+
100
+ def test_value_counts_na():
101
+ arr = pd.array([0.1, 0.2, 0.1, pd.NA], dtype="Float64")
102
+ result = arr.value_counts(dropna=False)
103
+ idx = pd.Index([0.1, 0.2, pd.NA], dtype=arr.dtype)
104
+ assert idx.dtype == arr.dtype
105
+ expected = pd.Series([2, 1, 1], index=idx, dtype="Int64", name="count")
106
+ tm.assert_series_equal(result, expected)
107
+
108
+ result = arr.value_counts(dropna=True)
109
+ expected = pd.Series([2, 1], index=idx[:-1], dtype="Int64", name="count")
110
+ tm.assert_series_equal(result, expected)
111
+
112
+
113
+ def test_value_counts_empty():
114
+ ser = pd.Series([], dtype="Float64")
115
+ result = ser.value_counts()
116
+ idx = pd.Index([], dtype="Float64")
117
+ assert idx.dtype == "Float64"
118
+ expected = pd.Series([], index=idx, dtype="Int64", name="count")
119
+ tm.assert_series_equal(result, expected)
120
+
121
+
122
+ def test_value_counts_with_normalize():
123
+ ser = pd.Series([0.1, 0.2, 0.1, pd.NA], dtype="Float64")
124
+ result = ser.value_counts(normalize=True)
125
+ expected = pd.Series([2, 1], index=ser[:2], dtype="Float64", name="proportion") / 3
126
+ assert expected.index.dtype == ser.dtype
127
+ tm.assert_series_equal(result, expected)
128
+
129
+
130
+ @pytest.mark.parametrize("skipna", [True, False])
131
+ @pytest.mark.parametrize("min_count", [0, 4])
132
+ def test_floating_array_sum(skipna, min_count, dtype):
133
+ arr = pd.array([1, 2, 3, None], dtype=dtype)
134
+ result = arr.sum(skipna=skipna, min_count=min_count)
135
+ if skipna and min_count == 0:
136
+ assert result == 6.0
137
+ else:
138
+ assert result is pd.NA
139
+
140
+
141
+ @pytest.mark.parametrize(
142
+ "values, expected", [([1, 2, 3], 6.0), ([1, 2, 3, None], 6.0), ([None], 0.0)]
143
+ )
144
+ def test_floating_array_numpy_sum(values, expected):
145
+ arr = pd.array(values, dtype="Float64")
146
+ result = np.sum(arr)
147
+ assert result == expected
148
+
149
+
150
+ @pytest.mark.parametrize("op", ["sum", "min", "max", "prod"])
151
+ def test_preserve_dtypes(op):
152
+ df = pd.DataFrame(
153
+ {
154
+ "A": ["a", "b", "b"],
155
+ "B": [1, None, 3],
156
+ "C": pd.array([0.1, None, 3.0], dtype="Float64"),
157
+ }
158
+ )
159
+
160
+ # op
161
+ result = getattr(df.C, op)()
162
+ assert isinstance(result, np.float64)
163
+
164
+ # groupby
165
+ result = getattr(df.groupby("A"), op)()
166
+
167
+ expected = pd.DataFrame(
168
+ {"B": np.array([1.0, 3.0]), "C": pd.array([0.1, 3], dtype="Float64")},
169
+ index=pd.Index(["a", "b"], name="A"),
170
+ )
171
+ tm.assert_frame_equal(result, expected)
172
+
173
+
174
+ @pytest.mark.parametrize("skipna", [True, False])
175
+ @pytest.mark.parametrize("method", ["min", "max"])
176
+ def test_floating_array_min_max(skipna, method, dtype):
177
+ arr = pd.array([0.0, 1.0, None], dtype=dtype)
178
+ func = getattr(arr, method)
179
+ result = func(skipna=skipna)
180
+ if skipna:
181
+ assert result == (0 if method == "min" else 1)
182
+ else:
183
+ assert result is pd.NA
184
+
185
+
186
+ @pytest.mark.parametrize("skipna", [True, False])
187
+ @pytest.mark.parametrize("min_count", [0, 9])
188
+ def test_floating_array_prod(skipna, min_count, dtype):
189
+ arr = pd.array([1.0, 2.0, None], dtype=dtype)
190
+ result = arr.prod(skipna=skipna, min_count=min_count)
191
+ if skipna and min_count == 0:
192
+ assert result == 2
193
+ else:
194
+ assert result is pd.NA
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_repr.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pytest
3
+
4
+ import pandas as pd
5
+ from pandas.core.arrays.floating import (
6
+ Float32Dtype,
7
+ Float64Dtype,
8
+ )
9
+
10
+
11
+ def test_dtypes(dtype):
12
+ # smoke tests on auto dtype construction
13
+
14
+ np.dtype(dtype.type).kind == "f"
15
+ assert dtype.name is not None
16
+
17
+
18
+ @pytest.mark.parametrize(
19
+ "dtype, expected",
20
+ [(Float32Dtype(), "Float32Dtype()"), (Float64Dtype(), "Float64Dtype()")],
21
+ )
22
+ def test_repr_dtype(dtype, expected):
23
+ assert repr(dtype) == expected
24
+
25
+
26
+ def test_repr_array():
27
+ result = repr(pd.array([1.0, None, 3.0]))
28
+ expected = "<FloatingArray>\n[1.0, <NA>, 3.0]\nLength: 3, dtype: Float64"
29
+ assert result == expected
30
+
31
+
32
+ def test_repr_array_long():
33
+ data = pd.array([1.0, 2.0, None] * 1000)
34
+ expected = """<FloatingArray>
35
+ [ 1.0, 2.0, <NA>, 1.0, 2.0, <NA>, 1.0, 2.0, <NA>, 1.0,
36
+ ...
37
+ <NA>, 1.0, 2.0, <NA>, 1.0, 2.0, <NA>, 1.0, 2.0, <NA>]
38
+ Length: 3000, dtype: Float64"""
39
+ result = repr(data)
40
+ assert result == expected
41
+
42
+
43
+ def test_frame_repr(data_missing):
44
+ df = pd.DataFrame({"A": data_missing})
45
+ result = repr(df)
46
+ expected = " A\n0 <NA>\n1 0.1"
47
+ assert result == expected
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/floating/test_to_numpy.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pytest
3
+
4
+ import pandas as pd
5
+ import pandas._testing as tm
6
+ from pandas.core.arrays import FloatingArray
7
+
8
+
9
+ @pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
10
+ def test_to_numpy(box):
11
+ con = pd.Series if box else pd.array
12
+
13
+ # default (with or without missing values) -> object dtype
14
+ arr = con([0.1, 0.2, 0.3], dtype="Float64")
15
+ result = arr.to_numpy()
16
+ expected = np.array([0.1, 0.2, 0.3], dtype="float64")
17
+ tm.assert_numpy_array_equal(result, expected)
18
+
19
+ arr = con([0.1, 0.2, None], dtype="Float64")
20
+ result = arr.to_numpy()
21
+ expected = np.array([0.1, 0.2, np.nan], dtype="float64")
22
+ tm.assert_numpy_array_equal(result, expected)
23
+
24
+
25
+ @pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
26
+ def test_to_numpy_float(box):
27
+ con = pd.Series if box else pd.array
28
+
29
+ # no missing values -> can convert to float, otherwise raises
30
+ arr = con([0.1, 0.2, 0.3], dtype="Float64")
31
+ result = arr.to_numpy(dtype="float64")
32
+ expected = np.array([0.1, 0.2, 0.3], dtype="float64")
33
+ tm.assert_numpy_array_equal(result, expected)
34
+
35
+ arr = con([0.1, 0.2, None], dtype="Float64")
36
+ result = arr.to_numpy(dtype="float64")
37
+ expected = np.array([0.1, 0.2, np.nan], dtype="float64")
38
+ tm.assert_numpy_array_equal(result, expected)
39
+
40
+ result = arr.to_numpy(dtype="float64", na_value=np.nan)
41
+ expected = np.array([0.1, 0.2, np.nan], dtype="float64")
42
+ tm.assert_numpy_array_equal(result, expected)
43
+
44
+
45
+ @pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
46
+ def test_to_numpy_int(box):
47
+ con = pd.Series if box else pd.array
48
+
49
+ # no missing values -> can convert to int, otherwise raises
50
+ arr = con([1.0, 2.0, 3.0], dtype="Float64")
51
+ result = arr.to_numpy(dtype="int64")
52
+ expected = np.array([1, 2, 3], dtype="int64")
53
+ tm.assert_numpy_array_equal(result, expected)
54
+
55
+ arr = con([1.0, 2.0, None], dtype="Float64")
56
+ with pytest.raises(ValueError, match="cannot convert to 'int64'-dtype"):
57
+ result = arr.to_numpy(dtype="int64")
58
+
59
+ # automatic casting (floors the values)
60
+ arr = con([0.1, 0.9, 1.1], dtype="Float64")
61
+ result = arr.to_numpy(dtype="int64")
62
+ expected = np.array([0, 0, 1], dtype="int64")
63
+ tm.assert_numpy_array_equal(result, expected)
64
+
65
+
66
+ @pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
67
+ def test_to_numpy_na_value(box):
68
+ con = pd.Series if box else pd.array
69
+
70
+ arr = con([0.0, 1.0, None], dtype="Float64")
71
+ result = arr.to_numpy(dtype=object, na_value=None)
72
+ expected = np.array([0.0, 1.0, None], dtype="object")
73
+ tm.assert_numpy_array_equal(result, expected)
74
+
75
+ result = arr.to_numpy(dtype=bool, na_value=False)
76
+ expected = np.array([False, True, False], dtype="bool")
77
+ tm.assert_numpy_array_equal(result, expected)
78
+
79
+ result = arr.to_numpy(dtype="int64", na_value=-99)
80
+ expected = np.array([0, 1, -99], dtype="int64")
81
+ tm.assert_numpy_array_equal(result, expected)
82
+
83
+
84
+ def test_to_numpy_na_value_with_nan():
85
+ # array with both NaN and NA -> only fill NA with `na_value`
86
+ arr = FloatingArray(np.array([0.0, np.nan, 0.0]), np.array([False, False, True]))
87
+ result = arr.to_numpy(dtype="float64", na_value=-1)
88
+ expected = np.array([0.0, np.nan, -1.0], dtype="float64")
89
+ tm.assert_numpy_array_equal(result, expected)
90
+
91
+
92
+ @pytest.mark.parametrize("dtype", ["float64", "float32", "int32", "int64", "bool"])
93
+ @pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
94
+ def test_to_numpy_dtype(box, dtype):
95
+ con = pd.Series if box else pd.array
96
+ arr = con([0.0, 1.0], dtype="Float64")
97
+
98
+ result = arr.to_numpy(dtype=dtype)
99
+ expected = np.array([0, 1], dtype=dtype)
100
+ tm.assert_numpy_array_equal(result, expected)
101
+
102
+
103
+ @pytest.mark.parametrize("dtype", ["int32", "int64", "bool"])
104
+ @pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
105
+ def test_to_numpy_na_raises(box, dtype):
106
+ con = pd.Series if box else pd.array
107
+ arr = con([0.0, 1.0, None], dtype="Float64")
108
+ with pytest.raises(ValueError, match=dtype):
109
+ arr.to_numpy(dtype=dtype)
110
+
111
+
112
+ @pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
113
+ def test_to_numpy_string(box, dtype):
114
+ con = pd.Series if box else pd.array
115
+ arr = con([0.0, 1.0, None], dtype="Float64")
116
+
117
+ result = arr.to_numpy(dtype="str")
118
+ expected = np.array([0.0, 1.0, pd.NA], dtype=f"{tm.ENDIAN}U32")
119
+ tm.assert_numpy_array_equal(result, expected)
120
+
121
+
122
+ def test_to_numpy_copy():
123
+ # to_numpy can be zero-copy if no missing values
124
+ arr = pd.array([0.1, 0.2, 0.3], dtype="Float64")
125
+ result = arr.to_numpy(dtype="float64")
126
+ result[0] = 10
127
+ tm.assert_extension_array_equal(arr, pd.array([10, 0.2, 0.3], dtype="Float64"))
128
+
129
+ arr = pd.array([0.1, 0.2, 0.3], dtype="Float64")
130
+ result = arr.to_numpy(dtype="float64", copy=True)
131
+ result[0] = 10
132
+ tm.assert_extension_array_equal(arr, pd.array([0.1, 0.2, 0.3], dtype="Float64"))
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/__init__.py ADDED
File without changes
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (218 Bytes). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/__pycache__/test_indexing.cpython-312.pyc ADDED
Binary file (2.98 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/__pycache__/test_numpy.cpython-312.pyc ADDED
Binary file (15.6 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/test_indexing.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ from pandas.core.dtypes.common import is_scalar
4
+
5
+ import pandas as pd
6
+ import pandas._testing as tm
7
+
8
+
9
+ class TestSearchsorted:
10
+ def test_searchsorted_string(self, string_dtype):
11
+ arr = pd.array(["a", "b", "c"], dtype=string_dtype)
12
+
13
+ result = arr.searchsorted("a", side="left")
14
+ assert is_scalar(result)
15
+ assert result == 0
16
+
17
+ result = arr.searchsorted("a", side="right")
18
+ assert is_scalar(result)
19
+ assert result == 1
20
+
21
+ def test_searchsorted_numeric_dtypes_scalar(self, any_real_numpy_dtype):
22
+ arr = pd.array([1, 3, 90], dtype=any_real_numpy_dtype)
23
+ result = arr.searchsorted(30)
24
+ assert is_scalar(result)
25
+ assert result == 2
26
+
27
+ result = arr.searchsorted([30])
28
+ expected = np.array([2], dtype=np.intp)
29
+ tm.assert_numpy_array_equal(result, expected)
30
+
31
+ def test_searchsorted_numeric_dtypes_vector(self, any_real_numpy_dtype):
32
+ arr = pd.array([1, 3, 90], dtype=any_real_numpy_dtype)
33
+ result = arr.searchsorted([2, 30])
34
+ expected = np.array([1, 2], dtype=np.intp)
35
+ tm.assert_numpy_array_equal(result, expected)
36
+
37
+ def test_searchsorted_sorter(self, any_real_numpy_dtype):
38
+ arr = pd.array([3, 1, 2], dtype=any_real_numpy_dtype)
39
+ result = arr.searchsorted([0, 3], sorter=np.argsort(arr))
40
+ expected = np.array([0, 2], dtype=np.intp)
41
+ tm.assert_numpy_array_equal(result, expected)
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/numpy_/test_numpy.py ADDED
@@ -0,0 +1,351 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Additional tests for NumpyExtensionArray that aren't covered by
3
+ the interface tests.
4
+ """
5
+ import numpy as np
6
+ import pytest
7
+
8
+ from pandas.core.dtypes.dtypes import NumpyEADtype
9
+
10
+ import pandas as pd
11
+ import pandas._testing as tm
12
+ from pandas.arrays import NumpyExtensionArray
13
+
14
+
15
+ @pytest.fixture(
16
+ params=[
17
+ np.array(["a", "b"], dtype=object),
18
+ np.array([0, 1], dtype=float),
19
+ np.array([0, 1], dtype=int),
20
+ np.array([0, 1 + 2j], dtype=complex),
21
+ np.array([True, False], dtype=bool),
22
+ np.array([0, 1], dtype="datetime64[ns]"),
23
+ np.array([0, 1], dtype="timedelta64[ns]"),
24
+ ],
25
+ )
26
+ def any_numpy_array(request):
27
+ """
28
+ Parametrized fixture for NumPy arrays with different dtypes.
29
+
30
+ This excludes string and bytes.
31
+ """
32
+ return request.param.copy()
33
+
34
+
35
+ # ----------------------------------------------------------------------------
36
+ # NumpyEADtype
37
+
38
+
39
+ @pytest.mark.parametrize(
40
+ "dtype, expected",
41
+ [
42
+ ("bool", True),
43
+ ("int", True),
44
+ ("uint", True),
45
+ ("float", True),
46
+ ("complex", True),
47
+ ("str", False),
48
+ ("bytes", False),
49
+ ("datetime64[ns]", False),
50
+ ("object", False),
51
+ ("void", False),
52
+ ],
53
+ )
54
+ def test_is_numeric(dtype, expected):
55
+ dtype = NumpyEADtype(dtype)
56
+ assert dtype._is_numeric is expected
57
+
58
+
59
+ @pytest.mark.parametrize(
60
+ "dtype, expected",
61
+ [
62
+ ("bool", True),
63
+ ("int", False),
64
+ ("uint", False),
65
+ ("float", False),
66
+ ("complex", False),
67
+ ("str", False),
68
+ ("bytes", False),
69
+ ("datetime64[ns]", False),
70
+ ("object", False),
71
+ ("void", False),
72
+ ],
73
+ )
74
+ def test_is_boolean(dtype, expected):
75
+ dtype = NumpyEADtype(dtype)
76
+ assert dtype._is_boolean is expected
77
+
78
+
79
+ def test_repr():
80
+ dtype = NumpyEADtype(np.dtype("int64"))
81
+ assert repr(dtype) == "NumpyEADtype('int64')"
82
+
83
+
84
+ def test_constructor_from_string():
85
+ result = NumpyEADtype.construct_from_string("int64")
86
+ expected = NumpyEADtype(np.dtype("int64"))
87
+ assert result == expected
88
+
89
+
90
+ def test_dtype_idempotent(any_numpy_dtype):
91
+ dtype = NumpyEADtype(any_numpy_dtype)
92
+
93
+ result = NumpyEADtype(dtype)
94
+ assert result == dtype
95
+
96
+
97
+ # ----------------------------------------------------------------------------
98
+ # Construction
99
+
100
+
101
+ def test_constructor_no_coercion():
102
+ with pytest.raises(ValueError, match="NumPy array"):
103
+ NumpyExtensionArray([1, 2, 3])
104
+
105
+
106
+ def test_series_constructor_with_copy():
107
+ ndarray = np.array([1, 2, 3])
108
+ ser = pd.Series(NumpyExtensionArray(ndarray), copy=True)
109
+
110
+ assert ser.values is not ndarray
111
+
112
+
113
+ def test_series_constructor_with_astype():
114
+ ndarray = np.array([1, 2, 3])
115
+ result = pd.Series(NumpyExtensionArray(ndarray), dtype="float64")
116
+ expected = pd.Series([1.0, 2.0, 3.0], dtype="float64")
117
+ tm.assert_series_equal(result, expected)
118
+
119
+
120
+ def test_from_sequence_dtype():
121
+ arr = np.array([1, 2, 3], dtype="int64")
122
+ result = NumpyExtensionArray._from_sequence(arr, dtype="uint64")
123
+ expected = NumpyExtensionArray(np.array([1, 2, 3], dtype="uint64"))
124
+ tm.assert_extension_array_equal(result, expected)
125
+
126
+
127
+ def test_constructor_copy():
128
+ arr = np.array([0, 1])
129
+ result = NumpyExtensionArray(arr, copy=True)
130
+
131
+ assert not tm.shares_memory(result, arr)
132
+
133
+
134
+ def test_constructor_with_data(any_numpy_array):
135
+ nparr = any_numpy_array
136
+ arr = NumpyExtensionArray(nparr)
137
+ assert arr.dtype.numpy_dtype == nparr.dtype
138
+
139
+
140
+ # ----------------------------------------------------------------------------
141
+ # Conversion
142
+
143
+
144
+ def test_to_numpy():
145
+ arr = NumpyExtensionArray(np.array([1, 2, 3]))
146
+ result = arr.to_numpy()
147
+ assert result is arr._ndarray
148
+
149
+ result = arr.to_numpy(copy=True)
150
+ assert result is not arr._ndarray
151
+
152
+ result = arr.to_numpy(dtype="f8")
153
+ expected = np.array([1, 2, 3], dtype="f8")
154
+ tm.assert_numpy_array_equal(result, expected)
155
+
156
+
157
+ # ----------------------------------------------------------------------------
158
+ # Setitem
159
+
160
+
161
+ def test_setitem_series():
162
+ ser = pd.Series([1, 2, 3])
163
+ ser.array[0] = 10
164
+ expected = pd.Series([10, 2, 3])
165
+ tm.assert_series_equal(ser, expected)
166
+
167
+
168
+ def test_setitem(any_numpy_array):
169
+ nparr = any_numpy_array
170
+ arr = NumpyExtensionArray(nparr, copy=True)
171
+
172
+ arr[0] = arr[1]
173
+ nparr[0] = nparr[1]
174
+
175
+ tm.assert_numpy_array_equal(arr.to_numpy(), nparr)
176
+
177
+
178
+ # ----------------------------------------------------------------------------
179
+ # Reductions
180
+
181
+
182
+ def test_bad_reduce_raises():
183
+ arr = np.array([1, 2, 3], dtype="int64")
184
+ arr = NumpyExtensionArray(arr)
185
+ msg = "cannot perform not_a_method with type int"
186
+ with pytest.raises(TypeError, match=msg):
187
+ arr._reduce(msg)
188
+
189
+
190
+ def test_validate_reduction_keyword_args():
191
+ arr = NumpyExtensionArray(np.array([1, 2, 3]))
192
+ msg = "the 'keepdims' parameter is not supported .*all"
193
+ with pytest.raises(ValueError, match=msg):
194
+ arr.all(keepdims=True)
195
+
196
+
197
+ def test_np_max_nested_tuples():
198
+ # case where checking in ufunc.nout works while checking for tuples
199
+ # does not
200
+ vals = [
201
+ (("j", "k"), ("l", "m")),
202
+ (("l", "m"), ("o", "p")),
203
+ (("o", "p"), ("j", "k")),
204
+ ]
205
+ ser = pd.Series(vals)
206
+ arr = ser.array
207
+
208
+ assert arr.max() is arr[2]
209
+ assert ser.max() is arr[2]
210
+
211
+ result = np.maximum.reduce(arr)
212
+ assert result == arr[2]
213
+
214
+ result = np.maximum.reduce(ser)
215
+ assert result == arr[2]
216
+
217
+
218
+ def test_np_reduce_2d():
219
+ raw = np.arange(12).reshape(4, 3)
220
+ arr = NumpyExtensionArray(raw)
221
+
222
+ res = np.maximum.reduce(arr, axis=0)
223
+ tm.assert_extension_array_equal(res, arr[-1])
224
+
225
+ alt = arr.max(axis=0)
226
+ tm.assert_extension_array_equal(alt, arr[-1])
227
+
228
+
229
+ # ----------------------------------------------------------------------------
230
+ # Ops
231
+
232
+
233
+ @pytest.mark.parametrize("ufunc", [np.abs, np.negative, np.positive])
234
+ def test_ufunc_unary(ufunc):
235
+ arr = NumpyExtensionArray(np.array([-1.0, 0.0, 1.0]))
236
+ result = ufunc(arr)
237
+ expected = NumpyExtensionArray(ufunc(arr._ndarray))
238
+ tm.assert_extension_array_equal(result, expected)
239
+
240
+ # same thing but with the 'out' keyword
241
+ out = NumpyExtensionArray(np.array([-9.0, -9.0, -9.0]))
242
+ ufunc(arr, out=out)
243
+ tm.assert_extension_array_equal(out, expected)
244
+
245
+
246
+ def test_ufunc():
247
+ arr = NumpyExtensionArray(np.array([-1.0, 0.0, 1.0]))
248
+
249
+ r1, r2 = np.divmod(arr, np.add(arr, 2))
250
+ e1, e2 = np.divmod(arr._ndarray, np.add(arr._ndarray, 2))
251
+ e1 = NumpyExtensionArray(e1)
252
+ e2 = NumpyExtensionArray(e2)
253
+ tm.assert_extension_array_equal(r1, e1)
254
+ tm.assert_extension_array_equal(r2, e2)
255
+
256
+
257
+ def test_basic_binop():
258
+ # Just a basic smoke test. The EA interface tests exercise this
259
+ # more thoroughly.
260
+ x = NumpyExtensionArray(np.array([1, 2, 3]))
261
+ result = x + x
262
+ expected = NumpyExtensionArray(np.array([2, 4, 6]))
263
+ tm.assert_extension_array_equal(result, expected)
264
+
265
+
266
+ @pytest.mark.parametrize("dtype", [None, object])
267
+ def test_setitem_object_typecode(dtype):
268
+ arr = NumpyExtensionArray(np.array(["a", "b", "c"], dtype=dtype))
269
+ arr[0] = "t"
270
+ expected = NumpyExtensionArray(np.array(["t", "b", "c"], dtype=dtype))
271
+ tm.assert_extension_array_equal(arr, expected)
272
+
273
+
274
+ def test_setitem_no_coercion():
275
+ # https://github.com/pandas-dev/pandas/issues/28150
276
+ arr = NumpyExtensionArray(np.array([1, 2, 3]))
277
+ with pytest.raises(ValueError, match="int"):
278
+ arr[0] = "a"
279
+
280
+ # With a value that we do coerce, check that we coerce the value
281
+ # and not the underlying array.
282
+ arr[0] = 2.5
283
+ assert isinstance(arr[0], (int, np.integer)), type(arr[0])
284
+
285
+
286
+ def test_setitem_preserves_views():
287
+ # GH#28150, see also extension test of the same name
288
+ arr = NumpyExtensionArray(np.array([1, 2, 3]))
289
+ view1 = arr.view()
290
+ view2 = arr[:]
291
+ view3 = np.asarray(arr)
292
+
293
+ arr[0] = 9
294
+ assert view1[0] == 9
295
+ assert view2[0] == 9
296
+ assert view3[0] == 9
297
+
298
+ arr[-1] = 2.5
299
+ view1[-1] = 5
300
+ assert arr[-1] == 5
301
+
302
+
303
+ @pytest.mark.parametrize("dtype", [np.int64, np.uint64])
304
+ def test_quantile_empty(dtype):
305
+ # we should get back np.nans, not -1s
306
+ arr = NumpyExtensionArray(np.array([], dtype=dtype))
307
+ idx = pd.Index([0.0, 0.5])
308
+
309
+ result = arr._quantile(idx, interpolation="linear")
310
+ expected = NumpyExtensionArray(np.array([np.nan, np.nan]))
311
+ tm.assert_extension_array_equal(result, expected)
312
+
313
+
314
+ def test_factorize_unsigned():
315
+ # don't raise when calling factorize on unsigned int NumpyExtensionArray
316
+ arr = np.array([1, 2, 3], dtype=np.uint64)
317
+ obj = NumpyExtensionArray(arr)
318
+
319
+ res_codes, res_unique = obj.factorize()
320
+ exp_codes, exp_unique = pd.factorize(arr)
321
+
322
+ tm.assert_numpy_array_equal(res_codes, exp_codes)
323
+
324
+ tm.assert_extension_array_equal(res_unique, NumpyExtensionArray(exp_unique))
325
+
326
+
327
+ # ----------------------------------------------------------------------------
328
+ # Output formatting
329
+
330
+
331
+ def test_array_repr(any_numpy_array):
332
+ # GH#61085
333
+ nparray = any_numpy_array
334
+ arr = NumpyExtensionArray(nparray)
335
+ if nparray.dtype == "object":
336
+ values = "['a', 'b']"
337
+ elif nparray.dtype == "float64":
338
+ values = "[0.0, 1.0]"
339
+ elif str(nparray.dtype).startswith("int"):
340
+ values = "[0, 1]"
341
+ elif nparray.dtype == "complex128":
342
+ values = "[0j, (1+2j)]"
343
+ elif nparray.dtype == "bool":
344
+ values = "[True, False]"
345
+ elif nparray.dtype == "datetime64[ns]":
346
+ values = "[1970-01-01T00:00:00.000000000, 1970-01-01T00:00:00.000000001]"
347
+ elif nparray.dtype == "timedelta64[ns]":
348
+ values = "[0 nanoseconds, 1 nanoseconds]"
349
+ expected = f"<NumpyExtensionArray>\n{values}\nLength: 2, dtype: {nparray.dtype}"
350
+ result = repr(arr)
351
+ assert result == expected, f"{result} vs {expected}"
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__init__.py ADDED
File without changes
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (218 Bytes). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_accessor.cpython-312.pyc ADDED
Binary file (16.4 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_arithmetics.cpython-312.pyc ADDED
Binary file (29.9 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_array.cpython-312.pyc ADDED
Binary file (31 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_astype.cpython-312.pyc ADDED
Binary file (7.15 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_combine_concat.cpython-312.pyc ADDED
Binary file (4.04 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_constructors.cpython-312.pyc ADDED
Binary file (20 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_dtype.cpython-312.pyc ADDED
Binary file (9.8 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_indexing.cpython-312.pyc ADDED
Binary file (20.9 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_libsparse.cpython-312.pyc ADDED
Binary file (32.3 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_reductions.cpython-312.pyc ADDED
Binary file (16.5 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/__pycache__/test_unary.cpython-312.pyc ADDED
Binary file (5.29 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_accessor.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import string
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
+ class TestSeriesAccessor:
13
+ def test_to_dense(self):
14
+ ser = pd.Series([0, 1, 0, 10], dtype="Sparse[int64]")
15
+ result = ser.sparse.to_dense()
16
+ expected = pd.Series([0, 1, 0, 10])
17
+ tm.assert_series_equal(result, expected)
18
+
19
+ @pytest.mark.parametrize("attr", ["npoints", "density", "fill_value", "sp_values"])
20
+ def test_get_attributes(self, attr):
21
+ arr = SparseArray([0, 1])
22
+ ser = pd.Series(arr)
23
+
24
+ result = getattr(ser.sparse, attr)
25
+ expected = getattr(arr, attr)
26
+ assert result == expected
27
+
28
+ def test_from_coo(self):
29
+ scipy_sparse = pytest.importorskip("scipy.sparse")
30
+
31
+ row = [0, 3, 1, 0]
32
+ col = [0, 3, 1, 2]
33
+ data = [4, 5, 7, 9]
34
+
35
+ sp_array = scipy_sparse.coo_matrix((data, (row, col)))
36
+ result = pd.Series.sparse.from_coo(sp_array)
37
+
38
+ index = pd.MultiIndex.from_arrays(
39
+ [
40
+ np.array([0, 0, 1, 3], dtype=np.int32),
41
+ np.array([0, 2, 1, 3], dtype=np.int32),
42
+ ],
43
+ )
44
+ expected = pd.Series([4, 9, 7, 5], index=index, dtype="Sparse[int]")
45
+ tm.assert_series_equal(result, expected)
46
+
47
+ @pytest.mark.parametrize(
48
+ "sort_labels, expected_rows, expected_cols, expected_values_pos",
49
+ [
50
+ (
51
+ False,
52
+ [("b", 2), ("a", 2), ("b", 1), ("a", 1)],
53
+ [("z", 1), ("z", 2), ("x", 2), ("z", 0)],
54
+ {1: (1, 0), 3: (3, 3)},
55
+ ),
56
+ (
57
+ True,
58
+ [("a", 1), ("a", 2), ("b", 1), ("b", 2)],
59
+ [("x", 2), ("z", 0), ("z", 1), ("z", 2)],
60
+ {1: (1, 2), 3: (0, 1)},
61
+ ),
62
+ ],
63
+ )
64
+ def test_to_coo(
65
+ self, sort_labels, expected_rows, expected_cols, expected_values_pos
66
+ ):
67
+ sp_sparse = pytest.importorskip("scipy.sparse")
68
+
69
+ values = SparseArray([0, np.nan, 1, 0, None, 3], fill_value=0)
70
+ index = pd.MultiIndex.from_tuples(
71
+ [
72
+ ("b", 2, "z", 1),
73
+ ("a", 2, "z", 2),
74
+ ("a", 2, "z", 1),
75
+ ("a", 2, "x", 2),
76
+ ("b", 1, "z", 1),
77
+ ("a", 1, "z", 0),
78
+ ]
79
+ )
80
+ ss = pd.Series(values, index=index)
81
+
82
+ expected_A = np.zeros((4, 4))
83
+ for value, (row, col) in expected_values_pos.items():
84
+ expected_A[row, col] = value
85
+
86
+ A, rows, cols = ss.sparse.to_coo(
87
+ row_levels=(0, 1), column_levels=(2, 3), sort_labels=sort_labels
88
+ )
89
+ assert isinstance(A, sp_sparse.coo_matrix)
90
+ tm.assert_numpy_array_equal(A.toarray(), expected_A)
91
+ assert rows == expected_rows
92
+ assert cols == expected_cols
93
+
94
+ def test_non_sparse_raises(self):
95
+ ser = pd.Series([1, 2, 3])
96
+ with pytest.raises(AttributeError, match=".sparse"):
97
+ ser.sparse.density
98
+
99
+
100
+ class TestFrameAccessor:
101
+ def test_accessor_raises(self):
102
+ df = pd.DataFrame({"A": [0, 1]})
103
+ with pytest.raises(AttributeError, match="sparse"):
104
+ df.sparse
105
+
106
+ @pytest.mark.parametrize("format", ["csc", "csr", "coo"])
107
+ @pytest.mark.parametrize("labels", [None, list(string.ascii_letters[:10])])
108
+ @pytest.mark.parametrize("dtype", ["float64", "int64"])
109
+ def test_from_spmatrix(self, format, labels, dtype):
110
+ sp_sparse = pytest.importorskip("scipy.sparse")
111
+
112
+ sp_dtype = SparseDtype(dtype, np.array(0, dtype=dtype).item())
113
+
114
+ mat = sp_sparse.eye(10, format=format, dtype=dtype)
115
+ result = pd.DataFrame.sparse.from_spmatrix(mat, index=labels, columns=labels)
116
+ expected = pd.DataFrame(
117
+ np.eye(10, dtype=dtype), index=labels, columns=labels
118
+ ).astype(sp_dtype)
119
+ tm.assert_frame_equal(result, expected)
120
+
121
+ @pytest.mark.parametrize("format", ["csc", "csr", "coo"])
122
+ def test_from_spmatrix_including_explicit_zero(self, format):
123
+ sp_sparse = pytest.importorskip("scipy.sparse")
124
+
125
+ mat = sp_sparse.random(10, 2, density=0.5, format=format)
126
+ mat.data[0] = 0
127
+ result = pd.DataFrame.sparse.from_spmatrix(mat)
128
+ dtype = SparseDtype("float64", 0.0)
129
+ expected = pd.DataFrame(mat.todense()).astype(dtype)
130
+ tm.assert_frame_equal(result, expected)
131
+
132
+ @pytest.mark.parametrize(
133
+ "columns",
134
+ [["a", "b"], pd.MultiIndex.from_product([["A"], ["a", "b"]]), ["a", "a"]],
135
+ )
136
+ def test_from_spmatrix_columns(self, columns):
137
+ sp_sparse = pytest.importorskip("scipy.sparse")
138
+
139
+ dtype = SparseDtype("float64", 0.0)
140
+
141
+ mat = sp_sparse.random(10, 2, density=0.5)
142
+ result = pd.DataFrame.sparse.from_spmatrix(mat, columns=columns)
143
+ expected = pd.DataFrame(mat.toarray(), columns=columns).astype(dtype)
144
+ tm.assert_frame_equal(result, expected)
145
+
146
+ @pytest.mark.parametrize(
147
+ "colnames", [("A", "B"), (1, 2), (1, pd.NA), (0.1, 0.2), ("x", "x"), (0, 0)]
148
+ )
149
+ def test_to_coo(self, colnames):
150
+ sp_sparse = pytest.importorskip("scipy.sparse")
151
+
152
+ df = pd.DataFrame(
153
+ {colnames[0]: [0, 1, 0], colnames[1]: [1, 0, 0]}, dtype="Sparse[int64, 0]"
154
+ )
155
+ result = df.sparse.to_coo()
156
+ expected = sp_sparse.coo_matrix(np.asarray(df))
157
+ assert (result != expected).nnz == 0
158
+
159
+ @pytest.mark.parametrize("fill_value", [1, np.nan])
160
+ def test_to_coo_nonzero_fill_val_raises(self, fill_value):
161
+ pytest.importorskip("scipy")
162
+ df = pd.DataFrame(
163
+ {
164
+ "A": SparseArray(
165
+ [fill_value, fill_value, fill_value, 2], fill_value=fill_value
166
+ ),
167
+ "B": SparseArray(
168
+ [fill_value, 2, fill_value, fill_value], fill_value=fill_value
169
+ ),
170
+ }
171
+ )
172
+ with pytest.raises(ValueError, match="fill value must be 0"):
173
+ df.sparse.to_coo()
174
+
175
+ def test_to_coo_midx_categorical(self):
176
+ # GH#50996
177
+ sp_sparse = pytest.importorskip("scipy.sparse")
178
+
179
+ midx = pd.MultiIndex.from_arrays(
180
+ [
181
+ pd.CategoricalIndex(list("ab"), name="x"),
182
+ pd.CategoricalIndex([0, 1], name="y"),
183
+ ]
184
+ )
185
+
186
+ ser = pd.Series(1, index=midx, dtype="Sparse[int]")
187
+ result = ser.sparse.to_coo(row_levels=["x"], column_levels=["y"])[0]
188
+ expected = sp_sparse.coo_matrix(
189
+ (np.array([1, 1]), (np.array([0, 1]), np.array([0, 1]))), shape=(2, 2)
190
+ )
191
+ assert (result != expected).nnz == 0
192
+
193
+ def test_to_dense(self):
194
+ df = pd.DataFrame(
195
+ {
196
+ "A": SparseArray([1, 0], dtype=SparseDtype("int64", 0)),
197
+ "B": SparseArray([1, 0], dtype=SparseDtype("int64", 1)),
198
+ "C": SparseArray([1.0, 0.0], dtype=SparseDtype("float64", 0.0)),
199
+ },
200
+ index=["b", "a"],
201
+ )
202
+ result = df.sparse.to_dense()
203
+ expected = pd.DataFrame(
204
+ {"A": [1, 0], "B": [1, 0], "C": [1.0, 0.0]}, index=["b", "a"]
205
+ )
206
+ tm.assert_frame_equal(result, expected)
207
+
208
+ def test_density(self):
209
+ df = pd.DataFrame(
210
+ {
211
+ "A": SparseArray([1, 0, 2, 1], fill_value=0),
212
+ "B": SparseArray([0, 1, 1, 1], fill_value=0),
213
+ }
214
+ )
215
+ res = df.sparse.density
216
+ expected = 0.75
217
+ assert res == expected
218
+
219
+ @pytest.mark.parametrize("dtype", ["int64", "float64"])
220
+ @pytest.mark.parametrize("dense_index", [True, False])
221
+ def test_series_from_coo(self, dtype, dense_index):
222
+ sp_sparse = pytest.importorskip("scipy.sparse")
223
+
224
+ A = sp_sparse.eye(3, format="coo", dtype=dtype)
225
+ result = pd.Series.sparse.from_coo(A, dense_index=dense_index)
226
+
227
+ index = pd.MultiIndex.from_tuples(
228
+ [
229
+ np.array([0, 0], dtype=np.int32),
230
+ np.array([1, 1], dtype=np.int32),
231
+ np.array([2, 2], dtype=np.int32),
232
+ ],
233
+ )
234
+ expected = pd.Series(SparseArray(np.array([1, 1, 1], dtype=dtype)), index=index)
235
+ if dense_index:
236
+ expected = expected.reindex(pd.MultiIndex.from_product(index.levels))
237
+
238
+ tm.assert_series_equal(result, expected)
239
+
240
+ def test_series_from_coo_incorrect_format_raises(self):
241
+ # gh-26554
242
+ sp_sparse = pytest.importorskip("scipy.sparse")
243
+
244
+ m = sp_sparse.csr_matrix(np.array([[0, 1], [0, 0]]))
245
+ with pytest.raises(
246
+ TypeError, match="Expected coo_matrix. Got csr_matrix instead."
247
+ ):
248
+ pd.Series.sparse.from_coo(m)
249
+
250
+ def test_with_column_named_sparse(self):
251
+ # https://github.com/pandas-dev/pandas/issues/30758
252
+ df = pd.DataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
253
+ assert isinstance(df.sparse, pd.core.arrays.sparse.accessor.SparseFrameAccessor)
.venv_haddock/lib/python3.12/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)
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_array.py ADDED
@@ -0,0 +1,511 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ import numpy as np
4
+ import pytest
5
+
6
+ from pandas._libs.sparse import IntIndex
7
+ from pandas.compat.numpy import np_version_gt2
8
+
9
+ import pandas as pd
10
+ from pandas import (
11
+ SparseDtype,
12
+ isna,
13
+ )
14
+ import pandas._testing as tm
15
+ from pandas.core.arrays.sparse import SparseArray
16
+
17
+
18
+ @pytest.fixture
19
+ def arr_data():
20
+ """Fixture returning numpy array with valid and missing entries"""
21
+ return np.array([np.nan, np.nan, 1, 2, 3, np.nan, 4, 5, np.nan, 6])
22
+
23
+
24
+ @pytest.fixture
25
+ def arr(arr_data):
26
+ """Fixture returning SparseArray from 'arr_data'"""
27
+ return SparseArray(arr_data)
28
+
29
+
30
+ @pytest.fixture
31
+ def zarr():
32
+ """Fixture returning SparseArray with integer entries and 'fill_value=0'"""
33
+ return SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)
34
+
35
+
36
+ class TestSparseArray:
37
+ @pytest.mark.parametrize("fill_value", [0, None, np.nan])
38
+ def test_shift_fill_value(self, fill_value):
39
+ # GH #24128
40
+ sparse = SparseArray(np.array([1, 0, 0, 3, 0]), fill_value=8.0)
41
+ res = sparse.shift(1, fill_value=fill_value)
42
+ if isna(fill_value):
43
+ fill_value = res.dtype.na_value
44
+ exp = SparseArray(np.array([fill_value, 1, 0, 0, 3]), fill_value=8.0)
45
+ tm.assert_sp_array_equal(res, exp)
46
+
47
+ def test_set_fill_value(self):
48
+ arr = SparseArray([1.0, np.nan, 2.0], fill_value=np.nan)
49
+ arr.fill_value = 2
50
+ assert arr.fill_value == 2
51
+
52
+ arr = SparseArray([1, 0, 2], fill_value=0, dtype=np.int64)
53
+ arr.fill_value = 2
54
+ assert arr.fill_value == 2
55
+
56
+ msg = "Allowing arbitrary scalar fill_value in SparseDtype is deprecated"
57
+ with tm.assert_produces_warning(FutureWarning, match=msg):
58
+ arr.fill_value = 3.1
59
+ assert arr.fill_value == 3.1
60
+
61
+ arr.fill_value = np.nan
62
+ assert np.isnan(arr.fill_value)
63
+
64
+ arr = SparseArray([True, False, True], fill_value=False, dtype=np.bool_)
65
+ arr.fill_value = True
66
+ assert arr.fill_value is True
67
+
68
+ with tm.assert_produces_warning(FutureWarning, match=msg):
69
+ arr.fill_value = 0
70
+
71
+ arr.fill_value = np.nan
72
+ assert np.isnan(arr.fill_value)
73
+
74
+ @pytest.mark.parametrize("val", [[1, 2, 3], np.array([1, 2]), (1, 2, 3)])
75
+ def test_set_fill_invalid_non_scalar(self, val):
76
+ arr = SparseArray([True, False, True], fill_value=False, dtype=np.bool_)
77
+ msg = "fill_value must be a scalar"
78
+
79
+ with pytest.raises(ValueError, match=msg):
80
+ arr.fill_value = val
81
+
82
+ def test_copy(self, arr):
83
+ arr2 = arr.copy()
84
+ assert arr2.sp_values is not arr.sp_values
85
+ assert arr2.sp_index is arr.sp_index
86
+
87
+ def test_values_asarray(self, arr_data, arr):
88
+ tm.assert_almost_equal(arr.to_dense(), arr_data)
89
+
90
+ @pytest.mark.parametrize(
91
+ "data,shape,dtype",
92
+ [
93
+ ([0, 0, 0, 0, 0], (5,), None),
94
+ ([], (0,), None),
95
+ ([0], (1,), None),
96
+ (["A", "A", np.nan, "B"], (4,), object),
97
+ ],
98
+ )
99
+ def test_shape(self, data, shape, dtype):
100
+ # GH 21126
101
+ out = SparseArray(data, dtype=dtype)
102
+ assert out.shape == shape
103
+
104
+ @pytest.mark.parametrize(
105
+ "vals",
106
+ [
107
+ [np.nan, np.nan, np.nan, np.nan, np.nan],
108
+ [1, np.nan, np.nan, 3, np.nan],
109
+ [1, np.nan, 0, 3, 0],
110
+ ],
111
+ )
112
+ @pytest.mark.parametrize("fill_value", [None, 0])
113
+ def test_dense_repr(self, vals, fill_value):
114
+ vals = np.array(vals)
115
+ arr = SparseArray(vals, fill_value=fill_value)
116
+
117
+ res = arr.to_dense()
118
+ tm.assert_numpy_array_equal(res, vals)
119
+
120
+ @pytest.mark.parametrize("fix", ["arr", "zarr"])
121
+ def test_pickle(self, fix, request):
122
+ obj = request.getfixturevalue(fix)
123
+ unpickled = tm.round_trip_pickle(obj)
124
+ tm.assert_sp_array_equal(unpickled, obj)
125
+
126
+ def test_generator_warnings(self):
127
+ sp_arr = SparseArray([1, 2, 3])
128
+ with tm.assert_produces_warning(None):
129
+ for _ in sp_arr:
130
+ pass
131
+
132
+ def test_where_retain_fill_value(self):
133
+ # GH#45691 don't lose fill_value on _where
134
+ arr = SparseArray([np.nan, 1.0], fill_value=0)
135
+
136
+ mask = np.array([True, False])
137
+
138
+ res = arr._where(~mask, 1)
139
+ exp = SparseArray([1, 1.0], fill_value=0)
140
+ tm.assert_sp_array_equal(res, exp)
141
+
142
+ ser = pd.Series(arr)
143
+ res = ser.where(~mask, 1)
144
+ tm.assert_series_equal(res, pd.Series(exp))
145
+
146
+ def test_fillna(self):
147
+ s = SparseArray([1, np.nan, np.nan, 3, np.nan])
148
+ res = s.fillna(-1)
149
+ exp = SparseArray([1, -1, -1, 3, -1], fill_value=-1, dtype=np.float64)
150
+ tm.assert_sp_array_equal(res, exp)
151
+
152
+ s = SparseArray([1, np.nan, np.nan, 3, np.nan], fill_value=0)
153
+ res = s.fillna(-1)
154
+ exp = SparseArray([1, -1, -1, 3, -1], fill_value=0, dtype=np.float64)
155
+ tm.assert_sp_array_equal(res, exp)
156
+
157
+ s = SparseArray([1, np.nan, 0, 3, 0])
158
+ res = s.fillna(-1)
159
+ exp = SparseArray([1, -1, 0, 3, 0], fill_value=-1, dtype=np.float64)
160
+ tm.assert_sp_array_equal(res, exp)
161
+
162
+ s = SparseArray([1, np.nan, 0, 3, 0], fill_value=0)
163
+ res = s.fillna(-1)
164
+ exp = SparseArray([1, -1, 0, 3, 0], fill_value=0, dtype=np.float64)
165
+ tm.assert_sp_array_equal(res, exp)
166
+
167
+ s = SparseArray([np.nan, np.nan, np.nan, np.nan])
168
+ res = s.fillna(-1)
169
+ exp = SparseArray([-1, -1, -1, -1], fill_value=-1, dtype=np.float64)
170
+ tm.assert_sp_array_equal(res, exp)
171
+
172
+ s = SparseArray([np.nan, np.nan, np.nan, np.nan], fill_value=0)
173
+ res = s.fillna(-1)
174
+ exp = SparseArray([-1, -1, -1, -1], fill_value=0, dtype=np.float64)
175
+ tm.assert_sp_array_equal(res, exp)
176
+
177
+ # float dtype's fill_value is np.nan, replaced by -1
178
+ s = SparseArray([0.0, 0.0, 0.0, 0.0])
179
+ res = s.fillna(-1)
180
+ exp = SparseArray([0.0, 0.0, 0.0, 0.0], fill_value=-1)
181
+ tm.assert_sp_array_equal(res, exp)
182
+
183
+ # int dtype shouldn't have missing. No changes.
184
+ s = SparseArray([0, 0, 0, 0])
185
+ assert s.dtype == SparseDtype(np.int64)
186
+ assert s.fill_value == 0
187
+ res = s.fillna(-1)
188
+ tm.assert_sp_array_equal(res, s)
189
+
190
+ s = SparseArray([0, 0, 0, 0], fill_value=0)
191
+ assert s.dtype == SparseDtype(np.int64)
192
+ assert s.fill_value == 0
193
+ res = s.fillna(-1)
194
+ exp = SparseArray([0, 0, 0, 0], fill_value=0)
195
+ tm.assert_sp_array_equal(res, exp)
196
+
197
+ # fill_value can be nan if there is no missing hole.
198
+ # only fill_value will be changed
199
+ s = SparseArray([0, 0, 0, 0], fill_value=np.nan)
200
+ assert s.dtype == SparseDtype(np.int64, fill_value=np.nan)
201
+ assert np.isnan(s.fill_value)
202
+ res = s.fillna(-1)
203
+ exp = SparseArray([0, 0, 0, 0], fill_value=-1)
204
+ tm.assert_sp_array_equal(res, exp)
205
+
206
+ def test_fillna_overlap(self):
207
+ s = SparseArray([1, np.nan, np.nan, 3, np.nan])
208
+ # filling with existing value doesn't replace existing value with
209
+ # fill_value, i.e. existing 3 remains in sp_values
210
+ res = s.fillna(3)
211
+ exp = np.array([1, 3, 3, 3, 3], dtype=np.float64)
212
+ tm.assert_numpy_array_equal(res.to_dense(), exp)
213
+
214
+ s = SparseArray([1, np.nan, np.nan, 3, np.nan], fill_value=0)
215
+ res = s.fillna(3)
216
+ exp = SparseArray([1, 3, 3, 3, 3], fill_value=0, dtype=np.float64)
217
+ tm.assert_sp_array_equal(res, exp)
218
+
219
+ def test_nonzero(self):
220
+ # Tests regression #21172.
221
+ sa = SparseArray([float("nan"), float("nan"), 1, 0, 0, 2, 0, 0, 0, 3, 0, 0])
222
+ expected = np.array([2, 5, 9], dtype=np.int32)
223
+ (result,) = sa.nonzero()
224
+ tm.assert_numpy_array_equal(expected, result)
225
+
226
+ sa = SparseArray([0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 0, 0])
227
+ (result,) = sa.nonzero()
228
+ tm.assert_numpy_array_equal(expected, result)
229
+
230
+
231
+ class TestSparseArrayAnalytics:
232
+ @pytest.mark.parametrize(
233
+ "data,expected",
234
+ [
235
+ (
236
+ np.array([1, 2, 3, 4, 5], dtype=float), # non-null data
237
+ SparseArray(np.array([1.0, 3.0, 6.0, 10.0, 15.0])),
238
+ ),
239
+ (
240
+ np.array([1, 2, np.nan, 4, 5], dtype=float), # null data
241
+ SparseArray(np.array([1.0, 3.0, np.nan, 7.0, 12.0])),
242
+ ),
243
+ ],
244
+ )
245
+ @pytest.mark.parametrize("numpy", [True, False])
246
+ def test_cumsum(self, data, expected, numpy):
247
+ cumsum = np.cumsum if numpy else lambda s: s.cumsum()
248
+
249
+ out = cumsum(SparseArray(data))
250
+ tm.assert_sp_array_equal(out, expected)
251
+
252
+ out = cumsum(SparseArray(data, fill_value=np.nan))
253
+ tm.assert_sp_array_equal(out, expected)
254
+
255
+ out = cumsum(SparseArray(data, fill_value=2))
256
+ tm.assert_sp_array_equal(out, expected)
257
+
258
+ if numpy: # numpy compatibility checks.
259
+ msg = "the 'dtype' parameter is not supported"
260
+ with pytest.raises(ValueError, match=msg):
261
+ np.cumsum(SparseArray(data), dtype=np.int64)
262
+
263
+ msg = "the 'out' parameter is not supported"
264
+ with pytest.raises(ValueError, match=msg):
265
+ np.cumsum(SparseArray(data), out=out)
266
+ else:
267
+ axis = 1 # SparseArray currently 1-D, so only axis = 0 is valid.
268
+ msg = re.escape(f"axis(={axis}) out of bounds")
269
+ with pytest.raises(ValueError, match=msg):
270
+ SparseArray(data).cumsum(axis=axis)
271
+
272
+ def test_ufunc(self):
273
+ # GH 13853 make sure ufunc is applied to fill_value
274
+ sparse = SparseArray([1, np.nan, 2, np.nan, -2])
275
+ result = SparseArray([1, np.nan, 2, np.nan, 2])
276
+ tm.assert_sp_array_equal(abs(sparse), result)
277
+ tm.assert_sp_array_equal(np.abs(sparse), result)
278
+
279
+ sparse = SparseArray([1, -1, 2, -2], fill_value=1)
280
+ result = SparseArray([1, 2, 2], sparse_index=sparse.sp_index, fill_value=1)
281
+ tm.assert_sp_array_equal(abs(sparse), result)
282
+ tm.assert_sp_array_equal(np.abs(sparse), result)
283
+
284
+ sparse = SparseArray([1, -1, 2, -2], fill_value=-1)
285
+ exp = SparseArray([1, 1, 2, 2], fill_value=1)
286
+ tm.assert_sp_array_equal(abs(sparse), exp)
287
+ tm.assert_sp_array_equal(np.abs(sparse), exp)
288
+
289
+ sparse = SparseArray([1, np.nan, 2, np.nan, -2])
290
+ result = SparseArray(np.sin([1, np.nan, 2, np.nan, -2]))
291
+ tm.assert_sp_array_equal(np.sin(sparse), result)
292
+
293
+ sparse = SparseArray([1, -1, 2, -2], fill_value=1)
294
+ result = SparseArray(np.sin([1, -1, 2, -2]), fill_value=np.sin(1))
295
+ tm.assert_sp_array_equal(np.sin(sparse), result)
296
+
297
+ sparse = SparseArray([1, -1, 0, -2], fill_value=0)
298
+ result = SparseArray(np.sin([1, -1, 0, -2]), fill_value=np.sin(0))
299
+ tm.assert_sp_array_equal(np.sin(sparse), result)
300
+
301
+ def test_ufunc_args(self):
302
+ # GH 13853 make sure ufunc is applied to fill_value, including its arg
303
+ sparse = SparseArray([1, np.nan, 2, np.nan, -2])
304
+ result = SparseArray([2, np.nan, 3, np.nan, -1])
305
+ tm.assert_sp_array_equal(np.add(sparse, 1), result)
306
+
307
+ sparse = SparseArray([1, -1, 2, -2], fill_value=1)
308
+ result = SparseArray([2, 0, 3, -1], fill_value=2)
309
+ tm.assert_sp_array_equal(np.add(sparse, 1), result)
310
+
311
+ sparse = SparseArray([1, -1, 0, -2], fill_value=0)
312
+ result = SparseArray([2, 0, 1, -1], fill_value=1)
313
+ tm.assert_sp_array_equal(np.add(sparse, 1), result)
314
+
315
+ @pytest.mark.parametrize("fill_value", [0.0, np.nan])
316
+ def test_modf(self, fill_value):
317
+ # https://github.com/pandas-dev/pandas/issues/26946
318
+ sparse = SparseArray([fill_value] * 10 + [1.1, 2.2], fill_value=fill_value)
319
+ r1, r2 = np.modf(sparse)
320
+ e1, e2 = np.modf(np.asarray(sparse))
321
+ tm.assert_sp_array_equal(r1, SparseArray(e1, fill_value=fill_value))
322
+ tm.assert_sp_array_equal(r2, SparseArray(e2, fill_value=fill_value))
323
+
324
+ def test_nbytes_integer(self):
325
+ arr = SparseArray([1, 0, 0, 0, 2], kind="integer")
326
+ result = arr.nbytes
327
+ # (2 * 8) + 2 * 4
328
+ assert result == 24
329
+
330
+ def test_nbytes_block(self):
331
+ arr = SparseArray([1, 2, 0, 0, 0], kind="block")
332
+ result = arr.nbytes
333
+ # (2 * 8) + 4 + 4
334
+ # sp_values, blocs, blengths
335
+ assert result == 24
336
+
337
+ def test_asarray_datetime64(self):
338
+ s = SparseArray(pd.to_datetime(["2012", None, None, "2013"]))
339
+ np.asarray(s)
340
+
341
+ def test_density(self):
342
+ arr = SparseArray([0, 1])
343
+ assert arr.density == 0.5
344
+
345
+ def test_npoints(self):
346
+ arr = SparseArray([0, 1])
347
+ assert arr.npoints == 1
348
+
349
+
350
+ def test_setting_fill_value_fillna_still_works():
351
+ # This is why letting users update fill_value / dtype is bad
352
+ # astype has the same problem.
353
+ arr = SparseArray([1.0, np.nan, 1.0], fill_value=0.0)
354
+ arr.fill_value = np.nan
355
+ result = arr.isna()
356
+ # Can't do direct comparison, since the sp_index will be different
357
+ # So let's convert to ndarray and check there.
358
+ result = np.asarray(result)
359
+
360
+ expected = np.array([False, True, False])
361
+ tm.assert_numpy_array_equal(result, expected)
362
+
363
+
364
+ def test_setting_fill_value_updates():
365
+ arr = SparseArray([0.0, np.nan], fill_value=0)
366
+ arr.fill_value = np.nan
367
+ # use private constructor to get the index right
368
+ # otherwise both nans would be un-stored.
369
+ expected = SparseArray._simple_new(
370
+ sparse_array=np.array([np.nan]),
371
+ sparse_index=IntIndex(2, [1]),
372
+ dtype=SparseDtype(float, np.nan),
373
+ )
374
+ tm.assert_sp_array_equal(arr, expected)
375
+
376
+
377
+ @pytest.mark.parametrize(
378
+ "arr,fill_value,loc",
379
+ [
380
+ ([None, 1, 2], None, 0),
381
+ ([0, None, 2], None, 1),
382
+ ([0, 1, None], None, 2),
383
+ ([0, 1, 1, None, None], None, 3),
384
+ ([1, 1, 1, 2], None, -1),
385
+ ([], None, -1),
386
+ ([None, 1, 0, 0, None, 2], None, 0),
387
+ ([None, 1, 0, 0, None, 2], 1, 1),
388
+ ([None, 1, 0, 0, None, 2], 2, 5),
389
+ ([None, 1, 0, 0, None, 2], 3, -1),
390
+ ([None, 0, 0, 1, 2, 1], 0, 1),
391
+ ([None, 0, 0, 1, 2, 1], 1, 3),
392
+ ],
393
+ )
394
+ def test_first_fill_value_loc(arr, fill_value, loc):
395
+ result = SparseArray(arr, fill_value=fill_value)._first_fill_value_loc()
396
+ assert result == loc
397
+
398
+
399
+ @pytest.mark.parametrize(
400
+ "arr",
401
+ [
402
+ [1, 2, np.nan, np.nan],
403
+ [1, np.nan, 2, np.nan],
404
+ [1, 2, np.nan],
405
+ [np.nan, 1, 0, 0, np.nan, 2],
406
+ [np.nan, 0, 0, 1, 2, 1],
407
+ ],
408
+ )
409
+ @pytest.mark.parametrize("fill_value", [np.nan, 0, 1])
410
+ def test_unique_na_fill(arr, fill_value):
411
+ a = SparseArray(arr, fill_value=fill_value).unique()
412
+ b = pd.Series(arr).unique()
413
+ assert isinstance(a, SparseArray)
414
+ a = np.asarray(a)
415
+ tm.assert_numpy_array_equal(a, b)
416
+
417
+
418
+ def test_unique_all_sparse():
419
+ # https://github.com/pandas-dev/pandas/issues/23168
420
+ arr = SparseArray([0, 0])
421
+ result = arr.unique()
422
+ expected = SparseArray([0])
423
+ tm.assert_sp_array_equal(result, expected)
424
+
425
+
426
+ def test_map():
427
+ arr = SparseArray([0, 1, 2])
428
+ expected = SparseArray([10, 11, 12], fill_value=10)
429
+
430
+ # dict
431
+ result = arr.map({0: 10, 1: 11, 2: 12})
432
+ tm.assert_sp_array_equal(result, expected)
433
+
434
+ # series
435
+ result = arr.map(pd.Series({0: 10, 1: 11, 2: 12}))
436
+ tm.assert_sp_array_equal(result, expected)
437
+
438
+ # function
439
+ result = arr.map(pd.Series({0: 10, 1: 11, 2: 12}))
440
+ expected = SparseArray([10, 11, 12], fill_value=10)
441
+ tm.assert_sp_array_equal(result, expected)
442
+
443
+
444
+ def test_map_missing():
445
+ arr = SparseArray([0, 1, 2])
446
+ expected = SparseArray([10, 11, None], fill_value=10)
447
+
448
+ result = arr.map({0: 10, 1: 11})
449
+ tm.assert_sp_array_equal(result, expected)
450
+
451
+
452
+ @pytest.mark.parametrize("fill_value", [np.nan, 1])
453
+ def test_dropna(fill_value):
454
+ # GH-28287
455
+ arr = SparseArray([np.nan, 1], fill_value=fill_value)
456
+ exp = SparseArray([1.0], fill_value=fill_value)
457
+ tm.assert_sp_array_equal(arr.dropna(), exp)
458
+
459
+ df = pd.DataFrame({"a": [0, 1], "b": arr})
460
+ expected_df = pd.DataFrame({"a": [1], "b": exp}, index=pd.Index([1]))
461
+ tm.assert_equal(df.dropna(), expected_df)
462
+
463
+
464
+ def test_drop_duplicates_fill_value():
465
+ # GH 11726
466
+ df = pd.DataFrame(np.zeros((5, 5))).apply(lambda x: SparseArray(x, fill_value=0))
467
+ result = df.drop_duplicates()
468
+ expected = pd.DataFrame({i: SparseArray([0.0], fill_value=0) for i in range(5)})
469
+ tm.assert_frame_equal(result, expected)
470
+
471
+
472
+ def test_zero_sparse_column():
473
+ # GH 27781
474
+ df1 = pd.DataFrame({"A": SparseArray([0, 0, 0]), "B": [1, 2, 3]})
475
+ df2 = pd.DataFrame({"A": SparseArray([0, 1, 0]), "B": [1, 2, 3]})
476
+ result = df1.loc[df1["B"] != 2]
477
+ expected = df2.loc[df2["B"] != 2]
478
+ tm.assert_frame_equal(result, expected)
479
+
480
+ expected = pd.DataFrame({"A": SparseArray([0, 0]), "B": [1, 3]}, index=[0, 2])
481
+ tm.assert_frame_equal(result, expected)
482
+
483
+
484
+ def test_array_interface(arr_data, arr):
485
+ # https://github.com/pandas-dev/pandas/pull/60046
486
+ result = np.asarray(arr)
487
+ tm.assert_numpy_array_equal(result, arr_data)
488
+
489
+ # it always gives a copy by default
490
+ result_copy1 = np.asarray(arr)
491
+ result_copy2 = np.asarray(arr)
492
+ assert not np.may_share_memory(result_copy1, result_copy2)
493
+
494
+ # or with explicit copy=True
495
+ result_copy1 = np.array(arr, copy=True)
496
+ result_copy2 = np.array(arr, copy=True)
497
+ assert not np.may_share_memory(result_copy1, result_copy2)
498
+
499
+ if not np_version_gt2:
500
+ # copy=False semantics are only supported in NumPy>=2.
501
+ return
502
+
503
+ msg = "Starting with NumPy 2.0, the behavior of the 'copy' keyword has changed"
504
+ with tm.assert_produces_warning(FutureWarning, match=msg):
505
+ np.array(arr, copy=False)
506
+
507
+ # except when there are actually no sparse filled values
508
+ arr2 = SparseArray(np.array([1, 2, 3]))
509
+ result_nocopy1 = np.array(arr2, copy=False)
510
+ result_nocopy2 = np.array(arr2, copy=False)
511
+ assert np.may_share_memory(result_nocopy1, result_nocopy2)
.venv_haddock/lib/python3.12/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
+ np.str_,
85
+ SparseArray(["0", "1", "10"], dtype=SparseDtype(np.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)
.venv_haddock/lib/python3.12/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)
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_constructors.py ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pytest
3
+
4
+ from pandas._libs.sparse import IntIndex
5
+
6
+ import pandas as pd
7
+ from pandas import (
8
+ SparseDtype,
9
+ isna,
10
+ )
11
+ import pandas._testing as tm
12
+ from pandas.core.arrays.sparse import SparseArray
13
+
14
+
15
+ class TestConstructors:
16
+ def test_constructor_dtype(self):
17
+ arr = SparseArray([np.nan, 1, 2, np.nan])
18
+ assert arr.dtype == SparseDtype(np.float64, np.nan)
19
+ assert arr.dtype.subtype == np.float64
20
+ assert np.isnan(arr.fill_value)
21
+
22
+ arr = SparseArray([np.nan, 1, 2, np.nan], fill_value=0)
23
+ assert arr.dtype == SparseDtype(np.float64, 0)
24
+ assert arr.fill_value == 0
25
+
26
+ arr = SparseArray([0, 1, 2, 4], dtype=np.float64)
27
+ assert arr.dtype == SparseDtype(np.float64, np.nan)
28
+ assert np.isnan(arr.fill_value)
29
+
30
+ arr = SparseArray([0, 1, 2, 4], dtype=np.int64)
31
+ assert arr.dtype == SparseDtype(np.int64, 0)
32
+ assert arr.fill_value == 0
33
+
34
+ arr = SparseArray([0, 1, 2, 4], fill_value=0, dtype=np.int64)
35
+ assert arr.dtype == SparseDtype(np.int64, 0)
36
+ assert arr.fill_value == 0
37
+
38
+ arr = SparseArray([0, 1, 2, 4], dtype=None)
39
+ assert arr.dtype == SparseDtype(np.int64, 0)
40
+ assert arr.fill_value == 0
41
+
42
+ arr = SparseArray([0, 1, 2, 4], fill_value=0, dtype=None)
43
+ assert arr.dtype == SparseDtype(np.int64, 0)
44
+ assert arr.fill_value == 0
45
+
46
+ def test_constructor_dtype_str(self):
47
+ result = SparseArray([1, 2, 3], dtype="int")
48
+ expected = SparseArray([1, 2, 3], dtype=int)
49
+ tm.assert_sp_array_equal(result, expected)
50
+
51
+ def test_constructor_sparse_dtype(self):
52
+ result = SparseArray([1, 0, 0, 1], dtype=SparseDtype("int64", -1))
53
+ expected = SparseArray([1, 0, 0, 1], fill_value=-1, dtype=np.int64)
54
+ tm.assert_sp_array_equal(result, expected)
55
+ assert result.sp_values.dtype == np.dtype("int64")
56
+
57
+ def test_constructor_sparse_dtype_str(self):
58
+ result = SparseArray([1, 0, 0, 1], dtype="Sparse[int32]")
59
+ expected = SparseArray([1, 0, 0, 1], dtype=np.int32)
60
+ tm.assert_sp_array_equal(result, expected)
61
+ assert result.sp_values.dtype == np.dtype("int32")
62
+
63
+ def test_constructor_object_dtype(self):
64
+ # GH#11856
65
+ arr = SparseArray(["A", "A", np.nan, "B"], dtype=object)
66
+ assert arr.dtype == SparseDtype(object)
67
+ assert np.isnan(arr.fill_value)
68
+
69
+ arr = SparseArray(["A", "A", np.nan, "B"], dtype=object, fill_value="A")
70
+ assert arr.dtype == SparseDtype(object, "A")
71
+ assert arr.fill_value == "A"
72
+
73
+ def test_constructor_object_dtype_bool_fill(self):
74
+ # GH#17574
75
+ data = [False, 0, 100.0, 0.0]
76
+ arr = SparseArray(data, dtype=object, fill_value=False)
77
+ assert arr.dtype == SparseDtype(object, False)
78
+ assert arr.fill_value is False
79
+ arr_expected = np.array(data, dtype=object)
80
+ it = (type(x) == type(y) and x == y for x, y in zip(arr, arr_expected))
81
+ assert np.fromiter(it, dtype=np.bool_).all()
82
+
83
+ @pytest.mark.parametrize("dtype", [SparseDtype(int, 0), int])
84
+ def test_constructor_na_dtype(self, dtype):
85
+ with pytest.raises(ValueError, match="Cannot convert"):
86
+ SparseArray([0, 1, np.nan], dtype=dtype)
87
+
88
+ def test_constructor_warns_when_losing_timezone(self):
89
+ # GH#32501 warn when losing timezone information
90
+ dti = pd.date_range("2016-01-01", periods=3, tz="US/Pacific")
91
+
92
+ expected = SparseArray(np.asarray(dti, dtype="datetime64[ns]"))
93
+
94
+ with tm.assert_produces_warning(UserWarning):
95
+ result = SparseArray(dti)
96
+
97
+ tm.assert_sp_array_equal(result, expected)
98
+
99
+ with tm.assert_produces_warning(UserWarning):
100
+ result = SparseArray(pd.Series(dti))
101
+
102
+ tm.assert_sp_array_equal(result, expected)
103
+
104
+ def test_constructor_spindex_dtype(self):
105
+ arr = SparseArray(data=[1, 2], sparse_index=IntIndex(4, [1, 2]))
106
+ # TODO: actionable?
107
+ # XXX: Behavior change: specifying SparseIndex no longer changes the
108
+ # fill_value
109
+ expected = SparseArray([0, 1, 2, 0], kind="integer")
110
+ tm.assert_sp_array_equal(arr, expected)
111
+ assert arr.dtype == SparseDtype(np.int64)
112
+ assert arr.fill_value == 0
113
+
114
+ arr = SparseArray(
115
+ data=[1, 2, 3],
116
+ sparse_index=IntIndex(4, [1, 2, 3]),
117
+ dtype=np.int64,
118
+ fill_value=0,
119
+ )
120
+ exp = SparseArray([0, 1, 2, 3], dtype=np.int64, fill_value=0)
121
+ tm.assert_sp_array_equal(arr, exp)
122
+ assert arr.dtype == SparseDtype(np.int64)
123
+ assert arr.fill_value == 0
124
+
125
+ arr = SparseArray(
126
+ data=[1, 2], sparse_index=IntIndex(4, [1, 2]), fill_value=0, dtype=np.int64
127
+ )
128
+ exp = SparseArray([0, 1, 2, 0], fill_value=0, dtype=np.int64)
129
+ tm.assert_sp_array_equal(arr, exp)
130
+ assert arr.dtype == SparseDtype(np.int64)
131
+ assert arr.fill_value == 0
132
+
133
+ arr = SparseArray(
134
+ data=[1, 2, 3],
135
+ sparse_index=IntIndex(4, [1, 2, 3]),
136
+ dtype=None,
137
+ fill_value=0,
138
+ )
139
+ exp = SparseArray([0, 1, 2, 3], dtype=None)
140
+ tm.assert_sp_array_equal(arr, exp)
141
+ assert arr.dtype == SparseDtype(np.int64)
142
+ assert arr.fill_value == 0
143
+
144
+ @pytest.mark.parametrize("sparse_index", [None, IntIndex(1, [0])])
145
+ def test_constructor_spindex_dtype_scalar(self, sparse_index):
146
+ # scalar input
147
+ msg = "Constructing SparseArray with scalar data is deprecated"
148
+ with tm.assert_produces_warning(FutureWarning, match=msg):
149
+ arr = SparseArray(data=1, sparse_index=sparse_index, dtype=None)
150
+ exp = SparseArray([1], dtype=None)
151
+ tm.assert_sp_array_equal(arr, exp)
152
+ assert arr.dtype == SparseDtype(np.int64)
153
+ assert arr.fill_value == 0
154
+
155
+ with tm.assert_produces_warning(FutureWarning, match=msg):
156
+ arr = SparseArray(data=1, sparse_index=IntIndex(1, [0]), dtype=None)
157
+ exp = SparseArray([1], dtype=None)
158
+ tm.assert_sp_array_equal(arr, exp)
159
+ assert arr.dtype == SparseDtype(np.int64)
160
+ assert arr.fill_value == 0
161
+
162
+ def test_constructor_spindex_dtype_scalar_broadcasts(self):
163
+ arr = SparseArray(
164
+ data=[1, 2], sparse_index=IntIndex(4, [1, 2]), fill_value=0, dtype=None
165
+ )
166
+ exp = SparseArray([0, 1, 2, 0], fill_value=0, dtype=None)
167
+ tm.assert_sp_array_equal(arr, exp)
168
+ assert arr.dtype == SparseDtype(np.int64)
169
+ assert arr.fill_value == 0
170
+
171
+ @pytest.mark.parametrize(
172
+ "data, fill_value",
173
+ [
174
+ (np.array([1, 2]), 0),
175
+ (np.array([1.0, 2.0]), np.nan),
176
+ ([True, False], False),
177
+ ([pd.Timestamp("2017-01-01")], pd.NaT),
178
+ ],
179
+ )
180
+ def test_constructor_inferred_fill_value(self, data, fill_value):
181
+ result = SparseArray(data).fill_value
182
+
183
+ if isna(fill_value):
184
+ assert isna(result)
185
+ else:
186
+ assert result == fill_value
187
+
188
+ @pytest.mark.parametrize("format", ["coo", "csc", "csr"])
189
+ @pytest.mark.parametrize("size", [0, 10])
190
+ def test_from_spmatrix(self, size, format):
191
+ sp_sparse = pytest.importorskip("scipy.sparse")
192
+
193
+ mat = sp_sparse.random(size, 1, density=0.5, format=format)
194
+ result = SparseArray.from_spmatrix(mat)
195
+
196
+ result = np.asarray(result)
197
+ expected = mat.toarray().ravel()
198
+ tm.assert_numpy_array_equal(result, expected)
199
+
200
+ @pytest.mark.parametrize("format", ["coo", "csc", "csr"])
201
+ def test_from_spmatrix_including_explicit_zero(self, format):
202
+ sp_sparse = pytest.importorskip("scipy.sparse")
203
+
204
+ mat = sp_sparse.random(10, 1, density=0.5, format=format)
205
+ mat.data[0] = 0
206
+ result = SparseArray.from_spmatrix(mat)
207
+
208
+ result = np.asarray(result)
209
+ expected = mat.toarray().ravel()
210
+ tm.assert_numpy_array_equal(result, expected)
211
+
212
+ def test_from_spmatrix_raises(self):
213
+ sp_sparse = pytest.importorskip("scipy.sparse")
214
+
215
+ mat = sp_sparse.eye(5, 4, format="csc")
216
+
217
+ with pytest.raises(ValueError, match="not '4'"):
218
+ SparseArray.from_spmatrix(mat)
219
+
220
+ def test_constructor_from_too_large_array(self):
221
+ with pytest.raises(TypeError, match="expected dimension <= 1 data"):
222
+ SparseArray(np.arange(10).reshape((2, 5)))
223
+
224
+ def test_constructor_from_sparse(self):
225
+ zarr = SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)
226
+ res = SparseArray(zarr)
227
+ assert res.fill_value == 0
228
+ tm.assert_almost_equal(res.sp_values, zarr.sp_values)
229
+
230
+ def test_constructor_copy(self):
231
+ arr_data = np.array([np.nan, np.nan, 1, 2, 3, np.nan, 4, 5, np.nan, 6])
232
+ arr = SparseArray(arr_data)
233
+
234
+ cp = SparseArray(arr, copy=True)
235
+ cp.sp_values[:3] = 0
236
+ assert not (arr.sp_values[:3] == 0).any()
237
+
238
+ not_copy = SparseArray(arr)
239
+ not_copy.sp_values[:3] = 0
240
+ assert (arr.sp_values[:3] == 0).all()
241
+
242
+ def test_constructor_bool(self):
243
+ # GH#10648
244
+ data = np.array([False, False, True, True, False, False])
245
+ arr = SparseArray(data, fill_value=False, dtype=bool)
246
+
247
+ assert arr.dtype == SparseDtype(bool)
248
+ tm.assert_numpy_array_equal(arr.sp_values, np.array([True, True]))
249
+ # Behavior change: np.asarray densifies.
250
+ # tm.assert_numpy_array_equal(arr.sp_values, np.asarray(arr))
251
+ tm.assert_numpy_array_equal(arr.sp_index.indices, np.array([2, 3], np.int32))
252
+
253
+ dense = arr.to_dense()
254
+ assert dense.dtype == bool
255
+ tm.assert_numpy_array_equal(dense, data)
256
+
257
+ def test_constructor_bool_fill_value(self):
258
+ arr = SparseArray([True, False, True], dtype=None)
259
+ assert arr.dtype == SparseDtype(np.bool_)
260
+ assert not arr.fill_value
261
+
262
+ arr = SparseArray([True, False, True], dtype=np.bool_)
263
+ assert arr.dtype == SparseDtype(np.bool_)
264
+ assert not arr.fill_value
265
+
266
+ arr = SparseArray([True, False, True], dtype=np.bool_, fill_value=True)
267
+ assert arr.dtype == SparseDtype(np.bool_, True)
268
+ assert arr.fill_value
269
+
270
+ def test_constructor_float32(self):
271
+ # GH#10648
272
+ data = np.array([1.0, np.nan, 3], dtype=np.float32)
273
+ arr = SparseArray(data, dtype=np.float32)
274
+
275
+ assert arr.dtype == SparseDtype(np.float32)
276
+ tm.assert_numpy_array_equal(arr.sp_values, np.array([1, 3], dtype=np.float32))
277
+ # Behavior change: np.asarray densifies.
278
+ # tm.assert_numpy_array_equal(arr.sp_values, np.asarray(arr))
279
+ tm.assert_numpy_array_equal(
280
+ arr.sp_index.indices, np.array([0, 2], dtype=np.int32)
281
+ )
282
+
283
+ dense = arr.to_dense()
284
+ assert dense.dtype == np.float32
285
+ tm.assert_numpy_array_equal(dense, data)
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_dtype.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import warnings
3
+
4
+ import numpy as np
5
+ import pytest
6
+
7
+ import pandas as pd
8
+ from pandas import SparseDtype
9
+
10
+
11
+ @pytest.mark.parametrize(
12
+ "dtype, fill_value",
13
+ [
14
+ ("int", 0),
15
+ ("float", np.nan),
16
+ ("bool", False),
17
+ ("object", np.nan),
18
+ ("datetime64[ns]", np.datetime64("NaT", "ns")),
19
+ ("timedelta64[ns]", np.timedelta64("NaT", "ns")),
20
+ ],
21
+ )
22
+ def test_inferred_dtype(dtype, fill_value):
23
+ sparse_dtype = SparseDtype(dtype)
24
+ result = sparse_dtype.fill_value
25
+ if pd.isna(fill_value):
26
+ assert pd.isna(result) and type(result) == type(fill_value)
27
+ else:
28
+ assert result == fill_value
29
+
30
+
31
+ def test_from_sparse_dtype():
32
+ dtype = SparseDtype("float", 0)
33
+ result = SparseDtype(dtype)
34
+ assert result.fill_value == 0
35
+
36
+
37
+ def test_from_sparse_dtype_fill_value():
38
+ dtype = SparseDtype("int", 1)
39
+ result = SparseDtype(dtype, fill_value=2)
40
+ expected = SparseDtype("int", 2)
41
+ assert result == expected
42
+
43
+
44
+ @pytest.mark.parametrize(
45
+ "dtype, fill_value",
46
+ [
47
+ ("int", None),
48
+ ("float", None),
49
+ ("bool", None),
50
+ ("object", None),
51
+ ("datetime64[ns]", None),
52
+ ("timedelta64[ns]", None),
53
+ ("int", np.nan),
54
+ ("float", 0),
55
+ ],
56
+ )
57
+ def test_equal(dtype, fill_value):
58
+ a = SparseDtype(dtype, fill_value)
59
+ b = SparseDtype(dtype, fill_value)
60
+ assert a == b
61
+ assert b == a
62
+
63
+
64
+ def test_nans_equal():
65
+ a = SparseDtype(float, float("nan"))
66
+ b = SparseDtype(float, np.nan)
67
+ assert a == b
68
+ assert b == a
69
+
70
+
71
+ with warnings.catch_warnings():
72
+ msg = "Allowing arbitrary scalar fill_value in SparseDtype is deprecated"
73
+ warnings.filterwarnings("ignore", msg, category=FutureWarning)
74
+
75
+ tups = [
76
+ (SparseDtype("float64"), SparseDtype("float32")),
77
+ (SparseDtype("float64"), SparseDtype("float64", 0)),
78
+ (SparseDtype("float64"), SparseDtype("datetime64[ns]", np.nan)),
79
+ (SparseDtype(int, pd.NaT), SparseDtype(float, pd.NaT)),
80
+ (SparseDtype("float64"), np.dtype("float64")),
81
+ ]
82
+
83
+
84
+ @pytest.mark.parametrize(
85
+ "a, b",
86
+ tups,
87
+ )
88
+ def test_not_equal(a, b):
89
+ assert a != b
90
+
91
+
92
+ def test_construct_from_string_raises():
93
+ with pytest.raises(
94
+ TypeError, match="Cannot construct a 'SparseDtype' from 'not a dtype'"
95
+ ):
96
+ SparseDtype.construct_from_string("not a dtype")
97
+
98
+
99
+ @pytest.mark.parametrize(
100
+ "dtype, expected",
101
+ [
102
+ (SparseDtype(int), True),
103
+ (SparseDtype(float), True),
104
+ (SparseDtype(bool), True),
105
+ (SparseDtype(object), False),
106
+ (SparseDtype(str), False),
107
+ ],
108
+ )
109
+ def test_is_numeric(dtype, expected):
110
+ assert dtype._is_numeric is expected
111
+
112
+
113
+ def test_str_uses_object():
114
+ result = SparseDtype(str).subtype
115
+ assert result == np.dtype("object")
116
+
117
+
118
+ @pytest.mark.parametrize(
119
+ "string, expected",
120
+ [
121
+ ("Sparse[float64]", SparseDtype(np.dtype("float64"))),
122
+ ("Sparse[float32]", SparseDtype(np.dtype("float32"))),
123
+ ("Sparse[int]", SparseDtype(np.dtype("int"))),
124
+ ("Sparse[str]", SparseDtype(np.dtype("str"))),
125
+ ("Sparse[datetime64[ns]]", SparseDtype(np.dtype("datetime64[ns]"))),
126
+ ("Sparse", SparseDtype(np.dtype("float"), np.nan)),
127
+ ],
128
+ )
129
+ def test_construct_from_string(string, expected):
130
+ result = SparseDtype.construct_from_string(string)
131
+ assert result == expected
132
+
133
+
134
+ @pytest.mark.parametrize(
135
+ "a, b, expected",
136
+ [
137
+ (SparseDtype(float, 0.0), SparseDtype(np.dtype("float"), 0.0), True),
138
+ (SparseDtype(int, 0), SparseDtype(int, 0), True),
139
+ (SparseDtype(float, float("nan")), SparseDtype(float, np.nan), True),
140
+ (SparseDtype(float, 0), SparseDtype(float, np.nan), False),
141
+ (SparseDtype(int, 0.0), SparseDtype(float, 0.0), False),
142
+ ],
143
+ )
144
+ def test_hash_equal(a, b, expected):
145
+ result = a == b
146
+ assert result is expected
147
+
148
+ result = hash(a) == hash(b)
149
+ assert result is expected
150
+
151
+
152
+ @pytest.mark.parametrize(
153
+ "string, expected",
154
+ [
155
+ ("Sparse[int]", "int"),
156
+ ("Sparse[int, 0]", "int"),
157
+ ("Sparse[int64]", "int64"),
158
+ ("Sparse[int64, 0]", "int64"),
159
+ ("Sparse[datetime64[ns], 0]", "datetime64[ns]"),
160
+ ],
161
+ )
162
+ def test_parse_subtype(string, expected):
163
+ subtype, _ = SparseDtype._parse_subtype(string)
164
+ assert subtype == expected
165
+
166
+
167
+ @pytest.mark.parametrize(
168
+ "string", ["Sparse[int, 1]", "Sparse[float, 0.0]", "Sparse[bool, True]"]
169
+ )
170
+ def test_construct_from_string_fill_value_raises(string):
171
+ with pytest.raises(TypeError, match="fill_value in the string is not"):
172
+ SparseDtype.construct_from_string(string)
173
+
174
+
175
+ @pytest.mark.parametrize(
176
+ "original, dtype, expected",
177
+ [
178
+ (SparseDtype(int, 0), float, SparseDtype(float, 0.0)),
179
+ (SparseDtype(int, 1), float, SparseDtype(float, 1.0)),
180
+ (SparseDtype(int, 1), np.str_, SparseDtype(object, "1")),
181
+ (SparseDtype(float, 1.5), int, SparseDtype(int, 1)),
182
+ ],
183
+ )
184
+ def test_update_dtype(original, dtype, expected):
185
+ result = original.update_dtype(dtype)
186
+ assert result == expected
187
+
188
+
189
+ @pytest.mark.parametrize(
190
+ "original, dtype, expected_error_msg",
191
+ [
192
+ (
193
+ SparseDtype(float, np.nan),
194
+ int,
195
+ re.escape("Cannot convert non-finite values (NA or inf) to integer"),
196
+ ),
197
+ (
198
+ SparseDtype(str, "abc"),
199
+ int,
200
+ r"invalid literal for int\(\) with base 10: ('abc'|np\.str_\('abc'\))",
201
+ ),
202
+ ],
203
+ )
204
+ def test_update_dtype_raises(original, dtype, expected_error_msg):
205
+ with pytest.raises(ValueError, match=expected_error_msg):
206
+ original.update_dtype(dtype)
207
+
208
+
209
+ def test_repr():
210
+ # GH-34352
211
+ result = str(SparseDtype("int64", fill_value=0))
212
+ expected = "Sparse[int64, 0]"
213
+ assert result == expected
214
+
215
+ result = str(SparseDtype(object, fill_value="0"))
216
+ expected = "Sparse[object, '0']"
217
+ assert result == expected
218
+
219
+
220
+ def test_sparse_dtype_subtype_must_be_numpy_dtype():
221
+ # GH#53160
222
+ msg = "SparseDtype subtype must be a numpy dtype"
223
+ with pytest.raises(TypeError, match=msg):
224
+ SparseDtype("category", fill_value="c")
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_indexing.py ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pytest
3
+
4
+ import pandas as pd
5
+ from pandas import SparseDtype
6
+ import pandas._testing as tm
7
+ from pandas.core.arrays.sparse import SparseArray
8
+
9
+
10
+ @pytest.fixture
11
+ def arr_data():
12
+ return np.array([np.nan, np.nan, 1, 2, 3, np.nan, 4, 5, np.nan, 6])
13
+
14
+
15
+ @pytest.fixture
16
+ def arr(arr_data):
17
+ return SparseArray(arr_data)
18
+
19
+
20
+ class TestGetitem:
21
+ def test_getitem(self, arr):
22
+ dense = arr.to_dense()
23
+ for i, value in enumerate(arr):
24
+ tm.assert_almost_equal(value, dense[i])
25
+ tm.assert_almost_equal(arr[-i], dense[-i])
26
+
27
+ def test_getitem_arraylike_mask(self, arr):
28
+ arr = SparseArray([0, 1, 2])
29
+ result = arr[[True, False, True]]
30
+ expected = SparseArray([0, 2])
31
+ tm.assert_sp_array_equal(result, expected)
32
+
33
+ @pytest.mark.parametrize(
34
+ "slc",
35
+ [
36
+ np.s_[:],
37
+ np.s_[1:10],
38
+ np.s_[1:100],
39
+ np.s_[10:1],
40
+ np.s_[:-3],
41
+ np.s_[-5:-4],
42
+ np.s_[:-12],
43
+ np.s_[-12:],
44
+ np.s_[2:],
45
+ np.s_[2::3],
46
+ np.s_[::2],
47
+ np.s_[::-1],
48
+ np.s_[::-2],
49
+ np.s_[1:6:2],
50
+ np.s_[:-6:-2],
51
+ ],
52
+ )
53
+ @pytest.mark.parametrize(
54
+ "as_dense", [[np.nan] * 10, [1] * 10, [np.nan] * 5 + [1] * 5, []]
55
+ )
56
+ def test_getslice(self, slc, as_dense):
57
+ as_dense = np.array(as_dense)
58
+ arr = SparseArray(as_dense)
59
+
60
+ result = arr[slc]
61
+ expected = SparseArray(as_dense[slc])
62
+
63
+ tm.assert_sp_array_equal(result, expected)
64
+
65
+ def test_getslice_tuple(self):
66
+ dense = np.array([np.nan, 0, 3, 4, 0, 5, np.nan, np.nan, 0])
67
+
68
+ sparse = SparseArray(dense)
69
+ res = sparse[(slice(4, None),)]
70
+ exp = SparseArray(dense[4:])
71
+ tm.assert_sp_array_equal(res, exp)
72
+
73
+ sparse = SparseArray(dense, fill_value=0)
74
+ res = sparse[(slice(4, None),)]
75
+ exp = SparseArray(dense[4:], fill_value=0)
76
+ tm.assert_sp_array_equal(res, exp)
77
+
78
+ msg = "too many indices for array"
79
+ with pytest.raises(IndexError, match=msg):
80
+ sparse[4:, :]
81
+
82
+ with pytest.raises(IndexError, match=msg):
83
+ # check numpy compat
84
+ dense[4:, :]
85
+
86
+ def test_boolean_slice_empty(self):
87
+ arr = SparseArray([0, 1, 2])
88
+ res = arr[[False, False, False]]
89
+ assert res.dtype == arr.dtype
90
+
91
+ def test_getitem_bool_sparse_array(self, arr):
92
+ # GH 23122
93
+ spar_bool = SparseArray([False, True] * 5, dtype=np.bool_, fill_value=True)
94
+ exp = SparseArray([np.nan, 2, np.nan, 5, 6])
95
+ tm.assert_sp_array_equal(arr[spar_bool], exp)
96
+
97
+ spar_bool = ~spar_bool
98
+ res = arr[spar_bool]
99
+ exp = SparseArray([np.nan, 1, 3, 4, np.nan])
100
+ tm.assert_sp_array_equal(res, exp)
101
+
102
+ spar_bool = SparseArray(
103
+ [False, True, np.nan] * 3, dtype=np.bool_, fill_value=np.nan
104
+ )
105
+ res = arr[spar_bool]
106
+ exp = SparseArray([np.nan, 3, 5])
107
+ tm.assert_sp_array_equal(res, exp)
108
+
109
+ def test_getitem_bool_sparse_array_as_comparison(self):
110
+ # GH 45110
111
+ arr = SparseArray([1, 2, 3, 4, np.nan, np.nan], fill_value=np.nan)
112
+ res = arr[arr > 2]
113
+ exp = SparseArray([3.0, 4.0], fill_value=np.nan)
114
+ tm.assert_sp_array_equal(res, exp)
115
+
116
+ def test_get_item(self, arr):
117
+ zarr = SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)
118
+
119
+ assert np.isnan(arr[1])
120
+ assert arr[2] == 1
121
+ assert arr[7] == 5
122
+
123
+ assert zarr[0] == 0
124
+ assert zarr[2] == 1
125
+ assert zarr[7] == 5
126
+
127
+ errmsg = "must be an integer between -10 and 10"
128
+
129
+ with pytest.raises(IndexError, match=errmsg):
130
+ arr[11]
131
+
132
+ with pytest.raises(IndexError, match=errmsg):
133
+ arr[-11]
134
+
135
+ assert arr[-1] == arr[len(arr) - 1]
136
+
137
+
138
+ class TestSetitem:
139
+ def test_set_item(self, arr_data):
140
+ arr = SparseArray(arr_data).copy()
141
+
142
+ def setitem():
143
+ arr[5] = 3
144
+
145
+ def setslice():
146
+ arr[1:5] = 2
147
+
148
+ with pytest.raises(TypeError, match="assignment via setitem"):
149
+ setitem()
150
+
151
+ with pytest.raises(TypeError, match="assignment via setitem"):
152
+ setslice()
153
+
154
+
155
+ class TestTake:
156
+ def test_take_scalar_raises(self, arr):
157
+ msg = "'indices' must be an array, not a scalar '2'."
158
+ with pytest.raises(ValueError, match=msg):
159
+ arr.take(2)
160
+
161
+ def test_take(self, arr_data, arr):
162
+ exp = SparseArray(np.take(arr_data, [2, 3]))
163
+ tm.assert_sp_array_equal(arr.take([2, 3]), exp)
164
+
165
+ exp = SparseArray(np.take(arr_data, [0, 1, 2]))
166
+ tm.assert_sp_array_equal(arr.take([0, 1, 2]), exp)
167
+
168
+ def test_take_all_empty(self):
169
+ sparse = pd.array([0, 0], dtype=SparseDtype("int64"))
170
+ result = sparse.take([0, 1], allow_fill=True, fill_value=np.nan)
171
+ tm.assert_sp_array_equal(sparse, result)
172
+
173
+ def test_take_different_fill_value(self):
174
+ # Take with a different fill value shouldn't overwrite the original
175
+ sparse = pd.array([0.0], dtype=SparseDtype("float64", fill_value=0.0))
176
+ result = sparse.take([0, -1], allow_fill=True, fill_value=np.nan)
177
+ expected = pd.array([0, np.nan], dtype=sparse.dtype)
178
+ tm.assert_sp_array_equal(expected, result)
179
+
180
+ def test_take_fill_value(self):
181
+ data = np.array([1, np.nan, 0, 3, 0])
182
+ sparse = SparseArray(data, fill_value=0)
183
+
184
+ exp = SparseArray(np.take(data, [0]), fill_value=0)
185
+ tm.assert_sp_array_equal(sparse.take([0]), exp)
186
+
187
+ exp = SparseArray(np.take(data, [1, 3, 4]), fill_value=0)
188
+ tm.assert_sp_array_equal(sparse.take([1, 3, 4]), exp)
189
+
190
+ def test_take_negative(self, arr_data, arr):
191
+ exp = SparseArray(np.take(arr_data, [-1]))
192
+ tm.assert_sp_array_equal(arr.take([-1]), exp)
193
+
194
+ exp = SparseArray(np.take(arr_data, [-4, -3, -2]))
195
+ tm.assert_sp_array_equal(arr.take([-4, -3, -2]), exp)
196
+
197
+ def test_bad_take(self, arr):
198
+ with pytest.raises(IndexError, match="bounds"):
199
+ arr.take([11])
200
+
201
+ def test_take_filling(self):
202
+ # similar tests as GH 12631
203
+ sparse = SparseArray([np.nan, np.nan, 1, np.nan, 4])
204
+ result = sparse.take(np.array([1, 0, -1]))
205
+ expected = SparseArray([np.nan, np.nan, 4])
206
+ tm.assert_sp_array_equal(result, expected)
207
+
208
+ # TODO: actionable?
209
+ # XXX: test change: fill_value=True -> allow_fill=True
210
+ result = sparse.take(np.array([1, 0, -1]), allow_fill=True)
211
+ expected = SparseArray([np.nan, np.nan, np.nan])
212
+ tm.assert_sp_array_equal(result, expected)
213
+
214
+ # allow_fill=False
215
+ result = sparse.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
216
+ expected = SparseArray([np.nan, np.nan, 4])
217
+ tm.assert_sp_array_equal(result, expected)
218
+
219
+ msg = "Invalid value in 'indices'"
220
+ with pytest.raises(ValueError, match=msg):
221
+ sparse.take(np.array([1, 0, -2]), allow_fill=True)
222
+
223
+ with pytest.raises(ValueError, match=msg):
224
+ sparse.take(np.array([1, 0, -5]), allow_fill=True)
225
+
226
+ msg = "out of bounds value in 'indices'"
227
+ with pytest.raises(IndexError, match=msg):
228
+ sparse.take(np.array([1, -6]))
229
+ with pytest.raises(IndexError, match=msg):
230
+ sparse.take(np.array([1, 5]))
231
+ with pytest.raises(IndexError, match=msg):
232
+ sparse.take(np.array([1, 5]), allow_fill=True)
233
+
234
+ def test_take_filling_fill_value(self):
235
+ # same tests as GH#12631
236
+ sparse = SparseArray([np.nan, 0, 1, 0, 4], fill_value=0)
237
+ result = sparse.take(np.array([1, 0, -1]))
238
+ expected = SparseArray([0, np.nan, 4], fill_value=0)
239
+ tm.assert_sp_array_equal(result, expected)
240
+
241
+ # fill_value
242
+ result = sparse.take(np.array([1, 0, -1]), allow_fill=True)
243
+ # TODO: actionable?
244
+ # XXX: behavior change.
245
+ # the old way of filling self.fill_value doesn't follow EA rules.
246
+ # It's supposed to be self.dtype.na_value (nan in this case)
247
+ expected = SparseArray([0, np.nan, np.nan], fill_value=0)
248
+ tm.assert_sp_array_equal(result, expected)
249
+
250
+ # allow_fill=False
251
+ result = sparse.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
252
+ expected = SparseArray([0, np.nan, 4], fill_value=0)
253
+ tm.assert_sp_array_equal(result, expected)
254
+
255
+ msg = "Invalid value in 'indices'."
256
+ with pytest.raises(ValueError, match=msg):
257
+ sparse.take(np.array([1, 0, -2]), allow_fill=True)
258
+ with pytest.raises(ValueError, match=msg):
259
+ sparse.take(np.array([1, 0, -5]), allow_fill=True)
260
+
261
+ msg = "out of bounds value in 'indices'"
262
+ with pytest.raises(IndexError, match=msg):
263
+ sparse.take(np.array([1, -6]))
264
+ with pytest.raises(IndexError, match=msg):
265
+ sparse.take(np.array([1, 5]))
266
+ with pytest.raises(IndexError, match=msg):
267
+ sparse.take(np.array([1, 5]), fill_value=True)
268
+
269
+ @pytest.mark.parametrize("kind", ["block", "integer"])
270
+ def test_take_filling_all_nan(self, kind):
271
+ sparse = SparseArray([np.nan, np.nan, np.nan, np.nan, np.nan], kind=kind)
272
+ result = sparse.take(np.array([1, 0, -1]))
273
+ expected = SparseArray([np.nan, np.nan, np.nan], kind=kind)
274
+ tm.assert_sp_array_equal(result, expected)
275
+
276
+ result = sparse.take(np.array([1, 0, -1]), fill_value=True)
277
+ expected = SparseArray([np.nan, np.nan, np.nan], kind=kind)
278
+ tm.assert_sp_array_equal(result, expected)
279
+
280
+ msg = "out of bounds value in 'indices'"
281
+ with pytest.raises(IndexError, match=msg):
282
+ sparse.take(np.array([1, -6]))
283
+ with pytest.raises(IndexError, match=msg):
284
+ sparse.take(np.array([1, 5]))
285
+ with pytest.raises(IndexError, match=msg):
286
+ sparse.take(np.array([1, 5]), fill_value=True)
287
+
288
+
289
+ class TestWhere:
290
+ def test_where_retain_fill_value(self):
291
+ # GH#45691 don't lose fill_value on _where
292
+ arr = SparseArray([np.nan, 1.0], fill_value=0)
293
+
294
+ mask = np.array([True, False])
295
+
296
+ res = arr._where(~mask, 1)
297
+ exp = SparseArray([1, 1.0], fill_value=0)
298
+ tm.assert_sp_array_equal(res, exp)
299
+
300
+ ser = pd.Series(arr)
301
+ res = ser.where(~mask, 1)
302
+ tm.assert_series_equal(res, pd.Series(exp))
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_libsparse.py ADDED
@@ -0,0 +1,551 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import operator
2
+
3
+ import numpy as np
4
+ import pytest
5
+
6
+ import pandas._libs.sparse as splib
7
+ import pandas.util._test_decorators as td
8
+
9
+ from pandas import Series
10
+ import pandas._testing as tm
11
+ from pandas.core.arrays.sparse import (
12
+ BlockIndex,
13
+ IntIndex,
14
+ make_sparse_index,
15
+ )
16
+
17
+
18
+ @pytest.fixture
19
+ def test_length():
20
+ return 20
21
+
22
+
23
+ @pytest.fixture(
24
+ params=[
25
+ [
26
+ [0, 7, 15],
27
+ [3, 5, 5],
28
+ [2, 9, 14],
29
+ [2, 3, 5],
30
+ [2, 9, 15],
31
+ [1, 3, 4],
32
+ ],
33
+ [
34
+ [0, 5],
35
+ [4, 4],
36
+ [1],
37
+ [4],
38
+ [1],
39
+ [3],
40
+ ],
41
+ [
42
+ [0],
43
+ [10],
44
+ [0, 5],
45
+ [3, 7],
46
+ [0, 5],
47
+ [3, 5],
48
+ ],
49
+ [
50
+ [10],
51
+ [5],
52
+ [0, 12],
53
+ [5, 3],
54
+ [12],
55
+ [3],
56
+ ],
57
+ [
58
+ [0, 10],
59
+ [4, 6],
60
+ [5, 17],
61
+ [4, 2],
62
+ [],
63
+ [],
64
+ ],
65
+ [
66
+ [0],
67
+ [5],
68
+ [],
69
+ [],
70
+ [],
71
+ [],
72
+ ],
73
+ ],
74
+ ids=[
75
+ "plain_case",
76
+ "delete_blocks",
77
+ "split_blocks",
78
+ "skip_block",
79
+ "no_intersect",
80
+ "one_empty",
81
+ ],
82
+ )
83
+ def cases(request):
84
+ return request.param
85
+
86
+
87
+ class TestSparseIndexUnion:
88
+ @pytest.mark.parametrize(
89
+ "xloc, xlen, yloc, ylen, eloc, elen",
90
+ [
91
+ [[0], [5], [5], [4], [0], [9]],
92
+ [[0, 10], [5, 5], [2, 17], [5, 2], [0, 10, 17], [7, 5, 2]],
93
+ [[1], [5], [3], [5], [1], [7]],
94
+ [[2, 10], [4, 4], [4], [8], [2], [12]],
95
+ [[0, 5], [3, 5], [0], [7], [0], [10]],
96
+ [[2, 10], [4, 4], [4, 13], [8, 4], [2], [15]],
97
+ [[2], [15], [4, 9, 14], [3, 2, 2], [2], [15]],
98
+ [[0, 10], [3, 3], [5, 15], [2, 2], [0, 5, 10, 15], [3, 2, 3, 2]],
99
+ ],
100
+ )
101
+ def test_index_make_union(self, xloc, xlen, yloc, ylen, eloc, elen, test_length):
102
+ # Case 1
103
+ # x: ----
104
+ # y: ----
105
+ # r: --------
106
+ # Case 2
107
+ # x: ----- -----
108
+ # y: ----- --
109
+ # Case 3
110
+ # x: ------
111
+ # y: -------
112
+ # r: ----------
113
+ # Case 4
114
+ # x: ------ -----
115
+ # y: -------
116
+ # r: -------------
117
+ # Case 5
118
+ # x: --- -----
119
+ # y: -------
120
+ # r: -------------
121
+ # Case 6
122
+ # x: ------ -----
123
+ # y: ------- ---
124
+ # r: -------------
125
+ # Case 7
126
+ # x: ----------------------
127
+ # y: ---- ---- ---
128
+ # r: ----------------------
129
+ # Case 8
130
+ # x: ---- ---
131
+ # y: --- ---
132
+ xindex = BlockIndex(test_length, xloc, xlen)
133
+ yindex = BlockIndex(test_length, yloc, ylen)
134
+ bresult = xindex.make_union(yindex)
135
+ assert isinstance(bresult, BlockIndex)
136
+ tm.assert_numpy_array_equal(bresult.blocs, np.array(eloc, dtype=np.int32))
137
+ tm.assert_numpy_array_equal(bresult.blengths, np.array(elen, dtype=np.int32))
138
+
139
+ ixindex = xindex.to_int_index()
140
+ iyindex = yindex.to_int_index()
141
+ iresult = ixindex.make_union(iyindex)
142
+ assert isinstance(iresult, IntIndex)
143
+ tm.assert_numpy_array_equal(iresult.indices, bresult.to_int_index().indices)
144
+
145
+ def test_int_index_make_union(self):
146
+ a = IntIndex(5, np.array([0, 3, 4], dtype=np.int32))
147
+ b = IntIndex(5, np.array([0, 2], dtype=np.int32))
148
+ res = a.make_union(b)
149
+ exp = IntIndex(5, np.array([0, 2, 3, 4], np.int32))
150
+ assert res.equals(exp)
151
+
152
+ a = IntIndex(5, np.array([], dtype=np.int32))
153
+ b = IntIndex(5, np.array([0, 2], dtype=np.int32))
154
+ res = a.make_union(b)
155
+ exp = IntIndex(5, np.array([0, 2], np.int32))
156
+ assert res.equals(exp)
157
+
158
+ a = IntIndex(5, np.array([], dtype=np.int32))
159
+ b = IntIndex(5, np.array([], dtype=np.int32))
160
+ res = a.make_union(b)
161
+ exp = IntIndex(5, np.array([], np.int32))
162
+ assert res.equals(exp)
163
+
164
+ a = IntIndex(5, np.array([0, 1, 2, 3, 4], dtype=np.int32))
165
+ b = IntIndex(5, np.array([0, 1, 2, 3, 4], dtype=np.int32))
166
+ res = a.make_union(b)
167
+ exp = IntIndex(5, np.array([0, 1, 2, 3, 4], np.int32))
168
+ assert res.equals(exp)
169
+
170
+ a = IntIndex(5, np.array([0, 1], dtype=np.int32))
171
+ b = IntIndex(4, np.array([0, 1], dtype=np.int32))
172
+
173
+ msg = "Indices must reference same underlying length"
174
+ with pytest.raises(ValueError, match=msg):
175
+ a.make_union(b)
176
+
177
+
178
+ class TestSparseIndexIntersect:
179
+ @td.skip_if_windows
180
+ def test_intersect(self, cases, test_length):
181
+ xloc, xlen, yloc, ylen, eloc, elen = cases
182
+ xindex = BlockIndex(test_length, xloc, xlen)
183
+ yindex = BlockIndex(test_length, yloc, ylen)
184
+ expected = BlockIndex(test_length, eloc, elen)
185
+ longer_index = BlockIndex(test_length + 1, yloc, ylen)
186
+
187
+ result = xindex.intersect(yindex)
188
+ assert result.equals(expected)
189
+ result = xindex.to_int_index().intersect(yindex.to_int_index())
190
+ assert result.equals(expected.to_int_index())
191
+
192
+ msg = "Indices must reference same underlying length"
193
+ with pytest.raises(Exception, match=msg):
194
+ xindex.intersect(longer_index)
195
+ with pytest.raises(Exception, match=msg):
196
+ xindex.to_int_index().intersect(longer_index.to_int_index())
197
+
198
+ def test_intersect_empty(self):
199
+ xindex = IntIndex(4, np.array([], dtype=np.int32))
200
+ yindex = IntIndex(4, np.array([2, 3], dtype=np.int32))
201
+ assert xindex.intersect(yindex).equals(xindex)
202
+ assert yindex.intersect(xindex).equals(xindex)
203
+
204
+ xindex = xindex.to_block_index()
205
+ yindex = yindex.to_block_index()
206
+ assert xindex.intersect(yindex).equals(xindex)
207
+ assert yindex.intersect(xindex).equals(xindex)
208
+
209
+ @pytest.mark.parametrize(
210
+ "case",
211
+ [
212
+ # Argument 2 to "IntIndex" has incompatible type "ndarray[Any,
213
+ # dtype[signedinteger[_32Bit]]]"; expected "Sequence[int]"
214
+ IntIndex(5, np.array([1, 2], dtype=np.int32)), # type: ignore[arg-type]
215
+ IntIndex(5, np.array([0, 2, 4], dtype=np.int32)), # type: ignore[arg-type]
216
+ IntIndex(0, np.array([], dtype=np.int32)), # type: ignore[arg-type]
217
+ IntIndex(5, np.array([], dtype=np.int32)), # type: ignore[arg-type]
218
+ ],
219
+ )
220
+ def test_intersect_identical(self, case):
221
+ assert case.intersect(case).equals(case)
222
+ case = case.to_block_index()
223
+ assert case.intersect(case).equals(case)
224
+
225
+
226
+ class TestSparseIndexCommon:
227
+ def test_int_internal(self):
228
+ idx = make_sparse_index(4, np.array([2, 3], dtype=np.int32), kind="integer")
229
+ assert isinstance(idx, IntIndex)
230
+ assert idx.npoints == 2
231
+ tm.assert_numpy_array_equal(idx.indices, np.array([2, 3], dtype=np.int32))
232
+
233
+ idx = make_sparse_index(4, np.array([], dtype=np.int32), kind="integer")
234
+ assert isinstance(idx, IntIndex)
235
+ assert idx.npoints == 0
236
+ tm.assert_numpy_array_equal(idx.indices, np.array([], dtype=np.int32))
237
+
238
+ idx = make_sparse_index(
239
+ 4, np.array([0, 1, 2, 3], dtype=np.int32), kind="integer"
240
+ )
241
+ assert isinstance(idx, IntIndex)
242
+ assert idx.npoints == 4
243
+ tm.assert_numpy_array_equal(idx.indices, np.array([0, 1, 2, 3], dtype=np.int32))
244
+
245
+ def test_block_internal(self):
246
+ idx = make_sparse_index(4, np.array([2, 3], dtype=np.int32), kind="block")
247
+ assert isinstance(idx, BlockIndex)
248
+ assert idx.npoints == 2
249
+ tm.assert_numpy_array_equal(idx.blocs, np.array([2], dtype=np.int32))
250
+ tm.assert_numpy_array_equal(idx.blengths, np.array([2], dtype=np.int32))
251
+
252
+ idx = make_sparse_index(4, np.array([], dtype=np.int32), kind="block")
253
+ assert isinstance(idx, BlockIndex)
254
+ assert idx.npoints == 0
255
+ tm.assert_numpy_array_equal(idx.blocs, np.array([], dtype=np.int32))
256
+ tm.assert_numpy_array_equal(idx.blengths, np.array([], dtype=np.int32))
257
+
258
+ idx = make_sparse_index(4, np.array([0, 1, 2, 3], dtype=np.int32), kind="block")
259
+ assert isinstance(idx, BlockIndex)
260
+ assert idx.npoints == 4
261
+ tm.assert_numpy_array_equal(idx.blocs, np.array([0], dtype=np.int32))
262
+ tm.assert_numpy_array_equal(idx.blengths, np.array([4], dtype=np.int32))
263
+
264
+ idx = make_sparse_index(4, np.array([0, 2, 3], dtype=np.int32), kind="block")
265
+ assert isinstance(idx, BlockIndex)
266
+ assert idx.npoints == 3
267
+ tm.assert_numpy_array_equal(idx.blocs, np.array([0, 2], dtype=np.int32))
268
+ tm.assert_numpy_array_equal(idx.blengths, np.array([1, 2], dtype=np.int32))
269
+
270
+ @pytest.mark.parametrize("kind", ["integer", "block"])
271
+ def test_lookup(self, kind):
272
+ idx = make_sparse_index(4, np.array([2, 3], dtype=np.int32), kind=kind)
273
+ assert idx.lookup(-1) == -1
274
+ assert idx.lookup(0) == -1
275
+ assert idx.lookup(1) == -1
276
+ assert idx.lookup(2) == 0
277
+ assert idx.lookup(3) == 1
278
+ assert idx.lookup(4) == -1
279
+
280
+ idx = make_sparse_index(4, np.array([], dtype=np.int32), kind=kind)
281
+
282
+ for i in range(-1, 5):
283
+ assert idx.lookup(i) == -1
284
+
285
+ idx = make_sparse_index(4, np.array([0, 1, 2, 3], dtype=np.int32), kind=kind)
286
+ assert idx.lookup(-1) == -1
287
+ assert idx.lookup(0) == 0
288
+ assert idx.lookup(1) == 1
289
+ assert idx.lookup(2) == 2
290
+ assert idx.lookup(3) == 3
291
+ assert idx.lookup(4) == -1
292
+
293
+ idx = make_sparse_index(4, np.array([0, 2, 3], dtype=np.int32), kind=kind)
294
+ assert idx.lookup(-1) == -1
295
+ assert idx.lookup(0) == 0
296
+ assert idx.lookup(1) == -1
297
+ assert idx.lookup(2) == 1
298
+ assert idx.lookup(3) == 2
299
+ assert idx.lookup(4) == -1
300
+
301
+ @pytest.mark.parametrize("kind", ["integer", "block"])
302
+ def test_lookup_array(self, kind):
303
+ idx = make_sparse_index(4, np.array([2, 3], dtype=np.int32), kind=kind)
304
+
305
+ res = idx.lookup_array(np.array([-1, 0, 2], dtype=np.int32))
306
+ exp = np.array([-1, -1, 0], dtype=np.int32)
307
+ tm.assert_numpy_array_equal(res, exp)
308
+
309
+ res = idx.lookup_array(np.array([4, 2, 1, 3], dtype=np.int32))
310
+ exp = np.array([-1, 0, -1, 1], dtype=np.int32)
311
+ tm.assert_numpy_array_equal(res, exp)
312
+
313
+ idx = make_sparse_index(4, np.array([], dtype=np.int32), kind=kind)
314
+ res = idx.lookup_array(np.array([-1, 0, 2, 4], dtype=np.int32))
315
+ exp = np.array([-1, -1, -1, -1], dtype=np.int32)
316
+ tm.assert_numpy_array_equal(res, exp)
317
+
318
+ idx = make_sparse_index(4, np.array([0, 1, 2, 3], dtype=np.int32), kind=kind)
319
+ res = idx.lookup_array(np.array([-1, 0, 2], dtype=np.int32))
320
+ exp = np.array([-1, 0, 2], dtype=np.int32)
321
+ tm.assert_numpy_array_equal(res, exp)
322
+
323
+ res = idx.lookup_array(np.array([4, 2, 1, 3], dtype=np.int32))
324
+ exp = np.array([-1, 2, 1, 3], dtype=np.int32)
325
+ tm.assert_numpy_array_equal(res, exp)
326
+
327
+ idx = make_sparse_index(4, np.array([0, 2, 3], dtype=np.int32), kind=kind)
328
+ res = idx.lookup_array(np.array([2, 1, 3, 0], dtype=np.int32))
329
+ exp = np.array([1, -1, 2, 0], dtype=np.int32)
330
+ tm.assert_numpy_array_equal(res, exp)
331
+
332
+ res = idx.lookup_array(np.array([1, 4, 2, 5], dtype=np.int32))
333
+ exp = np.array([-1, -1, 1, -1], dtype=np.int32)
334
+ tm.assert_numpy_array_equal(res, exp)
335
+
336
+ @pytest.mark.parametrize(
337
+ "idx, expected",
338
+ [
339
+ [0, -1],
340
+ [5, 0],
341
+ [7, 2],
342
+ [8, -1],
343
+ [9, -1],
344
+ [10, -1],
345
+ [11, -1],
346
+ [12, 3],
347
+ [17, 8],
348
+ [18, -1],
349
+ ],
350
+ )
351
+ def test_lookup_basics(self, idx, expected):
352
+ bindex = BlockIndex(20, [5, 12], [3, 6])
353
+ assert bindex.lookup(idx) == expected
354
+
355
+ iindex = bindex.to_int_index()
356
+ assert iindex.lookup(idx) == expected
357
+
358
+
359
+ class TestBlockIndex:
360
+ def test_block_internal(self):
361
+ idx = make_sparse_index(4, np.array([2, 3], dtype=np.int32), kind="block")
362
+ assert isinstance(idx, BlockIndex)
363
+ assert idx.npoints == 2
364
+ tm.assert_numpy_array_equal(idx.blocs, np.array([2], dtype=np.int32))
365
+ tm.assert_numpy_array_equal(idx.blengths, np.array([2], dtype=np.int32))
366
+
367
+ idx = make_sparse_index(4, np.array([], dtype=np.int32), kind="block")
368
+ assert isinstance(idx, BlockIndex)
369
+ assert idx.npoints == 0
370
+ tm.assert_numpy_array_equal(idx.blocs, np.array([], dtype=np.int32))
371
+ tm.assert_numpy_array_equal(idx.blengths, np.array([], dtype=np.int32))
372
+
373
+ idx = make_sparse_index(4, np.array([0, 1, 2, 3], dtype=np.int32), kind="block")
374
+ assert isinstance(idx, BlockIndex)
375
+ assert idx.npoints == 4
376
+ tm.assert_numpy_array_equal(idx.blocs, np.array([0], dtype=np.int32))
377
+ tm.assert_numpy_array_equal(idx.blengths, np.array([4], dtype=np.int32))
378
+
379
+ idx = make_sparse_index(4, np.array([0, 2, 3], dtype=np.int32), kind="block")
380
+ assert isinstance(idx, BlockIndex)
381
+ assert idx.npoints == 3
382
+ tm.assert_numpy_array_equal(idx.blocs, np.array([0, 2], dtype=np.int32))
383
+ tm.assert_numpy_array_equal(idx.blengths, np.array([1, 2], dtype=np.int32))
384
+
385
+ @pytest.mark.parametrize("i", [5, 10, 100, 101])
386
+ def test_make_block_boundary(self, i):
387
+ idx = make_sparse_index(i, np.arange(0, i, 2, dtype=np.int32), kind="block")
388
+
389
+ exp = np.arange(0, i, 2, dtype=np.int32)
390
+ tm.assert_numpy_array_equal(idx.blocs, exp)
391
+ tm.assert_numpy_array_equal(idx.blengths, np.ones(len(exp), dtype=np.int32))
392
+
393
+ def test_equals(self):
394
+ index = BlockIndex(10, [0, 4], [2, 5])
395
+
396
+ assert index.equals(index)
397
+ assert not index.equals(BlockIndex(10, [0, 4], [2, 6]))
398
+
399
+ def test_check_integrity(self):
400
+ locs = []
401
+ lengths = []
402
+
403
+ # 0-length OK
404
+ BlockIndex(0, locs, lengths)
405
+
406
+ # also OK even though empty
407
+ BlockIndex(1, locs, lengths)
408
+
409
+ msg = "Block 0 extends beyond end"
410
+ with pytest.raises(ValueError, match=msg):
411
+ BlockIndex(10, [5], [10])
412
+
413
+ msg = "Block 0 overlaps"
414
+ with pytest.raises(ValueError, match=msg):
415
+ BlockIndex(10, [2, 5], [5, 3])
416
+
417
+ def test_to_int_index(self):
418
+ locs = [0, 10]
419
+ lengths = [4, 6]
420
+ exp_inds = [0, 1, 2, 3, 10, 11, 12, 13, 14, 15]
421
+
422
+ block = BlockIndex(20, locs, lengths)
423
+ dense = block.to_int_index()
424
+
425
+ tm.assert_numpy_array_equal(dense.indices, np.array(exp_inds, dtype=np.int32))
426
+
427
+ def test_to_block_index(self):
428
+ index = BlockIndex(10, [0, 5], [4, 5])
429
+ assert index.to_block_index() is index
430
+
431
+
432
+ class TestIntIndex:
433
+ def test_check_integrity(self):
434
+ # Too many indices than specified in self.length
435
+ msg = "Too many indices"
436
+
437
+ with pytest.raises(ValueError, match=msg):
438
+ IntIndex(length=1, indices=[1, 2, 3])
439
+
440
+ # No index can be negative.
441
+ msg = "No index can be less than zero"
442
+
443
+ with pytest.raises(ValueError, match=msg):
444
+ IntIndex(length=5, indices=[1, -2, 3])
445
+
446
+ # No index can be negative.
447
+ msg = "No index can be less than zero"
448
+
449
+ with pytest.raises(ValueError, match=msg):
450
+ IntIndex(length=5, indices=[1, -2, 3])
451
+
452
+ # All indices must be less than the length.
453
+ msg = "All indices must be less than the length"
454
+
455
+ with pytest.raises(ValueError, match=msg):
456
+ IntIndex(length=5, indices=[1, 2, 5])
457
+
458
+ with pytest.raises(ValueError, match=msg):
459
+ IntIndex(length=5, indices=[1, 2, 6])
460
+
461
+ # Indices must be strictly ascending.
462
+ msg = "Indices must be strictly increasing"
463
+
464
+ with pytest.raises(ValueError, match=msg):
465
+ IntIndex(length=5, indices=[1, 3, 2])
466
+
467
+ with pytest.raises(ValueError, match=msg):
468
+ IntIndex(length=5, indices=[1, 3, 3])
469
+
470
+ def test_int_internal(self):
471
+ idx = make_sparse_index(4, np.array([2, 3], dtype=np.int32), kind="integer")
472
+ assert isinstance(idx, IntIndex)
473
+ assert idx.npoints == 2
474
+ tm.assert_numpy_array_equal(idx.indices, np.array([2, 3], dtype=np.int32))
475
+
476
+ idx = make_sparse_index(4, np.array([], dtype=np.int32), kind="integer")
477
+ assert isinstance(idx, IntIndex)
478
+ assert idx.npoints == 0
479
+ tm.assert_numpy_array_equal(idx.indices, np.array([], dtype=np.int32))
480
+
481
+ idx = make_sparse_index(
482
+ 4, np.array([0, 1, 2, 3], dtype=np.int32), kind="integer"
483
+ )
484
+ assert isinstance(idx, IntIndex)
485
+ assert idx.npoints == 4
486
+ tm.assert_numpy_array_equal(idx.indices, np.array([0, 1, 2, 3], dtype=np.int32))
487
+
488
+ def test_equals(self):
489
+ index = IntIndex(10, [0, 1, 2, 3, 4])
490
+ assert index.equals(index)
491
+ assert not index.equals(IntIndex(10, [0, 1, 2, 3]))
492
+
493
+ def test_to_block_index(self, cases, test_length):
494
+ xloc, xlen, yloc, ylen, _, _ = cases
495
+ xindex = BlockIndex(test_length, xloc, xlen)
496
+ yindex = BlockIndex(test_length, yloc, ylen)
497
+
498
+ # see if survive the round trip
499
+ xbindex = xindex.to_int_index().to_block_index()
500
+ ybindex = yindex.to_int_index().to_block_index()
501
+ assert isinstance(xbindex, BlockIndex)
502
+ assert xbindex.equals(xindex)
503
+ assert ybindex.equals(yindex)
504
+
505
+ def test_to_int_index(self):
506
+ index = IntIndex(10, [2, 3, 4, 5, 6])
507
+ assert index.to_int_index() is index
508
+
509
+
510
+ class TestSparseOperators:
511
+ @pytest.mark.parametrize("opname", ["add", "sub", "mul", "truediv", "floordiv"])
512
+ def test_op(self, opname, cases, test_length):
513
+ xloc, xlen, yloc, ylen, _, _ = cases
514
+ sparse_op = getattr(splib, f"sparse_{opname}_float64")
515
+ python_op = getattr(operator, opname)
516
+
517
+ xindex = BlockIndex(test_length, xloc, xlen)
518
+ yindex = BlockIndex(test_length, yloc, ylen)
519
+
520
+ xdindex = xindex.to_int_index()
521
+ ydindex = yindex.to_int_index()
522
+
523
+ x = np.arange(xindex.npoints) * 10.0 + 1
524
+ y = np.arange(yindex.npoints) * 100.0 + 1
525
+
526
+ xfill = 0
527
+ yfill = 2
528
+
529
+ result_block_vals, rb_index, bfill = sparse_op(
530
+ x, xindex, xfill, y, yindex, yfill
531
+ )
532
+ result_int_vals, ri_index, ifill = sparse_op(
533
+ x, xdindex, xfill, y, ydindex, yfill
534
+ )
535
+
536
+ assert rb_index.to_int_index().equals(ri_index)
537
+ tm.assert_numpy_array_equal(result_block_vals, result_int_vals)
538
+ assert bfill == ifill
539
+
540
+ # check versus Series...
541
+ xseries = Series(x, xdindex.indices)
542
+ xseries = xseries.reindex(np.arange(test_length)).fillna(xfill)
543
+
544
+ yseries = Series(y, ydindex.indices)
545
+ yseries = yseries.reindex(np.arange(test_length)).fillna(yfill)
546
+
547
+ series_result = python_op(xseries, yseries)
548
+ series_result = series_result.reindex(ri_index.indices)
549
+
550
+ tm.assert_numpy_array_equal(result_block_vals, series_result.values)
551
+ tm.assert_numpy_array_equal(result_int_vals, series_result.values)
.venv_haddock/lib/python3.12/site-packages/pandas/tests/arrays/sparse/test_reductions.py ADDED
@@ -0,0 +1,306 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pytest
3
+
4
+ from pandas import (
5
+ NaT,
6
+ SparseDtype,
7
+ Timestamp,
8
+ isna,
9
+ )
10
+ from pandas.core.arrays.sparse import SparseArray
11
+
12
+
13
+ class TestReductions:
14
+ @pytest.mark.parametrize(
15
+ "data,pos,neg",
16
+ [
17
+ ([True, True, True], True, False),
18
+ ([1, 2, 1], 1, 0),
19
+ ([1.0, 2.0, 1.0], 1.0, 0.0),
20
+ ],
21
+ )
22
+ def test_all(self, data, pos, neg):
23
+ # GH#17570
24
+ out = SparseArray(data).all()
25
+ assert out
26
+
27
+ out = SparseArray(data, fill_value=pos).all()
28
+ assert out
29
+
30
+ data[1] = neg
31
+ out = SparseArray(data).all()
32
+ assert not out
33
+
34
+ out = SparseArray(data, fill_value=pos).all()
35
+ assert not out
36
+
37
+ @pytest.mark.parametrize(
38
+ "data,pos,neg",
39
+ [
40
+ ([True, True, True], True, False),
41
+ ([1, 2, 1], 1, 0),
42
+ ([1.0, 2.0, 1.0], 1.0, 0.0),
43
+ ],
44
+ )
45
+ def test_numpy_all(self, data, pos, neg):
46
+ # GH#17570
47
+ out = np.all(SparseArray(data))
48
+ assert out
49
+
50
+ out = np.all(SparseArray(data, fill_value=pos))
51
+ assert out
52
+
53
+ data[1] = neg
54
+ out = np.all(SparseArray(data))
55
+ assert not out
56
+
57
+ out = np.all(SparseArray(data, fill_value=pos))
58
+ assert not out
59
+
60
+ # raises with a different message on py2.
61
+ msg = "the 'out' parameter is not supported"
62
+ with pytest.raises(ValueError, match=msg):
63
+ np.all(SparseArray(data), out=np.array([]))
64
+
65
+ @pytest.mark.parametrize(
66
+ "data,pos,neg",
67
+ [
68
+ ([False, True, False], True, False),
69
+ ([0, 2, 0], 2, 0),
70
+ ([0.0, 2.0, 0.0], 2.0, 0.0),
71
+ ],
72
+ )
73
+ def test_any(self, data, pos, neg):
74
+ # GH#17570
75
+ out = SparseArray(data).any()
76
+ assert out
77
+
78
+ out = SparseArray(data, fill_value=pos).any()
79
+ assert out
80
+
81
+ data[1] = neg
82
+ out = SparseArray(data).any()
83
+ assert not out
84
+
85
+ out = SparseArray(data, fill_value=pos).any()
86
+ assert not out
87
+
88
+ @pytest.mark.parametrize(
89
+ "data,pos,neg",
90
+ [
91
+ ([False, True, False], True, False),
92
+ ([0, 2, 0], 2, 0),
93
+ ([0.0, 2.0, 0.0], 2.0, 0.0),
94
+ ],
95
+ )
96
+ def test_numpy_any(self, data, pos, neg):
97
+ # GH#17570
98
+ out = np.any(SparseArray(data))
99
+ assert out
100
+
101
+ out = np.any(SparseArray(data, fill_value=pos))
102
+ assert out
103
+
104
+ data[1] = neg
105
+ out = np.any(SparseArray(data))
106
+ assert not out
107
+
108
+ out = np.any(SparseArray(data, fill_value=pos))
109
+ assert not out
110
+
111
+ msg = "the 'out' parameter is not supported"
112
+ with pytest.raises(ValueError, match=msg):
113
+ np.any(SparseArray(data), out=out)
114
+
115
+ def test_sum(self):
116
+ data = np.arange(10).astype(float)
117
+ out = SparseArray(data).sum()
118
+ assert out == 45.0
119
+
120
+ data[5] = np.nan
121
+ out = SparseArray(data, fill_value=2).sum()
122
+ assert out == 40.0
123
+
124
+ out = SparseArray(data, fill_value=np.nan).sum()
125
+ assert out == 40.0
126
+
127
+ @pytest.mark.parametrize(
128
+ "arr",
129
+ [np.array([0, 1, np.nan, 1]), np.array([0, 1, 1])],
130
+ )
131
+ @pytest.mark.parametrize("fill_value", [0, 1, np.nan])
132
+ @pytest.mark.parametrize("min_count, expected", [(3, 2), (4, np.nan)])
133
+ def test_sum_min_count(self, arr, fill_value, min_count, expected):
134
+ # GH#25777
135
+ sparray = SparseArray(arr, fill_value=fill_value)
136
+ result = sparray.sum(min_count=min_count)
137
+ if np.isnan(expected):
138
+ assert np.isnan(result)
139
+ else:
140
+ assert result == expected
141
+
142
+ def test_bool_sum_min_count(self):
143
+ spar_bool = SparseArray([False, True] * 5, dtype=np.bool_, fill_value=True)
144
+ res = spar_bool.sum(min_count=1)
145
+ assert res == 5
146
+ res = spar_bool.sum(min_count=11)
147
+ assert isna(res)
148
+
149
+ def test_numpy_sum(self):
150
+ data = np.arange(10).astype(float)
151
+ out = np.sum(SparseArray(data))
152
+ assert out == 45.0
153
+
154
+ data[5] = np.nan
155
+ out = np.sum(SparseArray(data, fill_value=2))
156
+ assert out == 40.0
157
+
158
+ out = np.sum(SparseArray(data, fill_value=np.nan))
159
+ assert out == 40.0
160
+
161
+ msg = "the 'dtype' parameter is not supported"
162
+ with pytest.raises(ValueError, match=msg):
163
+ np.sum(SparseArray(data), dtype=np.int64)
164
+
165
+ msg = "the 'out' parameter is not supported"
166
+ with pytest.raises(ValueError, match=msg):
167
+ np.sum(SparseArray(data), out=out)
168
+
169
+ def test_mean(self):
170
+ data = np.arange(10).astype(float)
171
+ out = SparseArray(data).mean()
172
+ assert out == 4.5
173
+
174
+ data[5] = np.nan
175
+ out = SparseArray(data).mean()
176
+ assert out == 40.0 / 9
177
+
178
+ def test_numpy_mean(self):
179
+ data = np.arange(10).astype(float)
180
+ out = np.mean(SparseArray(data))
181
+ assert out == 4.5
182
+
183
+ data[5] = np.nan
184
+ out = np.mean(SparseArray(data))
185
+ assert out == 40.0 / 9
186
+
187
+ msg = "the 'dtype' parameter is not supported"
188
+ with pytest.raises(ValueError, match=msg):
189
+ np.mean(SparseArray(data), dtype=np.int64)
190
+
191
+ msg = "the 'out' parameter is not supported"
192
+ with pytest.raises(ValueError, match=msg):
193
+ np.mean(SparseArray(data), out=out)
194
+
195
+
196
+ class TestMinMax:
197
+ @pytest.mark.parametrize(
198
+ "raw_data,max_expected,min_expected",
199
+ [
200
+ (np.arange(5.0), [4], [0]),
201
+ (-np.arange(5.0), [0], [-4]),
202
+ (np.array([0, 1, 2, np.nan, 4]), [4], [0]),
203
+ (np.array([np.nan] * 5), [np.nan], [np.nan]),
204
+ (np.array([]), [np.nan], [np.nan]),
205
+ ],
206
+ )
207
+ def test_nan_fill_value(self, raw_data, max_expected, min_expected):
208
+ arr = SparseArray(raw_data)
209
+ max_result = arr.max()
210
+ min_result = arr.min()
211
+ assert max_result in max_expected
212
+ assert min_result in min_expected
213
+
214
+ max_result = arr.max(skipna=False)
215
+ min_result = arr.min(skipna=False)
216
+ if np.isnan(raw_data).any():
217
+ assert np.isnan(max_result)
218
+ assert np.isnan(min_result)
219
+ else:
220
+ assert max_result in max_expected
221
+ assert min_result in min_expected
222
+
223
+ @pytest.mark.parametrize(
224
+ "fill_value,max_expected,min_expected",
225
+ [
226
+ (100, 100, 0),
227
+ (-100, 1, -100),
228
+ ],
229
+ )
230
+ def test_fill_value(self, fill_value, max_expected, min_expected):
231
+ arr = SparseArray(
232
+ np.array([fill_value, 0, 1]), dtype=SparseDtype("int", fill_value)
233
+ )
234
+ max_result = arr.max()
235
+ assert max_result == max_expected
236
+
237
+ min_result = arr.min()
238
+ assert min_result == min_expected
239
+
240
+ def test_only_fill_value(self):
241
+ fv = 100
242
+ arr = SparseArray(np.array([fv, fv, fv]), dtype=SparseDtype("int", fv))
243
+ assert len(arr._valid_sp_values) == 0
244
+
245
+ assert arr.max() == fv
246
+ assert arr.min() == fv
247
+ assert arr.max(skipna=False) == fv
248
+ assert arr.min(skipna=False) == fv
249
+
250
+ @pytest.mark.parametrize("func", ["min", "max"])
251
+ @pytest.mark.parametrize("data", [np.array([]), np.array([np.nan, np.nan])])
252
+ @pytest.mark.parametrize(
253
+ "dtype,expected",
254
+ [
255
+ (SparseDtype(np.float64, np.nan), np.nan),
256
+ (SparseDtype(np.float64, 5.0), np.nan),
257
+ (SparseDtype("datetime64[ns]", NaT), NaT),
258
+ (SparseDtype("datetime64[ns]", Timestamp("2018-05-05")), NaT),
259
+ ],
260
+ )
261
+ def test_na_value_if_no_valid_values(self, func, data, dtype, expected):
262
+ arr = SparseArray(data, dtype=dtype)
263
+ result = getattr(arr, func)()
264
+ if expected is NaT:
265
+ # TODO: pin down whether we wrap datetime64("NaT")
266
+ assert result is NaT or np.isnat(result)
267
+ else:
268
+ assert np.isnan(result)
269
+
270
+
271
+ class TestArgmaxArgmin:
272
+ @pytest.mark.parametrize(
273
+ "arr,argmax_expected,argmin_expected",
274
+ [
275
+ (SparseArray([1, 2, 0, 1, 2]), 1, 2),
276
+ (SparseArray([-1, -2, 0, -1, -2]), 2, 1),
277
+ (SparseArray([np.nan, 1, 0, 0, np.nan, -1]), 1, 5),
278
+ (SparseArray([np.nan, 1, 0, 0, np.nan, 2]), 5, 2),
279
+ (SparseArray([np.nan, 1, 0, 0, np.nan, 2], fill_value=-1), 5, 2),
280
+ (SparseArray([np.nan, 1, 0, 0, np.nan, 2], fill_value=0), 5, 2),
281
+ (SparseArray([np.nan, 1, 0, 0, np.nan, 2], fill_value=1), 5, 2),
282
+ (SparseArray([np.nan, 1, 0, 0, np.nan, 2], fill_value=2), 5, 2),
283
+ (SparseArray([np.nan, 1, 0, 0, np.nan, 2], fill_value=3), 5, 2),
284
+ (SparseArray([0] * 10 + [-1], fill_value=0), 0, 10),
285
+ (SparseArray([0] * 10 + [-1], fill_value=-1), 0, 10),
286
+ (SparseArray([0] * 10 + [-1], fill_value=1), 0, 10),
287
+ (SparseArray([-1] + [0] * 10, fill_value=0), 1, 0),
288
+ (SparseArray([1] + [0] * 10, fill_value=0), 0, 1),
289
+ (SparseArray([-1] + [0] * 10, fill_value=-1), 1, 0),
290
+ (SparseArray([1] + [0] * 10, fill_value=1), 0, 1),
291
+ ],
292
+ )
293
+ def test_argmax_argmin(self, arr, argmax_expected, argmin_expected):
294
+ argmax_result = arr.argmax()
295
+ argmin_result = arr.argmin()
296
+ assert argmax_result == argmax_expected
297
+ assert argmin_result == argmin_expected
298
+
299
+ @pytest.mark.parametrize(
300
+ "arr,method",
301
+ [(SparseArray([]), "argmax"), (SparseArray([]), "argmin")],
302
+ )
303
+ def test_empty_array(self, arr, method):
304
+ msg = f"attempt to get {method} of an empty sequence"
305
+ with pytest.raises(ValueError, match=msg):
306
+ arr.argmax() if method == "argmax" else arr.argmin()
.venv_haddock/lib/python3.12/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)
.venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__init__.py ADDED
File without changes
.venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (209 Bytes). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/common.cpython-312.pyc ADDED
Binary file (686 Bytes). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/test_constructors.cpython-312.pyc ADDED
Binary file (9.09 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/test_conversion.cpython-312.pyc ADDED
Binary file (28.4 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/pandas/tests/base/__pycache__/test_fillna.cpython-312.pyc ADDED
Binary file (2.47 kB). View file