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:`str.startswith` and :meth:`str.endswith` for Series with ``category`` dtype not propagating ``na`` parameter (:issue:`36241`)

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

Expand Down
34 changes: 24 additions & 10 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,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"])
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.

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))

# mixed
mixed = np.array(
Expand All @@ -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"])
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):
@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(
Expand Down Expand Up @@ -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")
Expand Down