Skip to content

REF: Avoid importing xlrd #57708

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

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

from pandas.core.dtypes.common import (
is_bool,
is_file_like,
is_float,
is_integer,
is_list_like,
Expand Down Expand Up @@ -1523,20 +1524,25 @@ def __init__(
# Always a string
self._io = stringify_path(path_or_buffer)

# Determine xlrd version if installed
if import_optional_dependency("xlrd", errors="ignore") is None:
xlrd_version = None
else:
import xlrd

xlrd_version = Version(get_version(xlrd))

if engine is None:
# Only determine ext if it is needed
ext: str | None
if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
ext = "xls"
else:
ext: str | None = None

if not isinstance(
path_or_buffer, (str, os.PathLike, ExcelFile)
) and not is_file_like(path_or_buffer):
# GH#56692 - avoid importing xlrd if possible
if import_optional_dependency("xlrd", errors="ignore") is None:
xlrd_version = None
else:
import xlrd

xlrd_version = Version(get_version(xlrd))

if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
ext = "xls"

if ext is None:
ext = inspect_excel_format(
content_or_path=path_or_buffer, storage_options=storage_options
)
Expand Down