Skip to content
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Bug in :meth:`Series.str.startswith` and :meth:`Series.str.endswith` with ``category`` dtype not propagating ``na`` parameter (:issue:`36241`)

.. ---------------------------------------------------------------------------

Expand Down
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
40 changes: 32 additions & 8 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]*)",), {}),
Expand Down Expand Up @@ -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,), {}),
Expand Down Expand Up @@ -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"])
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
Member 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
Member Author

Choose a reason for hiding this comment

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

@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(
Expand All @@ -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"])
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

@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(
Expand Down Expand Up @@ -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")
Expand Down