Skip to content

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

Closed
@pcorpet

Description

@pcorpet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions