Skip to content

FIX str.match uses na flag #6611

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 1 commit into from
Mar 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ Bug Fixes
- Disabled clipboard tests until release time (run locally with ``nosetests -A disabled`` (:issue:`6048`).
- Bug in ``DataFrame.replace()`` when passing a nested ``dict`` that contained
keys not in the values to be replaced (:issue:`6342`)
- ``str.match`` ignored the na flag (:issue:`6609`).
- Bug in take with duplicate columns not consolidated (:issue:`6240`)
- Bug in interpolate changing dtypes (:issue:`6290`)
- Bug in Series.get, was using a buggy access method (:issue:`6383`)
Expand Down
13 changes: 9 additions & 4 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,11 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan, as_indexer=False):
# Do this first, to make sure it happens even if the re.compile
# raises below.
warnings.warn("In future versions of pandas, match will change to"
" always return a bool indexer.""", UserWarning)
" always return a bool indexer.", UserWarning)

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

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

return _na_map(f, arr)
return _na_map(f, arr, na)


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

@copy(str_match)
def match(self, pat, case=True, flags=0, na=np.nan, as_indexer=False):
result = str_match(self.series, pat, case=case, flags=flags,
na=na, as_indexer=as_indexer)
return self._wrap_result(result)

@copy(str_replace)
def replace(self, pat, repl, n=-1, case=True, flags=0):
result = str_replace(self.series, pat, repl, n=n, case=case,
Expand Down Expand Up @@ -951,7 +957,6 @@ def get_dummies(self, sep='|'):
startswith = _pat_wrapper(str_startswith, na=True)
endswith = _pat_wrapper(str_endswith, na=True)
findall = _pat_wrapper(str_findall, flags=True)
match = _pat_wrapper(str_match, flags=True)
extract = _pat_wrapper(str_extract, flags=True)

len = _noarg_wrapper(str_len)
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_contains(self):
# na
values = Series(['om', 'foo',np.nan])
res = values.str.contains('foo', na="foo")
self.assertEqual (res.ix[2], "foo" )
self.assertEqual (res.ix[2], "foo")

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

# na GH #6609
res = Series(['a', 0, np.nan]).str.match('a', na=False)
exp = Series([True, False, False])
assert_series_equal(exp, res)
res = Series(['a', 0, np.nan]).str.match('a')
exp = Series([True, np.nan, np.nan])
assert_series_equal(exp, res)

def test_extract(self):
# Contains tests like those in test_match and some others.

Expand Down