diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index dcd31abaa8857..a9d9ed5dab8de 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -127,7 +127,8 @@ Other enhancements - :meth:`IntegerArray.all` , :meth:`IntegerArray.any`, :meth:`FloatingArray.any`, and :meth:`FloatingArray.all` use Kleene logic (:issue:`41967`) - Added support for nullable boolean and integer types in :meth:`DataFrame.to_stata`, :class:`~pandas.io.stata.StataWriter`, :class:`~pandas.io.stata.StataWriter117`, and :class:`~pandas.io.stata.StataWriterUTF8` (:issue:`40855`) - :meth:`DataFrame.__pos__`, :meth:`DataFrame.__neg__` now retain ``ExtensionDtype`` dtypes (:issue:`43883`) - +- The error raised when an optional dependency can't be imported now includes the original exception, for easier investigation (:issue:`43882`) +- .. --------------------------------------------------------------------------- diff --git a/pandas/compat/_optional.py b/pandas/compat/_optional.py index 651729cd0ad44..adf20f3322a79 100644 --- a/pandas/compat/_optional.py +++ b/pandas/compat/_optional.py @@ -115,7 +115,7 @@ def import_optional_dependency( module = importlib.import_module(name) except ImportError: if errors == "raise": - raise ImportError(msg) from None + raise ImportError(msg) else: return None diff --git a/pandas/tests/test_optional_dependency.py b/pandas/tests/test_optional_dependency.py index f75ee0d0ddd95..c1d1948d6c31a 100644 --- a/pandas/tests/test_optional_dependency.py +++ b/pandas/tests/test_optional_dependency.py @@ -13,8 +13,10 @@ def test_import_optional(): match = "Missing .*notapackage.* pip .* conda .* notapackage" - with pytest.raises(ImportError, match=match): + with pytest.raises(ImportError, match=match) as exc_info: import_optional_dependency("notapackage") + # The original exception should be there as context: + assert isinstance(exc_info.value.__context__, ImportError) result = import_optional_dependency("notapackage", errors="ignore") assert result is None