Skip to content

Commit c65a0f5

Browse files
Licht-Tjreback
authored andcommitted
TST: Add tests for sparse quantile/where (#17568)
1 parent 823cfc4 commit c65a0f5

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed

pandas/tests/sparse/test_frame.py

+136
Original file line numberDiff line numberDiff line change
@@ -1398,3 +1398,139 @@ def test_numpy_func_call(self):
13981398
'std', 'min', 'max']
13991399
for func in funcs:
14001400
getattr(np, func)(self.frame)
1401+
1402+
@pytest.mark.parametrize('data', [
1403+
[[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]],
1404+
[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]],
1405+
[
1406+
[1.0, 1.0 + 1.0j],
1407+
[2.0 + 2.0j, 2.0],
1408+
[3.0, 3.0 + 3.0j],
1409+
[4.0 + 4.0j, 4.0],
1410+
[nan, nan]
1411+
]
1412+
])
1413+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1414+
'(GH 17386)')
1415+
def test_where_with_numeric_data(self, data):
1416+
# GH 17386
1417+
lower_bound = 1.5
1418+
1419+
sparse = SparseDataFrame(data)
1420+
result = sparse.where(sparse > lower_bound)
1421+
1422+
dense = DataFrame(data)
1423+
dense_expected = dense.where(dense > lower_bound)
1424+
sparse_expected = SparseDataFrame(dense_expected)
1425+
1426+
tm.assert_frame_equal(result, dense_expected)
1427+
tm.assert_sp_frame_equal(result, sparse_expected)
1428+
1429+
@pytest.mark.parametrize('data', [
1430+
[[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]],
1431+
[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]],
1432+
[
1433+
[1.0, 1.0 + 1.0j],
1434+
[2.0 + 2.0j, 2.0],
1435+
[3.0, 3.0 + 3.0j],
1436+
[4.0 + 4.0j, 4.0],
1437+
[nan, nan]
1438+
]
1439+
])
1440+
@pytest.mark.parametrize('other', [
1441+
True,
1442+
-100,
1443+
0.1,
1444+
100.0 + 100.0j
1445+
])
1446+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1447+
'(GH 17386)')
1448+
def test_where_with_numeric_data_and_other(self, data, other):
1449+
# GH 17386
1450+
lower_bound = 1.5
1451+
1452+
sparse = SparseDataFrame(data)
1453+
result = sparse.where(sparse > lower_bound, other)
1454+
1455+
dense = DataFrame(data)
1456+
dense_expected = dense.where(dense > lower_bound, other)
1457+
sparse_expected = SparseDataFrame(dense_expected,
1458+
default_fill_value=other)
1459+
1460+
tm.assert_frame_equal(result, dense_expected)
1461+
tm.assert_sp_frame_equal(result, sparse_expected)
1462+
1463+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1464+
'(GH 17386)')
1465+
def test_where_with_bool_data(self):
1466+
# GH 17386
1467+
data = [[False, False], [True, True], [False, False]]
1468+
cond = True
1469+
1470+
sparse = SparseDataFrame(data)
1471+
result = sparse.where(sparse == cond)
1472+
1473+
dense = DataFrame(data)
1474+
dense_expected = dense.where(dense == cond)
1475+
sparse_expected = SparseDataFrame(dense_expected)
1476+
1477+
tm.assert_frame_equal(result, dense_expected)
1478+
tm.assert_sp_frame_equal(result, sparse_expected)
1479+
1480+
@pytest.mark.parametrize('other', [
1481+
True,
1482+
0,
1483+
0.1,
1484+
100.0 + 100.0j
1485+
])
1486+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1487+
'(GH 17386)')
1488+
def test_where_with_bool_data_and_other(self, other):
1489+
# GH 17386
1490+
data = [[False, False], [True, True], [False, False]]
1491+
cond = True
1492+
1493+
sparse = SparseDataFrame(data)
1494+
result = sparse.where(sparse == cond, other)
1495+
1496+
dense = DataFrame(data)
1497+
dense_expected = dense.where(dense == cond, other)
1498+
sparse_expected = SparseDataFrame(dense_expected,
1499+
default_fill_value=other)
1500+
1501+
tm.assert_frame_equal(result, dense_expected)
1502+
tm.assert_sp_frame_equal(result, sparse_expected)
1503+
1504+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1505+
'(GH 17386)')
1506+
def test_quantile(self):
1507+
# GH 17386
1508+
data = [[1, 1], [2, 10], [3, 100], [nan, nan]]
1509+
q = 0.1
1510+
1511+
sparse_df = SparseDataFrame(data)
1512+
result = sparse_df.quantile(q)
1513+
1514+
dense_df = DataFrame(data)
1515+
dense_expected = dense_df.quantile(q)
1516+
sparse_expected = SparseSeries(dense_expected)
1517+
1518+
tm.assert_series_equal(result, dense_expected)
1519+
tm.assert_sp_series_equal(result, sparse_expected)
1520+
1521+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1522+
'(GH 17386)')
1523+
def test_quantile_multi(self):
1524+
# GH 17386
1525+
data = [[1, 1], [2, 10], [3, 100], [nan, nan]]
1526+
q = [0.1, 0.5]
1527+
1528+
sparse_df = SparseDataFrame(data)
1529+
result = sparse_df.quantile(q)
1530+
1531+
dense_df = DataFrame(data)
1532+
dense_expected = dense_df.quantile(q)
1533+
sparse_expected = SparseDataFrame(dense_expected)
1534+
1535+
tm.assert_frame_equal(result, dense_expected)
1536+
tm.assert_sp_frame_equal(result, sparse_expected)

pandas/tests/sparse/test_series.py

+102
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,108 @@ def test_deprecated_reindex_axis(self):
14191419
self.bseries.reindex_axis([0, 1, 2])
14201420
assert 'reindex' in str(m[0].message)
14211421

1422+
@pytest.mark.parametrize('data', [
1423+
[1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
1424+
[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, nan, nan],
1425+
[
1426+
1.0, 1.0 + 1.0j,
1427+
2.0 + 2.0j, 2.0,
1428+
3.0, 3.0 + 3.0j,
1429+
4.0 + 4.0j, 4.0,
1430+
nan, nan
1431+
]
1432+
])
1433+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1434+
'(GH 17386)')
1435+
def test_where_with_numeric_data(self, data):
1436+
# GH 17386
1437+
lower_bound = 1.5
1438+
1439+
sparse = SparseSeries(data)
1440+
result = sparse.where(sparse > lower_bound)
1441+
1442+
dense = Series(data)
1443+
dense_expected = dense.where(dense > lower_bound)
1444+
sparse_expected = SparseSeries(dense_expected)
1445+
1446+
tm.assert_series_equal(result, dense_expected)
1447+
tm.assert_sp_series_equal(result, sparse_expected)
1448+
1449+
@pytest.mark.parametrize('data', [
1450+
[1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
1451+
[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, nan, nan],
1452+
[
1453+
1.0, 1.0 + 1.0j,
1454+
2.0 + 2.0j, 2.0,
1455+
3.0, 3.0 + 3.0j,
1456+
4.0 + 4.0j, 4.0,
1457+
nan, nan
1458+
]
1459+
])
1460+
@pytest.mark.parametrize('other', [
1461+
True,
1462+
-100,
1463+
0.1,
1464+
100.0 + 100.0j
1465+
])
1466+
@pytest.mark.skip(reason='Wrong SparseBlock initialization '
1467+
'(Segfault) '
1468+
'(GH 17386)')
1469+
def test_where_with_numeric_data_and_other(self, data, other):
1470+
# GH 17386
1471+
lower_bound = 1.5
1472+
1473+
sparse = SparseSeries(data)
1474+
result = sparse.where(sparse > lower_bound, other)
1475+
1476+
dense = Series(data)
1477+
dense_expected = dense.where(dense > lower_bound, other)
1478+
sparse_expected = SparseSeries(dense_expected, fill_value=other)
1479+
1480+
tm.assert_series_equal(result, dense_expected)
1481+
tm.assert_sp_series_equal(result, sparse_expected)
1482+
1483+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1484+
'(GH 17386)')
1485+
def test_where_with_bool_data(self):
1486+
# GH 17386
1487+
data = [False, False, True, True, False, False]
1488+
cond = True
1489+
1490+
sparse = SparseSeries(data)
1491+
result = sparse.where(sparse == cond)
1492+
1493+
dense = Series(data)
1494+
dense_expected = dense.where(dense == cond)
1495+
sparse_expected = SparseSeries(dense_expected)
1496+
1497+
tm.assert_series_equal(result, dense_expected)
1498+
tm.assert_sp_series_equal(result, sparse_expected)
1499+
1500+
@pytest.mark.parametrize('other', [
1501+
True,
1502+
0,
1503+
0.1,
1504+
100.0 + 100.0j
1505+
])
1506+
@pytest.mark.skip(reason='Wrong SparseBlock initialization '
1507+
'(Segfault) '
1508+
'(GH 17386)')
1509+
def test_where_with_bool_data_and_other(self, other):
1510+
# GH 17386
1511+
data = [False, False, True, True, False, False]
1512+
cond = True
1513+
1514+
sparse = SparseSeries(data)
1515+
result = sparse.where(sparse == cond, other)
1516+
1517+
dense = Series(data)
1518+
dense_expected = dense.where(dense == cond, other)
1519+
sparse_expected = SparseSeries(dense_expected, fill_value=other)
1520+
1521+
tm.assert_series_equal(result, dense_expected)
1522+
tm.assert_sp_series_equal(result, sparse_expected)
1523+
14221524

14231525
@pytest.mark.parametrize(
14241526
'datetime_type', (np.datetime64,

0 commit comments

Comments
 (0)