Skip to content

BUG: boolean/string value in OdsWriter (#54994) #54996

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 3 commits into from
Sep 13, 2023
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ I/O
^^^
- Bug in :func:`read_csv` where ``on_bad_lines="warn"`` would write to ``stderr`` instead of raise a Python warning. This now yields a :class:`.errors.ParserWarning` (:issue:`54296`)
- Bug in :func:`read_excel`, with ``engine="xlrd"`` (``xls`` files) erroring when file contains NaNs/Infs (:issue:`54564`)
- Bug in :func:`to_excel`, with ``OdsWriter`` (``ods`` files) writing boolean/string value (:issue:`54994`)

Period
^^^^^^
Expand Down
27 changes: 19 additions & 8 deletions pandas/io/excel/_odswriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,15 @@ def _make_table_cell(self, cell) -> tuple[object, Any]:
if isinstance(val, bool):
value = str(val).lower()
pvalue = str(val).upper()
if isinstance(val, datetime.datetime):
return (
pvalue,
TableCell(
valuetype="boolean",
booleanvalue=value,
attributes=attributes,
),
)
elif isinstance(val, datetime.datetime):
# Fast formatting
value = val.isoformat()
# Slow but locale-dependent
Expand All @@ -210,17 +218,20 @@ def _make_table_cell(self, cell) -> tuple[object, Any]:
pvalue,
TableCell(valuetype="date", datevalue=value, attributes=attributes),
)
elif isinstance(val, str):
return (
pvalue,
TableCell(
valuetype="string",
stringvalue=value,
attributes=attributes,
),
)
else:
class_to_cell_type = {
str: "string",
int: "float",
float: "float",
bool: "boolean",
}
return (
pvalue,
TableCell(
valuetype=class_to_cell_type[type(val)],
valuetype="float",
value=value,
attributes=attributes,
),
Expand Down
49 changes: 49 additions & 0 deletions pandas/tests/io/excel/test_odswriter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from datetime import (
date,
datetime,
)
import re

import pytest

import pandas as pd
import pandas._testing as tm

from pandas.io.excel import ExcelWriter
Expand Down Expand Up @@ -47,3 +52,47 @@ def test_book_and_sheets_consistent(ext):
table = odf.table.Table(name="test_name")
writer.book.spreadsheet.addElement(table)
assert writer.sheets == {"test_name": table}


@pytest.mark.parametrize(
["value", "cell_value_type", "cell_value_attribute", "cell_value"],
argvalues=[
(True, "boolean", "boolean-value", "true"),
("test string", "string", "string-value", "test string"),
(1, "float", "value", "1"),
(1.5, "float", "value", "1.5"),
(
datetime(2010, 10, 10, 10, 10, 10),
"date",
"date-value",
"2010-10-10T10:10:10",
),
(date(2010, 10, 10), "date", "date-value", "2010-10-10"),
],
)
def test_cell_value_type(ext, value, cell_value_type, cell_value_attribute, cell_value):
# GH#54994 ODS: cell attributes should follow specification
# http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13
from odf.namespaces import OFFICENS
from odf.table import (
TableCell,
TableRow,
)

table_cell_name = TableCell().qname

with tm.ensure_clean(ext) as f:
pd.DataFrame([[value]]).to_excel(f, header=False, index=False)

with pd.ExcelFile(f) as wb:
sheet = wb._reader.get_sheet_by_index(0)
sheet_rows = sheet.getElementsByType(TableRow)
sheet_cells = [
x
for x in sheet_rows[0].childNodes
if hasattr(x, "qname") and x.qname == table_cell_name
]

cell = sheet_cells[0]
assert cell.attributes.get((OFFICENS, "value-type")) == cell_value_type
assert cell.attributes.get((OFFICENS, cell_value_attribute)) == cell_value
24 changes: 3 additions & 21 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,17 +578,11 @@ def test_reader_dtype_str(self, read_ext, dtype, expected):
actual = pd.read_excel(basename + read_ext, dtype=dtype)
tm.assert_frame_equal(actual, expected)

def test_dtype_backend(self, request, engine, read_ext, dtype_backend):
def test_dtype_backend(self, read_ext, dtype_backend):
# GH#36712
if read_ext in (".xlsb", ".xls"):
pytest.skip(f"No engine for filetype: '{read_ext}'")

# GH 54994
if engine == "calamine" and read_ext == ".ods":
request.node.add_marker(
pytest.mark.xfail(reason="OdsWriter produces broken file")
)

df = DataFrame(
{
"a": Series([1, 3], dtype="Int64"),
Expand Down Expand Up @@ -629,17 +623,11 @@ def test_dtype_backend(self, request, engine, read_ext, dtype_backend):
expected = df
tm.assert_frame_equal(result, expected)

def test_dtype_backend_and_dtype(self, request, engine, read_ext):
def test_dtype_backend_and_dtype(self, read_ext):
# GH#36712
if read_ext in (".xlsb", ".xls"):
pytest.skip(f"No engine for filetype: '{read_ext}'")

# GH 54994
if engine == "calamine" and read_ext == ".ods":
request.node.add_marker(
pytest.mark.xfail(reason="OdsWriter produces broken file")
)

df = DataFrame({"a": [np.nan, 1.0], "b": [2.5, np.nan]})
with tm.ensure_clean(read_ext) as file_path:
df.to_excel(file_path, sheet_name="test", index=False)
Expand All @@ -651,17 +639,11 @@ def test_dtype_backend_and_dtype(self, request, engine, read_ext):
)
tm.assert_frame_equal(result, df)

def test_dtype_backend_string(self, request, engine, read_ext, string_storage):
def test_dtype_backend_string(self, read_ext, string_storage):
# GH#36712
if read_ext in (".xlsb", ".xls"):
pytest.skip(f"No engine for filetype: '{read_ext}'")

# GH 54994
if engine == "calamine" and read_ext == ".ods":
request.node.add_marker(
pytest.mark.xfail(reason="OdsWriter produces broken file")
)

pa = pytest.importorskip("pyarrow")

with pd.option_context("mode.string_storage", string_storage):
Expand Down