From 98767bfde7323486e5c4ade5cc67762763e9b4a5 Mon Sep 17 00:00:00 2001 From: Harsh Sharma Date: Sat, 5 Sep 2020 00:52:06 +0530 Subject: [PATCH 1/4] BUG: shows correct package name when import_optional_dependency is called. fixes #35948 --- pandas/compat/_optional.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/pandas/compat/_optional.py b/pandas/compat/_optional.py index 689c7c889ef66..ae792e85cbd15 100644 --- a/pandas/compat/_optional.py +++ b/pandas/compat/_optional.py @@ -33,6 +33,32 @@ "numba": "0.46.0", } +INSTALL_MAPPING = { + "bs4": "beautifulsoup4", + "bottleneck": "Bottleneck", + "fsspec": "fsspec", + "botocore": "botocore", + "fastparquet": "fastparquet", + "gcsfs": "gcsfs", + "lxml.etree": "lxml", + "matplotlib": "matplotlib", + "numexpr": "numexpr", + "odf": "odfpy", + "openpyxl": "openpyxl", + "pandas_gbq": "pandas-gbq", + "pyarrow": "pyarrow", + "pytest": "pytest", + "pyxlsb": "pyxlsb", + "scipy": "scipy", + "sqlalchemy": "SQLAlchemy", + "tables": "tables", + "tabulate": "tabulate", + "xarray": "xarray", + "xlrd": "xlrd", + "numba": "numba", + "jinja2": "Jinja2" +} + def _get_version(module: types.ModuleType) -> str: version = getattr(module, "__version__", None) @@ -82,9 +108,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) From 655febf7ef2522e4c501145e3f17bfb99f5a2fa0 Mon Sep 17 00:00:00 2001 From: Harsh Sharma <51477130+hs2361@users.noreply.github.com> Date: Sat, 5 Sep 2020 12:21:57 +0530 Subject: [PATCH 2/4] formatted according to black --- pandas/compat/_optional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/compat/_optional.py b/pandas/compat/_optional.py index ae792e85cbd15..88fcdca1f881f 100644 --- a/pandas/compat/_optional.py +++ b/pandas/compat/_optional.py @@ -56,7 +56,7 @@ "xarray": "xarray", "xlrd": "xlrd", "numba": "numba", - "jinja2": "Jinja2" + "jinja2": "Jinja2", } From 1830771250a8a7b711337d851860a1b7a0b8feaf Mon Sep 17 00:00:00 2001 From: Harsh Sharma <51477130+hs2361@users.noreply.github.com> Date: Sun, 6 Sep 2020 00:46:41 +0530 Subject: [PATCH 3/4] made changes as per review --- pandas/compat/_optional.py | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/pandas/compat/_optional.py b/pandas/compat/_optional.py index 88fcdca1f881f..40688a3978cfc 100644 --- a/pandas/compat/_optional.py +++ b/pandas/compat/_optional.py @@ -33,29 +33,16 @@ "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", - "fsspec": "fsspec", - "botocore": "botocore", - "fastparquet": "fastparquet", - "gcsfs": "gcsfs", "lxml.etree": "lxml", - "matplotlib": "matplotlib", - "numexpr": "numexpr", "odf": "odfpy", - "openpyxl": "openpyxl", "pandas_gbq": "pandas-gbq", - "pyarrow": "pyarrow", - "pytest": "pytest", - "pyxlsb": "pyxlsb", - "scipy": "scipy", "sqlalchemy": "SQLAlchemy", - "tables": "tables", - "tabulate": "tabulate", - "xarray": "xarray", - "xlrd": "xlrd", - "numba": "numba", "jinja2": "Jinja2", } From 71f4bb60cded9067743f1cbabedf2349eb505653 Mon Sep 17 00:00:00 2001 From: Harsh Sharma <51477130+hs2361@users.noreply.github.com> Date: Sun, 6 Sep 2020 00:52:14 +0530 Subject: [PATCH 4/4] updated whatsnew v1.1.2 according to review --- doc/source/whatsnew/v1.1.2.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.2.rst b/doc/source/whatsnew/v1.1.2.rst index 232d0c4b4bbcd..f74725583e98f 100644 --- a/doc/source/whatsnew/v1.1.2.rst +++ b/doc/source/whatsnew/v1.1.2.rst @@ -34,6 +34,7 @@ Bug fixes - Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should be ``""`` (:issue:`35712`) - Bug in :meth:`Float64Index.__contains__` incorrectly raising ``TypeError`` instead of returning ``False`` (:issue:`35788`) - 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:`import_optional_dependency` returning incorrect package names in cases where package name is different from import name (:issue:`35948`) .. ---------------------------------------------------------------------------