Skip to content

Commit 51b26d0

Browse files
committed
TST: Fix and add sparse where tests
1 parent f5ccbf0 commit 51b26d0

File tree

2 files changed

+589
-37
lines changed

2 files changed

+589
-37
lines changed

pandas/tests/sparse/test_frame.py

Lines changed: 305 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,40 +1386,323 @@ def test_numpy_func_call(self):
13861386
for func in funcs:
13871387
getattr(np, func)(self.frame)
13881388

1389-
def test_where(self):
1390-
data = [[1, 2], [3, 4]]
1389+
def test_where_with_int_data(self):
1390+
# GH 17386
1391+
data = [[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]]
1392+
lower_bound = 2.5
13911393

1392-
sparse_df = SparseDataFrame(data)
1393-
result = sparse_df.where(sparse_df >= 2)
1394+
sparse = SparseDataFrame(data)
1395+
result = sparse.where(sparse > lower_bound)
13941396

1395-
dense_df = DataFrame(data)
1396-
expected = dense_df.where(dense_df >= 2)
1397+
dense = DataFrame(data)
1398+
dense_expected = dense.where(dense > lower_bound)
1399+
sparse_expected = SparseDataFrame(dense_expected)
13971400

1398-
tm.assert_frame_equal(result, expected)
1401+
tm.assert_frame_equal(result, dense_expected)
1402+
tm.assert_sp_frame_equal(result, sparse_expected)
13991403

1400-
def test_where_with_zero_other(self):
1401-
data = [[1, 2], [3, 4]]
1402-
other = 0
1404+
def test_where_with_int_data_and_int_other(self):
1405+
# GH 17386
1406+
data = [[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]]
1407+
other = -100
1408+
lower_bound = 2.5
14031409

1404-
sparse_df = SparseDataFrame(data)
1405-
result = sparse_df.where(sparse_df >= 2, other)
1410+
sparse = SparseDataFrame(data)
1411+
result = sparse.where(sparse > lower_bound, other)
14061412

1407-
dense_df = DataFrame(data)
1408-
expected = dense_df.where(dense_df >= 2, other)
1413+
dense = DataFrame(data)
1414+
dense_expected = dense.where(dense > lower_bound, other)
1415+
sparse_expected = SparseDataFrame(dense_expected,
1416+
default_fill_value=other)
14091417

1410-
tm.assert_frame_equal(result, expected)
1418+
tm.assert_frame_equal(result, dense_expected)
1419+
tm.assert_sp_frame_equal(result, sparse_expected)
14111420

1412-
def test_where_with_float_other(self):
1413-
data = [[1, 2], [3, 4]]
1421+
def test_where_with_int_data_and_float_other(self):
1422+
# GH 17386
1423+
data = [[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]]
14141424
other = 0.1
1425+
lower_bound = 2.5
14151426

1416-
sparse_df = SparseDataFrame(data)
1417-
result = sparse_df.where(sparse_df >= 2, other)
1427+
sparse = SparseDataFrame(data)
1428+
result = sparse.where(sparse > lower_bound, other)
14181429

1419-
dense_df = DataFrame(data)
1420-
expected = dense_df.where(dense_df >= 2, other)
1430+
dense = DataFrame(data)
1431+
dense_expected = dense.where(dense > lower_bound, other)
1432+
sparse_expected = SparseDataFrame(dense_expected,
1433+
default_fill_value=other)
14211434

1422-
tm.assert_frame_equal(result, expected)
1435+
tm.assert_frame_equal(result, dense_expected)
1436+
tm.assert_sp_frame_equal(result, sparse_expected)
1437+
1438+
def test_where_with_int_data_and_complex_other(self):
1439+
# GH 17386
1440+
data = [[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]]
1441+
other = 100.0 + 100.0j
1442+
lower_bound = 2.5
1443+
1444+
sparse = SparseDataFrame(data)
1445+
result = sparse.where(sparse > lower_bound, other)
1446+
1447+
dense = DataFrame(data)
1448+
dense_expected = dense.where(dense > lower_bound, other)
1449+
sparse_expected = SparseDataFrame(dense_expected,
1450+
default_fill_value=other)
1451+
1452+
tm.assert_frame_equal(result, dense_expected)
1453+
tm.assert_sp_frame_equal(result, sparse_expected)
1454+
1455+
def test_where_with_int_data_and_bool_other(self):
1456+
# GH 17386
1457+
data = [[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]]
1458+
other = True
1459+
lower_bound = 2.5
1460+
1461+
sparse = SparseDataFrame(data)
1462+
result = sparse.where(sparse > lower_bound, other)
1463+
1464+
dense = DataFrame(data)
1465+
dense_expected = dense.where(dense > lower_bound, other)
1466+
sparse_expected = SparseDataFrame(dense_expected,
1467+
default_fill_value=other)
1468+
1469+
tm.assert_frame_equal(result, dense_expected)
1470+
tm.assert_sp_frame_equal(result, sparse_expected)
1471+
1472+
def test_where_with_float_data(self):
1473+
# GH 17386
1474+
data = [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]]
1475+
lower_bound = 2.5
1476+
1477+
sparse = SparseDataFrame(data)
1478+
result = sparse.where(sparse > lower_bound)
1479+
1480+
dense = DataFrame(data)
1481+
dense_expected = dense.where(dense > lower_bound)
1482+
sparse_expected = SparseDataFrame(dense_expected)
1483+
1484+
tm.assert_frame_equal(result, dense_expected)
1485+
tm.assert_sp_frame_equal(result, sparse_expected)
1486+
1487+
def test_where_with_float_data_and_int_other(self):
1488+
# GH 17386
1489+
data = [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]]
1490+
other = 0
1491+
lower_bound = 2.5
1492+
1493+
sparse = SparseDataFrame(data)
1494+
result = sparse.where(sparse > lower_bound, other)
1495+
1496+
dense = DataFrame(data)
1497+
dense_expected = dense.where(dense > lower_bound, 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+
def test_where_with_float_data_and_float_other(self):
1505+
# GH 17386
1506+
data = [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]]
1507+
other = 0.1
1508+
lower_bound = 2.5
1509+
1510+
sparse = SparseDataFrame(data)
1511+
result = sparse.where(sparse > lower_bound, other)
1512+
1513+
dense = DataFrame(data)
1514+
dense_expected = dense.where(dense > lower_bound, other)
1515+
sparse_expected = SparseDataFrame(dense_expected,
1516+
default_fill_value=other)
1517+
1518+
tm.assert_frame_equal(result, dense_expected)
1519+
tm.assert_sp_frame_equal(result, sparse_expected)
1520+
1521+
def test_where_with_float_data_and_complex_other(self):
1522+
# GH 17386
1523+
data = [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]]
1524+
other = 100.0 - 100j
1525+
lower_bound = 2.5
1526+
1527+
sparse = SparseDataFrame(data)
1528+
result = sparse.where(sparse > lower_bound, other)
1529+
1530+
dense = DataFrame(data)
1531+
dense_expected = dense.where(dense > lower_bound, other)
1532+
sparse_expected = SparseDataFrame(dense_expected,
1533+
default_fill_value=other)
1534+
1535+
tm.assert_frame_equal(result, dense_expected)
1536+
tm.assert_sp_frame_equal(result, sparse_expected)
1537+
1538+
def test_where_with_float_data_and_bool_other(self):
1539+
# GH 17386
1540+
data = [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]]
1541+
other = True
1542+
lower_bound = 2.5
1543+
1544+
sparse = SparseDataFrame(data)
1545+
result = sparse.where(sparse > lower_bound, other)
1546+
1547+
dense = DataFrame(data)
1548+
dense_expected = dense.where(dense > lower_bound, other)
1549+
sparse_expected = SparseDataFrame(dense_expected,
1550+
default_fill_value=other)
1551+
1552+
tm.assert_frame_equal(result, dense_expected)
1553+
tm.assert_sp_frame_equal(result, sparse_expected)
1554+
1555+
def test_where_with_complex_data(self):
1556+
# GH 17386
1557+
data = [[1.0, 1.0 + 1.0j],
1558+
[2.0 + 2.0j, 2.0],
1559+
[3.0, 3.0 + 3.0j],
1560+
[4.0 + 4.0j, 4.0],
1561+
[nan, nan]]
1562+
lower_bound = 1.5
1563+
1564+
sparse = SparseDataFrame(data)
1565+
result = sparse.where(sparse > lower_bound)
1566+
1567+
dense = DataFrame(data)
1568+
dense_expected = dense.where(dense > lower_bound)
1569+
sparse_expected = SparseDataFrame(dense_expected)
1570+
1571+
tm.assert_frame_equal(result, dense_expected)
1572+
tm.assert_sp_frame_equal(result, sparse_expected)
1573+
1574+
def test_where_with_complex_data_and_int_other(self):
1575+
# GH 17386
1576+
data = [[1.0, 1.0 + 1.0j],
1577+
[2.0 + 2.0j, 2.0],
1578+
[3.0, 3.0 + 3.0j],
1579+
[4.0 + 4.0j, 4.0],
1580+
[nan, nan]]
1581+
other = 0
1582+
lower_bound = 1.5
1583+
1584+
sparse = SparseDataFrame(data)
1585+
result = sparse.where(sparse > lower_bound, other)
1586+
1587+
dense = DataFrame(data)
1588+
dense_expected = dense.where(dense > lower_bound, other)
1589+
sparse_expected = SparseDataFrame(dense_expected,
1590+
default_fill_value=other)
1591+
1592+
tm.assert_frame_equal(result, dense_expected)
1593+
tm.assert_sp_frame_equal(result, sparse_expected)
1594+
1595+
def test_where_with_complex_data_and_float_other(self):
1596+
# GH 17386
1597+
data = [[1.0, 1.0 + 1.0j],
1598+
[2.0 + 2.0j, 2.0],
1599+
[3.0, 3.0 + 3.0j],
1600+
[4.0 + 4.0j, 4.0],
1601+
[nan, nan]]
1602+
other = 0.1
1603+
lower_bound = 1.5
1604+
1605+
sparse = SparseDataFrame(data)
1606+
result = sparse.where(sparse > lower_bound, other)
1607+
1608+
dense = DataFrame(data)
1609+
dense_expected = dense.where(dense > lower_bound, other)
1610+
sparse_expected = SparseDataFrame(dense_expected,
1611+
default_fill_value=other)
1612+
1613+
tm.assert_frame_equal(result, dense_expected)
1614+
tm.assert_sp_frame_equal(result, sparse_expected)
1615+
1616+
def test_where_with_complex_data_and_complex_other(self):
1617+
# GH 17386
1618+
data = [[1.0, 1.0 + 1.0j],
1619+
[2.0 + 2.0j, 2.0],
1620+
[3.0, 3.0 + 3.0j],
1621+
[4.0 + 4.0j, 4.0],
1622+
[nan, nan]]
1623+
other = 100.0 - 100.0j
1624+
lower_bound = 1.5
1625+
1626+
sparse = SparseDataFrame(data)
1627+
result = sparse.where(sparse > lower_bound, other)
1628+
1629+
dense = DataFrame(data)
1630+
dense_expected = dense.where(dense > lower_bound, other)
1631+
sparse_expected = SparseDataFrame(dense_expected,
1632+
default_fill_value=other)
1633+
1634+
tm.assert_frame_equal(result, dense_expected)
1635+
tm.assert_sp_frame_equal(result, sparse_expected)
1636+
1637+
def test_where_with_complex_data_and_bool_other(self):
1638+
# GH 17386
1639+
data = [[1.0, 1.0 + 1.0j],
1640+
[2.0 + 2.0j, 2.0],
1641+
[3.0, 3.0 + 3.0j],
1642+
[4.0 + 4.0j, 4.0],
1643+
[nan, nan]]
1644+
other = True
1645+
lower_bound = 2.5
1646+
1647+
sparse = SparseDataFrame(data)
1648+
result = sparse.where(sparse > lower_bound, other)
1649+
1650+
dense = DataFrame(data)
1651+
dense_expected = dense.where(dense > lower_bound, other)
1652+
sparse_expected = SparseDataFrame(dense_expected,
1653+
default_fill_value=other)
1654+
1655+
tm.assert_frame_equal(result, dense_expected)
1656+
tm.assert_sp_frame_equal(result, sparse_expected)
1657+
1658+
def test_where_with_bool_data(self):
1659+
# GH 17386
1660+
data = [[False, False], [True, True], [False, False]]
1661+
cond = True
1662+
1663+
sparse = SparseDataFrame(data)
1664+
result = sparse.where(sparse == cond)
1665+
1666+
dense = DataFrame(data)
1667+
dense_expected = dense.where(dense == cond)
1668+
sparse_expected = SparseDataFrame(dense_expected)
1669+
1670+
tm.assert_frame_equal(result, dense_expected)
1671+
tm.assert_sp_frame_equal(result, sparse_expected)
1672+
1673+
def test_where_with_bool_data_and_bool_other(self):
1674+
# GH 17386
1675+
data = [[False, False], [True, True], [False, False]]
1676+
other = True
1677+
cond = True
1678+
1679+
sparse = SparseDataFrame(data)
1680+
result = sparse.where(sparse == cond, other)
1681+
1682+
dense = DataFrame(data)
1683+
dense_expected = dense.where(dense == cond, other)
1684+
sparse_expected = SparseDataFrame(dense_expected,
1685+
default_fill_value=other)
1686+
1687+
tm.assert_frame_equal(result, dense_expected)
1688+
tm.assert_sp_frame_equal(result, sparse_expected)
1689+
1690+
def test_where_with_bool_data_and_complex_other(self):
1691+
# GH 17386
1692+
data = [[False, False], [True, True], [False, False]]
1693+
other = 100.0 + 100.0j
1694+
cond = True
1695+
1696+
sparse = SparseDataFrame(data)
1697+
result = sparse.where(sparse == cond, other)
1698+
1699+
dense = DataFrame(data)
1700+
dense_expected = dense.where(dense == cond, other)
1701+
sparse_expected = SparseDataFrame(dense_expected,
1702+
default_fill_value=other)
1703+
1704+
tm.assert_frame_equal(result, dense_expected)
1705+
tm.assert_sp_frame_equal(result, sparse_expected)
14231706

14241707
def test_quantile(self):
14251708
data = [[1, 1], [2, 10], [3, 100], [4, 100]]

0 commit comments

Comments
 (0)