Skip to content

Cannot write MultiIndex DataFrame to excel (to_excel) which contains Peroid IntervalIndex #30986

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
kurtforrester opened this issue Jan 13, 2020 · 1 comment · Fixed by #31196
Labels
IO Excel read_excel, to_excel MultiIndex Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@kurtforrester
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np

midx = pd.MultiIndex.from_arrays(
    [
        np.repeat([1, 2], 2),
        pd.interval_range(start=pd.Timestamp("2020-01-01"), periods=4, freq="6M"),
    ]
)

df = pd.DataFrame(data=np.random.randint(0, 2, size=(4,)), index=midx)
print(df)

df.to_excel("tmp.xlsx")

Problem description

I receive a TypeError and the DataFrame does not write to file.

...
  File "pandas/_libs/tslibs/timestamps.pyx", line 412, in pandas._libs.tslibs.timestamps.Timestamp.__new__

  File "pandas/_libs/tslibs/conversion.pyx", line 329, in pandas._libs.tslibs.conversion.convert_to_tsobject

TypeError: Cannot convert input [True] of type <class 'bool'> to Timestamp

Expected Output

Expect the DataFrame to write to file. A DataFrame with a Period/IntervalIndex alone can be written to an excel file. Seems to be an issue with MultiIndex.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.7.4.final.0 python-bits : 64 OS : Linux OS-release : 5.4.7-200.fc31.x86_64 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_GB.UTF-8 LOCALE : en_GB.UTF-8

pandas : 0.25.3
numpy : 1.17.2
pytz : 2019.3
dateutil : 2.8.1
pip : 19.2.3
setuptools : 41.4.0
Cython : 0.29.13
pytest : 5.2.1
hypothesis : 4.54.2
sphinx : 2.2.0
blosc : None
feather : None
xlsxwriter : 1.2.1
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.8.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : 1.3.1
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : 2.7.0
odfpy : None
openpyxl : 3.0.0
pandas_gbq : None
pyarrow : 0.13.0
pytables : None
s3fs : None
scipy : 1.3.2
sqlalchemy : 1.3.9
tables : 3.6.1
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.1

@jschendel
Copy link
Member

Thanks for the report. I can confirm that this was broken on 0.25.3 but it looks like it's been fixed on master as well as on the release candidate 1.0.0rc0.

It doesn't look any fixes are necessary but could use some tests to prevent a regression in the future since I don't immediately see any tests that are related to this issue.

@jschendel jschendel added IO Excel read_excel, to_excel MultiIndex Needs Tests Unit test(s) needed to prevent regressions labels Jan 14, 2020
@jschendel jschendel added this to the Contributions Welcome milestone Jan 14, 2020
@simonjayhawkins simonjayhawkins modified the milestones: Contributions Welcome, 1.1 Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO Excel read_excel, to_excel MultiIndex Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants