-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: na parameter for str.startswith and str.endswith not propagating for Series with categorical dtype #36249
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e9a418d
add breaking test
asishm 1a5cf4f
propagate na to _wrap_result
asishm 2f90e6b
add string, fill value and null value parametrize
asishm 54e486e
add na=True/False param to string tests; parametrize over na and null…
asishm 70b6e3c
Merge branch 'master' of github.com:pandas-dev/pandas into strmethods
asishm 44d386e
add whatsnew note
asishm 5e3be5a
black
asishm d8e5bb7
Update doc/source/whatsnew/v1.1.3.rst
asishm 1467a0a
make expected series explicit
asishm 9d0070c
Merge branch 'strmethods' of github.com:asishm/pandas into strmethods
asishm 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 |
---|---|---|
|
@@ -29,6 +29,8 @@ def assert_series_or_index_equal(left, right): | |
("decode", ("UTF-8",), {}), | ||
("encode", ("UTF-8",), {}), | ||
("endswith", ("a",), {}), | ||
("endswith", ("a",), {"na": True}), | ||
("endswith", ("a",), {"na": False}), | ||
("extract", ("([a-z]*)",), {"expand": False}), | ||
("extract", ("([a-z]*)",), {"expand": True}), | ||
("extractall", ("([a-z]*)",), {}), | ||
|
@@ -58,6 +60,8 @@ def assert_series_or_index_equal(left, right): | |
("split", (" ",), {"expand": False}), | ||
("split", (" ",), {"expand": True}), | ||
("startswith", ("a",), {}), | ||
("startswith", ("a",), {"na": True}), | ||
("startswith", ("a",), {"na": False}), | ||
# translating unicode points of "a" to "d" | ||
("translate", ({97: 100},), {}), | ||
("wrap", (2,), {}), | ||
|
@@ -838,15 +842,23 @@ def test_contains_for_object_category(self): | |
expected = Series([True, False, False, True, False]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_startswith(self): | ||
values = Series(["om", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"]) | ||
@pytest.mark.parametrize("dtype", [None, "category"]) | ||
@pytest.mark.parametrize("null_value", [None, np.nan, pd.NA]) | ||
@pytest.mark.parametrize("na", [True, False]) | ||
def test_startswith(self, dtype, null_value, na): | ||
# add category dtype parametrizations for GH-36241 | ||
values = Series( | ||
["om", null_value, "foo_nom", "nom", "bar_foo", null_value, "foo"], | ||
dtype=dtype, | ||
) | ||
|
||
result = values.str.startswith("foo") | ||
exp = Series([False, np.nan, True, False, False, np.nan, True]) | ||
tm.assert_series_equal(result, exp) | ||
|
||
result = values.str.startswith("foo", na=True) | ||
tm.assert_series_equal(result, exp.fillna(True).astype(bool)) | ||
result = values.str.startswith("foo", na=na) | ||
exp = Series([False, na, True, False, False, na, True]) | ||
tm.assert_series_equal(result, exp) | ||
|
||
# mixed | ||
mixed = np.array( | ||
|
@@ -867,15 +879,23 @@ def test_startswith(self): | |
) | ||
tm.assert_series_equal(rs, xp) | ||
|
||
def test_endswith(self): | ||
values = Series(["om", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"]) | ||
@pytest.mark.parametrize("dtype", [None, "category"]) | ||
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. same as above |
||
@pytest.mark.parametrize("null_value", [None, np.nan, pd.NA]) | ||
@pytest.mark.parametrize("na", [True, False]) | ||
def test_endswith(self, dtype, null_value, na): | ||
# add category dtype parametrizations for GH-36241 | ||
values = Series( | ||
["om", null_value, "foo_nom", "nom", "bar_foo", null_value, "foo"], | ||
dtype=dtype, | ||
) | ||
|
||
result = values.str.endswith("foo") | ||
exp = Series([False, np.nan, False, False, True, np.nan, True]) | ||
tm.assert_series_equal(result, exp) | ||
|
||
result = values.str.endswith("foo", na=False) | ||
tm.assert_series_equal(result, exp.fillna(False).astype(bool)) | ||
result = values.str.endswith("foo", na=na) | ||
exp = Series([False, na, False, False, True, na, True]) | ||
tm.assert_series_equal(result, exp) | ||
|
||
# mixed | ||
mixed = np.array( | ||
|
@@ -3552,6 +3572,10 @@ def test_string_array(any_string_method): | |
assert result.dtype == "boolean" | ||
result = result.astype(object) | ||
|
||
elif expected.dtype == "bool": | ||
assert result.dtype == "boolean" | ||
result = result.astype("bool") | ||
|
||
elif expected.dtype == "float" and expected.isna().any(): | ||
assert result.dtype == "Int64" | ||
result = result.astype("float") | ||
|
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.
we want to parameterize over 'string' dtype as well rigth?
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.
string arrays are already tested here
https://github.com/pandas-dev/pandas/blob/1.1.x/pandas/tests/test_strings.py#L3530-L3531
parametrizing over string dtype and na=True/False was making it a bit tricky as these methods return boolean series causing a dtype mismatch (boolean vs object)
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.
@jreback tried to do that changing the referenced tests slightly with
https://github.com/pandas-dev/pandas/pull/36249/files#diff-683f8dacd08abda4913680fafe3a4ea7R3573-R3576 , https://github.com/pandas-dev/pandas/pull/36249/files#diff-683f8dacd08abda4913680fafe3a4ea7R32-R33 and https://github.com/pandas-dev/pandas/pull/36249/files#diff-683f8dacd08abda4913680fafe3a4ea7R63-R64