Skip to content

Commit af354c3

Browse files
authored
REF: Avoid importing xlrd (#57708)
1 parent 2d7df18 commit af354c3

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

pandas/io/excel/_base.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
from pandas.core.dtypes.common import (
4545
is_bool,
46+
is_file_like,
4647
is_float,
4748
is_integer,
4849
is_list_like,
@@ -1523,20 +1524,25 @@ def __init__(
15231524
# Always a string
15241525
self._io = stringify_path(path_or_buffer)
15251526

1526-
# Determine xlrd version if installed
1527-
if import_optional_dependency("xlrd", errors="ignore") is None:
1528-
xlrd_version = None
1529-
else:
1530-
import xlrd
1531-
1532-
xlrd_version = Version(get_version(xlrd))
1533-
15341527
if engine is None:
15351528
# Only determine ext if it is needed
1536-
ext: str | None
1537-
if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
1538-
ext = "xls"
1539-
else:
1529+
ext: str | None = None
1530+
1531+
if not isinstance(
1532+
path_or_buffer, (str, os.PathLike, ExcelFile)
1533+
) and not is_file_like(path_or_buffer):
1534+
# GH#56692 - avoid importing xlrd if possible
1535+
if import_optional_dependency("xlrd", errors="ignore") is None:
1536+
xlrd_version = None
1537+
else:
1538+
import xlrd
1539+
1540+
xlrd_version = Version(get_version(xlrd))
1541+
1542+
if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
1543+
ext = "xls"
1544+
1545+
if ext is None:
15401546
ext = inspect_excel_format(
15411547
content_or_path=path_or_buffer, storage_options=storage_options
15421548
)

0 commit comments

Comments
 (0)