From 24070b370a3adbf0dc2985f1e868955d1cd74c17 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 4 Oct 2021 18:36:52 +0100 Subject: [PATCH 1/6] Don't suppress exception chaining for optional dependencies --- 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 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 From e0f2dc8ace35dfd2d0655ac4124c297e47e67b29 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 4 Oct 2021 21:00:30 +0100 Subject: [PATCH 2/6] Mention exception context in release notes --- doc/source/whatsnew/v1.4.0.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 0c841078fe9b4..308fe5350f52a 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -126,6 +126,7 @@ Other enhancements - Attempting to write into a file in missing parent directory with :meth:`DataFrame.to_csv`, :meth:`DataFrame.to_html`, :meth:`DataFrame.to_excel`, :meth:`DataFrame.to_feather`, :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata`, :meth:`DataFrame.to_json`, :meth:`DataFrame.to_pickle`, and :meth:`DataFrame.to_xml` now explicitly mentions missing parent directory, the same is true for :class:`Series` counterparts (:issue:`24306`) - :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`) +- The error raised when an optional dependency can't be imported now includes the original exception, for easier investigation. - .. --------------------------------------------------------------------------- @@ -142,7 +143,8 @@ These are bug fixes that might have notable behavior changes. Inconsistent date string parsing ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The ``dayfirst`` option of :func:`to_datetime` isn't strict, and this can lead to surprising behaviour: +The ``dayfirst`` option of :func:`to_datetime` isn't strict, and this can lead to sur +ising behaviour: .. ipython:: python :okwarning: From 2b39cbe266f5cfb862cd7cb5284b3feeee5cc004 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 4 Oct 2021 21:04:43 +0100 Subject: [PATCH 3/6] Test that original ImportError is preserved --- pandas/tests/test_optional_dependency.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/test_optional_dependency.py b/pandas/tests/test_optional_dependency.py index f75ee0d0ddd95..5ce1143c8247c 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(excinfo.value.__context__, ImportError) result = import_optional_dependency("notapackage", errors="ignore") assert result is None From ce9816bf43603a25c31eadbc2c69c027c0c4ceef Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 4 Oct 2021 21:17:45 +0100 Subject: [PATCH 4/6] Fix variable name in test --- pandas/tests/test_optional_dependency.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_optional_dependency.py b/pandas/tests/test_optional_dependency.py index 5ce1143c8247c..c1d1948d6c31a 100644 --- a/pandas/tests/test_optional_dependency.py +++ b/pandas/tests/test_optional_dependency.py @@ -16,7 +16,7 @@ def test_import_optional(): with pytest.raises(ImportError, match=match) as exc_info: import_optional_dependency("notapackage") # The original exception should be there as context: - assert isinstance(excinfo.value.__context__, ImportError) + assert isinstance(exc_info.value.__context__, ImportError) result = import_optional_dependency("notapackage", errors="ignore") assert result is None From 5092e52632417a564108a2ac99faa3cb145f9c02 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Tue, 5 Oct 2021 08:57:07 +0100 Subject: [PATCH 5/6] Undo accidental change --- doc/source/whatsnew/v1.4.0.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 308fe5350f52a..1876a03718264 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -143,8 +143,7 @@ These are bug fixes that might have notable behavior changes. Inconsistent date string parsing ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The ``dayfirst`` option of :func:`to_datetime` isn't strict, and this can lead to sur -ising behaviour: +The ``dayfirst`` option of :func:`to_datetime` isn't strict, and this can lead to surprising behaviour: .. ipython:: python :okwarning: From bf0fa35685a4f2c57337896e1a434ab9a4b398cf Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Tue, 5 Oct 2021 15:28:21 +0100 Subject: [PATCH 6/6] Add PR number in release note Co-authored-by: Thomas Li <47963215+lithomas1@users.noreply.github.com> --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index dcb2985065e66..a9d9ed5dab8de 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -127,7 +127,7 @@ 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. +- The error raised when an optional dependency can't be imported now includes the original exception, for easier investigation (:issue:`43882`) - .. ---------------------------------------------------------------------------