Skip to content

Commit c22000a

Browse files
committed
TST: Add tests for sparse quantile/where
1 parent cbb090f commit c22000a

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

pandas/tests/sparse/test_frame.py

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

pandas/tests/sparse/test_series.py

+101
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,107 @@ def test_numpy_func_call(self):
13841384
for series in ('bseries', 'zbseries'):
13851385
getattr(np, func)(getattr(self, series))
13861386

1387+
@pytest.mark.parametrize('data', [
1388+
[1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
1389+
[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, nan, nan],
1390+
[
1391+
1.0, 1.0 + 1.0j,
1392+
2.0 + 2.0j, 2.0,
1393+
3.0, 3.0 + 3.0j,
1394+
4.0 + 4.0j, 4.0,
1395+
nan, nan
1396+
]
1397+
])
1398+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1399+
'(GH 17386)')
1400+
def test_where_with_numeric_data(self, data):
1401+
# GH 17386
1402+
lower_bound = 1.5
1403+
1404+
sparse = SparseSeries(data)
1405+
result = sparse.where(sparse > lower_bound)
1406+
1407+
dense = Series(data)
1408+
dense_expected = dense.where(dense > lower_bound)
1409+
sparse_expected = SparseSeries(dense_expected)
1410+
1411+
tm.assert_series_equal(result, dense_expected)
1412+
tm.assert_sp_series_equal(result, sparse_expected)
1413+
1414+
@pytest.mark.parametrize('data', [
1415+
[1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
1416+
[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, nan, nan],
1417+
[
1418+
1.0, 1.0 + 1.0j,
1419+
2.0 + 2.0j, 2.0,
1420+
3.0, 3.0 + 3.0j,
1421+
4.0 + 4.0j, 4.0,
1422+
nan, nan
1423+
]
1424+
])
1425+
@pytest.mark.parametrize('other', [
1426+
True,
1427+
-100,
1428+
0.1,
1429+
100.0 + 100.0j
1430+
])
1431+
@pytest.mark.skip(reason='Wrong SparseBlock initialization '
1432+
'(Segfault) '
1433+
'(GH 17386)')
1434+
def test_where_with_numeric_data_and_other(self, data, other):
1435+
# GH 17386
1436+
lower_bound = 1.5
1437+
1438+
sparse = SparseSeries(data)
1439+
result = sparse.where(sparse > lower_bound, other)
1440+
1441+
dense = Series(data)
1442+
dense_expected = dense.where(dense > lower_bound, other)
1443+
sparse_expected = SparseSeries(dense_expected, fill_value=other)
1444+
1445+
tm.assert_series_equal(result, dense_expected)
1446+
tm.assert_sp_series_equal(result, sparse_expected)
1447+
1448+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1449+
'(GH 17386)')
1450+
def test_where_with_bool_data(self):
1451+
# GH 17386
1452+
data = [False, False, True, True, False, False]
1453+
cond = True
1454+
1455+
sparse = SparseSeries(data)
1456+
result = sparse.where(sparse == cond)
1457+
1458+
dense = Series(data)
1459+
dense_expected = dense.where(dense == cond)
1460+
sparse_expected = SparseSeries(dense_expected)
1461+
1462+
tm.assert_series_equal(result, dense_expected)
1463+
tm.assert_sp_series_equal(result, sparse_expected)
1464+
1465+
@pytest.mark.parametrize('other', [
1466+
True,
1467+
0,
1468+
0.1,
1469+
100.0 + 100.0j
1470+
])
1471+
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
1472+
'(GH 17386)')
1473+
def test_where_with_bool_data_and_other(self, other):
1474+
# GH 17386
1475+
data = [False, False, True, True, False, False]
1476+
cond = True
1477+
1478+
sparse = SparseSeries(data)
1479+
result = sparse.where(sparse == cond, other)
1480+
1481+
dense = Series(data)
1482+
dense_expected = dense.where(dense == cond, other)
1483+
sparse_expected = SparseSeries(dense_expected, fill_value=other)
1484+
1485+
tm.assert_series_equal(result, dense_expected)
1486+
tm.assert_sp_series_equal(result, sparse_expected)
1487+
13871488

13881489
@pytest.mark.parametrize(
13891490
'datetime_type', (np.datetime64,

0 commit comments

Comments
 (0)