Skip to content

pandas export_to_excel (time stamp in tuple) #2294

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

Closed
jassinm opened this issue Nov 19, 2012 · 0 comments
Closed

pandas export_to_excel (time stamp in tuple) #2294

jassinm opened this issue Nov 19, 2012 · 0 comments
Milestone

Comments

@jassinm
Copy link

jassinm commented Nov 19, 2012

import pandas as pd
import StringIO
s = StringIO.StringIO(
"""Date,ticker,type,value
2001-01-01,x,close,12.2
2001-01-01,x,open ,12.1
2001-01-01,y,close,12.2
2001-01-01,y,open ,12.1
2001-02-01,x,close,12.2
2001-02-01,x,open ,12.1
2001-02-01,y,close,12.2
2001-02-01,y,open ,12.1
2001-03-01,x,close,12.2
2001-03-01,x,open ,12.1
2001-03-01,y,close,12.2
2001-03-01,y,open ,12.1""")
df = pd.read_csv(s, parse_dates=["Date"])
pdf = df.pivot_table(values="value", rows=["ticker"], cols=["Date", "type"])
pdf.to_excel('test.xlsx')

xlsx erros in

Traceback (most recent call last):
  File "ipython_log.py", line 19, in <module>
    pdf.to_excel('test.xlsx')
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/pandas-0.9.2.dev_c103897-py2.7-macosx-10.8-x86_64.egg/pandas/core/frame.py", line 1343, in to_excel
    index_label=index_label)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/pandas-0.9.2.dev_c103897-py2.7-macosx-10.8-x86_64.egg/pandas/core/frame.py", line 1198, in _helper_csvexcel
    writer.writerow(encoded_labels + encoded_cols)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/pandas-0.9.2.dev_c103897-py2.7-macosx-10.8-x86_64.egg/pandas/io/parsers.py", line 1932, in writerow
    self._writerow_xlsx(row, sheet_name)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/pandas-0.9.2.dev_c103897-py2.7-macosx-10.8-x86_64.egg/pandas/io/parsers.py", line 1977, in _writerow_xlsx
    sheet.append(conv_row)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/openpyxl/writer/dump_worksheet.py", line 238, in append
    cell = self._string_builder.add(cell)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/openpyxl/writer/strings.py", line 82, in add
    key = key.strip()
AttributeError: 'tuple' object has no attribute 'strip'

xls errors in

pdf.to_excel('test.xls')
Traceback (most recent call last):
  File "ipython_log.py", line 19, in <module>
    pdf.to_excel('test.xls')
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/pandas-0.9.2.dev_c103897-py2.7-macosx-10.8-x86_64.egg/pandas/core/frame.py", line 1343, in to_excel
    index_label=index_label)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/pandas-0.9.2.dev_c103897-py2.7-macosx-10.8-x86_64.egg/pandas/core/frame.py", line 1198, in _helper_csvexcel
    writer.writerow(encoded_labels + encoded_cols)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/pandas-0.9.2.dev_c103897-py2.7-macosx-10.8-x86_64.egg/pandas/io/parsers.py", line 1934, in writerow
    self._writerow_xls(row, sheet_name)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/pandas-0.9.2.dev_c103897-py2.7-macosx-10.8-x86_64.egg/pandas/io/parsers.py", line 1956, in _writerow_xls
    sheetrow.write(i, val)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/xlwt/Row.py", line 257, in write
    self.__rich_text_helper(col, label, style, style_index)
  File "/Volumes/Locodrive/Dev/.virtualenvs/pandasdev/lib/python2.7/site-packages/xlwt/Row.py", line 283, in __rich_text_helper
    raise Exception ("Unexpected data type %r" % type(data))
Exception: Unexpected data type <class 'pandas.lib.Timestamp'>

a Fix seams relatively easy . just test for tuple or MultiIndex and fix the output (similar to csv parser)

I was thinking of adding an ExcelFormatter similar to the HTMLFormatter which would merge cells , bold header....
I am currently using the to_html and then importing in excel but would like to have the ability to dump multiple dataframe in different tabs.

did anyone else start a branch? otherwise i would give it a go
Thanks

@jassinm jassinm mentioned this issue Nov 27, 2012
@wesm wesm closed this as completed Dec 2, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants