Skip to content

Commit 669bf55

Browse files
committed
REF: numpy.random-related imports
1 parent 9cb3723 commit 669bf55

File tree

6 files changed

+129
-136
lines changed

6 files changed

+129
-136
lines changed

pandas/tests/plotting/common.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import warnings
33

44
import numpy as np
5-
from numpy import random
5+
from numpy.random import choice, normal, randint, uniform
66

77
from pandas.util._decorators import cache_readonly
88
import pandas.util._test_decorators as td
@@ -43,18 +43,18 @@ def setup_method(self, method):
4343

4444
n = 100
4545
with tm.RNGContext(42):
46-
gender = np.random.choice(["Male", "Female"], size=n)
47-
classroom = np.random.choice(["A", "B", "C"], size=n)
46+
gender = choice(["Male", "Female"], size=n)
47+
classroom = choice(["A", "B", "C"], size=n)
4848

4949
self.hist_df = DataFrame(
5050
{
5151
"gender": gender,
5252
"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),
53+
"height": normal(66, 4, size=n),
54+
"weight": normal(161, 32, size=n),
55+
"category": randint(4, size=n),
5656
"datetime": to_datetime(
57-
random.randint(
57+
randint(
5858
self.start_date_to_int64,
5959
self.end_date_to_int64,
6060
size=n,
@@ -67,9 +67,9 @@ def setup_method(self, method):
6767
self.tdf = tm.makeTimeDataFrame()
6868
self.hexbin_df = DataFrame(
6969
{
70-
"A": np.random.uniform(size=20),
71-
"B": np.random.uniform(size=20),
72-
"C": np.arange(20) + np.random.uniform(size=20),
70+
"A": uniform(size=20),
71+
"B": uniform(size=20),
72+
"C": np.arange(20) + uniform(size=20),
7373
}
7474
)
7575

pandas/tests/plotting/test_boxplot_method.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import string
33

44
import numpy as np
5-
from numpy import random
5+
from numpy.random import choice, normal, rand, randint, randn, random
66
import pytest
77

88
import pandas.util._test_decorators as td
@@ -21,7 +21,7 @@ class TestDataFramePlots(TestPlotBase):
2121
@pytest.mark.slow
2222
def test_boxplot_legacy1(self):
2323
df = DataFrame(
24-
np.random.randn(6, 4),
24+
randn(6, 4),
2525
index=list(string.ascii_letters[:6]),
2626
columns=["one", "two", "three", "four"],
2727
)
@@ -45,7 +45,7 @@ def test_boxplot_legacy1(self):
4545

4646
@pytest.mark.slow
4747
def test_boxplot_legacy2(self):
48-
df = DataFrame(np.random.rand(10, 2), columns=["Col1", "Col2"])
48+
df = DataFrame(rand(10, 2), columns=["Col1", "Col2"])
4949
df["X"] = Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
5050
df["Y"] = Series(["A"] * 10)
5151
with tm.assert_produces_warning(UserWarning):
@@ -90,7 +90,7 @@ def test_boxplot_return_type_legacy(self):
9090
import matplotlib as mpl # noqa
9191

9292
df = DataFrame(
93-
np.random.randn(6, 4),
93+
randn(6, 4),
9494
index=list(string.ascii_letters[:6]),
9595
columns=["one", "two", "three", "four"],
9696
)
@@ -120,7 +120,7 @@ def _check_ax_limits(col, ax):
120120
assert y_max >= col.max()
121121

122122
df = self.hist_df.copy()
123-
df["age"] = np.random.randint(1, 20, df.shape[0])
123+
df["age"] = randint(1, 20, df.shape[0])
124124
# One full row
125125
height_ax, weight_ax = df.boxplot(["height", "weight"], by="category")
126126
_check_ax_limits(df["height"], height_ax)
@@ -141,13 +141,13 @@ def _check_ax_limits(col, ax):
141141

142142
@pytest.mark.slow
143143
def test_boxplot_empty_column(self):
144-
df = DataFrame(np.random.randn(20, 4))
144+
df = DataFrame(randn(20, 4))
145145
df.loc[:, 0] = np.nan
146146
_check_plot_works(df.boxplot, return_type="axes")
147147

148148
@pytest.mark.slow
149149
def test_figsize(self):
150-
df = DataFrame(np.random.rand(10, 5), columns=["A", "B", "C", "D", "E"])
150+
df = DataFrame(rand(10, 5), columns=["A", "B", "C", "D", "E"])
151151
result = df.boxplot(return_type="axes", figsize=(12, 8))
152152
assert result.figure.bbox_inches.width == 12
153153
assert result.figure.bbox_inches.height == 8
@@ -163,8 +163,8 @@ def test_boxplot_numeric_data(self):
163163
df = DataFrame(
164164
{
165165
"a": date_range("2012-01-01", periods=100),
166-
"b": np.random.randn(100),
167-
"c": np.random.randn(100) + 2,
166+
"b": randn(100),
167+
"c": randn(100) + 2,
168168
"d": date_range("2012-01-01", periods=100).astype(str),
169169
"e": date_range("2012-01-01", periods=100, tz="UTC"),
170170
"f": timedelta_range("1 days", periods=100),
@@ -186,7 +186,7 @@ def test_boxplot_numeric_data(self):
186186
)
187187
def test_color_kwd(self, colors_kwd, expected):
188188
# GH: 26214
189-
df = DataFrame(random.rand(10, 2))
189+
df = DataFrame(rand(10, 2))
190190
result = df.boxplot(color=colors_kwd, return_type="dict")
191191
for k, v in expected.items():
192192
assert result[k][0].get_color() == v
@@ -197,7 +197,7 @@ def test_color_kwd(self, colors_kwd, expected):
197197
)
198198
def test_color_kwd_errors(self, dict_colors, msg):
199199
# GH: 26214
200-
df = DataFrame(random.rand(10, 2))
200+
df = DataFrame(rand(10, 2))
201201
with pytest.raises(ValueError, match=msg):
202202
df.boxplot(color=dict_colors, return_type="dict")
203203

@@ -212,7 +212,7 @@ def test_color_kwd_errors(self, dict_colors, msg):
212212
)
213213
def test_specified_props_kwd(self, props, expected):
214214
# GH 30346
215-
df = DataFrame({k: np.random.random(100) for k in "ABC"})
215+
df = DataFrame({k: random(100) for k in "ABC"})
216216
kwd = {props: dict(color="C1")}
217217
result = df.boxplot(return_type="dict", **kwd)
218218

@@ -233,7 +233,7 @@ def test_boxplot_legacy1(self):
233233
@pytest.mark.slow
234234
def test_boxplot_legacy2(self):
235235
tuples = zip(string.ascii_letters[:10], range(10))
236-
df = DataFrame(np.random.rand(10, 3), index=MultiIndex.from_tuples(tuples))
236+
df = DataFrame(rand(10, 3), index=MultiIndex.from_tuples(tuples))
237237
grouped = df.groupby(level=1)
238238
with tm.assert_produces_warning(UserWarning):
239239
axes = _check_plot_works(grouped.boxplot, return_type="axes")
@@ -245,7 +245,7 @@ def test_boxplot_legacy2(self):
245245
@pytest.mark.slow
246246
def test_boxplot_legacy3(self):
247247
tuples = zip(string.ascii_letters[:10], range(10))
248-
df = DataFrame(np.random.rand(10, 3), index=MultiIndex.from_tuples(tuples))
248+
df = DataFrame(rand(10, 3), index=MultiIndex.from_tuples(tuples))
249249
grouped = df.unstack(level=1).groupby(level=0, axis=1)
250250
with tm.assert_produces_warning(UserWarning):
251251
axes = _check_plot_works(grouped.boxplot, return_type="axes")
@@ -256,10 +256,10 @@ def test_boxplot_legacy3(self):
256256
@pytest.mark.slow
257257
def test_grouped_plot_fignums(self):
258258
n = 10
259-
weight = Series(np.random.normal(166, 20, size=n))
260-
height = Series(np.random.normal(60, 10, size=n))
259+
weight = Series(normal(166, 20, size=n))
260+
height = Series(normal(60, 10, size=n))
261261
with tm.RNGContext(42):
262-
gender = np.random.choice(["male", "female"], size=n)
262+
gender = choice(["male", "female"], size=n)
263263
df = DataFrame({"height": height, "weight": weight, "gender": gender})
264264
gb = df.groupby("gender")
265265

@@ -293,7 +293,7 @@ def test_grouped_box_return_type(self):
293293
self._check_box_return_type(result, "dict", expected_keys=["Male", "Female"])
294294

295295
columns2 = "X B C D A G Y N Q O".split()
296-
df2 = DataFrame(random.randn(50, 10), columns=columns2)
296+
df2 = DataFrame(randn(50, 10), columns=columns2)
297297
categories2 = "A B C D E F G H I J".split()
298298
df2["category"] = categories2 * 5
299299

0 commit comments

Comments
 (0)