Skip to content

Commit e331a87

Browse files
committed
TST: provide a clear argument to assert_produces_warning to guarantee that we are clearing
specific warnings before trying to catch them (in case they have already happened) requires the user to specificy the exact class where they are coming
1 parent 6301f06 commit e331a87

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

pandas/tests/test_indexing.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable-msg=W0612,E1101
2+
import sys
23
import nose
34
import itertools
45
import warnings
@@ -19,6 +20,7 @@
1920
assert_frame_equal, assert_panel_equal,
2021
assert_attr_equal)
2122
from pandas import concat
23+
from pandas.io.common import PerformanceWarning
2224

2325
import pandas.util.testing as tm
2426
from pandas import date_range
@@ -1489,8 +1491,6 @@ def test_loc_multiindex(self):
14891491
assert_series_equal(result, expected)
14901492

14911493
def test_multiindex_perf_warn(self):
1492-
import sys
1493-
from pandas.io.common import PerformanceWarning
14941494

14951495
if sys.version_info < (2, 7):
14961496
raise nose.SkipTest('python version < 2.7')
@@ -1499,14 +1499,18 @@ def test_multiindex_perf_warn(self):
14991499
'joe':['x', 'x', 'z', 'y'],
15001500
'jolie':np.random.rand(4)}).set_index(['jim', 'joe'])
15011501

1502-
with tm.assert_produces_warning(PerformanceWarning):
1502+
with tm.assert_produces_warning(PerformanceWarning, clear=[pd.core.index]):
15031503
_ = df.loc[(1, 'z')]
15041504

15051505
df = df.iloc[[2,1,3,0]]
15061506
with tm.assert_produces_warning(PerformanceWarning):
15071507
_ = df.loc[(0,)]
15081508

15091509
def test_multiindex_get_loc(self): # GH7724, GH2646
1510+
1511+
# ignore the warning here
1512+
warnings.simplefilter('ignore', PerformanceWarning)
1513+
15101514
# test indexing into a multi-index before & past the lexsort depth
15111515
from numpy.random import randint, choice, randn
15121516
cols = ['jim', 'joe', 'jolie', 'joline', 'jolia']
@@ -1568,6 +1572,9 @@ def loop(mi, df, keys):
15681572
assert not mi.index.lexsort_depth < i
15691573
loop(mi, df, keys)
15701574

1575+
# restore
1576+
warnings.simplefilter('always', PerformanceWarning)
1577+
15711578
def test_series_getitem_multiindex(self):
15721579

15731580
# GH 6018
@@ -1621,6 +1628,8 @@ def test_ix_general(self):
16211628
'year': {0: 2012, 1: 2011, 2: 2012, 3: 2012, 4: 2012}}
16221629
df = DataFrame(data).set_index(keys=['col', 'year'])
16231630
key = 4.0, 2012
1631+
1632+
# emits a PerformanceWarning, ok
16241633
tm.assert_frame_equal(df.ix[key], df.iloc[2:])
16251634

16261635
# this is ok

pandas/util/testing.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from numpy.testing import assert_array_equal
2525

2626
import pandas as pd
27-
from pandas.core.common import _is_sequence, array_equivalent
27+
from pandas.core.common import _is_sequence, array_equivalent, is_list_like
2828
import pandas.core.index as index
2929
import pandas.core.series as series
3030
import pandas.core.frame as frame
@@ -1619,7 +1619,7 @@ def handle_success(self, exc_type, exc_value, traceback):
16191619

16201620

16211621
@contextmanager
1622-
def assert_produces_warning(expected_warning=Warning, filter_level="always"):
1622+
def assert_produces_warning(expected_warning=Warning, filter_level="always", clear=None):
16231623
"""
16241624
Context manager for running code that expects to raise (or not raise)
16251625
warnings. Checks that code raises the expected warning and only the
@@ -1646,6 +1646,19 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always"):
16461646
..warn:: This is *not* thread-safe.
16471647
"""
16481648
with warnings.catch_warnings(record=True) as w:
1649+
1650+
if clear is not None:
1651+
# make sure that we are clearning these warnings
1652+
# if they have happened before
1653+
# to guarantee that we will catch them
1654+
if not is_list_like(clear):
1655+
clear = [ clear ]
1656+
for m in clear:
1657+
try:
1658+
m.__warningregistry__.clear()
1659+
except:
1660+
pass
1661+
16491662
saw_warning = False
16501663
warnings.simplefilter(filter_level)
16511664
yield w

0 commit comments

Comments
 (0)