Skip to content

Fix for issue 62001; ENH: Context-aware error messages for optional dependencies #62003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

wilocu
Copy link

@wilocu wilocu commented Jul 30, 2025

#62001

Summary

This PR enhances import_optional_dependency() to provide context-aware error messages that suggest relevant alternatives when
dependencies are missing, addressing issue #62001.

Before:
Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl.

After:
Missing optional dependency 'openpyxl'. For Excel file operations, try installing xlsxwriter, calamine, xlrd, pyxlsb, or odfpy.
Use pip or conda to install openpyxl.

Implementation Details

  • Core Enhancement: Added operation_context parameter to import_optional_dependency() with 13 predefined contexts (excel,
    plotting, html, xml, sql, performance, compression, cloud, formats, computation, timezone, testing, development)
  • Smart Alternative Filtering: Excludes the failed dependency from suggestions to avoid confusion
  • Backward Compatibility: All existing calls work unchanged; new parameter is optional
  • Strategic Implementation: Updated high-impact locations where users commonly encounter missing dependencies:
    • Excel operations (5 readers: openpyxl, xlrd, calamine, pyxlsb, odf)
    • Plotting operations (matplotlib)
    • HTML parsing operations (html5lib)

Files Modified

  1. pandas/compat/_optional.py: Core enhancement with context mapping and message building
  2. pandas/tests/test_optional_dependency.py: Updated test patterns and added comprehensive context tests
  3. pandas/io/excel/_*.py: Added context to 5 Excel readers
  4. pandas/plotting/_core.py: Added plotting context
  5. pandas/io/html.py: Added HTML parsing context
  6. doc/source/whatsnew/v3.0.0.rst: Added whatsnew entry

Testing

  • All existing tests pass with updated patterns

  • New tests verify context functionality works correctly

  • Manual verification confirms all files compile successfully

  • Backward compatibility maintained for existing calls

  • closes #xxxx (Replace xxxx with the GitHub issue number)

  • Tests added and passed if fixing a bug or adding a new feature

  • All code checks passed.

  • Added type annotations to new arguments/methods/functions.

  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

Please make sure to double check I am still new to contributions and let me know if there are any mistakes.

@wilocu wilocu requested a review from rhshadrach as a code owner July 30, 2025 19:18
"description": "SQL database operations",
},
"performance": {
"alternatives": ["numexpr", "bottleneck", "numba"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are "alternatives" but only in a very loose sense. They are not alternatives for the same operations.

# Mapping of operation contexts to alternative dependencies
OPERATION_CONTEXTS = {
"excel": {
"alternatives": ["openpyxl", "xlsxwriter", "calamine", "xlrd", "pyxlsb", "odfpy"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These may be alternatives, but they may not be. They are not completely equivalent. If a user is doing .write_excel(..., engine="openpyxl"), I don't think we should be suggesting they try installing xlsxwriter.

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not fond of maintaining a separate mapping of optional dependencies. I would just suggest an error message providing a web link to our optional dependency documentation.

@mroeschke
Copy link
Member

Also I suspect this PR was AI generated so closing. We discourage heavily AI generated pull requests

@mroeschke mroeschke closed this Jul 30, 2025
@wilocu
Copy link
Author

wilocu commented Jul 31, 2025

It partially was, I appreciate the review, it makes sense, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants