From f2b7bd81ba1db8f8be61c902b60b4675c9c953ed Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Sat, 7 Jun 2025 17:14:23 +0800 Subject: [PATCH 1/3] improve optional import error message --- pandas/compat/_optional.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/compat/_optional.py b/pandas/compat/_optional.py index f01dfab0de829..07a07ba4ab60c 100644 --- a/pandas/compat/_optional.py +++ b/pandas/compat/_optional.py @@ -152,8 +152,8 @@ def import_optional_dependency( install_name = package_name if package_name is not None else name msg = ( - f"Missing optional dependency '{install_name}'. {extra} " - f"Use pip or conda to install {install_name}." + f"`Import {install_name}` failed. {extra} " + f"Use pip or conda to install the {install_name} package." ) try: module = importlib.import_module(name) From 627e510705cb9b07afbaa53888b43b70788eb939 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Sat, 7 Jun 2025 21:57:02 +0800 Subject: [PATCH 2/3] add fix on tests --- pandas/tests/io/test_fsspec.py | 2 +- pandas/tests/io/test_parquet.py | 2 +- pandas/tests/test_optional_dependency.py | 2 +- pandas/tests/util/test_numba.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/test_fsspec.py b/pandas/tests/io/test_fsspec.py index 2e3e74a9d31ff..8fb790b24b621 100644 --- a/pandas/tests/io/test_fsspec.py +++ b/pandas/tests/io/test_fsspec.py @@ -282,7 +282,7 @@ def test_s3_parquet(s3_public_bucket, s3so, df1): @td.skip_if_installed("fsspec") def test_not_present_exception(): - msg = "Missing optional dependency 'fsspec'|fsspec library is required" + msg = "`Import fsspec` failed. Use pip or conda to install the fsspec package." with pytest.raises(ImportError, match=msg): read_csv("memory://test/test.csv") diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py index 78f39b649cb9a..52f8d6b2f06d8 100644 --- a/pandas/tests/io/test_parquet.py +++ b/pandas/tests/io/test_parquet.py @@ -325,7 +325,7 @@ def test_get_engine_auto_error_message(): with pytest.raises(ImportError, match=match): get_engine("auto") else: - match = "Missing optional dependency .pyarrow." + match = "Import .pyarrow failed." with pytest.raises(ImportError, match=match): get_engine("auto") diff --git a/pandas/tests/test_optional_dependency.py b/pandas/tests/test_optional_dependency.py index 9127981d1845d..cd276914bfb21 100644 --- a/pandas/tests/test_optional_dependency.py +++ b/pandas/tests/test_optional_dependency.py @@ -12,7 +12,7 @@ def test_import_optional(): - match = "Missing .*notapackage.* pip .* conda .* notapackage" + match = "Import .*notapackage.* pip .* conda .* notapackage" with pytest.raises(ImportError, match=match) as exc_info: import_optional_dependency("notapackage") # The original exception should be there as context: diff --git a/pandas/tests/util/test_numba.py b/pandas/tests/util/test_numba.py index 27b68ff0f6044..b555240b3b657 100644 --- a/pandas/tests/util/test_numba.py +++ b/pandas/tests/util/test_numba.py @@ -7,6 +7,6 @@ @td.skip_if_installed("numba") def test_numba_not_installed_option_context(): - with pytest.raises(ImportError, match="Missing optional"): + with pytest.raises(ImportError, match="Import numba failed"): with option_context("compute.use_numba", True): pass From ccab26e73418ea3780d54ebceff02679ff03a0d1 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Thu, 12 Jun 2025 22:12:47 +0800 Subject: [PATCH 3/3] fix tests --- pandas/tests/io/test_parquet.py | 8 ++++---- pandas/tests/util/test_numba.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py index 52f8d6b2f06d8..d2515af58b5d3 100644 --- a/pandas/tests/io/test_parquet.py +++ b/pandas/tests/io/test_parquet.py @@ -325,7 +325,7 @@ def test_get_engine_auto_error_message(): with pytest.raises(ImportError, match=match): get_engine("auto") else: - match = "Import .pyarrow failed." + match = "Unable to find a usable engine; tried using: 'pyarrow'" with pytest.raises(ImportError, match=match): get_engine("auto") @@ -1133,9 +1133,9 @@ def test_string_inference(self, tmp_path, pa, using_infer_string): index=pd.Index(["a", "b"], dtype=dtype), columns=pd.Index( ["a"], - dtype=object - if pa_version_under19p0 and not using_infer_string - else dtype, + dtype=( + object if pa_version_under19p0 and not using_infer_string else dtype + ), ), ) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/util/test_numba.py b/pandas/tests/util/test_numba.py index b555240b3b657..5dc4fa96f15ae 100644 --- a/pandas/tests/util/test_numba.py +++ b/pandas/tests/util/test_numba.py @@ -7,6 +7,6 @@ @td.skip_if_installed("numba") def test_numba_not_installed_option_context(): - with pytest.raises(ImportError, match="Import numba failed"): + with pytest.raises(ImportError, match="`Import numba` failed"): with option_context("compute.use_numba", True): pass