Skip to content

Commit c928775

Browse files
DOC: Showcase writing comments in Styler.to_excel() (pandas-dev#58070)
Co-authored-by: diogomsmiranda <[email protected]>
1 parent f036b41 commit c928775

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pandas/core/generic.py

+2
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,7 @@ def _repr_data_resource_(self):
21312131
Arbitrary keyword arguments passed to excel engine.
21322132
"""
21332133
),
2134+
extra_examples="",
21342135
)
21352136
def to_excel(
21362137
self,
@@ -2262,6 +2263,7 @@ def to_excel(
22622263
automatically chosen depending on the file extension):
22632264
22642265
>>> df1.to_excel("output1.xlsx", engine="xlsxwriter") # doctest: +SKIP
2266+
{extra_examples}
22652267
"""
22662268
if engine_kwargs is None:
22672269
engine_kwargs = {}

pandas/io/formats/style.py

+31
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@
8282

8383
from pandas import ExcelWriter
8484

85+
try:
86+
import matplotlib as mpl
87+
import matplotlib.pyplot as plt
88+
89+
has_mpl = True
90+
except ImportError:
91+
has_mpl = False
92+
93+
import textwrap
94+
95+
96+
@contextmanager
97+
def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
98+
if has_mpl:
99+
yield plt, mpl
100+
else:
101+
raise ImportError(f"{func.__name__} requires matplotlib.")
102+
85103

86104
####
87105
# Shared Doc Strings
@@ -537,6 +555,19 @@ def set_tooltips(
537555
storage_options=_shared_docs["storage_options"],
538556
storage_options_versionadded="1.5.0",
539557
extra_parameters="",
558+
extra_examples=textwrap.dedent(
559+
"""\
560+
If you wish to write excel notes to the workbook, you can do so by
561+
passing a DataFrame to ``set_tooltips``. This process is independent
562+
from writing data to the workbook, therefore both DataFrames can have
563+
different dimensions.
564+
565+
>>> notes = pd.DataFrame(
566+
... [["cell 1", "cell 2"], ["cell 3", "cell 4"]],
567+
... )
568+
>>> df1.style.set_tooltips(notes).to_excel("output.xlsx") # doctest: +SKIP
569+
"""
570+
),
540571
)
541572
def to_excel(
542573
self,

0 commit comments

Comments
 (0)