Skip to content

CLN: unify numpy.random-related imports #37492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 37 additions & 38 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import warnings

import numpy as np
from numpy.random import rand, randint, randn
import pytest

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

nan_df1 = DataFrame(rand(10, 5))
nan_df1 = DataFrame(np.random.rand(10, 5))
nan_df1[nan_df1 > 0.5] = np.nan

opts = (
DataFrame(randn(10, 5)),
Series(randn(5)),
DataFrame(np.random.randn(10, 5)),
Series(np.random.randn(5)),
Series([1, 2, np.nan, np.nan, 5]),
nan_df1,
randn(),
np.random.randn(),
)
return opts[request.param]

Expand Down Expand Up @@ -455,7 +454,7 @@ def test_frame_invert(self):
# ~ ##
# frame
# float always raises
lhs = DataFrame(randn(5, 2))
lhs = DataFrame(np.random.randn(5, 2))
if self.engine == "numexpr":
msg = "couldn't find matching opcode for 'invert_dd'"
with pytest.raises(NotImplementedError, match=msg):
Expand All @@ -466,7 +465,7 @@ def test_frame_invert(self):
result = pd.eval(expr, engine=self.engine, parser=self.parser)

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

# bool always works
lhs = DataFrame(rand(5, 2) > 0.5)
lhs = DataFrame(np.random.rand(5, 2) > 0.5)
expect = ~lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
tm.assert_frame_equal(expect, result)

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

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

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

# bool
lhs = Series(rand(5) > 0.5)
lhs = Series(np.random.rand(5) > 0.5)
expect = ~lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
tm.assert_series_equal(expect, result)
Expand All @@ -543,19 +542,19 @@ def test_frame_negate(self):
expr = self.ex("-")

# float
lhs = DataFrame(randn(5, 2))
lhs = DataFrame(np.random.randn(5, 2))
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
tm.assert_frame_equal(expect, result)

# int
lhs = DataFrame(randint(5, size=(5, 2)))
lhs = DataFrame(np.random.randint(5, size=(5, 2)))
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
tm.assert_frame_equal(expect, result)

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

# float
lhs = Series(randn(5))
lhs = Series(np.random.randn(5))
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
tm.assert_series_equal(expect, result)

# int
lhs = Series(randint(5, size=5))
lhs = Series(np.random.randint(5, size=5))
expect = -lhs
result = pd.eval(expr, engine=self.engine, parser=self.parser)
tm.assert_series_equal(expect, result)

# bool doesn't work with numexpr but works elsewhere
lhs = Series(rand(5) > 0.5)
lhs = Series(np.random.rand(5) > 0.5)
if self.engine == "numexpr":
msg = "couldn't find matching opcode for 'neg_bb'"
with pytest.raises(NotImplementedError, match=msg):
Expand All @@ -595,11 +594,11 @@ def test_series_negate(self):
"lhs",
[
# Float
DataFrame(randn(5, 2)),
DataFrame(np.random.randn(5, 2)),
# Int
DataFrame(randint(5, size=(5, 2))),
DataFrame(np.random.randint(5, size=(5, 2))),
# bool doesn't work with numexpr but works elsewhere
DataFrame(rand(5, 2) > 0.5),
DataFrame(np.random.rand(5, 2) > 0.5),
],
)
def test_frame_pos(self, lhs):
Expand All @@ -613,11 +612,11 @@ def test_frame_pos(self, lhs):
"lhs",
[
# Float
Series(randn(5)),
Series(np.random.randn(5)),
# Int
Series(randint(5, size=5)),
Series(np.random.randint(5, size=5)),
# bool doesn't work with numexpr but works elsewhere
Series(rand(5) > 0.5),
Series(np.random.rand(5) > 0.5),
],
)
def test_series_pos(self, lhs):
Expand Down Expand Up @@ -688,7 +687,7 @@ def test_disallow_scalar_bool_ops(self):
exprs += ("2 * x > 2 or 1 and 2",)
exprs += ("2 * df > 3 and 1 or a",)

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

df3 = DataFrame(randn(*df.shape), index=df.index, columns=df.columns)
df3 = DataFrame(np.random.randn(*df.shape), index=df.index, columns=df.columns)
res = pd.eval("df < df3", engine=engine, parser=parser)
tm.assert_frame_equal(res, df < df3)

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

def test_performance_warning_for_poor_alignment(self, engine, parser):
df = DataFrame(randn(1000, 10))
s = Series(randn(10000))
df = DataFrame(np.random.randn(1000, 10))
s = Series(np.random.randn(10000))
if engine == "numexpr":
seen = PerformanceWarning
else:
Expand All @@ -1099,17 +1098,17 @@ def test_performance_warning_for_poor_alignment(self, engine, parser):
with tm.assert_produces_warning(seen):
pd.eval("df + s", engine=engine, parser=parser)

s = Series(randn(1000))
s = Series(np.random.randn(1000))
with tm.assert_produces_warning(False):
pd.eval("df + s", engine=engine, parser=parser)

df = DataFrame(randn(10, 10000))
s = Series(randn(10000))
df = DataFrame(np.random.randn(10, 10000))
s = Series(np.random.randn(10000))
with tm.assert_produces_warning(False):
pd.eval("df + s", engine=engine, parser=parser)

df = DataFrame(randn(10, 10))
s = Series(randn(10000))
df = DataFrame(np.random.randn(10, 10))
s = Series(np.random.randn(10000))

is_python_engine = engine == "python"

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

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

def test_single_variable(self):
df = DataFrame(randn(10, 2))
df = DataFrame(np.random.randn(10, 2))
df2 = self.eval("df", local_dict={"df": df})
tm.assert_frame_equal(df, df2)

Expand Down Expand Up @@ -1574,7 +1573,7 @@ def test_nested_period_index_subscript_expression(self):
tm.assert_frame_equal(r, e)

def test_date_boolean(self):
df = DataFrame(randn(5, 3))
df = DataFrame(np.random.randn(5, 3))
df["dates1"] = date_range("1/1/2012", periods=5)
res = self.eval(
"df.dates1 < 20130101",
Expand Down Expand Up @@ -1859,7 +1858,7 @@ class TestMathNumExprPython(TestMathPythonPython):
parser = "python"


_var_s = randn(10)
_var_s = np.random.randn(10)


class TestScope:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/multiindex/test_insert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from numpy.random import randn
import numpy as np

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

tuples = sorted(zip(*arrays))
index = MultiIndex.from_tuples(tuples)
df = DataFrame(randn(4, 6), columns=index)
df = DataFrame(np.random.randn(4, 6), columns=index)

result = df.copy()
expected = df.copy()
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexing/multiindex/test_setitem.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
from numpy.random import randn
import pytest

import pandas as pd
Expand Down Expand Up @@ -311,7 +310,9 @@ def test_frame_getitem_setitem_multislice(self):
tm.assert_frame_equal(df, result)

def test_frame_setitem_multi_column(self):
df = DataFrame(randn(10, 4), columns=[["a", "a", "b", "b"], [0, 1, 0, 1]])
df = DataFrame(
np.random.randn(10, 4), columns=[["a", "a", "b", "b"], [0, 1, 0, 1]]
)

cp = df.copy()
cp["a"] = cp["b"]
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexing/multiindex/test_sorted.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
from numpy.random import randn
import pytest

from pandas import DataFrame, MultiIndex, Series
Expand Down Expand Up @@ -115,7 +114,7 @@ def test_series_getitem_not_sorted(self):
]
tuples = zip(*arrays)
index = MultiIndex.from_tuples(tuples)
s = Series(randn(8), index=index)
s = Series(np.random.randn(8), index=index)

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

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/io/test_clipboard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from textwrap import dedent

import numpy as np
from numpy.random import randint
import pytest

import pandas as pd
Expand Down Expand Up @@ -54,7 +53,7 @@ def df(request):
return tm.makeCustomDataframe(
max_rows + 1,
3,
data_gen_f=lambda *args: randint(2),
data_gen_f=lambda *args: np.random.randint(2),
c_idx_type="s",
r_idx_type="i",
c_idx_names=[None],
Expand Down Expand Up @@ -95,7 +94,7 @@ def df(request):
return tm.makeCustomDataframe(
5,
3,
data_gen_f=lambda *args: randint(2),
data_gen_f=lambda *args: np.random.randint(2),
c_idx_type="s",
r_idx_type="i",
c_idx_names=[None],
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from urllib.error import URLError

import numpy as np
from numpy.random import rand
import pytest

from pandas.compat import is_platform_windows
Expand Down Expand Up @@ -110,7 +109,7 @@ def test_to_html_compat(self):
tm.makeCustomDataframe(
4,
3,
data_gen_f=lambda *args: rand(),
data_gen_f=lambda *args: np.random.rand(),
c_idx_names=False,
r_idx_names=False,
)
Expand Down
9 changes: 4 additions & 5 deletions pandas/tests/plotting/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import warnings

import numpy as np
from numpy import random

from pandas.util._decorators import cache_readonly
import pandas.util._test_decorators as td
Expand Down Expand Up @@ -50,11 +49,11 @@ def setup_method(self, method):
{
"gender": gender,
"classroom": classroom,
"height": random.normal(66, 4, size=n),
"weight": random.normal(161, 32, size=n),
"category": random.randint(4, size=n),
"height": np.random.normal(66, 4, size=n),
"weight": np.random.normal(161, 32, size=n),
"category": np.random.randint(4, size=n),
"datetime": to_datetime(
random.randint(
np.random.randint(
self.start_date_to_int64,
self.end_date_to_int64,
size=n,
Expand Down
Loading