Skip to content

Commit

Permalink
BUG: shows correct package name when import_optional_dependency is ca… (
Browse files Browse the repository at this point in the history
  • Loading branch information
hs2361 authored and Kevin D Smith committed Nov 2, 2020
1 parent 2265cc4 commit 6f961e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Bug fixes
- Bug in :meth:`Series.dt.isocalendar` and :meth:`DatetimeIndex.isocalendar` that returned incorrect year for certain dates (:issue:`36032`)
- 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`)
- Bug in :meth:`DataFrame.corr` causing subsequent indexing lookups to be incorrect (:issue:`35882`)
- Bug in :meth:`import_optional_dependency` returning incorrect package names in cases where package name is different from import name (:issue:`35948`)

.. ---------------------------------------------------------------------------
Expand Down
21 changes: 19 additions & 2 deletions pandas/compat/_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@
"numba": "0.46.0",
}

# A mapping from import name to package name (on PyPI) for packages where
# these two names are different.

INSTALL_MAPPING = {
"bs4": "beautifulsoup4",
"bottleneck": "Bottleneck",
"lxml.etree": "lxml",
"odf": "odfpy",
"pandas_gbq": "pandas-gbq",
"sqlalchemy": "SQLAlchemy",
"jinja2": "Jinja2",
}


def _get_version(module: types.ModuleType) -> str:
version = getattr(module, "__version__", None)
Expand Down Expand Up @@ -82,9 +95,13 @@ def import_optional_dependency(
is False, or when the package's version is too old and `on_version`
is ``'warn'``.
"""

package_name = INSTALL_MAPPING.get(name)
install_name = package_name if package_name is not None else name

msg = (
f"Missing optional dependency '{name}'. {extra} "
f"Use pip or conda to install {name}."
f"Missing optional dependency '{install_name}'. {extra} "
f"Use pip or conda to install {install_name}."
)
try:
module = importlib.import_module(name)
Expand Down

0 comments on commit 6f961e6

Please sign in to comment.