Skip to content

BUG: Typing: pd.ExcelWriter cannot accept an io.BytesIO instance as first arg #58424

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
3 tasks done
pcorpet opened this issue Apr 25, 2024 · 0 comments · Fixed by #58422
Closed
3 tasks done

BUG: Typing: pd.ExcelWriter cannot accept an io.BytesIO instance as first arg #58424

pcorpet opened this issue Apr 25, 2024 · 0 comments · Fixed by #58422
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@pcorpet
Copy link
Contributor

pcorpet commented Apr 25, 2024

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

excel_data = io.BytesIO()
with pd.ExcelWriter(excel_data, engine="openpyxl", mode="w") as writer:
    pd.DataFrame({"a": [1, 2], "b": [3, 4]}).to_excel(writer, sheet_name="Example", index=False)

Issue Description

The code above writes a DataFrame in an ExcelFile directly in memory, in a io.BytesIO. The code actually works and does what it's expected to do, however the typing fails.

This is because the first argument of pd.DataFrame expects path: FilePath | WriteExcelBuffer | ExcelWriter, however
the truncate function's signature differs in WriteExcelBuffer and io.BytesIO:

- def truncate(self, size: int | None = ...) -> int:
+ def truncate(self, size: int | None = ..., /) -> int:

Expected Behavior

This should pass type checks in mypy or PyLance.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.11.8.final.0
python-bits : 64
OS : Darwin
OS-release : 23.4.0
Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.5.1
pip : 23.2.1
Cython : None
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.2.0
lxml.etree : 5.2.1
html5lib : None
pymysql : None
psycopg2 : 2.9.9
jinja2 : 3.1.3
IPython : 8.23.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.3.1
gcsfs : None
matplotlib : 3.8.4
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.0
pandas_gbq : None
pyarrow : 15.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.3.1
scipy : 1.12.0
sqlalchemy : 2.0.29
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant