Skip to content

Commit 1dcfc5f

Browse files
jbrockmendeljreback
authored andcommitted
avoid non-standard imports (#24822)
1 parent e984947 commit 1dcfc5f

15 files changed

+293
-302
lines changed

ci/code_checks.sh

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
112112
# Check for imports from pandas.core.common instead of `import pandas.core.common as com`
113113
MSG='Check for non-standard imports' ; echo $MSG
114114
invgrep -R --include="*.py*" -E "from pandas.core.common import " pandas
115+
# invgrep -R --include="*.py*" -E "from numpy import nan " pandas # GH#24822 not yet implemented since the offending imports have not all been removed
115116
RET=$(($RET + $?)) ; echo $MSG "DONE"
116117

117118
MSG='Check for pytest warns' ; echo $MSG

pandas/tests/frame/test_analytics.py

+37-34
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import warnings
77

88
import numpy as np
9-
from numpy import nan
10-
from numpy.random import randn
119
import pytest
1210

1311
from pandas.compat import PY35, lrange
@@ -240,22 +238,22 @@ class TestDataFrameAnalytics():
240238

241239
@td.skip_if_no_scipy
242240
def test_corr_pearson(self, float_frame):
243-
float_frame['A'][:5] = nan
244-
float_frame['B'][5:10] = nan
241+
float_frame['A'][:5] = np.nan
242+
float_frame['B'][5:10] = np.nan
245243

246244
self._check_method(float_frame, 'pearson')
247245

248246
@td.skip_if_no_scipy
249247
def test_corr_kendall(self, float_frame):
250-
float_frame['A'][:5] = nan
251-
float_frame['B'][5:10] = nan
248+
float_frame['A'][:5] = np.nan
249+
float_frame['B'][5:10] = np.nan
252250

253251
self._check_method(float_frame, 'kendall')
254252

255253
@td.skip_if_no_scipy
256254
def test_corr_spearman(self, float_frame):
257-
float_frame['A'][:5] = nan
258-
float_frame['B'][5:10] = nan
255+
float_frame['A'][:5] = np.nan
256+
float_frame['B'][5:10] = np.nan
259257

260258
self._check_method(float_frame, 'spearman')
261259

@@ -266,8 +264,8 @@ def _check_method(self, frame, method='pearson'):
266264

267265
@td.skip_if_no_scipy
268266
def test_corr_non_numeric(self, float_frame, float_string_frame):
269-
float_frame['A'][:5] = nan
270-
float_frame['B'][5:10] = nan
267+
float_frame['A'][:5] = np.nan
268+
float_frame['B'][5:10] = np.nan
271269

272270
# exclude non-numeric types
273271
result = float_string_frame.corr()
@@ -351,16 +349,16 @@ def test_cov(self, float_frame, float_string_frame):
351349

352350
# with NAs
353351
frame = float_frame.copy()
354-
frame['A'][:5] = nan
355-
frame['B'][5:10] = nan
352+
frame['A'][:5] = np.nan
353+
frame['B'][5:10] = np.nan
356354
result = float_frame.cov(min_periods=len(float_frame) - 8)
357355
expected = float_frame.cov()
358356
expected.loc['A', 'B'] = np.nan
359357
expected.loc['B', 'A'] = np.nan
360358

361359
# regular
362-
float_frame['A'][:5] = nan
363-
float_frame['B'][:10] = nan
360+
float_frame['A'][:5] = np.nan
361+
float_frame['B'][:10] = np.nan
364362
cov = float_frame.cov()
365363

366364
tm.assert_almost_equal(cov['A']['C'],
@@ -385,7 +383,7 @@ def test_cov(self, float_frame, float_string_frame):
385383

386384
def test_corrwith(self, datetime_frame):
387385
a = datetime_frame
388-
noise = Series(randn(len(a)), index=a.index)
386+
noise = Series(np.random.randn(len(a)), index=a.index)
389387

390388
b = datetime_frame.add(noise, axis=0)
391389

@@ -409,8 +407,9 @@ def test_corrwith(self, datetime_frame):
409407
# non time-series data
410408
index = ['a', 'b', 'c', 'd', 'e']
411409
columns = ['one', 'two', 'three', 'four']
412-
df1 = DataFrame(randn(5, 4), index=index, columns=columns)
413-
df2 = DataFrame(randn(4, 4), index=index[:4], columns=columns)
410+
df1 = DataFrame(np.random.randn(5, 4), index=index, columns=columns)
411+
df2 = DataFrame(np.random.randn(4, 4),
412+
index=index[:4], columns=columns)
414413
correls = df1.corrwith(df2, axis=1)
415414
for row in index[:4]:
416415
tm.assert_almost_equal(correls[row],
@@ -823,9 +822,9 @@ def test_min(self, float_frame_with_na, int_frame,
823822
assert_stat_op_api('min', float_frame, float_string_frame)
824823

825824
def test_cummin(self, datetime_frame):
826-
datetime_frame.loc[5:10, 0] = nan
827-
datetime_frame.loc[10:15, 1] = nan
828-
datetime_frame.loc[15:, 2] = nan
825+
datetime_frame.loc[5:10, 0] = np.nan
826+
datetime_frame.loc[10:15, 1] = np.nan
827+
datetime_frame.loc[15:, 2] = np.nan
829828

830829
# axis = 0
831830
cummin = datetime_frame.cummin()
@@ -846,9 +845,9 @@ def test_cummin(self, datetime_frame):
846845
assert np.shape(cummin_xs) == np.shape(datetime_frame)
847846

848847
def test_cummax(self, datetime_frame):
849-
datetime_frame.loc[5:10, 0] = nan
850-
datetime_frame.loc[10:15, 1] = nan
851-
datetime_frame.loc[15:, 2] = nan
848+
datetime_frame.loc[5:10, 0] = np.nan
849+
datetime_frame.loc[10:15, 1] = np.nan
850+
datetime_frame.loc[15:, 2] = np.nan
852851

853852
# axis = 0
854853
cummax = datetime_frame.cummax()
@@ -950,9 +949,9 @@ def test_mixed_ops(self, op):
950949
assert len(result) == 2
951950

952951
def test_cumsum(self, datetime_frame):
953-
datetime_frame.loc[5:10, 0] = nan
954-
datetime_frame.loc[10:15, 1] = nan
955-
datetime_frame.loc[15:, 2] = nan
952+
datetime_frame.loc[5:10, 0] = np.nan
953+
datetime_frame.loc[10:15, 1] = np.nan
954+
datetime_frame.loc[15:, 2] = np.nan
956955

957956
# axis = 0
958957
cumsum = datetime_frame.cumsum()
@@ -973,9 +972,9 @@ def test_cumsum(self, datetime_frame):
973972
assert np.shape(cumsum_xs) == np.shape(datetime_frame)
974973

975974
def test_cumprod(self, datetime_frame):
976-
datetime_frame.loc[5:10, 0] = nan
977-
datetime_frame.loc[10:15, 1] = nan
978-
datetime_frame.loc[15:, 2] = nan
975+
datetime_frame.loc[5:10, 0] = np.nan
976+
datetime_frame.loc[10:15, 1] = np.nan
977+
datetime_frame.loc[15:, 2] = np.nan
979978

980979
# axis = 0
981980
cumprod = datetime_frame.cumprod()
@@ -1753,7 +1752,7 @@ def test_round(self):
17531752
expected_neg_rounded)
17541753

17551754
# nan in Series round
1756-
nan_round_Series = Series({'col1': nan, 'col2': 1})
1755+
nan_round_Series = Series({'col1': np.nan, 'col2': 1})
17571756

17581757
# TODO(wesm): unused?
17591758
expected_nan_round = DataFrame({ # noqa
@@ -2084,8 +2083,10 @@ def test_dot(self):
20842083
result = A.dot(b)
20852084

20862085
# unaligned
2087-
df = DataFrame(randn(3, 4), index=[1, 2, 3], columns=lrange(4))
2088-
df2 = DataFrame(randn(5, 3), index=lrange(5), columns=[1, 2, 3])
2086+
df = DataFrame(np.random.randn(3, 4),
2087+
index=[1, 2, 3], columns=lrange(4))
2088+
df2 = DataFrame(np.random.randn(5, 3),
2089+
index=lrange(5), columns=[1, 2, 3])
20892090

20902091
with pytest.raises(ValueError, match='aligned'):
20912092
df.dot(df2)
@@ -2144,8 +2145,10 @@ def test_matmul(self):
21442145
tm.assert_frame_equal(result, expected)
21452146

21462147
# unaligned
2147-
df = DataFrame(randn(3, 4), index=[1, 2, 3], columns=lrange(4))
2148-
df2 = DataFrame(randn(5, 3), index=lrange(5), columns=[1, 2, 3])
2148+
df = DataFrame(np.random.randn(3, 4),
2149+
index=[1, 2, 3], columns=lrange(4))
2150+
df2 = DataFrame(np.random.randn(5, 3),
2151+
index=lrange(5), columns=[1, 2, 3])
21492152

21502153
with pytest.raises(ValueError, match='aligned'):
21512154
operator.matmul(df, df2)

pandas/tests/frame/test_api.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import pydoc
88

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

1312
from pandas.compat import long, lrange, range
@@ -149,8 +148,8 @@ def test_not_hashable(self, empty_frame):
149148
pytest.raises(TypeError, hash, empty_frame)
150149

151150
def test_new_empty_index(self):
152-
df1 = self.klass(randn(0, 3))
153-
df2 = self.klass(randn(0, 3))
151+
df1 = self.klass(np.random.randn(0, 3))
152+
df2 = self.klass(np.random.randn(0, 3))
154153
df1.index.name = 'foo'
155154
assert df2.index.name is None
156155

pandas/tests/frame/test_axis_select_reindex.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from datetime import datetime
66

77
import numpy as np
8-
from numpy import random
98
import pytest
109

1110
from pandas.compat import lrange, lzip, u
@@ -289,7 +288,7 @@ def test_reindex_nan(self):
289288
assert_frame_equal(left, right)
290289

291290
def test_reindex_name_remains(self):
292-
s = Series(random.rand(10))
291+
s = Series(np.random.rand(10))
293292
df = DataFrame(s, index=np.arange(len(s)))
294293
i = Series(np.arange(10), name='iname')
295294

@@ -299,7 +298,7 @@ def test_reindex_name_remains(self):
299298
df = df.reindex(Index(np.arange(10), name='tmpname'))
300299
assert df.index.name == 'tmpname'
301300

302-
s = Series(random.rand(10))
301+
s = Series(np.random.rand(10))
303302
df = DataFrame(s.T, index=np.arange(len(s)))
304303
i = Series(np.arange(10), name='iname')
305304
df = df.reindex(columns=i)

pandas/tests/frame/test_block_internals.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import itertools
77

88
import numpy as np
9-
from numpy import nan
109
import pytest
1110

1211
from pandas.compat import StringIO
@@ -216,7 +215,7 @@ def test_construction_with_mixed(self, float_string_frame):
216215
# test construction edge cases with mixed types
217216

218217
# f7u12, this does not work without extensive workaround
219-
data = [[datetime(2001, 1, 5), nan, datetime(2001, 1, 2)],
218+
data = [[datetime(2001, 1, 5), np.nan, datetime(2001, 1, 2)],
220219
[datetime(2000, 1, 2), datetime(2000, 1, 3),
221220
datetime(2000, 1, 1)]]
222221
df = DataFrame(data)
@@ -558,18 +557,18 @@ def test_get_X_columns(self):
558557
def test_strange_column_corruption_issue(self):
559558
# (wesm) Unclear how exactly this is related to internal matters
560559
df = DataFrame(index=[0, 1])
561-
df[0] = nan
560+
df[0] = np.nan
562561
wasCol = {}
563562
# uncommenting these makes the results match
564563
# for col in xrange(100, 200):
565564
# wasCol[col] = 1
566-
# df[col] = nan
565+
# df[col] = np.nan
567566

568567
for i, dt in enumerate(df.index):
569568
for col in range(100, 200):
570569
if col not in wasCol:
571570
wasCol[col] = 1
572-
df[col] = nan
571+
df[col] = np.nan
573572
df[col][dt] = i
574573

575574
myid = 100

pandas/tests/frame/test_combine_concat.py

+27-28
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from datetime import datetime
66

77
import numpy as np
8-
from numpy import nan
98
import pytest
109

1110
from pandas.compat import lrange
@@ -247,20 +246,20 @@ def test_append_dtypes(self):
247246
assert_frame_equal(result, expected)
248247

249248
def test_update(self):
250-
df = DataFrame([[1.5, nan, 3.],
251-
[1.5, nan, 3.],
252-
[1.5, nan, 3],
253-
[1.5, nan, 3]])
249+
df = DataFrame([[1.5, np.nan, 3.],
250+
[1.5, np.nan, 3.],
251+
[1.5, np.nan, 3],
252+
[1.5, np.nan, 3]])
254253

255254
other = DataFrame([[3.6, 2., np.nan],
256255
[np.nan, np.nan, 7]], index=[1, 3])
257256

258257
df.update(other)
259258

260-
expected = DataFrame([[1.5, nan, 3],
259+
expected = DataFrame([[1.5, np.nan, 3],
261260
[3.6, 2, 3],
262-
[1.5, nan, 3],
263-
[1.5, nan, 7.]])
261+
[1.5, np.nan, 3],
262+
[1.5, np.nan, 7.]])
264263
assert_frame_equal(df, expected)
265264

266265
def test_update_dtypes(self):
@@ -277,37 +276,37 @@ def test_update_dtypes(self):
277276
assert_frame_equal(df, expected)
278277

279278
def test_update_nooverwrite(self):
280-
df = DataFrame([[1.5, nan, 3.],
281-
[1.5, nan, 3.],
282-
[1.5, nan, 3],
283-
[1.5, nan, 3]])
279+
df = DataFrame([[1.5, np.nan, 3.],
280+
[1.5, np.nan, 3.],
281+
[1.5, np.nan, 3],
282+
[1.5, np.nan, 3]])
284283

285284
other = DataFrame([[3.6, 2., np.nan],
286285
[np.nan, np.nan, 7]], index=[1, 3])
287286

288287
df.update(other, overwrite=False)
289288

290-
expected = DataFrame([[1.5, nan, 3],
289+
expected = DataFrame([[1.5, np.nan, 3],
291290
[1.5, 2, 3],
292-
[1.5, nan, 3],
293-
[1.5, nan, 3.]])
291+
[1.5, np.nan, 3],
292+
[1.5, np.nan, 3.]])
294293
assert_frame_equal(df, expected)
295294

296295
def test_update_filtered(self):
297-
df = DataFrame([[1.5, nan, 3.],
298-
[1.5, nan, 3.],
299-
[1.5, nan, 3],
300-
[1.5, nan, 3]])
296+
df = DataFrame([[1.5, np.nan, 3.],
297+
[1.5, np.nan, 3.],
298+
[1.5, np.nan, 3],
299+
[1.5, np.nan, 3]])
301300

302301
other = DataFrame([[3.6, 2., np.nan],
303302
[np.nan, np.nan, 7]], index=[1, 3])
304303

305304
df.update(other, filter_func=lambda x: x > 2)
306305

307-
expected = DataFrame([[1.5, nan, 3],
308-
[1.5, nan, 3],
309-
[1.5, nan, 3],
310-
[1.5, nan, 7.]])
306+
expected = DataFrame([[1.5, np.nan, 3],
307+
[1.5, np.nan, 3],
308+
[1.5, np.nan, 3],
309+
[1.5, np.nan, 7.]])
311310
assert_frame_equal(df, expected)
312311

313312
@pytest.mark.parametrize('bad_kwarg, exception, msg', [
@@ -322,12 +321,12 @@ def test_update_raise_bad_parameter(self, bad_kwarg, exception, msg):
322321

323322
def test_update_raise_on_overlap(self):
324323
df = DataFrame([[1.5, 1, 3.],
325-
[1.5, nan, 3.],
326-
[1.5, nan, 3],
327-
[1.5, nan, 3]])
324+
[1.5, np.nan, 3.],
325+
[1.5, np.nan, 3],
326+
[1.5, np.nan, 3]])
328327

329-
other = DataFrame([[2., nan],
330-
[nan, 7]], index=[1, 3], columns=[1, 2])
328+
other = DataFrame([[2., np.nan],
329+
[np.nan, 7]], index=[1, 3], columns=[1, 2])
331330
with pytest.raises(ValueError, match="Data overlaps"):
332331
df.update(other, errors='raise')
333332

0 commit comments

Comments
 (0)