Skip to content
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

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

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Loading