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

Series.str.contains doesn't correct if series contains only nan values. #14171

ponomarevvl90 opened this issue Sep 7, 2016 · 3 comments


None yet
3 participants
Copy link

commented Sep 7, 2016

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
import re

value = '3'
t1 = pd.Series(data=[np.nan, np.nan, np.nan], dtype=np.object)
t2 = t1.str.contains(CONTAINS_REG_EXP.format(re.escape(value)),
                                  flags=re.U, na=False)
print t2

Expected Output

0    False
1    False
2    False
dtype: bool

output of pd.show_versions()


commit: None
python-bits: 64
OS: Linux
OS-release: 4.2.0-42-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None

pandas: 0.17.1
nose: None
pip: 8.0.3
setuptools: 18.0.1
Cython: 0.23.4
numpy: 1.10.1
scipy: None
statsmodels: None
IPython: 4.0.1
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.4.6
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: 2.6.1 (dt dec pq3 ext lo64)
Jinja2: None


This comment has been minimized.

Copy link

commented Sep 7, 2016

Here's a simpler example. This is casting only when na flag is passed.

In [20]: Series([np.nan, np.nan, np.nan],dtype='object').str.contains('foo')
0   NaN
1   NaN
2   NaN
dtype: float64

In [21]: Series([np.nan, np.nan, np.nan],dtype='object').str.contains('foo',na=False)
0    0.0
1    0.0
2    0.0
dtype: float64

In [22]: Series([np.nan, np.nan, np.nan],dtype='object').str.contains('foo',na=True)
0    1.0
1    1.0
2    1.0
dtype: float64

works fine if not all-nan object dtype.

In [23]: Series([np.nan, np.nan, np.nan, 'a'],dtype='object').str.contains('foo')
0      NaN
1      NaN
2      NaN
3    False
dtype: object

In [25]: Series([np.nan, np.nan, np.nan, 'a'],dtype='object').str.contains('foo', na=False)
0    False
1    False
2    False
3    False
dtype: bool

In [26]: Series([np.nan, np.nan, np.nan, 'a'],dtype='object').str.contains('foo', na=True)
0     True
1     True
2     True
3    False
dtype: bool

@jreback jreback added this to the Next Major Release milestone Sep 7, 2016


This comment has been minimized.

Copy link

commented Sep 7, 2016

pull-requests to fix are welcome!


This comment has been minimized.

Copy link

commented Sep 7, 2016

First time contributor here. I'll take this on.

@jreback jreback modified the milestones: 0.19.0, Next Major Release Sep 8, 2016

@jreback jreback closed this in 289cd6d Sep 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.