Skip to content

pandas export_to_excel (time stamp in tuple) #2294

Closed
@jassinm

Description

@jassinm
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions