Skip to content

Commit 266a2e3

Browse files
committed
CLN: Minor touchups to docstrings + use assert_produces_warning
1 parent 3b832d0 commit 266a2e3

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

pandas/core/strings.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def str_contains(arr, pat, case=True, flags=0, na=np.nan):
170170
regex = re.compile(pat, flags=flags)
171171

172172
if regex.groups > 0:
173-
warnings.warn("""This pattern has match groups. To actually get the
174-
groups, use str.extract.""", UserWarning)
173+
warnings.warn("This pattern has match groups. To actually get the"
174+
" groups, use str.extract.", UserWarning)
175175

176176
f = lambda x: bool(regex.search(x))
177177
return _na_map(f, arr, na)
@@ -329,7 +329,7 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan, as_indexer=False):
329329
-------
330330
matches : boolean array (if as_indexer=True)
331331
matches : array of tuples (if as_indexer=False, default but deprecated)
332-
332+
333333
Note
334334
----
335335
To extract matched groups, which is the deprecated behavior of match, use
@@ -344,12 +344,12 @@ def str_match(arr, pat, case=True, flags=0, na=np.nan, as_indexer=False):
344344
if (not as_indexer) and regex.groups > 0:
345345
# Do this first, to make sure it happens even if the re.compile
346346
# raises below.
347-
warnings.warn("""In future versions of pandas, match will change
348-
to always return a bool indexer.""", UserWarning)
347+
warnings.warn("In future versions of pandas, match will change to"
348+
" always return a bool indexer.""", UserWarning)
349349

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

354354
# If not as_indexer and regex.groups == 0, this returns empty lists
355355
# and is basically useless, so we will not warn.

pandas/tests/test_strings.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -397,42 +397,34 @@ def test_deprecated_match(self):
397397
# Old match behavior, deprecated (but still default) in 0.13
398398
values = Series(['fooBAD__barBAD', NA, 'foo'])
399399

400-
with warnings.catch_warnings(record=True) as w:
401-
warnings.simplefilter('always')
400+
with tm.assert_produces_warning():
402401
result = values.str.match('.*(BAD[_]+).*(BAD)')
403-
assert issubclass(w[-1].category, UserWarning)
404402
exp = Series([('BAD__', 'BAD'), NA, []])
405403
tm.assert_series_equal(result, exp)
406404

407405
# mixed
408406
mixed = Series(['aBAD_BAD', NA, 'BAD_b_BAD', True, datetime.today(),
409407
'foo', None, 1, 2.])
410408

411-
with warnings.catch_warnings(record=True) as w:
412-
warnings.simplefilter('always')
409+
with tm.assert_produces_warning():
413410
rs = Series(mixed).str.match('.*(BAD[_]+).*(BAD)')
414-
assert issubclass(w[-1].category, UserWarning)
415411
xp = [('BAD_', 'BAD'), NA, ('BAD_', 'BAD'), NA, NA, [], NA, NA, NA]
416412
tm.assert_isinstance(rs, Series)
417413
tm.assert_almost_equal(rs, xp)
418414

419415
# unicode
420416
values = Series([u('fooBAD__barBAD'), NA, u('foo')])
421417

422-
with warnings.catch_warnings(record=True) as w:
423-
warnings.simplefilter('always')
418+
with tm.assert_produces_warning():
424419
result = values.str.match('.*(BAD[_]+).*(BAD)')
425-
assert issubclass(w[-1].category, UserWarning)
426420
exp = Series([(u('BAD__'), u('BAD')), NA, []])
427421
tm.assert_series_equal(result, exp)
428422

429423
def test_match(self):
430424
# New match behavior introduced in 0.13
431425
values = Series(['fooBAD__barBAD', NA, 'foo'])
432-
with warnings.catch_warnings(record=True) as w:
433-
warnings.simplefilter('always')
426+
with tm.assert_produces_warning():
434427
result = values.str.match('.*(BAD[_]+).*(BAD)', as_indexer=True)
435-
assert issubclass(w[-1].category, UserWarning)
436428
exp = Series([True, NA, False])
437429
tm.assert_series_equal(result, exp)
438430

@@ -447,21 +439,17 @@ def test_match(self):
447439
mixed = Series(['aBAD_BAD', NA, 'BAD_b_BAD', True, datetime.today(),
448440
'foo', None, 1, 2.])
449441

450-
with warnings.catch_warnings(record=True) as w:
451-
warnings.simplefilter('always')
442+
with tm.assert_produces_warning():
452443
rs = Series(mixed).str.match('.*(BAD[_]+).*(BAD)', as_indexer=True)
453-
assert issubclass(w[-1].category, UserWarning)
454444
xp = [True, NA, True, NA, NA, False, NA, NA, NA]
455445
tm.assert_isinstance(rs, Series)
456446
tm.assert_almost_equal(rs, xp)
457447

458448
# unicode
459449
values = Series([u('fooBAD__barBAD'), NA, u('foo')])
460450

461-
with warnings.catch_warnings(record=True) as w:
462-
warnings.simplefilter('always')
451+
with tm.assert_produces_warning():
463452
result = values.str.match('.*(BAD[_]+).*(BAD)', as_indexer=True)
464-
assert issubclass(w[-1].category, UserWarning)
465453
exp = Series([True, NA, False])
466454
tm.assert_series_equal(result, exp)
467455

0 commit comments

Comments
 (0)