Skip to content

Commit 40f6ff5

Browse files
twoertweinmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#38819: REGR: read_excel does not work for most file handles
1 parent 6c01ced commit 40f6ff5

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

doc/source/whatsnew/v1.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Fixed regressions
1919
- Bug in repr of float-like strings of an ``object`` dtype having trailing 0's truncated after the decimal (:issue:`38708`)
2020
- Fixed regression in :meth:`DataFrame.groupby()` with :class:`Categorical` grouping column not showing unused categories for ``grouped.indices`` (:issue:`38642`)
2121
- Fixed regression in :meth:`DataFrame.any` and :meth:`DataFrame.all` not returning a result for tz-aware ``datetime64`` columns (:issue:`38723`)
22+
- :func:`read_excel` does not work for non-rawbyte file handles (issue:`38788`)
2223
- Bug in :meth:`read_csv` with ``float_precision="high"`` caused segfault or wrong parsing of long exponent strings (:issue:`38753`)
2324
-
2425

pandas/io/excel/_base.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1051,16 +1051,11 @@ def __init__(
10511051

10521052
xlrd_version = LooseVersion(xlrd.__version__)
10531053

1054-
if isinstance(path_or_buffer, (BufferedIOBase, RawIOBase, bytes)):
1055-
ext = inspect_excel_format(
1056-
content=path_or_buffer, storage_options=storage_options
1057-
)
1058-
elif xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
1054+
if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
10591055
ext = "xls"
10601056
else:
1061-
# path_or_buffer is path-like, use stringified path
10621057
ext = inspect_excel_format(
1063-
path=str(self._io), storage_options=storage_options
1058+
content=path_or_buffer, storage_options=storage_options
10641059
)
10651060

10661061
if engine is None:

pandas/tests/io/excel/test_readers.py

+16
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,22 @@ def test_read_from_s3_url(self, read_ext, s3_resource, s3so):
657657
local_table = pd.read_excel("test1" + read_ext)
658658
tm.assert_frame_equal(url_table, local_table)
659659

660+
def test_read_from_s3_object(self, read_ext, s3_resource, s3so):
661+
# GH 38788
662+
# Bucket "pandas-test" created in tests/io/conftest.py
663+
with open("test1" + read_ext, "rb") as f:
664+
s3_resource.Bucket("pandas-test").put_object(Key="test1" + read_ext, Body=f)
665+
666+
import s3fs
667+
668+
s3 = s3fs.S3FileSystem(**s3so)
669+
670+
with s3.open("s3://pandas-test/test1" + read_ext) as f:
671+
url_table = pd.read_excel(f)
672+
673+
local_table = pd.read_excel("test1" + read_ext)
674+
tm.assert_frame_equal(url_table, local_table)
675+
660676
@pytest.mark.slow
661677
def test_read_from_file_url(self, read_ext, datapath):
662678

0 commit comments

Comments
 (0)