Skip to content
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

feat: support polars non-strict expand_selector #368

Merged
merged 4 commits into from
May 30, 2024
Merged

Conversation

machow
Copy link
Collaborator

@machow machow commented May 29, 2024

This PR adds support for polars expressions to be used in column selection in addition to polars selectors. It requires polars v0.20.30, but falls back to the old behavior (or requiring a selector object) otherwise.

Note that this is the result of a chain of things:

Which resulted in polars super quickly adding broader support for accepting Expressions and selectors that only result in column selection (and not e.g. transformation, or aliasing; PR pola-rs/polars#16479).

I tried to give reasonably helpful error messages for people who are not on v0.20.3 that use Expr, but also suspect people will upgrade polaras patch versions fairly quickly.

Example

from great_tables import exibble, GT

GT(pl.from_pandas(exibble)).tab_spanner("A", [pl.col("fctr"), pl.all().exclude("date")])

Note both that a list is supported along with the use of expressions (e.g. pl.col("fctr"))

Copy link

codecov bot commented May 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.11%. Comparing base (e3649d2) to head (f879854).
Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #368      +/-   ##
==========================================
+ Coverage   85.90%   86.11%   +0.21%     
==========================================
  Files          41       41              
  Lines        4328     4337       +9     
==========================================
+ Hits         3718     3735      +17     
+ Misses        610      602       -8     

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

@github-actions github-actions bot temporarily deployed to pr-368 May 29, 2024 19:11 Destroyed
@github-actions github-actions bot temporarily deployed to pr-368 May 29, 2024 19:36 Destroyed
@github-actions github-actions bot temporarily deployed to pr-368 May 29, 2024 19:40 Destroyed
@machow
Copy link
Collaborator Author

machow commented May 29, 2024

@jrycw if you have a min, do you mind taking a look? Hopefully this rounds out our polars column selection work!

@machow machow marked this pull request as ready for review May 29, 2024 19:41
@machow machow requested a review from rich-iannone May 29, 2024 19:41
Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

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

LGTM!

@jrycw
Copy link
Collaborator

jrycw commented May 30, 2024

@machow it's amazing how quickly Polars is supporting our use case. I need to grab a coffee and take a detailed look at this, as it might impact how users utilize it.

Copy link
Collaborator

@jrycw jrycw left a comment

Choose a reason for hiding this comment

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

@machow Overall, it looks good. We might need to update the documentation after merging this PR.

@@ -313,38 +329,64 @@ def _(data: PlDataFrame, expr: Union[list[str], _selector_proxy_], strict: bool
from operator import or_
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are these legacy imports?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm referring to the lines from functools import reduce and from operator import or_.

all_selectors = [
cs.by_name(x) if isinstance(x, str) else cs.by_index(x) if isinstance(x, int) else x
for x in expr
]

_validate_selector_list(all_selectors)
# validate all entries ----
_validate_selector_list(all_selectors, strict=False if expand_opts else True)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Personally, I would call _validate_selector_list(all_selectors, **expand_opts) directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, good catch!

@machow machow merged commit a2ecbbe into main May 30, 2024
13 checks passed
@rich-iannone rich-iannone deleted the feat-polars-sel-expr branch May 30, 2024 21:19
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