Skip to content
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

.str.contains(..., na=False) consistency between categorical and object series #22158

Closed
AnthonyGaruccio opened this issue Aug 1, 2018 · 2 comments · Fixed by #22170
Closed
Labels
Bug Categorical Categorical Data Type Strings String extension data type and string data
Milestone

Comments

@AnthonyGaruccio
Copy link

Code Sample, a copy-pastable example if possible

>>> import pandas as pd
>>> import numpy as np
>>> s = pd.Series(["a","b","c","a", np.nan], dtype="category")
>>> s
0      a
1      b
2      c
3      a
4    NaN
dtype: category
Categories (3, object): [a, b, c]
# `na=False` kwarg should make all missing values False, but this seems broken for categoricals 
>>> s.str.contains("a", na=False)                                                                                                                                                                                               
0     True
1    False
2    False
3     True
4      NaN
dtype: object
# compare that to object series
>>> s2 = pd.Series(["a","b","c","a", np.nan])
>>> s2
0      a
1      b
2      c
3      a
4    NaN
dtype: object
>>> s2.str.contains("a", na=False)
0     True
1    False
2    False
3     True
4    False
dtype: bool

Problem description

.str.contains(..., na=False) should make missing values False when the calling series is of type categorical just like it does for object series.

Expected Output

>>> s = pd.Series(["a","b","c","a", np.nan], dtype="category")
>>> s2 = pd.Series(["a","b","c","a", np.nan])
>>> (s.str.contains("a", na=False) == s2.str.contains("a", na=False)).all()
True

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.0.final.0
python-bits: 64
OS: Darwin
OS-release: 17.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.3
pytest: None
pip: 18.0
setuptools: 40.0.0
Cython: None
numpy: 1.15.0
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None

@TomAugspurger TomAugspurger added Bug Strings String extension data type and string data Categorical Categorical Data Type Effort Low good first issue and removed good first issue labels Aug 1, 2018
@TomAugspurger
Copy link
Contributor

I think it'll require updating StringMethods._wrap_result to be aware of how NA values should be handled. Might be a bit tricky, not sure ahead of time.

@pulkitmaloo
Copy link
Contributor

I'd like to work on this issue.

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 a pull request may close this issue.

4 participants