Skip to content

Commit cf75c28

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

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
@@ -329,6 +329,153 @@ def test_multiindex_interval_datetimes(self, tmp_excel):
329329
)
330330
tm.assert_frame_equal(result, expected)
331331

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

333480
@pytest.mark.parametrize(
334481
"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)