-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: Add Expr.item
to strictly extract a single value from an expression
#24888
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
Conversation
Expr.single
to strictly extract a single value from an expressionExpr.single
to strictly extract a single value from an expression
py-polars/src/polars/expr/list.py
Outdated
Traceback (most recent call last): | ||
... ComputeError: cannot unpack single value from list of length 2 | ||
""" | ||
return wrap_expr(self._pyexpr.list_single()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think list.single()
can be implemented with the same perf as list.agg(pl.element().single())
.
Also fixes #21856 (the list variant) |
If we decide to go with single, I think we should rename |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #24888 +/- ##
==========================================
+ Coverage 81.76% 81.82% +0.05%
==========================================
Files 1700 1700
Lines 233868 234045 +177
Branches 2997 2997
==========================================
+ Hits 191219 191501 +282
+ Misses 41884 41779 -105
Partials 765 765 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@ritchie46 The inconvenient thing about |
ac02ad9
to
3870eeb
Compare
IRAggExpr::Implode(node) | ||
| IRAggExpr::First(node) | ||
| IRAggExpr::Last(node) | ||
| IRAggExpr::Single(node) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed optimization - Single
doesn't observe ordering - it only observes length.
It should be in a separate branch that just returns O::None
(i.e. no observable orderings, since we are returning a scalar in the Ok(_)
case).
I am not a huge fan of I like |
#24475 suggested the name |
The problem is that |
a5b3234
to
de4f8df
Compare
@MarcoGorelli It's more that there is a mismatch between |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, a few small changes.
Ai, this was merged a bit too early. :') Not a big deal, will work around it. |
List of all changes
Expr.item()
to select the only item from anExpr
. In other words: it is likeExpr.first()
but it will raise an error ifExpr
does not contain a single valueList.item()
, for listsAddDataFrame.item()
on dataframesAddSeries.item()
on seriesDeprecateDataFrame.item()
in favor ofDataFrame.single()
andDataFrame[row, col]
DeprecateSeries.item()
in favor ofSeries.single()
andSeries[index]
Review suggestions
Issingle
the correct name? Or should we consider another name?item()
Still Todo
Expr.item
docs that it does not take arguments, and that if the user wants item retrieval, they should useExpr.get
Fixes: #8689
Fixes: #21856
Related issue: #24475