Skip to content

Commit fb2ebb6

Browse files
Merge pull request #10701 from jorisvandenbossche/fix-warning-test
TST: fix usage of assert_produces_warning
2 parents 829893d + c0b43fa commit fb2ebb6

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

pandas/io/tests/test_stata.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def test_timestamp_and_label(self):
464464
data_label = 'This is a data file.'
465465
with tm.ensure_clean() as path:
466466
original.to_stata(path, time_stamp=time_stamp, data_label=data_label)
467-
467+
468468
with StataReader(path) as reader:
469469
parsed_time_stamp = dt.datetime.strptime(reader.time_stamp, ('%d %b %Y %H:%M'))
470470
assert parsed_time_stamp == time_stamp
@@ -475,10 +475,8 @@ def test_numeric_column_names(self):
475475
original.index.name = 'index'
476476
with tm.ensure_clean() as path:
477477
# should get a warning for that format.
478-
with warnings.catch_warnings(record=True) as w:
479-
tm.assert_produces_warning(original.to_stata(path), InvalidColumnName)
480-
# should produce a single warning
481-
tm.assert_equal(len(w), 1)
478+
with tm.assert_produces_warning(InvalidColumnName):
479+
original.to_stata(path)
482480

483481
written_and_read_again = self.read_dta(path)
484482
written_and_read_again = written_and_read_again.set_index('index')
@@ -530,11 +528,8 @@ def test_large_value_conversion(self):
530528
original = DataFrame({'s0': s0, 's1': s1, 's2': s2, 's3': s3})
531529
original.index.name = 'index'
532530
with tm.ensure_clean() as path:
533-
with warnings.catch_warnings(record=True) as w:
534-
tm.assert_produces_warning(original.to_stata(path),
535-
PossiblePrecisionLoss)
536-
# should produce a single warning
537-
tm.assert_equal(len(w), 1)
531+
with tm.assert_produces_warning(PossiblePrecisionLoss):
532+
original.to_stata(path)
538533

539534
written_and_read_again = self.read_dta(path)
540535
modified = original.copy()
@@ -548,10 +543,8 @@ def test_dates_invalid_column(self):
548543
original = DataFrame([datetime(2006, 11, 19, 23, 13, 20)])
549544
original.index.name = 'index'
550545
with tm.ensure_clean() as path:
551-
with warnings.catch_warnings(record=True) as w:
552-
tm.assert_produces_warning(original.to_stata(path, {0: 'tc'}),
553-
InvalidColumnName)
554-
tm.assert_equal(len(w), 1)
546+
with tm.assert_produces_warning(InvalidColumnName):
547+
original.to_stata(path, {0: 'tc'})
555548

556549
written_and_read_again = self.read_dta(path)
557550
modified = original.copy()

pandas/tests/test_indexing.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -4103,9 +4103,12 @@ def test_slice_indexer(self):
41034103

41044104
def check_iloc_compat(s):
41054105
# invalid type for iloc (but works with a warning)
4106-
self.assert_produces_warning(FutureWarning, lambda : s.iloc[6.0:8])
4107-
self.assert_produces_warning(FutureWarning, lambda : s.iloc[6.0:8.0])
4108-
self.assert_produces_warning(FutureWarning, lambda : s.iloc[6:8.0])
4106+
with self.assert_produces_warning(FutureWarning):
4107+
s.iloc[6.0:8]
4108+
with self.assert_produces_warning(FutureWarning):
4109+
s.iloc[6.0:8.0]
4110+
with self.assert_produces_warning(FutureWarning):
4111+
s.iloc[6:8.0]
41094112

41104113
def check_slicing_positional(index):
41114114

0 commit comments

Comments
 (0)