-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 5 commits
e9a418d
1a5cf4f
2f90e6b
54e486e
70b6e3c
44d386e
5e3be5a
d8e5bb7
1467a0a
9d0070c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,19 +842,22 @@ def test_contains_for_object_category(self): | |
expected = Series([True, False, False, True, False]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# add category dtype parametrizations for GH-36241 | ||
@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. we want to parameterize over 'string' dtype as well rigth? 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. string arrays are already tested here 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 commentThe reason will be displayed to describe this comment to others. Learn more. @jreback tried to do that changing the referenced tests slightly with |
||
def test_startswith(self, dtype): | ||
@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", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"], dtype=dtype | ||
["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) | ||
tm.assert_series_equal(result, exp.fillna(na).astype(bool)) | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# mixed | ||
mixed = np.array( | ||
|
@@ -871,19 +878,22 @@ def test_startswith(self, dtype): | |
) | ||
tm.assert_series_equal(rs, xp) | ||
|
||
# add category dtype parametrizations for GH-36241 | ||
@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 |
||
def test_endswith(self, dtype): | ||
@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", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"], dtype=dtype | ||
["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) | ||
tm.assert_series_equal(result, exp.fillna(na).astype(bool)) | ||
|
||
# mixed | ||
mixed = np.array( | ||
|
@@ -3560,6 +3570,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") | ||
|
Uh oh!
There was an error while loading. Please reload this page.