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

Conversation

asishm
Copy link
Contributor

@asishm asishm commented Sep 9, 2020

I parametrized dtype in existing tests. Should I create a separate test instead?

@dsaxton dsaxton added Bug Categorical Categorical Data Type Strings String extension data type and string data labels Sep 11, 2020
Copy link
Member

@dsaxton dsaxton left a comment

Choose a reason for hiding this comment

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

Thanks @asishm, can you add a 1.1.3 release note?

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"])
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!!

@pytest.mark.parametrize("dtype", [None, "category"])
def test_startswith(self, dtype):
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

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_endswith(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.

same as above

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

@jreback jreback added this to the 1.1.3 milestone Sep 12, 2020
@jreback jreback merged commit a9f8d3c into pandas-dev:master Sep 12, 2020
@jreback
Copy link
Contributor

jreback commented Sep 12, 2020

thanks @asishm

@asishm asishm deleted the strmethods branch September 13, 2020 04:04
@simonjayhawkins
Copy link
Member

@meeseeksdev backport 1.1.x

meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this pull request Sep 13, 2020
…d str.endswith not propagating for Series with categorical dtype
simonjayhawkins pushed a commit that referenced this pull request Sep 13, 2020
…with not propagating for Series with categorical dtype (#36331)

Co-authored-by: Asish Mahapatra <[email protected]>
kesmit13 pushed a commit to kesmit13/pandas that referenced this pull request Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: .str.startswith(..., na=False) consistency between categorical and string series (again)
4 participants