Skip to content

BUG: Fix render as column name in to_excel #34335

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

Merged
Show file tree
Hide file tree
Changes from 4 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/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ I/O
- Bug in :meth:`~DataFrame.read_feather` was raising an `ArrowIOError` when reading an s3 or http file path (:issue:`29055`)
- Bug in :meth:`read_parquet` was raising a ``FileNotFoundError`` when passed an s3 directory path. (:issue:`26388`)
- Bug in :meth:`~DataFrame.to_parquet` was throwing an ``AttributeError`` when writing a partitioned parquet file to s3 (:issue:`27596`)
- Bug in :meth:`~DataFrame.to_excel` could not handle the column name `render` and was raising an ``KeyError`` (:issue:`34331`)

Plotting
^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pandas.core.dtypes.common import is_float, is_scalar
from pandas.core.dtypes.generic import ABCIndex

from pandas import Index, MultiIndex, PeriodIndex
from pandas import DataFrame, Index, MultiIndex, PeriodIndex
import pandas.core.common as com

from pandas.io.common import stringify_path
Expand Down Expand Up @@ -385,7 +385,7 @@ def __init__(
):
self.rowcounter = 0
self.na_rep = na_rep
if hasattr(df, "render"):
if hasattr(df, "render") and not isinstance(df, DataFrame):
self.styler = df
df = df.data
if style_converter is None:
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/io/excel/test_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ def test_write_append_mode(ext, mode, expected):
assert wb2.worksheets[index]["A1"].value == cell_value


def test_to_excel_with_openpyxl_engine(ext, tmpdir):
@pytest.mark.parametrize("col", ["B", "render"])
def test_to_excel_with_openpyxl_engine(ext, tmpdir, col):
# GH 29854
df1 = DataFrame({"A": np.linspace(1, 10, 10)})
df2 = DataFrame({"B": np.linspace(1, 20, 10)})
df2 = DataFrame({col: np.linspace(1, 20, 10)})
df = pd.concat([df1, df2], axis=1)
styled = df.style.applymap(
lambda val: "color: %s" % ("red" if val < 0 else "black")
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,14 @@ def test_write_lists_dict(self, path):

tm.assert_frame_equal(read, expected)

def test_render_as_column_name(self, path):
# see gh-34331
df = DataFrame({"render": [1, 2], "data": [3, 4]})
df.to_excel(path, "Sheet1")
read = pd.read_excel(path, "Sheet1", index_col=0)
expected = df
tm.assert_frame_equal(read, expected)

def test_true_and_false_value_options(self, path):
# see gh-13347
df = pd.DataFrame([["foo", "bar"]], columns=["col1", "col2"])
Expand Down