Skip to content

Commit 54ed5bc

Browse files
committed
CLN-23123 Add missing sparse tests
1 parent e8808e0 commit 54ed5bc

File tree

8 files changed

+978
-0
lines changed

8 files changed

+978
-0
lines changed

pandas/tests/arrays/sparse/common.py

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
import numpy as np
3+
from pandas import SparseDataFrame, DataFrame, SparseSeries
4+
from pandas.util import testing as tm
5+
6+
7+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)',
8+
strict=True)
9+
def test_quantile():
10+
# GH 17386
11+
data = [[1, 1], [2, 10], [3, 100], [np.nan, np.nan]]
12+
q = 0.1
13+
14+
sparse_df = SparseDataFrame(data)
15+
result = sparse_df.quantile(q)
16+
17+
dense_df = DataFrame(data)
18+
dense_expected = dense_df.quantile(q)
19+
sparse_expected = SparseSeries(dense_expected)
20+
21+
tm.assert_series_equal(result, dense_expected)
22+
tm.assert_sp_series_equal(result, sparse_expected)
23+
24+
25+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)',
26+
strict=True)
27+
def test_quantile_multi():
28+
# GH 17386
29+
data = [[1, 1], [2, 10], [3, 100], [np.nan, np.nan]]
30+
q = [0.1, 0.5]
31+
32+
sparse_df = SparseDataFrame(data)
33+
result = sparse_df.quantile(q)
34+
35+
dense_df = DataFrame(data)
36+
dense_expected = dense_df.quantile(q)
37+
sparse_expected = SparseDataFrame(dense_expected)
38+
39+
tm.assert_frame_equal(result, dense_expected)
40+
tm.assert_sp_frame_equal(result, sparse_expected)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import pytest
2+
import numpy as np
3+
from pandas import SparseDataFrame, DataFrame
4+
from pandas.util import testing as tm
5+
6+
7+
pytestmark = pytest.mark.skip("Wrong SparseBlock initialization (GH 17386)")
8+
9+
10+
@pytest.mark.parametrize('data', [
11+
[[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]],
12+
[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [np.nan, np.nan]],
13+
[
14+
[1.0, 1.0 + 1.0j],
15+
[2.0 + 2.0j, 2.0],
16+
[3.0, 3.0 + 3.0j],
17+
[4.0 + 4.0j, 4.0],
18+
[np.nan, np.nan]
19+
]
20+
])
21+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)',
22+
strict=True)
23+
def test_where_with_numeric_data(data):
24+
# GH 17386
25+
lower_bound = 1.5
26+
27+
sparse = SparseDataFrame(data)
28+
result = sparse.where(sparse > lower_bound)
29+
30+
dense = DataFrame(data)
31+
dense_expected = dense.where(dense > lower_bound)
32+
sparse_expected = SparseDataFrame(dense_expected)
33+
34+
tm.assert_frame_equal(result, dense_expected)
35+
tm.assert_sp_frame_equal(result, sparse_expected)
36+
37+
38+
@pytest.mark.parametrize('data', [
39+
[[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]],
40+
[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [np.nan, np.nan]],
41+
[
42+
[1.0, 1.0 + 1.0j],
43+
[2.0 + 2.0j, 2.0],
44+
[3.0, 3.0 + 3.0j],
45+
[4.0 + 4.0j, 4.0],
46+
[np.nan, np.nan]
47+
]
48+
])
49+
@pytest.mark.parametrize('other', [
50+
True,
51+
-100,
52+
0.1,
53+
100.0 + 100.0j
54+
])
55+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)',
56+
strict=True)
57+
def test_where_with_numeric_data_and_other(data, other):
58+
# GH 17386
59+
lower_bound = 1.5
60+
61+
sparse = SparseDataFrame(data)
62+
result = sparse.where(sparse > lower_bound, other)
63+
64+
dense = DataFrame(data)
65+
dense_expected = dense.where(dense > lower_bound, other)
66+
sparse_expected = SparseDataFrame(dense_expected,
67+
default_fill_value=other)
68+
69+
tm.assert_frame_equal(result, dense_expected)
70+
tm.assert_sp_frame_equal(result, sparse_expected)
71+
72+
73+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)',
74+
strict=True)
75+
def test_where_with_bool_data():
76+
# GH 17386
77+
data = [[False, False], [True, True], [False, False]]
78+
cond = True
79+
80+
sparse = SparseDataFrame(data)
81+
result = sparse.where(sparse == cond)
82+
83+
dense = DataFrame(data)
84+
dense_expected = dense.where(dense == cond)
85+
sparse_expected = SparseDataFrame(dense_expected)
86+
87+
tm.assert_frame_equal(result, dense_expected)
88+
tm.assert_sp_frame_equal(result, sparse_expected)
89+
90+
91+
@pytest.mark.parametrize('other', [
92+
True,
93+
0,
94+
0.1,
95+
100.0 + 100.0j
96+
])
97+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)',
98+
strict=True)
99+
def test_where_with_bool_data_and_other(other):
100+
# GH 17386
101+
data = [[False, False], [True, True], [False, False]]
102+
cond = True
103+
104+
sparse = SparseDataFrame(data)
105+
result = sparse.where(sparse == cond, other)
106+
107+
dense = DataFrame(data)
108+
dense_expected = dense.where(dense == cond, other)
109+
sparse_expected = SparseDataFrame(dense_expected,
110+
default_fill_value=other)
111+
112+
tm.assert_frame_equal(result, dense_expected)
113+
tm.assert_sp_frame_equal(result, sparse_expected)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
import pytest
3+
from pandas import SparseDataFrame, read_csv
4+
from pandas.util import testing as tm
5+
6+
7+
class TestSparseDataFrameToCsv(object):
8+
fill_values = [np.nan, 0, None, 1]
9+
10+
@pytest.mark.parametrize('fill_value', fill_values)
11+
def test_to_csv_sparse_dataframe(self, fill_value):
12+
# GH19384
13+
sdf = SparseDataFrame({'a': type(self).fill_values},
14+
default_fill_value=fill_value)
15+
16+
with tm.ensure_clean('sparse_df.csv') as path:
17+
sdf.to_csv(path, index=False)
18+
df = read_csv(path, skip_blank_lines=False)
19+
20+
tm.assert_sp_frame_equal(df.to_sparse(fill_value=fill_value), sdf)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import pytest
2+
import numpy as np
3+
from pandas import SparseSeries, Series
4+
from pandas.util import testing as tm
5+
6+
7+
pytestmark = pytest.mark.skip("Wrong SparseBlock initialization (GH 17386)")
8+
9+
10+
@pytest.mark.parametrize('data', [
11+
[1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
12+
[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, np.nan, np.nan],
13+
[
14+
1.0, 1.0 + 1.0j,
15+
2.0 + 2.0j, 2.0,
16+
3.0, 3.0 + 3.0j,
17+
4.0 + 4.0j, 4.0,
18+
np.nan, np.nan
19+
]
20+
])
21+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)',
22+
strict=True)
23+
def test_where_with_numeric_data(data):
24+
# GH 17386
25+
lower_bound = 1.5
26+
27+
sparse = SparseSeries(data)
28+
result = sparse.where(sparse > lower_bound)
29+
30+
dense = Series(data)
31+
dense_expected = dense.where(dense > lower_bound)
32+
sparse_expected = SparseSeries(dense_expected)
33+
34+
tm.assert_series_equal(result, dense_expected)
35+
tm.assert_sp_series_equal(result, sparse_expected)
36+
37+
38+
@pytest.mark.parametrize('data', [
39+
[1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
40+
[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, np.nan, np.nan],
41+
[
42+
1.0, 1.0 + 1.0j,
43+
2.0 + 2.0j, 2.0,
44+
3.0, 3.0 + 3.0j,
45+
4.0 + 4.0j, 4.0,
46+
np.nan, np.nan
47+
]
48+
])
49+
@pytest.mark.parametrize('other', [
50+
True,
51+
-100,
52+
0.1,
53+
100.0 + 100.0j
54+
])
55+
@pytest.mark.skip(reason='Wrong SparseBlock initialization '
56+
'(Segfault) '
57+
'(GH 17386)')
58+
def test_where_with_numeric_data_and_other(data, other):
59+
# GH 17386
60+
lower_bound = 1.5
61+
62+
sparse = SparseSeries(data)
63+
result = sparse.where(sparse > lower_bound, other)
64+
65+
dense = Series(data)
66+
dense_expected = dense.where(dense > lower_bound, other)
67+
sparse_expected = SparseSeries(dense_expected, fill_value=other)
68+
69+
tm.assert_series_equal(result, dense_expected)
70+
tm.assert_sp_series_equal(result, sparse_expected)
71+
72+
73+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization (GH#17386)',
74+
strict=True)
75+
def test_where_with_bool_data():
76+
# GH 17386
77+
data = [False, False, True, True, False, False]
78+
cond = True
79+
80+
sparse = SparseSeries(data)
81+
result = sparse.where(sparse == cond)
82+
83+
dense = Series(data)
84+
dense_expected = dense.where(dense == cond)
85+
sparse_expected = SparseSeries(dense_expected)
86+
87+
tm.assert_series_equal(result, dense_expected)
88+
tm.assert_sp_series_equal(result, sparse_expected)
89+
90+
91+
@pytest.mark.parametrize('other', [
92+
True,
93+
0,
94+
0.1,
95+
100.0 + 100.0j
96+
])
97+
@pytest.mark.skip(reason='Wrong SparseBlock initialization '
98+
'(Segfault) '
99+
'(GH 17386)')
100+
def test_where_with_bool_data_and_other(other):
101+
# GH 17386
102+
data = [False, False, True, True, False, False]
103+
cond = True
104+
105+
sparse = SparseSeries(data)
106+
result = sparse.where(sparse == cond, other)
107+
108+
dense = Series(data)
109+
dense_expected = dense.where(dense == cond, other)
110+
sparse_expected = SparseSeries(dense_expected, fill_value=other)
111+
112+
tm.assert_series_equal(result, dense_expected)
113+
tm.assert_sp_series_equal(result, sparse_expected)

0 commit comments

Comments
 (0)