Skip to content

Commit d123538

Browse files
committed
Added properly functioning case insensitive match without regex
pandas.core.strings.str_contains should now match case insensitivity properly if given regex=False and case=False. Also added test cases for case insensitive pandas.core.strings.str_contains with and without regex.
1 parent 9d064f3 commit d123538

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pandas/core/strings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ def str_contains(arr, pat, case=True, flags=0, na=np.nan, regex=True):
189189

190190
f = lambda x: bool(regex.search(x))
191191
else:
192-
f = lambda x: pat in x
192+
if case:
193+
f = lambda x: pat in x
194+
else:
195+
upper_pat = pat.upper()
196+
f = lambda x: upper_pat in x
197+
return _na_map(f, str_upper(arr), na, dtype=bool)
193198
return _na_map(f, arr, na, dtype=bool)
194199

195200

pandas/tests/test_strings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ def test_contains(self):
189189
self.assertEqual(result.dtype, np.bool_)
190190
tm.assert_almost_equal(result, expected)
191191

192+
# case insensitive using regex
193+
values = ['Foo', 'xYz', 'fOOomMm__fOo', 'MMM_']
194+
result = strings.str_contains(values, 'FOO|mmm', case=False)
195+
expected = [True, False, True, True]
196+
tm.assert_almost_equal(result, expected)
197+
198+
# case insensitive without regex
199+
result = strings.str_contains(values, 'foo', regex=False, case=False)
200+
expected = [True, False, True, False]
201+
tm.assert_almost_equal(result, expected)
202+
192203
# mixed
193204
mixed = ['a', NA, 'b', True, datetime.today(), 'foo', None, 1, 2.]
194205
rs = strings.str_contains(mixed, 'o')

0 commit comments

Comments
 (0)