Skip to content

BUG: Error writing an empty DataFrame to an ods file #45794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ I/O
- Bug in :meth:`DataFrame.info` where a new line at the end of the output is omitted when called on an empty :class:`DataFrame` (:issue:`45494`)
- Bug in :func:`read_csv` not recognizing line break for ``on_bad_lines="warn"`` for ``engine="c"`` (:issue:`41710`)
- Bug in :func:`read_parquet` when ``engine="pyarrow"`` which caused partial write to disk when column of unsupported datatype was passed (:issue:`44914`)
-
- Bug in :func:`DataFrame.to_excel` and :class:`ExcelWriter` would raise when writing an empty DataFrame to a ``.ods`` file (:issue:`45793`)

Period
^^^^^^
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/excel/_odswriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ def write_cells(
tc.addElement(p)

# add all rows to the sheet
for row_nr in range(max(rows.keys()) + 1):
wks.addElement(rows[row_nr])
if len(rows) > 0:
for row_nr in range(max(rows.keys()) + 1):
wks.addElement(rows[row_nr])

def _make_table_cell_attributes(self, cell) -> dict[str, int | str]:
"""Convert cell attributes to OpenDocument attributes
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,23 @@ def test_if_sheet_exists_raises(self, ext):
with pytest.raises(ValueError, match=re.escape(msg)):
ExcelWriter(f, if_sheet_exists="replace")

def test_excel_writer_empty_frame(self, engine, ext):
# GH#45793
with tm.ensure_clean(ext) as path:
with ExcelWriter(path, engine=engine) as writer:
DataFrame().to_excel(writer)
result = pd.read_excel(path)
expected = DataFrame()
tm.assert_frame_equal(result, expected)

def test_to_excel_empty_frame(self, engine, ext):
# GH#45793
with tm.ensure_clean(ext) as path:
DataFrame().to_excel(path, engine=engine)
result = pd.read_excel(path)
expected = DataFrame()
tm.assert_frame_equal(result, expected)


class TestExcelWriterEngineTests:
@pytest.mark.parametrize(
Expand Down