From 6f551600624e33e05de7e5378ad540aeedb16fd2 Mon Sep 17 00:00:00 2001 From: antznette1 Date: Thu, 27 Nov 2025 12:09:41 +0100 Subject: [PATCH] TST: parametrize Excel autofilter edge cases --- pandas/tests/io/excel/test_writers.py | 79 +++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index 1e58be24ee96c..38c922597ce89 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -1529,6 +1529,85 @@ def test_autofilter(self, engine, with_index, tmp_excel): assert ws.auto_filter.ref is not None assert ws.auto_filter.ref == "A1:D3" if with_index else "A1:C3" + def test_autofilter_empty_dataframe(self, engine, tmp_excel): + df = DataFrame() + + if engine in ["odf"]: + with pytest.raises( + ValueError, match="Autofilter is not supported with odf!" + ): + df.to_excel(tmp_excel, engine=engine, autofilter=True) + else: + df.to_excel(tmp_excel, engine=engine, autofilter=True) + + openpyxl = pytest.importorskip("openpyxl") + with contextlib.closing(openpyxl.load_workbook(tmp_excel)) as wb: + ws = wb.active + assert ws.auto_filter.ref is not None + + def test_autofilter_single_row(self, engine, tmp_excel): + df = DataFrame({"A": [1], "B": [2]}) + + if engine in ["odf"]: + with pytest.raises( + ValueError, match="Autofilter is not supported with odf!" + ): + df.to_excel(tmp_excel, engine=engine, autofilter=True, index=False) + else: + df.to_excel(tmp_excel, engine=engine, autofilter=True, index=False) + + openpyxl = pytest.importorskip("openpyxl") + with contextlib.closing(openpyxl.load_workbook(tmp_excel)) as wb: + ws = wb.active + assert ws.auto_filter.ref is not None + assert ws.auto_filter.ref == "A1:B2" + + def test_autofilter_single_column(self, engine, tmp_excel): + df = DataFrame({"A": [1, 2, 3]}) + + if engine in ["odf"]: + with pytest.raises( + ValueError, match="Autofilter is not supported with odf!" + ): + df.to_excel(tmp_excel, engine=engine, autofilter=True, index=False) + else: + df.to_excel(tmp_excel, engine=engine, autofilter=True, index=False) + + openpyxl = pytest.importorskip("openpyxl") + with contextlib.closing(openpyxl.load_workbook(tmp_excel)) as wb: + ws = wb.active + assert ws.auto_filter.ref is not None + assert ws.auto_filter.ref == "A1:A4" + + def test_autofilter_no_header(self, engine, tmp_excel): + df = DataFrame([[1, 2], [3, 4]]) + + if engine in ["odf"]: + with pytest.raises( + ValueError, match="Autofilter is not supported with odf!" + ): + df.to_excel( + tmp_excel, + engine=engine, + autofilter=True, + header=False, + index=False, + ) + else: + df.to_excel( + tmp_excel, + engine=engine, + autofilter=True, + header=False, + index=False, + ) + + openpyxl = pytest.importorskip("openpyxl") + with contextlib.closing(openpyxl.load_workbook(tmp_excel)) as wb: + ws = wb.active + assert ws.auto_filter.ref is not None + assert ws.auto_filter.ref == "A1:B2" + def test_autofilter_with_startrow_startcol(self, engine, tmp_excel): # GH 61194 df = DataFrame.from_dict([{"A": 1, "B": 2, "C": 3}, {"A": 4, "B": 5, "C": 6}])