Skip to content

df.to_excel() error.Unsupported type <type 'pandas._libs.interval.Interval'> in write() #19242

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
fcfangcc opened this issue Jan 15, 2018 · 1 comment · Fixed by #19244
Closed
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue IO Excel read_excel, to_excel
Milestone

Comments

@fcfangcc
Copy link

With pandas version 0.19.*,code can run normally.

But in pandas0.20+,the following error will be displayed.

Code

import pandas as pd
import numpy as np

print(pd.__version__)
df = pd.DataFrame(np.random.random((20, 2)))
df['new'] = pd.cut(df[0], 10)
df.to_excel('test.xlsx')

Error description

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/fc/virtual/rock_env/lib/python2.7/site-packages/pandas/core/frame.py", line 1424, in to_excel
    engine=engine)
  File "/Users/fc/virtual/rock_env/lib/python2.7/site-packages/pandas/io/formats/excel.py", line 628, in write
    freeze_panes=freeze_panes)
  File "/Users/fc/virtual/rock_env/lib/python2.7/site-packages/pandas/io/excel.py", line 1600, in write_cells
    val, style)
  File "/Users/fc/virtual/rock_env/lib/python2.7/site-packages/xlsxwriter/worksheet.py", line 65, in cell_wrapper
    return method(self, *args, **kwargs)
  File "/Users/fc/virtual/rock_env/lib/python2.7/site-packages/xlsxwriter/worksheet.py", line 433, in write
    raise TypeError("Unsupported type %s in write()" % type(token))
TypeError: Unsupported type <type 'pandas._libs.interval.Interval'> in write()

Note: The same error in pandas0.21,0.20. Right in 0.19.*

Output of pd.show_versions()

commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Darwin
OS-release: 17.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.20.3
pytest: None
pip: 9.0.1
setuptools: 38.2.4
Cython: None
numpy: 1.13.3
scipy: 1.0.0
xarray: None
IPython: None
sphinx: None
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.0
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: 1.0.2
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None

@cbertinato
Copy link
Contributor

The Interval type was introduced in 0.20. Before then, it was just a string. The upcoming PR should fix it, but a quick fix is:

df['new'] = pd.cut(df[0], 10).astype(str)

though not desirable if one wants to keep the Interval type in the data frame. Note that this exception comes up only when labels are not assigned. So:

df['new'] = pd.cut(df[0], 10, labels=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'])

should also work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants