Skip to content

Commit bb5cf8e

Browse files
BUG: shows correct package name when import_optional_dependency is ca… (#36134) (#36199)
Co-authored-by: Harsh Sharma <[email protected]>
1 parent 89057f4 commit bb5cf8e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Bug fixes
4141
- Bug in :meth:`Series.dt.isocalendar` and :meth:`DatetimeIndex.isocalendar` that returned incorrect year for certain dates (:issue:`36032`)
4242
- Bug in :class:`DataFrame` indexing returning an incorrect :class:`Series` in some cases when the series has been altered and a cache not invalidated (:issue:`33675`)
4343
- Bug in :meth:`DataFrame.corr` causing subsequent indexing lookups to be incorrect (:issue:`35882`)
44+
- Bug in :meth:`import_optional_dependency` returning incorrect package names in cases where package name is different from import name (:issue:`35948`)
4445

4546
.. ---------------------------------------------------------------------------
4647

pandas/compat/_optional.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@
3333
"numba": "0.46.0",
3434
}
3535

36+
# A mapping from import name to package name (on PyPI) for packages where
37+
# these two names are different.
38+
39+
INSTALL_MAPPING = {
40+
"bs4": "beautifulsoup4",
41+
"bottleneck": "Bottleneck",
42+
"lxml.etree": "lxml",
43+
"odf": "odfpy",
44+
"pandas_gbq": "pandas-gbq",
45+
"sqlalchemy": "SQLAlchemy",
46+
"jinja2": "Jinja2",
47+
}
48+
3649

3750
def _get_version(module: types.ModuleType) -> str:
3851
version = getattr(module, "__version__", None)
@@ -82,9 +95,13 @@ def import_optional_dependency(
8295
is False, or when the package's version is too old and `on_version`
8396
is ``'warn'``.
8497
"""
98+
99+
package_name = INSTALL_MAPPING.get(name)
100+
install_name = package_name if package_name is not None else name
101+
85102
msg = (
86-
f"Missing optional dependency '{name}'. {extra} "
87-
f"Use pip or conda to install {name}."
103+
f"Missing optional dependency '{install_name}'. {extra} "
104+
f"Use pip or conda to install {install_name}."
88105
)
89106
try:
90107
module = importlib.import_module(name)

0 commit comments

Comments
 (0)