-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
[ArrowStringArray] implement ArrowStringArray._str_contains #41025
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
jorisvandenbossche
merged 8 commits into
pandas-dev:master
from
simonjayhawkins:_str_contains
Apr 26, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5b8aca3
[ArrowStringArray] implement ArrowStringArray._str_contains
simonjayhawkins 6901807
Merge remote-tracking branch 'upstream/master' into _str_contains
simonjayhawkins 419f82e
Merge remote-tracking branch 'upstream/master' into _str_contains
simonjayhawkins 26719a1
add benchmark
simonjayhawkins 607c8ca
Merge remote-tracking branch 'upstream/master' into _str_contains
simonjayhawkins 9b8c404
add tests
simonjayhawkins 5f68797
handle na kwarg
simonjayhawkins 66251de
Merge remote-tracking branch 'upstream/master' into _str_contains
simonjayhawkins 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
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 |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
import numpy as np | ||
import pytest | ||
|
||
import pandas.util._test_decorators as td | ||
|
||
import pandas as pd | ||
from pandas import ( | ||
Index, | ||
|
@@ -12,79 +14,118 @@ | |
) | ||
|
||
|
||
def test_contains(): | ||
@pytest.fixture( | ||
params=[ | ||
"object", | ||
"string", | ||
pytest.param( | ||
"arrow_string", marks=td.skip_if_no("pyarrow", min_version="1.0.0") | ||
), | ||
] | ||
) | ||
def any_string_dtype(request): | ||
""" | ||
Parametrized fixture for string dtypes. | ||
* 'object' | ||
* 'string' | ||
* 'arrow_string' | ||
""" | ||
from pandas.core.arrays.string_arrow import ArrowStringDtype # noqa: F401 | ||
|
||
return request.param | ||
|
||
|
||
def test_contains(any_string_dtype): | ||
values = np.array( | ||
["foo", np.nan, "fooommm__foo", "mmm_", "foommm[_]+bar"], dtype=np.object_ | ||
) | ||
values = Series(values) | ||
values = Series(values, dtype=any_string_dtype) | ||
pat = "mmm[_]+" | ||
|
||
result = values.str.contains(pat) | ||
expected = Series(np.array([False, np.nan, True, True, False], dtype=np.object_)) | ||
expected_dtype = "object" if any_string_dtype == "object" else "boolean" | ||
expected = Series( | ||
np.array([False, np.nan, True, True, False], dtype=np.object_), | ||
dtype=expected_dtype, | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = values.str.contains(pat, regex=False) | ||
expected = Series(np.array([False, np.nan, False, False, True], dtype=np.object_)) | ||
expected = Series( | ||
np.array([False, np.nan, False, False, True], dtype=np.object_), | ||
dtype=expected_dtype, | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
values = Series(np.array(["foo", "xyz", "fooommm__foo", "mmm_"], dtype=object)) | ||
values = Series( | ||
np.array(["foo", "xyz", "fooommm__foo", "mmm_"], dtype=object), | ||
dtype=any_string_dtype, | ||
) | ||
result = values.str.contains(pat) | ||
expected = Series(np.array([False, False, True, True])) | ||
assert result.dtype == np.bool_ | ||
expected_dtype = np.bool_ if any_string_dtype == "object" else "boolean" | ||
expected = Series(np.array([False, False, True, True]), dtype=expected_dtype) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# case insensitive using regex | ||
values = Series(np.array(["Foo", "xYz", "fOOomMm__fOo", "MMM_"], dtype=object)) | ||
values = Series( | ||
np.array(["Foo", "xYz", "fOOomMm__fOo", "MMM_"], dtype=object), | ||
dtype=any_string_dtype, | ||
) | ||
result = values.str.contains("FOO|mmm", case=False) | ||
expected = Series(np.array([True, False, True, True])) | ||
expected = Series(np.array([True, False, True, True]), dtype=expected_dtype) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# case insensitive without regex | ||
result = Series(values).str.contains("foo", regex=False, case=False) | ||
expected = Series(np.array([True, False, True, False])) | ||
result = values.str.contains("foo", regex=False, case=False) | ||
expected = Series(np.array([True, False, True, False]), dtype=expected_dtype) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# mixed | ||
# unicode | ||
values = Series( | ||
np.array(["foo", np.nan, "fooommm__foo", "mmm_"], dtype=np.object_), | ||
dtype=any_string_dtype, | ||
) | ||
pat = "mmm[_]+" | ||
|
||
result = values.str.contains(pat) | ||
expected_dtype = "object" if any_string_dtype == "object" else "boolean" | ||
expected = Series( | ||
np.array([False, np.nan, True, True], dtype=np.object_), dtype=expected_dtype | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = values.str.contains(pat, na=False) | ||
expected_dtype = np.bool_ if any_string_dtype == "object" else "boolean" | ||
expected = Series(np.array([False, False, True, True]), dtype=expected_dtype) | ||
tm.assert_series_equal(result, expected) | ||
|
||
values = Series( | ||
np.array(["foo", "xyz", "fooommm__foo", "mmm_"], dtype=np.object_), | ||
dtype=any_string_dtype, | ||
) | ||
result = values.str.contains(pat) | ||
expected = Series(np.array([False, False, True, True]), dtype=expected_dtype) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_contains_object_mixed(): | ||
mixed = Series( | ||
np.array( | ||
["a", np.nan, "b", True, datetime.today(), "foo", None, 1, 2.0], | ||
dtype=object, | ||
) | ||
) | ||
rs = mixed.str.contains("o") | ||
xp = Series( | ||
result = mixed.str.contains("o") | ||
expected = Series( | ||
np.array( | ||
[False, np.nan, False, np.nan, np.nan, True, np.nan, np.nan, np.nan], | ||
dtype=np.object_, | ||
) | ||
) | ||
tm.assert_series_equal(rs, xp) | ||
|
||
rs = mixed.str.contains("o") | ||
xp = Series([False, np.nan, False, np.nan, np.nan, True, np.nan, np.nan, np.nan]) | ||
assert isinstance(rs, Series) | ||
tm.assert_series_equal(rs, xp) | ||
|
||
# unicode | ||
values = Series(np.array(["foo", np.nan, "fooommm__foo", "mmm_"], dtype=np.object_)) | ||
pat = "mmm[_]+" | ||
|
||
result = values.str.contains(pat) | ||
expected = Series(np.array([False, np.nan, True, True], dtype=np.object_)) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = values.str.contains(pat, na=False) | ||
expected = Series(np.array([False, False, True, True])) | ||
tm.assert_series_equal(result, expected) | ||
|
||
values = Series(np.array(["foo", "xyz", "fooommm__foo", "mmm_"], dtype=np.object_)) | ||
result = values.str.contains(pat) | ||
expected = Series(np.array([False, False, True, True])) | ||
assert result.dtype == np.bool_ | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_contains_for_object_category(): | ||
def test_contains_na_kwarg_for_object_category(): | ||
# gh 22158 | ||
|
||
# na for category | ||
|
@@ -108,6 +149,29 @@ def test_contains_for_object_category(): | |
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"na, expected", | ||
[ | ||
(None, pd.NA), | ||
(True, True), | ||
(False, False), | ||
(0, False), | ||
(3, True), | ||
(np.nan, pd.NA), | ||
], | ||
) | ||
@pytest.mark.parametrize("regex", [True, False]) | ||
def test_contains_na_kwarg_for_nullable_string_dtype( | ||
nullable_string_dtype, na, expected, regex | ||
): | ||
# https://github.com/pandas-dev/pandas/pull/41025#issuecomment-824062416 | ||
|
||
values = Series(["a", "b", "c", "a", np.nan], dtype=nullable_string_dtype) | ||
result = values.str.contains("a", na=na, regex=regex) | ||
expected = Series([True, False, False, True, expected], dtype="boolean") | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("dtype", [None, "category"]) | ||
@pytest.mark.parametrize("null_value", [None, np.nan, pd.NA]) | ||
@pytest.mark.parametrize("na", [True, False]) | ||
|
@@ -508,59 +572,73 @@ def _check(result, expected): | |
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_contains_moar(): | ||
def test_contains_moar(any_string_dtype): | ||
# PR #1179 | ||
s = Series(["A", "B", "C", "Aaba", "Baca", "", np.nan, "CABA", "dog", "cat"]) | ||
s = Series( | ||
["A", "B", "C", "Aaba", "Baca", "", np.nan, "CABA", "dog", "cat"], | ||
dtype=any_string_dtype, | ||
) | ||
|
||
result = s.str.contains("a") | ||
expected_dtype = "object" if any_string_dtype == "object" else "boolean" | ||
expected = Series( | ||
[False, False, False, True, True, False, np.nan, False, False, True] | ||
[False, False, False, True, True, False, np.nan, False, False, True], | ||
dtype=expected_dtype, | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = s.str.contains("a", case=False) | ||
expected = Series( | ||
[True, False, False, True, True, False, np.nan, True, False, True] | ||
[True, False, False, True, True, False, np.nan, True, False, True], | ||
dtype=expected_dtype, | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = s.str.contains("Aa") | ||
expected = Series( | ||
[False, False, False, True, False, False, np.nan, False, False, False] | ||
[False, False, False, True, False, False, np.nan, False, False, False], | ||
dtype=expected_dtype, | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = s.str.contains("ba") | ||
expected = Series( | ||
[False, False, False, True, False, False, np.nan, False, False, False] | ||
[False, False, False, True, False, False, np.nan, False, False, False], | ||
dtype=expected_dtype, | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = s.str.contains("ba", case=False) | ||
expected = Series( | ||
[False, False, False, True, True, False, np.nan, True, False, False] | ||
[False, False, False, True, True, False, np.nan, True, False, False], | ||
dtype=expected_dtype, | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_contains_nan(): | ||
def test_contains_nan(any_string_dtype): | ||
# PR #14171 | ||
s = Series([np.nan, np.nan, np.nan], dtype=np.object_) | ||
s = Series([np.nan, np.nan, np.nan], dtype=any_string_dtype) | ||
|
||
result = s.str.contains("foo", na=False) | ||
expected = Series([False, False, False], dtype=np.bool_) | ||
expected_dtype = np.bool_ if any_string_dtype == "object" else "boolean" | ||
expected = Series([False, False, False], dtype=expected_dtype) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = s.str.contains("foo", na=True) | ||
expected = Series([True, True, True], dtype=np.bool_) | ||
expected = Series([True, True, True], dtype=expected_dtype) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = s.str.contains("foo", na="foo") | ||
expected = Series(["foo", "foo", "foo"], dtype=np.object_) | ||
if any_string_dtype == "object": | ||
expected = Series(["foo", "foo", "foo"], dtype=np.object_) | ||
else: | ||
expected = Series([True, True, True], dtype="boolean") | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = s.str.contains("foo") | ||
expected = Series([np.nan, np.nan, np.nan], dtype=np.object_) | ||
expected_dtype = "object" if any_string_dtype == "object" else "boolean" | ||
expected = Series([np.nan, np.nan, np.nan], dtype=expected_dtype) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
|
@@ -609,14 +687,14 @@ def test_replace_moar(): | |
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_match_findall_flags(): | ||
def test_flags_kwarg(any_string_dtype): | ||
data = { | ||
"Dave": "[email protected]", | ||
"Steve": "[email protected]", | ||
"Rob": "[email protected]", | ||
"Wes": np.nan, | ||
} | ||
data = Series(data) | ||
data = Series(data, dtype=any_string_dtype) | ||
|
||
pat = r"([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})" | ||
|
||
|
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.
Uh oh!
There was an error while loading. Please reload this page.