Skip to content

Commit f74a186

Browse files
eshirvanajreback
andauthoredJul 9, 2022
BUG: Add storage_option parameter to to_excel method in Styler (pandas-dev#46491)
Co-authored-by: Jeff Reback <jeff@reback.net>
1 parent 5506476 commit f74a186

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed
 

‎pandas/core/generic.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2132,7 +2132,11 @@ def _repr_data_resource_(self):
21322132
# I/O Methods
21332133

21342134
@final
2135-
@doc(klass="object", storage_options=_shared_docs["storage_options"])
2135+
@doc(
2136+
klass="object",
2137+
storage_options=_shared_docs["storage_options"],
2138+
storage_options_versionadded="1.2.0",
2139+
)
21362140
def to_excel(
21372141
self,
21382142
excel_writer,
@@ -2218,7 +2222,7 @@ def to_excel(
22182222
is to be frozen.
22192223
{storage_options}
22202224
2221-
.. versionadded:: 1.2.0
2225+
.. versionadded:: {storage_options_versionadded}
22222226
22232227
See Also
22242228
--------

‎pandas/io/formats/style.py

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Level,
3030
QuantileInterpolation,
3131
Scalar,
32+
StorageOptions,
3233
WriteBuffer,
3334
)
3435
from pandas.compat._optional import import_optional_dependency
@@ -550,6 +551,7 @@ def set_tooltips(
550551
NDFrame.to_excel,
551552
klass="Styler",
552553
storage_options=_shared_docs["storage_options"],
554+
storage_options_versionadded="1.5.0",
553555
)
554556
def to_excel(
555557
self,
@@ -569,6 +571,7 @@ def to_excel(
569571
inf_rep: str = "inf",
570572
verbose: bool = True,
571573
freeze_panes: tuple[int, int] | None = None,
574+
storage_options: StorageOptions = None,
572575
) -> None:
573576

574577
from pandas.io.formats.excel import ExcelFormatter
@@ -591,6 +594,7 @@ def to_excel(
591594
startcol=startcol,
592595
freeze_panes=freeze_panes,
593596
engine=engine,
597+
storage_options=storage_options,
594598
)
595599

596600
@overload

‎pandas/tests/io/excel/test_style.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import contextlib
2+
import time
23

34
import numpy as np
45
import pytest
56

6-
from pandas import DataFrame
7+
import pandas.util._test_decorators as td
8+
9+
from pandas import (
10+
DataFrame,
11+
read_excel,
12+
)
713
import pandas._testing as tm
814

915
from pandas.io.excel import ExcelWriter
@@ -206,3 +212,27 @@ def custom_converter(css):
206212

207213
with contextlib.closing(openpyxl.load_workbook(path)) as wb:
208214
assert wb["custom"].cell(2, 2).font.color.value == "00111222"
215+
216+
217+
@pytest.mark.single_cpu
218+
@td.skip_if_not_us_locale
219+
def test_styler_to_s3(s3_resource, s3so):
220+
# GH#46381
221+
222+
mock_bucket_name, target_file = "pandas-test", "test.xlsx"
223+
df = DataFrame({"x": [1, 2, 3], "y": [2, 4, 6]})
224+
styler = df.style.set_sticky(axis="index")
225+
styler.to_excel(f"s3://{mock_bucket_name}/{target_file}", storage_options=s3so)
226+
timeout = 5
227+
while True:
228+
if target_file in (
229+
obj.key for obj in s3_resource.Bucket("pandas-test").objects.all()
230+
):
231+
break
232+
time.sleep(0.1)
233+
timeout -= 0.1
234+
assert timeout > 0, "Timed out waiting for file to appear on moto"
235+
result = read_excel(
236+
f"s3://{mock_bucket_name}/{target_file}", index_col=0, storage_options=s3so
237+
)
238+
tm.assert_frame_equal(result, df)

0 commit comments

Comments
 (0)