1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
| import string
|
| import numpy as np
| import pytest
|
| import pandas.util._test_decorators as td
|
| import pandas as pd
| import pandas._testing as tm
| from pandas.core.arrays.sparse import (
| SparseArray,
| SparseDtype,
| )
|
|
| class TestSeriesAccessor:
| def test_to_dense(self):
| ser = pd.Series([0, 1, 0, 10], dtype="Sparse[int64]")
| result = ser.sparse.to_dense()
| expected = pd.Series([0, 1, 0, 10])
| tm.assert_series_equal(result, expected)
|
| @pytest.mark.parametrize("attr", ["npoints", "density", "fill_value", "sp_values"])
| def test_get_attributes(self, attr):
| arr = SparseArray([0, 1])
| ser = pd.Series(arr)
|
| result = getattr(ser.sparse, attr)
| expected = getattr(arr, attr)
| assert result == expected
|
| @td.skip_if_no_scipy
| def test_from_coo(self):
| import scipy.sparse
|
| row = [0, 3, 1, 0]
| col = [0, 3, 1, 2]
| data = [4, 5, 7, 9]
| # TODO(scipy#13585): Remove dtype when scipy is fixed
| # https://github.com/scipy/scipy/issues/13585
| sp_array = scipy.sparse.coo_matrix((data, (row, col)), dtype="int")
| result = pd.Series.sparse.from_coo(sp_array)
|
| index = pd.MultiIndex.from_arrays(
| [
| np.array([0, 0, 1, 3], dtype=np.int32),
| np.array([0, 2, 1, 3], dtype=np.int32),
| ],
| )
| expected = pd.Series([4, 9, 7, 5], index=index, dtype="Sparse[int]")
| tm.assert_series_equal(result, expected)
|
| @td.skip_if_no_scipy
| @pytest.mark.parametrize(
| "sort_labels, expected_rows, expected_cols, expected_values_pos",
| [
| (
| False,
| [("b", 2), ("a", 2), ("b", 1), ("a", 1)],
| [("z", 1), ("z", 2), ("x", 2), ("z", 0)],
| {1: (1, 0), 3: (3, 3)},
| ),
| (
| True,
| [("a", 1), ("a", 2), ("b", 1), ("b", 2)],
| [("x", 2), ("z", 0), ("z", 1), ("z", 2)],
| {1: (1, 2), 3: (0, 1)},
| ),
| ],
| )
| def test_to_coo(
| self, sort_labels, expected_rows, expected_cols, expected_values_pos
| ):
| import scipy.sparse
|
| values = SparseArray([0, np.nan, 1, 0, None, 3], fill_value=0)
| index = pd.MultiIndex.from_tuples(
| [
| ("b", 2, "z", 1),
| ("a", 2, "z", 2),
| ("a", 2, "z", 1),
| ("a", 2, "x", 2),
| ("b", 1, "z", 1),
| ("a", 1, "z", 0),
| ]
| )
| ss = pd.Series(values, index=index)
|
| expected_A = np.zeros((4, 4))
| for value, (row, col) in expected_values_pos.items():
| expected_A[row, col] = value
|
| A, rows, cols = ss.sparse.to_coo(
| row_levels=(0, 1), column_levels=(2, 3), sort_labels=sort_labels
| )
| assert isinstance(A, scipy.sparse.coo_matrix)
| tm.assert_numpy_array_equal(A.toarray(), expected_A)
| assert rows == expected_rows
| assert cols == expected_cols
|
| def test_non_sparse_raises(self):
| ser = pd.Series([1, 2, 3])
| with pytest.raises(AttributeError, match=".sparse"):
| ser.sparse.density
|
|
| class TestFrameAccessor:
| def test_accessor_raises(self):
| df = pd.DataFrame({"A": [0, 1]})
| with pytest.raises(AttributeError, match="sparse"):
| df.sparse
|
| @pytest.mark.parametrize("format", ["csc", "csr", "coo"])
| @pytest.mark.parametrize("labels", [None, list(string.ascii_letters[:10])])
| @pytest.mark.parametrize("dtype", ["float64", "int64"])
| @td.skip_if_no_scipy
| def test_from_spmatrix(self, format, labels, dtype):
| import scipy.sparse
|
| sp_dtype = SparseDtype(dtype, np.array(0, dtype=dtype).item())
|
| mat = scipy.sparse.eye(10, format=format, dtype=dtype)
| result = pd.DataFrame.sparse.from_spmatrix(mat, index=labels, columns=labels)
| expected = pd.DataFrame(
| np.eye(10, dtype=dtype), index=labels, columns=labels
| ).astype(sp_dtype)
| tm.assert_frame_equal(result, expected)
|
| @pytest.mark.parametrize("format", ["csc", "csr", "coo"])
| @td.skip_if_no_scipy
| def test_from_spmatrix_including_explicit_zero(self, format):
| import scipy.sparse
|
| mat = scipy.sparse.random(10, 2, density=0.5, format=format)
| mat.data[0] = 0
| result = pd.DataFrame.sparse.from_spmatrix(mat)
| dtype = SparseDtype("float64", 0.0)
| expected = pd.DataFrame(mat.todense()).astype(dtype)
| tm.assert_frame_equal(result, expected)
|
| @pytest.mark.parametrize(
| "columns",
| [["a", "b"], pd.MultiIndex.from_product([["A"], ["a", "b"]]), ["a", "a"]],
| )
| @td.skip_if_no_scipy
| def test_from_spmatrix_columns(self, columns):
| import scipy.sparse
|
| dtype = SparseDtype("float64", 0.0)
|
| mat = scipy.sparse.random(10, 2, density=0.5)
| result = pd.DataFrame.sparse.from_spmatrix(mat, columns=columns)
| expected = pd.DataFrame(mat.toarray(), columns=columns).astype(dtype)
| tm.assert_frame_equal(result, expected)
|
| @pytest.mark.parametrize(
| "colnames", [("A", "B"), (1, 2), (1, pd.NA), (0.1, 0.2), ("x", "x"), (0, 0)]
| )
| @td.skip_if_no_scipy
| def test_to_coo(self, colnames):
| import scipy.sparse
|
| df = pd.DataFrame(
| {colnames[0]: [0, 1, 0], colnames[1]: [1, 0, 0]}, dtype="Sparse[int64, 0]"
| )
| result = df.sparse.to_coo()
| expected = scipy.sparse.coo_matrix(np.asarray(df))
| assert (result != expected).nnz == 0
|
| @pytest.mark.parametrize("fill_value", [1, np.nan])
| @td.skip_if_no_scipy
| def test_to_coo_nonzero_fill_val_raises(self, fill_value):
| df = pd.DataFrame(
| {
| "A": SparseArray(
| [fill_value, fill_value, fill_value, 2], fill_value=fill_value
| ),
| "B": SparseArray(
| [fill_value, 2, fill_value, fill_value], fill_value=fill_value
| ),
| }
| )
| with pytest.raises(ValueError, match="fill value must be 0"):
| df.sparse.to_coo()
|
| @td.skip_if_no_scipy
| def test_to_coo_midx_categorical(self):
| # GH#50996
| import scipy.sparse
|
| midx = pd.MultiIndex.from_arrays(
| [
| pd.CategoricalIndex(list("ab"), name="x"),
| pd.CategoricalIndex([0, 1], name="y"),
| ]
| )
|
| ser = pd.Series(1, index=midx, dtype="Sparse[int]")
| result = ser.sparse.to_coo(row_levels=["x"], column_levels=["y"])[0]
| expected = scipy.sparse.coo_matrix(
| (np.array([1, 1]), (np.array([0, 1]), np.array([0, 1]))), shape=(2, 2)
| )
| assert (result != expected).nnz == 0
|
| def test_to_dense(self):
| df = pd.DataFrame(
| {
| "A": SparseArray([1, 0], dtype=SparseDtype("int64", 0)),
| "B": SparseArray([1, 0], dtype=SparseDtype("int64", 1)),
| "C": SparseArray([1.0, 0.0], dtype=SparseDtype("float64", 0.0)),
| },
| index=["b", "a"],
| )
| result = df.sparse.to_dense()
| expected = pd.DataFrame(
| {"A": [1, 0], "B": [1, 0], "C": [1.0, 0.0]}, index=["b", "a"]
| )
| tm.assert_frame_equal(result, expected)
|
| def test_density(self):
| df = pd.DataFrame(
| {
| "A": SparseArray([1, 0, 2, 1], fill_value=0),
| "B": SparseArray([0, 1, 1, 1], fill_value=0),
| }
| )
| res = df.sparse.density
| expected = 0.75
| assert res == expected
|
| @pytest.mark.parametrize("dtype", ["int64", "float64"])
| @pytest.mark.parametrize("dense_index", [True, False])
| @td.skip_if_no_scipy
| def test_series_from_coo(self, dtype, dense_index):
| import scipy.sparse
|
| A = scipy.sparse.eye(3, format="coo", dtype=dtype)
| result = pd.Series.sparse.from_coo(A, dense_index=dense_index)
|
| index = pd.MultiIndex.from_tuples(
| [
| np.array([0, 0], dtype=np.int32),
| np.array([1, 1], dtype=np.int32),
| np.array([2, 2], dtype=np.int32),
| ],
| )
| expected = pd.Series(SparseArray(np.array([1, 1, 1], dtype=dtype)), index=index)
| if dense_index:
| expected = expected.reindex(pd.MultiIndex.from_product(index.levels))
|
| tm.assert_series_equal(result, expected)
|
| @td.skip_if_no_scipy
| def test_series_from_coo_incorrect_format_raises(self):
| # gh-26554
| import scipy.sparse
|
| m = scipy.sparse.csr_matrix(np.array([[0, 1], [0, 0]]))
| with pytest.raises(
| TypeError, match="Expected coo_matrix. Got csr_matrix instead."
| ):
| pd.Series.sparse.from_coo(m)
|
| def test_with_column_named_sparse(self):
| # https://github.com/pandas-dev/pandas/issues/30758
| df = pd.DataFrame({"sparse": pd.arrays.SparseArray([1, 2])})
| assert isinstance(df.sparse, pd.core.arrays.sparse.accessor.SparseFrameAccessor)
|
|