Conversation
😂😂😂 |
Only a tiny dent into the 600, but ... #2962 (comment)
| def is_close( | ||
| self, | ||
| other: Self | NumericLiteral, | ||
| *, | ||
| abs_tol: float, | ||
| rel_tol: float, | ||
| nans_equal: bool, | ||
| ) -> Self: | ||
| return self._reuse_series( | ||
| "is_close", | ||
| other=other, | ||
| abs_tol=abs_tol, | ||
| rel_tol=rel_tol, | ||
| nans_equal=nans_equal, | ||
| ) |
There was a problem hiding this comment.
This might not be needed 🤔
narwhals/_utils.py
Outdated
| return deep_attrgetter(name_1, *nested)(obj) | ||
|
|
||
|
|
||
| def _is_close_impl( |
There was a problem hiding this comment.
In principle, we could use this implementation also for polars pre 1.32.0, however polars has a couple of native methods which play a bit nicer (e.g. .sign, .is_infinite, .not_). If we were to introduce those, then we can have a single implementation function
There was a problem hiding this comment.
not_ and __invert__ are identical btw
| """ | ||
| if abs_tol < 0: | ||
| msg = f"`abs_tol` must be non-negative but got {abs_tol}" | ||
| raise ComputeError(msg) | ||
|
|
||
| if not (0 <= rel_tol < 1): | ||
| msg = f"`rel_tol` must be in the range [0, 1) but got {rel_tol}" | ||
| raise ComputeError(msg) |
There was a problem hiding this comment.
For lazy backends, we are not raising for non-numeric dtypes
|
@dangotbanned I am quite happy with the state of this. I left a couple of comments. Main blocker is typing. In a way I feel like mypy it's not picking up the obvious, but clearly I am not doing something as it's expected to be done 😂 |
|
I've got 3 different ideas for fixing the typing, each with varying trade-offs I'll try to write them up when I get a chance later today/tomorrow |
@dangotbanned look at 22e2203 🙏🏼 |
Bad news: that didn't fix the typing 😭 Good news: I still have 3 other ideas 😄 |
Idea 1 - Use a constrained
|
|
Hey @dangotbanned thanks for the amazing writeup in #2962 (comment) It's kind of re-assuring that I somehow thought at a 2/3 of those ideas.
This was my first attempt, and yes you are correct that it leads to the issues with the return type not being For better or for worse, I retained from using cast, and preferred adding one
This was indeed working, yet personally I am not too keen in triggering an extra ...which bring us to
which I couldn't dream of 🤯 It works smoothly (I assume), maybe a little verbose, yet possibly it's the preferred solution? What's you opinion? |
aha thanks and I agree great to see we're on the same page 😄
Yeah that would be my preference! The verbosity is only temporary though, as it gave me an idea for a follow-up. We could move quite a lot into a shared So the end result would be shrinking both protocols, removing But we can save that part for another PR (provided it sounds good to you) |
Co-authored-by: Dan Redding <125183946+dangotbanned@users.noreply.github.com>
There was a problem hiding this comment.
Looking pretty good, thanks @FBruzzesi
Only a few notes/suggestions from me 🙂
| >>> s.is_close(1.4, abs_tol=0.1).to_native() # doctest:+ELLIPSIS | ||
| <pyarrow.lib.ChunkedArray object at ...> | ||
| [ | ||
| [ |
There was a problem hiding this comment.
😂 that's the only way to use it in doctest, maybe it's an idea?
|
Thanks for the additional simplification @dangotbanned I addressed the
in e4b7a1e and figured out how katex wants |
I'll look now! I'm pretty sure the rest was good, but will do a thrice over 😄 |
|
No luck for me in VSCode
but it seems to be the same story with https://narwhals-dev.github.io/narwhals/api-reference/expr/#narwhals.Expr.ewm_mean 😂
|
Expr|Series.is_close{Expr,Series}.is_close
{Expr,Series}.is_close{Expr,Series}.is_close
|
thanks both, great feature! |
Now that #2962 has merged, this part of the plan is possible (#2962 (comment))




What type of PR is this? (check all applicable)
Related issues
is_close#2911Checklist
If you have comments or can explain your changes, please do so below
Reason for "help wanted" label:
_is_close_implis almost possible to avoid any code duplication. All we need is already in narwhals, and no backend has this natively implemented (I think). However.... typing issues (which I didn't spend too much time on as of now but will give it a try)test_is_close_expr_with_scalarwith other being a finite scalar andnans_equal=Truestill returning True when the column value is nan.It's been a long day and I couldn't point down the reason. It could be that logical operators are not supposed to be used with scalar booleans. If that's the case, the implementation might need to be split up between eager and lazy.Logic is now fixed 😇Also I keep submitting PRs which as 600 line changes with one functionality implemented 😩