Skip to content

Commit 95d6fb3

Browse files
committed
FIX str.match uses na flag
1 parent a265c5c commit 95d6fb3

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Bug Fixes
186186
- Disabled clipboard tests until release time (run locally with ``nosetests -A disabled`` (:issue:`6048`).
187187
- Bug in ``DataFrame.replace()`` when passing a nested ``dict`` that contained
188188
keys not in the values to be replaced (:issue:`6342`)
189+
- ``str.match`` ignored the na flag (:issue:`6609`).
189190
- Bug in take with duplicate columns not consolidated (:issue:`6240`)
190191
- Bug in interpolate changing dtypes (:issue:`6290`)
191192
- Bug in Series.get, was using a buggy access method (:issue:`6383`)

pandas/core/strings.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,11 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan, as_indexer=False):
364364
# Do this first, to make sure it happens even if the re.compile
365365
# raises below.
366366
warnings.warn("In future versions of pandas, match will change to"
367-
" always return a bool indexer.""", UserWarning)
367+
" always return a bool indexer.", UserWarning)
368368

369369
if as_indexer and regex.groups > 0:
370370
warnings.warn("This pattern has match groups. To actually get the"
371-
" groups, use str.extract.""", UserWarning)
371+
" groups, use str.extract.", UserWarning)
372372

373373
# If not as_indexer and regex.groups == 0, this returns empty lists
374374
# and is basically useless, so we will not warn.
@@ -384,7 +384,7 @@ def f(x):
384384
# This is the new behavior of str_match.
385385
f = lambda x: bool(regex.match(x))
386386

387-
return _na_map(f, arr)
387+
return _na_map(f, arr, na)
388388

389389

390390
def str_extract(arr, pat, flags=0):
@@ -887,6 +887,12 @@ def contains(self, pat, case=True, flags=0, na=np.nan, regex=True):
887887
na=na, regex=regex)
888888
return self._wrap_result(result)
889889

890+
@copy(str_match)
891+
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=False):
892+
result = str_match(self.series, pat, case=case, flags=flags,
893+
na=na, as_indexer=as_indexer)
894+
return self._wrap_result(result)
895+
890896
@copy(str_replace)
891897
def replace(self, pat, repl, n=-1, case=True, flags=0):
892898
result = str_replace(self.series, pat, repl, n=n, case=case,
@@ -951,7 +957,6 @@ def get_dummies(self, sep='|'):
951957
startswith = _pat_wrapper(str_startswith, na=True)
952958
endswith = _pat_wrapper(str_endswith, na=True)
953959
findall = _pat_wrapper(str_findall, flags=True)
954-
match = _pat_wrapper(str_match, flags=True)
955960
extract = _pat_wrapper(str_extract, flags=True)
956961

957962
len = _noarg_wrapper(str_len)

pandas/tests/test_strings.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def test_contains(self):
220220
# na
221221
values = Series(['om', 'foo',np.nan])
222222
res = values.str.contains('foo', na="foo")
223-
self.assertEqual (res.ix[2], "foo" )
223+
self.assertEqual (res.ix[2], "foo")
224224

225225
def test_startswith(self):
226226
values = Series(['om', NA, 'foo_nom', 'nom', 'bar_foo', NA, 'foo'])
@@ -460,6 +460,14 @@ def test_match(self):
460460
exp = Series([True, NA, False])
461461
tm.assert_series_equal(result, exp)
462462

463+
# na GH #6609
464+
res = Series(['a', 0, np.nan]).str.match('a', na=False)
465+
exp = Series([True, False, False])
466+
assert_series_equal(exp, res)
467+
res = Series(['a', 0, np.nan]).str.match('a')
468+
exp = Series([True, np.nan, np.nan])
469+
assert_series_equal(exp, res)
470+
463471
def test_extract(self):
464472
# Contains tests like those in test_match and some others.
465473

0 commit comments

Comments
 (0)