Skip to content

Conversation

@misha-artyugin
Copy link

What does this PR do?

Adds support for passing a predicate function (Callable[[str], bool]) to the sheet_name parameter of
pl.read_excel, allowing flexible sheet selection.

Why?

In practice, Excel files often contain sheets with dynamically generated names (e.g. dates) or a variable number of sheets. A predicate-based parameter allows users to select sheets programmatically without relying on
exact string matches.

Changes

  • Extended sheet_name to accept a callable predicate
  • Applied the predicate to available sheet names during selection
  • Added tests covering predicate-based sheet filtering

Backward compatibility

This change is fully backward compatible. Existing usages of
sheet_name (string, list of strings, etc.) are unaffected.

Tests

  • All Excel-related tests pass locally

…arameter of `polars.read_excel`

The `sheet_name` parameter now supports predicate function (`Callable[[str], bool]`) for flexible  sheet selection.  This is useful when sheet names are generated dynamically (e.g. by date) or when the number of sheets varies between files.
This change is fully backward compatible.
@github-actions github-actions bot added A-io-spreadsheet Area: reading/writing Excel/ODS files enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Dec 26, 2025
@misha-artyugin misha-artyugin marked this pull request as draft December 26, 2025 11:20
@misha-artyugin misha-artyugin marked this pull request as ready for review December 26, 2025 11:29
@codecov
Copy link

codecov bot commented Dec 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.83%. Comparing base (eb9f101) to head (343570e).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25882      +/-   ##
==========================================
- Coverage   80.62%   79.83%   -0.80%     
==========================================
  Files        1765     1763       -2     
  Lines      243158   242869     -289     
  Branches     3044     3044              
==========================================
- Hits       196055   193885    -2170     
- Misses      46321    48203    +1882     
+ Partials      782      781       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@alexander-beedie alexander-beedie left a comment

Choose a reason for hiding this comment

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

Lint needs to pass (appears to be failing in the commit tests).
Otherwise this would be fine by me, thanks 👍

msg = f"cannot specify both `sheet_name` ({sheet_name!r}) and `sheet_id` ({sheet_id!r})"
raise ValueError(msg)

if isinstance(sheet_name, Callable): # rewrite sheet_name to a list of names
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn’t this be

Suggested change
if isinstance(sheet_name, Callable): # rewrite sheet_name to a list of names
if callable(sheet_name): # rewrite sheet_name to a list of names

Copy link
Author

Choose a reason for hiding this comment

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

Hi,
I’ve addressed the requested changes.
Thanks and Happy New Year!

@misha-artyugin
Copy link
Author

All requested changes have been addressed.
CI is green.
Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-io-spreadsheet Area: reading/writing Excel/ODS files enhancement New feature or an improvement of an existing feature python Related to Python Polars

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants