Skip to content

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 10 commits into from
Sep 12, 2020
2 changes: 1 addition & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2050,7 +2050,7 @@ def wrapper2(self, pat, flags=0, **kwargs):
@forbid_nonstring_types(forbidden_types, name=name)
def wrapper3(self, pat, na=np.nan):
result = f(self._parent, pat, na=na)
return self._wrap_result(result, returns_string=returns_string)
return self._wrap_result(result, returns_string=returns_string, fill_value=na)

wrapper = wrapper3 if na else wrapper2 if flags else wrapper1

Expand Down
16 changes: 12 additions & 4 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,12 @@ 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"])
# add category dtype parametrizations for GH-36241
@pytest.mark.parametrize("dtype", [None, "category"])
Copy link
Contributor

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?

Copy link
Contributor Author

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def test_startswith(self, dtype):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would want to parameterize over different fill values to test the behavior from the OP

Copy link
Contributor Author

@asishm asishm Sep 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameterized over True, False

question here:

for string Series with dtype = object, passing strings into the na parameter of these two methods causes it to replace the nan with the string - but for StringArrays it replaces the nans with bool(na). The current fix behaves as the former.

In [49]: pd.Series(['a', np.nan, 'b']).str.startswith('a', na='c')
Out[49]:
0     True
1        c
2    False
dtype: object

In [50]: pd.Series(['a', np.nan, 'b'], dtype='string').str.startswith('a', na='c')
Out[50]:
0     True
1     True
2    False
dtype: boolean

In [53]: pd.Series(['a', np.nan, 'b'], dtype='category').str.startswith('a', na='c')
Out[53]: 
0     True
1        c
2    False
dtype: object

edits: copying the wrong cells!!

values = Series(
["om", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"], dtype=dtype
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also pls tests with pd.NA as well (you can parameterize over the null value as well

)

result = values.str.startswith("foo")
exp = Series([False, np.nan, True, False, False, np.nan, True])
Expand Down Expand Up @@ -867,8 +871,12 @@ 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"])
# add category dtype parametrizations for GH-36241
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add inside the test

@pytest.mark.parametrize("dtype", [None, "category"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

def test_endswith(self, dtype):
values = Series(
["om", np.nan, "foo_nom", "nom", "bar_foo", np.nan, "foo"], dtype=dtype
)

result = values.str.endswith("foo")
exp = Series([False, np.nan, False, False, True, np.nan, True])
Expand Down