Skip to content

Commit 8fc8b5a

Browse files
author
Tom Augspurger
committed
Merge pull request pandas-dev#6670 from TomAugspurger/partially-invalid-plot
BUG/ENH: RNG ContextManager. Use in test_partially_invalid_plot_data
2 parents 8d87a29 + 96d4968 commit 8fc8b5a

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

pandas/tests/test_graphics.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1106,12 +1106,13 @@ def test_all_invalid_plot_data(self):
11061106

11071107
@slow
11081108
def test_partially_invalid_plot_data(self):
1109-
kinds = 'line', 'bar', 'barh', 'kde', 'density'
1110-
df = DataFrame(randn(10, 2), dtype=object)
1111-
df[np.random.rand(df.shape[0]) > 0.5] = 'a'
1112-
for kind in kinds:
1113-
with tm.assertRaises(TypeError):
1114-
df.plot(kind=kind)
1109+
with tm.RNGContext(42):
1110+
kinds = 'line', 'bar', 'barh', 'kde', 'density'
1111+
df = DataFrame(randn(10, 2), dtype=object)
1112+
df[np.random.rand(df.shape[0]) > 0.5] = 'a'
1113+
for kind in kinds:
1114+
with tm.assertRaises(TypeError):
1115+
df.plot(kind=kind)
11151116

11161117
def test_invalid_kind(self):
11171118
df = DataFrame(randn(10, 2))

pandas/tests/test_testing.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import sys
99
from pandas import Series
1010
from pandas.util.testing import (
11-
assert_almost_equal, assertRaisesRegexp, raise_with_traceback, assert_series_equal
11+
assert_almost_equal, assertRaisesRegexp, raise_with_traceback, assert_series_equal,
12+
RNGContext
1213
)
1314

1415
# let's get meta.
@@ -153,3 +154,14 @@ def test_not_equal(self):
153154
# ATM meta data is not checked in assert_series_equal
154155
# self._assert_not_equal(Series(range(3)),Series(range(3),name='foo'),check_names=True)
155156

157+
158+
class TestRNGContext(unittest.TestCase):
159+
160+
def test_RNGContext(self):
161+
expected0 = 1.764052345967664
162+
expected1 = 1.6243453636632417
163+
164+
with RNGContext(0):
165+
with RNGContext(1):
166+
self.assertEqual(np.random.randn(), expected1)
167+
self.assertEqual(np.random.randn(), expected0)

pandas/util/testing.py

+30
Original file line numberDiff line numberDiff line change
@@ -1528,3 +1528,33 @@ def skip_if_no_ne(engine='numexpr'):
15281528
def disabled(t):
15291529
t.disabled = True
15301530
return t
1531+
1532+
1533+
class RNGContext(object):
1534+
"""
1535+
Context manager to set the numpy random number generator speed. Returns
1536+
to the original value upon exiting the context manager.
1537+
1538+
Parameters
1539+
----------
1540+
seed : int
1541+
Seed for numpy.random.seed
1542+
1543+
Examples
1544+
--------
1545+
1546+
with RNGContext(42):
1547+
np.random.randn()
1548+
"""
1549+
1550+
def __init__(self, seed):
1551+
self.seed = seed
1552+
1553+
def __enter__(self):
1554+
1555+
self.start_state = np.random.get_state()
1556+
np.random.seed(self.seed)
1557+
1558+
def __exit__(self, exc_type, exc_value, traceback):
1559+
1560+
np.random.set_state(self.start_state)

0 commit comments

Comments
 (0)