Skip to content

Commit d6a863e

Browse files
jbrockmendelphofl
authored andcommitted
TST/CLN: organize SparseArray tests (pandas-dev#45693)
1 parent b8bdc1a commit d6a863e

File tree

8 files changed

+1305
-1218
lines changed

8 files changed

+1305
-1218
lines changed

pandas/tests/arrays/sparse/test_accessor.py

+80-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,90 @@
1414

1515

1616
class TestSeriesAccessor:
17-
# TODO: collect other Series accessor tests
1817
def test_to_dense(self):
19-
s = pd.Series([0, 1, 0, 10], dtype="Sparse[int64]")
20-
result = s.sparse.to_dense()
18+
ser = pd.Series([0, 1, 0, 10], dtype="Sparse[int64]")
19+
result = ser.sparse.to_dense()
2120
expected = pd.Series([0, 1, 0, 10])
2221
tm.assert_series_equal(result, expected)
2322

23+
@pytest.mark.parametrize("attr", ["npoints", "density", "fill_value", "sp_values"])
24+
def test_get_attributes(self, attr):
25+
arr = SparseArray([0, 1])
26+
ser = pd.Series(arr)
27+
28+
result = getattr(ser.sparse, attr)
29+
expected = getattr(arr, attr)
30+
assert result == expected
31+
32+
@td.skip_if_no_scipy
33+
def test_from_coo(self):
34+
import scipy.sparse
35+
36+
row = [0, 3, 1, 0]
37+
col = [0, 3, 1, 2]
38+
data = [4, 5, 7, 9]
39+
# TODO(scipy#13585): Remove dtype when scipy is fixed
40+
# https://github.com/scipy/scipy/issues/13585
41+
sp_array = scipy.sparse.coo_matrix((data, (row, col)), dtype="int")
42+
result = pd.Series.sparse.from_coo(sp_array)
43+
44+
index = pd.MultiIndex.from_arrays([[0, 0, 1, 3], [0, 2, 1, 3]])
45+
expected = pd.Series([4, 9, 7, 5], index=index, dtype="Sparse[int]")
46+
tm.assert_series_equal(result, expected)
47+
48+
@td.skip_if_no_scipy
49+
@pytest.mark.parametrize(
50+
"sort_labels, expected_rows, expected_cols, expected_values_pos",
51+
[
52+
(
53+
False,
54+
[("b", 2), ("a", 2), ("b", 1), ("a", 1)],
55+
[("z", 1), ("z", 2), ("x", 2), ("z", 0)],
56+
{1: (1, 0), 3: (3, 3)},
57+
),
58+
(
59+
True,
60+
[("a", 1), ("a", 2), ("b", 1), ("b", 2)],
61+
[("x", 2), ("z", 0), ("z", 1), ("z", 2)],
62+
{1: (1, 2), 3: (0, 1)},
63+
),
64+
],
65+
)
66+
def test_to_coo(
67+
self, sort_labels, expected_rows, expected_cols, expected_values_pos
68+
):
69+
import scipy.sparse
70+
71+
values = SparseArray([0, np.nan, 1, 0, None, 3], fill_value=0)
72+
index = pd.MultiIndex.from_tuples(
73+
[
74+
("b", 2, "z", 1),
75+
("a", 2, "z", 2),
76+
("a", 2, "z", 1),
77+
("a", 2, "x", 2),
78+
("b", 1, "z", 1),
79+
("a", 1, "z", 0),
80+
]
81+
)
82+
ss = pd.Series(values, index=index)
83+
84+
expected_A = np.zeros((4, 4))
85+
for value, (row, col) in expected_values_pos.items():
86+
expected_A[row, col] = value
87+
88+
A, rows, cols = ss.sparse.to_coo(
89+
row_levels=(0, 1), column_levels=(2, 3), sort_labels=sort_labels
90+
)
91+
assert isinstance(A, scipy.sparse.coo_matrix)
92+
tm.assert_numpy_array_equal(A.toarray(), expected_A)
93+
assert rows == expected_rows
94+
assert cols == expected_cols
95+
96+
def test_non_sparse_raises(self):
97+
ser = pd.Series([1, 2, 3])
98+
with pytest.raises(AttributeError, match=".sparse"):
99+
ser.sparse.density
100+
24101

25102
class TestFrameAccessor:
26103
def test_accessor_raises(self):

0 commit comments

Comments
 (0)