Skip to content

Commit

Permalink
avoid non-standard imports (pandas-dev#24822)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and Pingviinituutti committed Feb 28, 2019
1 parent 287a5d7 commit 4ba6f6a
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 302 deletions.
1 change: 1 addition & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
# Check for imports from pandas.core.common instead of `import pandas.core.common as com`
MSG='Check for non-standard imports' ; echo $MSG
invgrep -R --include="*.py*" -E "from pandas.core.common import " pandas
# invgrep -R --include="*.py*" -E "from numpy import nan " pandas # GH#24822 not yet implemented since the offending imports have not all been removed
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Check for pytest warns' ; echo $MSG
Expand Down
71 changes: 37 additions & 34 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import warnings

import numpy as np
from numpy import nan
from numpy.random import randn
import pytest

from pandas.compat import PY35, lrange
Expand Down Expand Up @@ -240,22 +238,22 @@ class TestDataFrameAnalytics():

@td.skip_if_no_scipy
def test_corr_pearson(self, float_frame):
float_frame['A'][:5] = nan
float_frame['B'][5:10] = nan
float_frame['A'][:5] = np.nan
float_frame['B'][5:10] = np.nan

self._check_method(float_frame, 'pearson')

@td.skip_if_no_scipy
def test_corr_kendall(self, float_frame):
float_frame['A'][:5] = nan
float_frame['B'][5:10] = nan
float_frame['A'][:5] = np.nan
float_frame['B'][5:10] = np.nan

self._check_method(float_frame, 'kendall')

@td.skip_if_no_scipy
def test_corr_spearman(self, float_frame):
float_frame['A'][:5] = nan
float_frame['B'][5:10] = nan
float_frame['A'][:5] = np.nan
float_frame['B'][5:10] = np.nan

self._check_method(float_frame, 'spearman')

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

@td.skip_if_no_scipy
def test_corr_non_numeric(self, float_frame, float_string_frame):
float_frame['A'][:5] = nan
float_frame['B'][5:10] = nan
float_frame['A'][:5] = np.nan
float_frame['B'][5:10] = np.nan

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

# with NAs
frame = float_frame.copy()
frame['A'][:5] = nan
frame['B'][5:10] = nan
frame['A'][:5] = np.nan
frame['B'][5:10] = np.nan
result = float_frame.cov(min_periods=len(float_frame) - 8)
expected = float_frame.cov()
expected.loc['A', 'B'] = np.nan
expected.loc['B', 'A'] = np.nan

# regular
float_frame['A'][:5] = nan
float_frame['B'][:10] = nan
float_frame['A'][:5] = np.nan
float_frame['B'][:10] = np.nan
cov = float_frame.cov()

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

def test_corrwith(self, datetime_frame):
a = datetime_frame
noise = Series(randn(len(a)), index=a.index)
noise = Series(np.random.randn(len(a)), index=a.index)

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

Expand All @@ -409,8 +407,9 @@ def test_corrwith(self, datetime_frame):
# non time-series data
index = ['a', 'b', 'c', 'd', 'e']
columns = ['one', 'two', 'three', 'four']
df1 = DataFrame(randn(5, 4), index=index, columns=columns)
df2 = DataFrame(randn(4, 4), index=index[:4], columns=columns)
df1 = DataFrame(np.random.randn(5, 4), index=index, columns=columns)
df2 = DataFrame(np.random.randn(4, 4),
index=index[:4], columns=columns)
correls = df1.corrwith(df2, axis=1)
for row in index[:4]:
tm.assert_almost_equal(correls[row],
Expand Down Expand Up @@ -823,9 +822,9 @@ def test_min(self, float_frame_with_na, int_frame,
assert_stat_op_api('min', float_frame, float_string_frame)

def test_cummin(self, datetime_frame):
datetime_frame.loc[5:10, 0] = nan
datetime_frame.loc[10:15, 1] = nan
datetime_frame.loc[15:, 2] = nan
datetime_frame.loc[5:10, 0] = np.nan
datetime_frame.loc[10:15, 1] = np.nan
datetime_frame.loc[15:, 2] = np.nan

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

def test_cummax(self, datetime_frame):
datetime_frame.loc[5:10, 0] = nan
datetime_frame.loc[10:15, 1] = nan
datetime_frame.loc[15:, 2] = nan
datetime_frame.loc[5:10, 0] = np.nan
datetime_frame.loc[10:15, 1] = np.nan
datetime_frame.loc[15:, 2] = np.nan

# axis = 0
cummax = datetime_frame.cummax()
Expand Down Expand Up @@ -950,9 +949,9 @@ def test_mixed_ops(self, op):
assert len(result) == 2

def test_cumsum(self, datetime_frame):
datetime_frame.loc[5:10, 0] = nan
datetime_frame.loc[10:15, 1] = nan
datetime_frame.loc[15:, 2] = nan
datetime_frame.loc[5:10, 0] = np.nan
datetime_frame.loc[10:15, 1] = np.nan
datetime_frame.loc[15:, 2] = np.nan

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

def test_cumprod(self, datetime_frame):
datetime_frame.loc[5:10, 0] = nan
datetime_frame.loc[10:15, 1] = nan
datetime_frame.loc[15:, 2] = nan
datetime_frame.loc[5:10, 0] = np.nan
datetime_frame.loc[10:15, 1] = np.nan
datetime_frame.loc[15:, 2] = np.nan

# axis = 0
cumprod = datetime_frame.cumprod()
Expand Down Expand Up @@ -1753,7 +1752,7 @@ def test_round(self):
expected_neg_rounded)

# nan in Series round
nan_round_Series = Series({'col1': nan, 'col2': 1})
nan_round_Series = Series({'col1': np.nan, 'col2': 1})

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

# unaligned
df = DataFrame(randn(3, 4), index=[1, 2, 3], columns=lrange(4))
df2 = DataFrame(randn(5, 3), index=lrange(5), columns=[1, 2, 3])
df = DataFrame(np.random.randn(3, 4),
index=[1, 2, 3], columns=lrange(4))
df2 = DataFrame(np.random.randn(5, 3),
index=lrange(5), columns=[1, 2, 3])

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

# unaligned
df = DataFrame(randn(3, 4), index=[1, 2, 3], columns=lrange(4))
df2 = DataFrame(randn(5, 3), index=lrange(5), columns=[1, 2, 3])
df = DataFrame(np.random.randn(3, 4),
index=[1, 2, 3], columns=lrange(4))
df2 = DataFrame(np.random.randn(5, 3),
index=lrange(5), columns=[1, 2, 3])

with pytest.raises(ValueError, match='aligned'):
operator.matmul(df, df2)
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pydoc

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

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

def test_new_empty_index(self):
df1 = self.klass(randn(0, 3))
df2 = self.klass(randn(0, 3))
df1 = self.klass(np.random.randn(0, 3))
df2 = self.klass(np.random.randn(0, 3))
df1.index.name = 'foo'
assert df2.index.name is None

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from datetime import datetime

import numpy as np
from numpy import random
import pytest

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

def test_reindex_name_remains(self):
s = Series(random.rand(10))
s = Series(np.random.rand(10))
df = DataFrame(s, index=np.arange(len(s)))
i = Series(np.arange(10), name='iname')

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

s = Series(random.rand(10))
s = Series(np.random.rand(10))
df = DataFrame(s.T, index=np.arange(len(s)))
i = Series(np.arange(10), name='iname')
df = df.reindex(columns=i)
Expand Down
9 changes: 4 additions & 5 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import itertools

import numpy as np
from numpy import nan
import pytest

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

# f7u12, this does not work without extensive workaround
data = [[datetime(2001, 1, 5), nan, datetime(2001, 1, 2)],
data = [[datetime(2001, 1, 5), np.nan, datetime(2001, 1, 2)],
[datetime(2000, 1, 2), datetime(2000, 1, 3),
datetime(2000, 1, 1)]]
df = DataFrame(data)
Expand Down Expand Up @@ -558,18 +557,18 @@ def test_get_X_columns(self):
def test_strange_column_corruption_issue(self):
# (wesm) Unclear how exactly this is related to internal matters
df = DataFrame(index=[0, 1])
df[0] = nan
df[0] = np.nan
wasCol = {}
# uncommenting these makes the results match
# for col in xrange(100, 200):
# wasCol[col] = 1
# df[col] = nan
# df[col] = np.nan

for i, dt in enumerate(df.index):
for col in range(100, 200):
if col not in wasCol:
wasCol[col] = 1
df[col] = nan
df[col] = np.nan
df[col][dt] = i

myid = 100
Expand Down
55 changes: 27 additions & 28 deletions pandas/tests/frame/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from datetime import datetime

import numpy as np
from numpy import nan
import pytest

from pandas.compat import lrange
Expand Down Expand Up @@ -247,20 +246,20 @@ def test_append_dtypes(self):
assert_frame_equal(result, expected)

def test_update(self):
df = DataFrame([[1.5, nan, 3.],
[1.5, nan, 3.],
[1.5, nan, 3],
[1.5, nan, 3]])
df = DataFrame([[1.5, np.nan, 3.],
[1.5, np.nan, 3.],
[1.5, np.nan, 3],
[1.5, np.nan, 3]])

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

df.update(other)

expected = DataFrame([[1.5, nan, 3],
expected = DataFrame([[1.5, np.nan, 3],
[3.6, 2, 3],
[1.5, nan, 3],
[1.5, nan, 7.]])
[1.5, np.nan, 3],
[1.5, np.nan, 7.]])
assert_frame_equal(df, expected)

def test_update_dtypes(self):
Expand All @@ -277,37 +276,37 @@ def test_update_dtypes(self):
assert_frame_equal(df, expected)

def test_update_nooverwrite(self):
df = DataFrame([[1.5, nan, 3.],
[1.5, nan, 3.],
[1.5, nan, 3],
[1.5, nan, 3]])
df = DataFrame([[1.5, np.nan, 3.],
[1.5, np.nan, 3.],
[1.5, np.nan, 3],
[1.5, np.nan, 3]])

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

df.update(other, overwrite=False)

expected = DataFrame([[1.5, nan, 3],
expected = DataFrame([[1.5, np.nan, 3],
[1.5, 2, 3],
[1.5, nan, 3],
[1.5, nan, 3.]])
[1.5, np.nan, 3],
[1.5, np.nan, 3.]])
assert_frame_equal(df, expected)

def test_update_filtered(self):
df = DataFrame([[1.5, nan, 3.],
[1.5, nan, 3.],
[1.5, nan, 3],
[1.5, nan, 3]])
df = DataFrame([[1.5, np.nan, 3.],
[1.5, np.nan, 3.],
[1.5, np.nan, 3],
[1.5, np.nan, 3]])

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

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

expected = DataFrame([[1.5, nan, 3],
[1.5, nan, 3],
[1.5, nan, 3],
[1.5, nan, 7.]])
expected = DataFrame([[1.5, np.nan, 3],
[1.5, np.nan, 3],
[1.5, np.nan, 3],
[1.5, np.nan, 7.]])
assert_frame_equal(df, expected)

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

def test_update_raise_on_overlap(self):
df = DataFrame([[1.5, 1, 3.],
[1.5, nan, 3.],
[1.5, nan, 3],
[1.5, nan, 3]])
[1.5, np.nan, 3.],
[1.5, np.nan, 3],
[1.5, np.nan, 3]])

other = DataFrame([[2., nan],
[nan, 7]], index=[1, 3], columns=[1, 2])
other = DataFrame([[2., np.nan],
[np.nan, 7]], index=[1, 3], columns=[1, 2])
with pytest.raises(ValueError, match="Data overlaps"):
df.update(other, errors='raise')

Expand Down
Loading

0 comments on commit 4ba6f6a

Please sign in to comment.