-
Notifications
You must be signed in to change notification settings - Fork 94
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: add Series.__contains__
#1480
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e36d4ed
feat: add Series.__contains__
FBruzzesi e6d9419
use pa.scalar with explicit type
FBruzzesi 9a90632
use pa.scalar with explicit type
FBruzzesi 3aa5b75
use pa.scalar with explicit type
FBruzzesi c653e48
Merge branch 'main' into feat/series-contains
FBruzzesi 3f66045
I got fooled
FBruzzesi c9f0b36
Merge branch 'main' into feat/series-contains
FBruzzesi b2b005a
use pandas-like equals, raise custom error, deal with arrow scalar
FBruzzesi 38c5833
arrow None
FBruzzesi 634c262
add ArrowInvalid
FBruzzesi 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 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 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 |
---|---|---|
|
@@ -1021,6 +1021,31 @@ def __iter__(self: Self) -> Iterator[Any]: | |
for x in self._native_series.__iter__() | ||
) | ||
|
||
def __contains__(self: Self, other: Any) -> bool: | ||
from pyarrow import ArrowInvalid # ignore-banned-imports | ||
from pyarrow import ArrowNotImplementedError # ignore-banned-imports | ||
from pyarrow import ArrowTypeError # ignore-banned-imports | ||
|
||
try: | ||
import pyarrow as pa # ignore-banned-imports | ||
import pyarrow.compute as pc # ignore-banned-imports | ||
|
||
native_series = self._native_series | ||
other_ = ( | ||
pa.scalar(other) | ||
if other is not None | ||
else pa.scalar(None, type=native_series.type) | ||
) | ||
return maybe_extract_py_scalar( # type: ignore[no-any-return] | ||
pc.is_in(other_, native_series), | ||
return_py_scalar=True, | ||
) | ||
except (ArrowInvalid, ArrowNotImplementedError, ArrowTypeError) as exc: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be bare exception? |
||
from narwhals.exceptions import InvalidOperationError | ||
|
||
msg = f"Unable to compare other of type {type(other)} with series of type {self.dtype}." | ||
raise InvalidOperationError(msg) from exc | ||
|
||
@property | ||
def shape(self: Self) -> tuple[int]: | ||
return (len(self._native_series),) | ||
|
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -53,6 +53,7 @@ lint.select = [ | |
] | ||
lint.ignore = [ | ||
"A001", | ||
"A004", | ||
"ARG002", | ||
"ANN401", | ||
"C901", | ||
|
This file contains 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 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,44 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
import narwhals.stable.v1 as nw | ||
from narwhals.exceptions import InvalidOperationError | ||
|
||
if TYPE_CHECKING: | ||
from tests.utils import ConstructorEager | ||
|
||
data = [100, 200, None] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("other", "expected"), [(100, True), (None, True), (1, False), (100.314, False)] | ||
) | ||
def test_contains( | ||
constructor_eager: ConstructorEager, | ||
other: int | None, | ||
expected: bool, # noqa: FBT001 | ||
) -> None: | ||
s = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] | ||
|
||
assert (other in s) == expected | ||
|
||
|
||
@pytest.mark.parametrize("other", ["foo", [1, 2, 3]]) | ||
def test_contains_invalid_type( | ||
request: pytest.FixtureRequest, | ||
constructor_eager: ConstructorEager, | ||
other: Any, | ||
) -> None: | ||
if "polars" not in str(constructor_eager) and "pyarrow_table" not in str( | ||
constructor_eager | ||
): | ||
request.applymarker(pytest.mark.xfail) | ||
|
||
s = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"] | ||
|
||
with pytest.raises(InvalidOperationError): | ||
_ = other in s |
This file contains 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 |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
"value_counts", | ||
"zip_with", | ||
"__iter__", | ||
"__contains__", | ||
} | ||
BASE_DTYPES = { | ||
"NumericType", | ||
|
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.
Ok something must have gone wrong with the pre-commit sync :|
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'm so confused π