-
Notifications
You must be signed in to change notification settings - Fork 168
feat: Add {Expr,Series}.is_close
#2962
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
02a746e
WIP: Pyarrow series
FBruzzesi 69907eb
pandas
FBruzzesi fd4a3cf
merge main
FBruzzesi 7be5176
fix pyarrow case
FBruzzesi 1df7bf2
polars series and expr
FBruzzesi 0b4de26
move implementation to compliant level
FBruzzesi c6ce52c
help typing
FBruzzesi 89c947b
lazy almost there
FBruzzesi a271a94
simplify
FBruzzesi e59d8e7
coverage
FBruzzesi 148c8bc
docstring examples
FBruzzesi ad9be73
merge main
FBruzzesi 4756371
fixing nan's issue
FBruzzesi cf8f83d
test: trim some fat π
dangotbanned 6bb54e9
Merge remote-tracking branch 'upstream/main' into feat/is-close
dangotbanned 22e2203
fix typing
FBruzzesi dccd612
chore(typing): un-hide typing issues
dangotbanned 0dfaf5a
Dan's idea 3 in practice
FBruzzesi 8a1cb13
Add note on abs typing
FBruzzesi df24a41
fix(typing): resolve polars issues
dangotbanned 5838826
refactor(suggestion): simplify polars
dangotbanned 602fa29
Merge remote-tracking branch 'upstream/main' into feat/is-close
dangotbanned e4b7a1e
feedback adjustments
FBruzzesi d61c5c7
fix katex issue with \text
FBruzzesi 8d3532f
Merge branch 'main' into feat/is-close
dangotbanned File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| - filter | ||
| - clip | ||
| - is_between | ||
| - is_close | ||
| - is_duplicated | ||
| - is_finite | ||
| - is_first_distinct | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ | |
| - hist | ||
| - implementation | ||
| - is_between | ||
| - is_close | ||
| - is_duplicated | ||
| - is_empty | ||
| - is_finite | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any, Protocol | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing_extensions import Self | ||
|
|
||
| from narwhals.typing import NumericLiteral, TemporalLiteral | ||
|
|
||
|
|
||
| class IsClose(Protocol): | ||
| """Every member defined is a dependency of `is_close` method.""" | ||
|
|
||
| def __and__(self, other: Any) -> Self: ... | ||
| def __or__(self, other: Any) -> Self: ... | ||
| def __invert__(self) -> Self: ... | ||
| def __sub__(self, other: Any) -> Self: ... | ||
| def __mul__(self, other: Any) -> Self: ... | ||
| def __eq__(self, other: Self | Any) -> Self: ... # type: ignore[override] | ||
| def __gt__(self, other: Any) -> Self: ... | ||
| def __le__(self, other: Any) -> Self: ... | ||
| def abs(self) -> Self: ... | ||
| def is_nan(self) -> Self: ... | ||
| def is_finite(self) -> Self: ... | ||
| def clip( | ||
| self, | ||
| lower_bound: Self | NumericLiteral | TemporalLiteral | None, | ||
| upper_bound: Self | NumericLiteral | TemporalLiteral | None, | ||
| ) -> Self: ... | ||
| def is_close( | ||
| self, | ||
| other: Self | NumericLiteral, | ||
| *, | ||
| abs_tol: float, | ||
| rel_tol: float, | ||
| nans_equal: bool, | ||
| ) -> Self: | ||
dangotbanned marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from decimal import Decimal | ||
|
|
||
| other_abs: Self | NumericLiteral | ||
| other_is_nan: Self | bool | ||
| other_is_inf: Self | bool | ||
| other_is_not_inf: Self | bool | ||
|
|
||
| if isinstance(other, (float, int, Decimal)): | ||
| from math import isinf, isnan | ||
|
|
||
| other_abs = other.__abs__() | ||
FBruzzesi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| other_is_nan = isnan(other) | ||
| other_is_inf = isinf(other) | ||
|
|
||
| # Define the other_is_not_inf variable to prevent triggering the following warning: | ||
| # > DeprecationWarning: Bitwise inversion '~' on bool is deprecated and will be | ||
| # > removed in Python 3.16. | ||
| other_is_not_inf = not other_is_inf | ||
|
|
||
| else: | ||
| other_abs, other_is_nan = other.abs(), other.is_nan() | ||
| other_is_not_inf = other.is_finite() | other_is_nan | ||
| other_is_inf = ~other_is_not_inf | ||
|
|
||
| rel_threshold = self.abs().clip(lower_bound=other_abs, upper_bound=None) * rel_tol | ||
| tolerance = rel_threshold.clip(lower_bound=abs_tol, upper_bound=None) | ||
|
|
||
| self_is_nan = self.is_nan() | ||
| self_is_not_inf = self.is_finite() | self_is_nan | ||
|
|
||
| # Values are close if abs_diff <= tolerance, and both finite | ||
| is_close = ( | ||
| ((self - other).abs() <= tolerance) & self_is_not_inf & other_is_not_inf | ||
| ) | ||
|
|
||
| # Handle infinity cases: infinities are close/equal if they have the same sign | ||
| self_sign, other_sign = self > 0, other > 0 | ||
| is_same_inf = (~self_is_not_inf) & other_is_inf & (self_sign == other_sign) | ||
|
|
||
| # Handle nan cases: | ||
| # * If any value is NaN, then False (via `& ~either_nan`) | ||
| # * However, if `nans_equals = True` and if _both_ values are NaN, then True | ||
| either_nan = self_is_nan | other_is_nan | ||
| result = (is_close | is_same_inf) & ~either_nan | ||
|
|
||
| if nans_equal: | ||
| both_nan = self_is_nan & other_is_nan | ||
| result = result | both_nan | ||
|
|
||
| return result | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This might not be needed π€