Skip to content

Commit ecd10d8

Browse files
TST: Add test cases for note reading/writing (pandas-dev#58070)
Co-authored-by: Dacops <[email protected]>
1 parent 8deb2ef commit ecd10d8

File tree

9 files changed

+202
-1
lines changed

9 files changed

+202
-1
lines changed

pandas/io/excel/_odfreader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def get_sheet_data(
117117
if notes is not None:
118118
raise NotImplementedError(
119119
"""
120-
Notes are not supported in odfreader engine,
120+
Notes are not supported by the odfreader engine,
121121
see https://github.com/eea/odfpy
122122
"""
123123
)
Binary file not shown.
10.1 KB
Binary file not shown.

pandas/tests/io/excel/test_odf.py

+12
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ def test_read_cell_annotation():
7070
result = pd.read_excel("test_cell_annotation.ods")
7171

7272
tm.assert_frame_equal(result, expected)
73+
74+
75+
def test_exception_read_with_notes():
76+
with pytest.raises(
77+
NotImplementedError,
78+
match="""
79+
Notes are not supported by the odfreader engine,
80+
see https://github.com/eea/odfpy
81+
""",
82+
):
83+
df_notes = pd.DataFrame()
84+
pd.read_excel("test_unempty_cells.ods", notes=df_notes)

pandas/tests/io/excel/test_odswriter.py

+15
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,18 @@ def test_cell_value_type(
104104
cell = sheet_cells[0]
105105
assert cell.attributes.get((OFFICENS, "value-type")) == cell_value_type
106106
assert cell.attributes.get((OFFICENS, cell_value_attribute)) == cell_value
107+
108+
109+
def test_exception_write_with_notes(tmp_excel):
110+
with pytest.raises(
111+
NotImplementedError,
112+
match="""
113+
Notes are not supported by the odswriter engine,
114+
see https://github.com/mmulqueen/odswriter
115+
""",
116+
):
117+
notes = pd.DataFrame([["note 1", "note 2"], ["", "note 4"], ["note 5", ""]])
118+
119+
df = pd.DataFrame([[1, 2], [3, 4], [5, 6]])
120+
121+
df.style.set_tooltips(notes).to_excel(tmp_excel)

pandas/tests/io/excel/test_openpyxl.py

+9
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,12 @@ def test_read_multiindex_header_no_index_names(datapath, ext):
429429
index=pd.MultiIndex.from_tuples([("A", "AA", "AAA"), ("A", "BB", "BBB")]),
430430
)
431431
tm.assert_frame_equal(result, expected)
432+
433+
434+
def test_read_notes_from_xlsx_files(datapath, ext):
435+
path = datapath("io", "data", "excel", f"test_read_notes{ext}")
436+
expected = DataFrame([["note 1", "note 2"], ["", "note 4"], ["note 5", ""]])
437+
result = DataFrame()
438+
pd.read_excel(path, engine="openpyxl", notes=result)
439+
440+
tm.assert_frame_equal(result, expected)

pandas/tests/io/excel/test_writers.py

+147
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,153 @@ def test_multiindex_interval_datetimes(self, tmp_excel):
334334
)
335335
tm.assert_frame_equal(result, expected)
336336

337+
def test_read_write_with_notes(self, tmp_excel, ext):
338+
if ext in ["xlsm", "xlsx"]:
339+
expected = DataFrame(
340+
[
341+
["note 1", "note 2", "note 3"],
342+
["note 1", "note 2", "note 3"],
343+
["note 1", "note 2", "note 3"],
344+
]
345+
)
346+
df = DataFrame(
347+
[
348+
[1, 100, 200],
349+
[2, 200, 300],
350+
[3, 300, 400],
351+
]
352+
)
353+
df.style.set_tooltips(expected).to_excel(tmp_excel)
354+
result = DataFrame()
355+
result = pd.read_excel(tmp_excel, notes=result)
356+
tm.assert_frame_equal(result, expected)
357+
358+
def test_read_write_with_notes_trim_rows(self, tmp_excel, ext):
359+
if ext in ["xlsm", "xlsx"]:
360+
expected = DataFrame(
361+
[
362+
["", "", ""],
363+
["note 1", "note 2", "note3"],
364+
["", "", ""],
365+
]
366+
)
367+
df = DataFrame(
368+
[
369+
[1, 100, 200],
370+
[2, 200, 300],
371+
[3, 300, 400],
372+
]
373+
)
374+
df.style.set_tooltips(expected).to_excel(tmp_excel)
375+
result = DataFrame()
376+
result = pd.read_excel(tmp_excel, notes=result)
377+
tm.assert_frame_equal(result, expected)
378+
379+
def test_read_write_with_notes_trim_columns(self, tmp_excel, ext):
380+
if ext in ["xlsm", "xlsx"]:
381+
expected = DataFrame(
382+
[
383+
["", "note 2", ""],
384+
["", "note 2", ""],
385+
["", "note 2", ""],
386+
]
387+
)
388+
df = DataFrame(
389+
[
390+
[1, 100, 200],
391+
[2, 200, 300],
392+
[3, 300, 400],
393+
]
394+
)
395+
df.style.set_tooltips(expected).to_excel(tmp_excel)
396+
result = DataFrame()
397+
result = pd.read_excel(tmp_excel, notes=result)
398+
tm.assert_frame_equal(result, expected)
399+
400+
def test_read_write_with_notes_trim_rows_and_columns(self, tmp_excel, ext):
401+
if ext in ["xlsm", "xlsx"]:
402+
expected = DataFrame(
403+
[
404+
["", "", ""],
405+
["", "note 2", ""],
406+
["", "", ""],
407+
]
408+
)
409+
df = DataFrame(
410+
[
411+
[1, 100, 200],
412+
[2, 200, 300],
413+
[3, 300, 400],
414+
]
415+
)
416+
df.style.set_tooltips(expected).to_excel(tmp_excel)
417+
result = DataFrame()
418+
result = pd.read_excel(tmp_excel, notes=result)
419+
tm.assert_frame_equal(result, expected)
420+
421+
def test_read_write_with_notes_empty_comments_no_trim(self, tmp_excel, ext):
422+
if ext in ["xlsm", "xlsx"]:
423+
expected = DataFrame(
424+
[
425+
["note 1", "", ""],
426+
["", "", ""],
427+
["", "", "note 3"],
428+
]
429+
)
430+
df = DataFrame(
431+
[
432+
[1, 100, 200],
433+
[2, 200, 300],
434+
[3, 300, 400],
435+
]
436+
)
437+
df.style.set_tooltips(expected).to_excel(tmp_excel)
438+
result = DataFrame()
439+
result = pd.read_excel(tmp_excel, notes=result)
440+
tm.assert_frame_equal(result, expected)
441+
442+
def test_read_write_with_notes_smaller_dimensions(self, tmp_excel, ext):
443+
if ext in ["xlsm", "xlsx"]:
444+
expected = DataFrame(
445+
[
446+
["note 1", "note 2"],
447+
["note 1", "note 2"],
448+
]
449+
)
450+
df = DataFrame(
451+
[
452+
[1, 100, 200],
453+
[2, 200, 300],
454+
[3, 300, 400],
455+
]
456+
)
457+
df.style.set_tooltips(expected).to_excel(tmp_excel)
458+
result = DataFrame()
459+
result = pd.read_excel(tmp_excel, notes=result)
460+
tm.assert_frame_equal(result, expected)
461+
462+
def test_read_write_with_notes_bigger_dimensions(self, tmp_excel, ext):
463+
if ext in ["xlsm", "xlsx"]:
464+
expected = DataFrame(
465+
[
466+
["note 1", "note 2", "note 3", "note 4"],
467+
["note 1", "note 2", "note 3", "note 4"],
468+
["note 1", "note 2", "note 3", "note 4"],
469+
["note 1", "note 2", "note 3", "note 4"],
470+
]
471+
)
472+
df = DataFrame(
473+
[
474+
[1, 100, 200],
475+
[2, 200, 300],
476+
[3, 300, 400],
477+
]
478+
)
479+
df.style.set_tooltips(expected).to_excel(tmp_excel)
480+
result = DataFrame()
481+
result = pd.read_excel(tmp_excel, notes=result)
482+
tm.assert_frame_equal(result, expected)
483+
337484

338485
@pytest.mark.parametrize(
339486
"engine,ext",

pandas/tests/io/excel/test_xlrd.py

+14
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,17 @@ def test_read_old_xls_files(file_header):
6969
# GH 41226
7070
f = io.BytesIO(file_header)
7171
assert inspect_excel_format(f) == "xls"
72+
73+
74+
def test_read_notes_from_xls_files(datapath, read_ext_xlrd):
75+
expected = pd.DataFrame("", index=range(10), columns=range(10))
76+
77+
# Set specific values at (0, 0) and (10, 10)
78+
expected.iloc[0, 0] = "note 1x1"
79+
expected.iloc[9, 9] = "note 10x10"
80+
path = datapath("io", "data", "excel", f"test_read_notes{read_ext_xlrd}")
81+
result = pd.DataFrame()
82+
pd.read_excel(path, engine="xlrd", notes=result)
83+
84+
tm.assert_frame_equal(result, expected)
85+

pandas/tests/io/excel/test_xlsxwriter.py

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
from pandas.io.excel import ExcelWriter
99

10+
import pandas as pd
11+
12+
import pandas._testing as tm
13+
1014
xlsxwriter = pytest.importorskip("xlsxwriter")
1115

1216

0 commit comments

Comments
 (0)