Skip to content

Commit 6f55160

Browse files
committed
TST: parametrize Excel autofilter edge cases
1 parent 1b5b02c commit 6f55160

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

pandas/tests/io/excel/test_writers.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,85 @@ def test_autofilter(self, engine, with_index, tmp_excel):
15291529
assert ws.auto_filter.ref is not None
15301530
assert ws.auto_filter.ref == "A1:D3" if with_index else "A1:C3"
15311531

1532+
def test_autofilter_empty_dataframe(self, engine, tmp_excel):
1533+
df = DataFrame()
1534+
1535+
if engine in ["odf"]:
1536+
with pytest.raises(
1537+
ValueError, match="Autofilter is not supported with odf!"
1538+
):
1539+
df.to_excel(tmp_excel, engine=engine, autofilter=True)
1540+
else:
1541+
df.to_excel(tmp_excel, engine=engine, autofilter=True)
1542+
1543+
openpyxl = pytest.importorskip("openpyxl")
1544+
with contextlib.closing(openpyxl.load_workbook(tmp_excel)) as wb:
1545+
ws = wb.active
1546+
assert ws.auto_filter.ref is not None
1547+
1548+
def test_autofilter_single_row(self, engine, tmp_excel):
1549+
df = DataFrame({"A": [1], "B": [2]})
1550+
1551+
if engine in ["odf"]:
1552+
with pytest.raises(
1553+
ValueError, match="Autofilter is not supported with odf!"
1554+
):
1555+
df.to_excel(tmp_excel, engine=engine, autofilter=True, index=False)
1556+
else:
1557+
df.to_excel(tmp_excel, engine=engine, autofilter=True, index=False)
1558+
1559+
openpyxl = pytest.importorskip("openpyxl")
1560+
with contextlib.closing(openpyxl.load_workbook(tmp_excel)) as wb:
1561+
ws = wb.active
1562+
assert ws.auto_filter.ref is not None
1563+
assert ws.auto_filter.ref == "A1:B2"
1564+
1565+
def test_autofilter_single_column(self, engine, tmp_excel):
1566+
df = DataFrame({"A": [1, 2, 3]})
1567+
1568+
if engine in ["odf"]:
1569+
with pytest.raises(
1570+
ValueError, match="Autofilter is not supported with odf!"
1571+
):
1572+
df.to_excel(tmp_excel, engine=engine, autofilter=True, index=False)
1573+
else:
1574+
df.to_excel(tmp_excel, engine=engine, autofilter=True, index=False)
1575+
1576+
openpyxl = pytest.importorskip("openpyxl")
1577+
with contextlib.closing(openpyxl.load_workbook(tmp_excel)) as wb:
1578+
ws = wb.active
1579+
assert ws.auto_filter.ref is not None
1580+
assert ws.auto_filter.ref == "A1:A4"
1581+
1582+
def test_autofilter_no_header(self, engine, tmp_excel):
1583+
df = DataFrame([[1, 2], [3, 4]])
1584+
1585+
if engine in ["odf"]:
1586+
with pytest.raises(
1587+
ValueError, match="Autofilter is not supported with odf!"
1588+
):
1589+
df.to_excel(
1590+
tmp_excel,
1591+
engine=engine,
1592+
autofilter=True,
1593+
header=False,
1594+
index=False,
1595+
)
1596+
else:
1597+
df.to_excel(
1598+
tmp_excel,
1599+
engine=engine,
1600+
autofilter=True,
1601+
header=False,
1602+
index=False,
1603+
)
1604+
1605+
openpyxl = pytest.importorskip("openpyxl")
1606+
with contextlib.closing(openpyxl.load_workbook(tmp_excel)) as wb:
1607+
ws = wb.active
1608+
assert ws.auto_filter.ref is not None
1609+
assert ws.auto_filter.ref == "A1:B2"
1610+
15321611
def test_autofilter_with_startrow_startcol(self, engine, tmp_excel):
15331612
# GH 61194
15341613
df = DataFrame.from_dict([{"A": 1, "B": 2, "C": 3}, {"A": 4, "B": 5, "C": 6}])

0 commit comments

Comments
 (0)