Skip to content
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

BUG: writing dataframe to excel if one of the rows has name "render" fails #34331

Closed
dwintergruen opened this issue May 23, 2020 · 2 comments · Fixed by #34335
Closed

BUG: writing dataframe to excel if one of the rows has name "render" fails #34331

dwintergruen opened this issue May 23, 2020 · 2 comments · Fixed by #34335
Labels
Bug IO Excel read_excel, to_excel
Milestone

Comments

@dwintergruen
Copy link

I try to write an dataframe to excel with excel. It fails if a row is called "render".

Pandas version 1.0.3

Reproduce with:
s1 = pandas.Series(("1","2"),index=["a","b"])
s2 = pandas.Series(("3","4"),index=["b","b"])

df = pandas.DataFrame({"render":s1,"huhu":s2})
df.to_excel("/tmp/out.xlsx")

gives:

AttributeError Traceback (most recent call last)
in
----> 1 df.to_excel("/tmp/out.xlsx")

/usr/local/anaconda3/envs/exoplanets/lib/python3.7/site-packages/pandas/core/generic.py in to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep, verbose, freeze_panes)
2171 index_label=index_label,
2172 merge_cells=merge_cells,
-> 2173 inf_rep=inf_rep,
2174 )
2175 formatter.write(

/usr/local/anaconda3/envs/exoplanets/lib/python3.7/site-packages/pandas/io/formats/excel.py in init(self, df, na_rep, float_format, cols, header, index, index_label, merge_cells, inf_rep, style_converter)
384 if hasattr(df, "render"):
385 self.styler = df
--> 386 df = df.data
387 if style_converter is None:
388 style_converter = CSSToExcelConverter()

/usr/local/anaconda3/envs/exoplanets/lib/python3.7/site-packages/pandas/core/generic.py in getattr(self, name)
5272 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5273 return self[name]
-> 5274 return object.getattribute(self, name)
5275
5276 def setattr(self, name: str, value) -> None:

AttributeError: 'DataFrame' object has no attribute 'data'

@dwintergruen dwintergruen added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 23, 2020
@phofl
Copy link
Member

phofl commented May 23, 2020

I tested your example on master and got a different traceback

Traceback (most recent call last):
  File "/home/developer/.config/JetBrains/PyCharmCE2020.1/scratches/scratch_pandas.py", line 7, in <module>
    df = pd.DataFrame({"render":s1,"huhu":s2})
  File "/home/developer/PycharmProjects/pandas/pandas/core/frame.py", line 460, in __init__
    mgr = init_dict(data, index, columns, dtype=dtype)
  File "/home/developer/PycharmProjects/pandas/pandas/core/internals/construction.py", line 283, in init_dict
    return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "/home/developer/PycharmProjects/pandas/pandas/core/internals/construction.py", line 83, in arrays_to_mgr
    arrays = _homogenize(arrays, index, dtype)
  File "/home/developer/PycharmProjects/pandas/pandas/core/internals/construction.py", line 340, in _homogenize
    val = val.reindex(index, copy=False)
  File "/home/developer/PycharmProjects/pandas/pandas/core/series.py", line 4204, in reindex
    return super().reindex(index=index, **kwargs)
  File "/home/developer/PycharmProjects/pandas/pandas/core/generic.py", line 4482, in reindex
    axes, level, limit, tolerance, method, fill_value, copy
  File "/home/developer/PycharmProjects/pandas/pandas/core/generic.py", line 4505, in _reindex_axes
    allow_dups=False,
  File "/home/developer/PycharmProjects/pandas/pandas/core/generic.py", line 4550, in _reindex_with_indexers
    copy=copy,
  File "/home/developer/PycharmProjects/pandas/pandas/core/internals/managers.py", line 1255, in reindex_indexer
    self.axes[axis]._can_reindex(indexer)
  File "/home/developer/PycharmProjects/pandas/pandas/core/indexes/base.py", line 3235, in _can_reindex
    raise ValueError("cannot reindex from a duplicate axis")
ValueError: cannot reindex from a duplicate axis

Process finished with exit code 1

You have a small error in your example, You have to exchange the first b with an a in your second series.

import pandas as pd


s1 = pd.Series(("1","2"),index=["a","b"])
s2 = pd.Series(("3","4"),index=["a","b"])

df = pd.DataFrame({"render":s1,"huhu":s2})
df.to_excel("/tmp/out.xlsx")

@dwintergruen
Copy link
Author

dwintergruen commented May 23, 2020 via email

@jreback jreback added this to the 1.1 milestone May 26, 2020
@jreback jreback added IO Excel read_excel, to_excel and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants