Skip to content

Commit 31f59f7

Browse files
authored
CNL: unify numpy.random-related imports (#37492)
1 parent ce1e8d6 commit 31f59f7

23 files changed

+171
-180
lines changed

pandas/tests/computation/test_eval.py

+37-38
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import warnings
77

88
import numpy as np
9-
from numpy.random import rand, randint, randn
109
import pytest
1110

1211
from pandas.compat import is_platform_windows
@@ -126,15 +125,15 @@ def _is_py3_complex_incompat(result, expected):
126125
@pytest.fixture(params=list(range(5)))
127126
def lhs(request):
128127

129-
nan_df1 = DataFrame(rand(10, 5))
128+
nan_df1 = DataFrame(np.random.rand(10, 5))
130129
nan_df1[nan_df1 > 0.5] = np.nan
131130

132131
opts = (
133-
DataFrame(randn(10, 5)),
134-
Series(randn(5)),
132+
DataFrame(np.random.randn(10, 5)),
133+
Series(np.random.randn(5)),
135134
Series([1, 2, np.nan, np.nan, 5]),
136135
nan_df1,
137-
randn(),
136+
np.random.randn(),
138137
)
139138
return opts[request.param]
140139

@@ -455,7 +454,7 @@ def test_frame_invert(self):
455454
# ~ ##
456455
# frame
457456
# float always raises
458-
lhs = DataFrame(randn(5, 2))
457+
lhs = DataFrame(np.random.randn(5, 2))
459458
if self.engine == "numexpr":
460459
msg = "couldn't find matching opcode for 'invert_dd'"
461460
with pytest.raises(NotImplementedError, match=msg):
@@ -466,7 +465,7 @@ def test_frame_invert(self):
466465
result = pd.eval(expr, engine=self.engine, parser=self.parser)
467466

468467
# int raises on numexpr
469-
lhs = DataFrame(randint(5, size=(5, 2)))
468+
lhs = DataFrame(np.random.randint(5, size=(5, 2)))
470469
if self.engine == "numexpr":
471470
msg = "couldn't find matching opcode for 'invert"
472471
with pytest.raises(NotImplementedError, match=msg):
@@ -477,13 +476,13 @@ def test_frame_invert(self):
477476
tm.assert_frame_equal(expect, result)
478477

479478
# bool always works
480-
lhs = DataFrame(rand(5, 2) > 0.5)
479+
lhs = DataFrame(np.random.rand(5, 2) > 0.5)
481480
expect = ~lhs
482481
result = pd.eval(expr, engine=self.engine, parser=self.parser)
483482
tm.assert_frame_equal(expect, result)
484483

485484
# object raises
486-
lhs = DataFrame({"b": ["a", 1, 2.0], "c": rand(3) > 0.5})
485+
lhs = DataFrame({"b": ["a", 1, 2.0], "c": np.random.rand(3) > 0.5})
487486
if self.engine == "numexpr":
488487
with pytest.raises(ValueError, match="unknown type object"):
489488
result = pd.eval(expr, engine=self.engine, parser=self.parser)
@@ -498,7 +497,7 @@ def test_series_invert(self):
498497

499498
# series
500499
# float raises
501-
lhs = Series(randn(5))
500+
lhs = Series(np.random.randn(5))
502501
if self.engine == "numexpr":
503502
msg = "couldn't find matching opcode for 'invert_dd'"
504503
with pytest.raises(NotImplementedError, match=msg):
@@ -509,7 +508,7 @@ def test_series_invert(self):
509508
result = pd.eval(expr, engine=self.engine, parser=self.parser)
510509

511510
# int raises on numexpr
512-
lhs = Series(randint(5, size=5))
511+
lhs = Series(np.random.randint(5, size=5))
513512
if self.engine == "numexpr":
514513
msg = "couldn't find matching opcode for 'invert"
515514
with pytest.raises(NotImplementedError, match=msg):
@@ -520,7 +519,7 @@ def test_series_invert(self):
520519
tm.assert_series_equal(expect, result)
521520

522521
# bool
523-
lhs = Series(rand(5) > 0.5)
522+
lhs = Series(np.random.rand(5) > 0.5)
524523
expect = ~lhs
525524
result = pd.eval(expr, engine=self.engine, parser=self.parser)
526525
tm.assert_series_equal(expect, result)
@@ -543,19 +542,19 @@ def test_frame_negate(self):
543542
expr = self.ex("-")
544543

545544
# float
546-
lhs = DataFrame(randn(5, 2))
545+
lhs = DataFrame(np.random.randn(5, 2))
547546
expect = -lhs
548547
result = pd.eval(expr, engine=self.engine, parser=self.parser)
549548
tm.assert_frame_equal(expect, result)
550549

551550
# int
552-
lhs = DataFrame(randint(5, size=(5, 2)))
551+
lhs = DataFrame(np.random.randint(5, size=(5, 2)))
553552
expect = -lhs
554553
result = pd.eval(expr, engine=self.engine, parser=self.parser)
555554
tm.assert_frame_equal(expect, result)
556555

557556
# bool doesn't work with numexpr but works elsewhere
558-
lhs = DataFrame(rand(5, 2) > 0.5)
557+
lhs = DataFrame(np.random.rand(5, 2) > 0.5)
559558
if self.engine == "numexpr":
560559
msg = "couldn't find matching opcode for 'neg_bb'"
561560
with pytest.raises(NotImplementedError, match=msg):
@@ -569,19 +568,19 @@ def test_series_negate(self):
569568
expr = self.ex("-")
570569

571570
# float
572-
lhs = Series(randn(5))
571+
lhs = Series(np.random.randn(5))
573572
expect = -lhs
574573
result = pd.eval(expr, engine=self.engine, parser=self.parser)
575574
tm.assert_series_equal(expect, result)
576575

577576
# int
578-
lhs = Series(randint(5, size=5))
577+
lhs = Series(np.random.randint(5, size=5))
579578
expect = -lhs
580579
result = pd.eval(expr, engine=self.engine, parser=self.parser)
581580
tm.assert_series_equal(expect, result)
582581

583582
# bool doesn't work with numexpr but works elsewhere
584-
lhs = Series(rand(5) > 0.5)
583+
lhs = Series(np.random.rand(5) > 0.5)
585584
if self.engine == "numexpr":
586585
msg = "couldn't find matching opcode for 'neg_bb'"
587586
with pytest.raises(NotImplementedError, match=msg):
@@ -595,11 +594,11 @@ def test_series_negate(self):
595594
"lhs",
596595
[
597596
# Float
598-
DataFrame(randn(5, 2)),
597+
DataFrame(np.random.randn(5, 2)),
599598
# Int
600-
DataFrame(randint(5, size=(5, 2))),
599+
DataFrame(np.random.randint(5, size=(5, 2))),
601600
# bool doesn't work with numexpr but works elsewhere
602-
DataFrame(rand(5, 2) > 0.5),
601+
DataFrame(np.random.rand(5, 2) > 0.5),
603602
],
604603
)
605604
def test_frame_pos(self, lhs):
@@ -613,11 +612,11 @@ def test_frame_pos(self, lhs):
613612
"lhs",
614613
[
615614
# Float
616-
Series(randn(5)),
615+
Series(np.random.randn(5)),
617616
# Int
618-
Series(randint(5, size=5)),
617+
Series(np.random.randint(5, size=5)),
619618
# bool doesn't work with numexpr but works elsewhere
620-
Series(rand(5) > 0.5),
619+
Series(np.random.rand(5) > 0.5),
621620
],
622621
)
623622
def test_series_pos(self, lhs):
@@ -688,7 +687,7 @@ def test_disallow_scalar_bool_ops(self):
688687
exprs += ("2 * x > 2 or 1 and 2",)
689688
exprs += ("2 * df > 3 and 1 or a",)
690689

691-
x, a, b, df = np.random.randn(3), 1, 2, DataFrame(randn(3, 2)) # noqa
690+
x, a, b, df = np.random.randn(3), 1, 2, DataFrame(np.random.randn(3, 2)) # noqa
692691
for ex in exprs:
693692
msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
694693
with pytest.raises(NotImplementedError, match=msg):
@@ -909,7 +908,7 @@ def test_frame_comparison(self, engine, parser, r_idx_type, c_idx_type):
909908
res = pd.eval("df < 2", engine=engine, parser=parser)
910909
tm.assert_frame_equal(res, df < 2)
911910

912-
df3 = DataFrame(randn(*df.shape), index=df.index, columns=df.columns)
911+
df3 = DataFrame(np.random.randn(*df.shape), index=df.index, columns=df.columns)
913912
res = pd.eval("df < df3", engine=engine, parser=parser)
914913
tm.assert_frame_equal(res, df < df3)
915914

@@ -1089,8 +1088,8 @@ def test_complex_series_frame_alignment(self, engine, parser, r1, c1, r2, c2):
10891088
tm.assert_frame_equal(res, expected)
10901089

10911090
def test_performance_warning_for_poor_alignment(self, engine, parser):
1092-
df = DataFrame(randn(1000, 10))
1093-
s = Series(randn(10000))
1091+
df = DataFrame(np.random.randn(1000, 10))
1092+
s = Series(np.random.randn(10000))
10941093
if engine == "numexpr":
10951094
seen = PerformanceWarning
10961095
else:
@@ -1099,17 +1098,17 @@ def test_performance_warning_for_poor_alignment(self, engine, parser):
10991098
with tm.assert_produces_warning(seen):
11001099
pd.eval("df + s", engine=engine, parser=parser)
11011100

1102-
s = Series(randn(1000))
1101+
s = Series(np.random.randn(1000))
11031102
with tm.assert_produces_warning(False):
11041103
pd.eval("df + s", engine=engine, parser=parser)
11051104

1106-
df = DataFrame(randn(10, 10000))
1107-
s = Series(randn(10000))
1105+
df = DataFrame(np.random.randn(10, 10000))
1106+
s = Series(np.random.randn(10000))
11081107
with tm.assert_produces_warning(False):
11091108
pd.eval("df + s", engine=engine, parser=parser)
11101109

1111-
df = DataFrame(randn(10, 10))
1112-
s = Series(randn(10000))
1110+
df = DataFrame(np.random.randn(10, 10))
1111+
s = Series(np.random.randn(10000))
11131112

11141113
is_python_engine = engine == "python"
11151114

@@ -1206,8 +1205,8 @@ def test_bool_ops_with_constants(self, rhs, lhs, op):
12061205
assert res == exp
12071206

12081207
def test_4d_ndarray_fails(self):
1209-
x = randn(3, 4, 5, 6)
1210-
y = Series(randn(10))
1208+
x = np.random.randn(3, 4, 5, 6)
1209+
y = Series(np.random.randn(10))
12111210
msg = "N-dimensional objects, where N > 2, are not supported with eval"
12121211
with pytest.raises(NotImplementedError, match=msg):
12131212
self.eval("x + y", local_dict={"x": x, "y": y})
@@ -1217,7 +1216,7 @@ def test_constant(self):
12171216
assert x == 1
12181217

12191218
def test_single_variable(self):
1220-
df = DataFrame(randn(10, 2))
1219+
df = DataFrame(np.random.randn(10, 2))
12211220
df2 = self.eval("df", local_dict={"df": df})
12221221
tm.assert_frame_equal(df, df2)
12231222

@@ -1574,7 +1573,7 @@ def test_nested_period_index_subscript_expression(self):
15741573
tm.assert_frame_equal(r, e)
15751574

15761575
def test_date_boolean(self):
1577-
df = DataFrame(randn(5, 3))
1576+
df = DataFrame(np.random.randn(5, 3))
15781577
df["dates1"] = date_range("1/1/2012", periods=5)
15791578
res = self.eval(
15801579
"df.dates1 < 20130101",
@@ -1859,7 +1858,7 @@ class TestMathNumExprPython(TestMathPythonPython):
18591858
parser = "python"
18601859

18611860

1862-
_var_s = randn(10)
1861+
_var_s = np.random.randn(10)
18631862

18641863

18651864
class TestScope:

pandas/tests/indexing/multiindex/test_insert.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from numpy.random import randn
1+
import numpy as np
22

33
from pandas import DataFrame, MultiIndex, Series
44
import pandas._testing as tm
@@ -14,7 +14,7 @@ def test_setitem_mixed_depth(self):
1414

1515
tuples = sorted(zip(*arrays))
1616
index = MultiIndex.from_tuples(tuples)
17-
df = DataFrame(randn(4, 6), columns=index)
17+
df = DataFrame(np.random.randn(4, 6), columns=index)
1818

1919
result = df.copy()
2020
expected = df.copy()

pandas/tests/indexing/multiindex/test_setitem.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from numpy.random import randn
32
import pytest
43

54
import pandas as pd
@@ -311,7 +310,9 @@ def test_frame_getitem_setitem_multislice(self):
311310
tm.assert_frame_equal(df, result)
312311

313312
def test_frame_setitem_multi_column(self):
314-
df = DataFrame(randn(10, 4), columns=[["a", "a", "b", "b"], [0, 1, 0, 1]])
313+
df = DataFrame(
314+
np.random.randn(10, 4), columns=[["a", "a", "b", "b"], [0, 1, 0, 1]]
315+
)
315316

316317
cp = df.copy()
317318
cp["a"] = cp["b"]

pandas/tests/indexing/multiindex/test_sorted.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from numpy.random import randn
32
import pytest
43

54
from pandas import DataFrame, MultiIndex, Series
@@ -115,7 +114,7 @@ def test_series_getitem_not_sorted(self):
115114
]
116115
tuples = zip(*arrays)
117116
index = MultiIndex.from_tuples(tuples)
118-
s = Series(randn(8), index=index)
117+
s = Series(np.random.randn(8), index=index)
119118

120119
arrays = [np.array(x) for x in zip(*index.values)]
121120

pandas/tests/io/test_clipboard.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from textwrap import dedent
22

33
import numpy as np
4-
from numpy.random import randint
54
import pytest
65

76
import pandas as pd
@@ -54,7 +53,7 @@ def df(request):
5453
return tm.makeCustomDataframe(
5554
max_rows + 1,
5655
3,
57-
data_gen_f=lambda *args: randint(2),
56+
data_gen_f=lambda *args: np.random.randint(2),
5857
c_idx_type="s",
5958
r_idx_type="i",
6059
c_idx_names=[None],
@@ -95,7 +94,7 @@ def df(request):
9594
return tm.makeCustomDataframe(
9695
5,
9796
3,
98-
data_gen_f=lambda *args: randint(2),
97+
data_gen_f=lambda *args: np.random.randint(2),
9998
c_idx_type="s",
10099
r_idx_type="i",
101100
c_idx_names=[None],

pandas/tests/io/test_html.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from urllib.error import URLError
88

99
import numpy as np
10-
from numpy.random import rand
1110
import pytest
1211

1312
from pandas.compat import is_platform_windows
@@ -110,7 +109,7 @@ def test_to_html_compat(self):
110109
tm.makeCustomDataframe(
111110
4,
112111
3,
113-
data_gen_f=lambda *args: rand(),
112+
data_gen_f=lambda *args: np.random.rand(),
114113
c_idx_names=False,
115114
r_idx_names=False,
116115
)

pandas/tests/plotting/common.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import warnings
33

44
import numpy as np
5-
from numpy import random
65

76
from pandas.util._decorators import cache_readonly
87
import pandas.util._test_decorators as td
@@ -50,11 +49,11 @@ def setup_method(self, method):
5049
{
5150
"gender": gender,
5251
"classroom": classroom,
53-
"height": random.normal(66, 4, size=n),
54-
"weight": random.normal(161, 32, size=n),
55-
"category": random.randint(4, size=n),
52+
"height": np.random.normal(66, 4, size=n),
53+
"weight": np.random.normal(161, 32, size=n),
54+
"category": np.random.randint(4, size=n),
5655
"datetime": to_datetime(
57-
random.randint(
56+
np.random.randint(
5857
self.start_date_to_int64,
5958
self.end_date_to_int64,
6059
size=n,

0 commit comments

Comments
 (0)