diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 3e62a08975dad..c8bfc564e7573 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -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 diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 244e8f83bea37..f2c3f50c291c3 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -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 @@ -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') @@ -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() @@ -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'], @@ -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) @@ -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], @@ -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() @@ -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() @@ -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() @@ -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() @@ -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 @@ -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) @@ -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) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index c1be64829c303..0934dd20638e4 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -7,7 +7,6 @@ import pydoc import numpy as np -from numpy.random import randn import pytest from pandas.compat import long, lrange, range @@ -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 diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index 508a68d44bb04..dea925dcde676 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -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 @@ -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') @@ -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) diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index 67f27948343f7..5419f4d5127f6 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -6,7 +6,6 @@ import itertools import numpy as np -from numpy import nan import pytest from pandas.compat import StringIO @@ -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) @@ -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 diff --git a/pandas/tests/frame/test_combine_concat.py b/pandas/tests/frame/test_combine_concat.py index b38acbf5dd72f..59497153c8524 100644 --- a/pandas/tests/frame/test_combine_concat.py +++ b/pandas/tests/frame/test_combine_concat.py @@ -5,7 +5,6 @@ from datetime import datetime import numpy as np -from numpy import nan import pytest from pandas.compat import lrange @@ -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): @@ -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', [ @@ -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') diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 4e0143c368e10..4f6a2e2bfbebf 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -8,8 +8,6 @@ import numpy as np import numpy.ma as ma -import numpy.ma.mrecords as mrecords -from numpy.random import randn import pytest from pandas.compat import ( @@ -489,7 +487,7 @@ def test_constructor_dict_cast(self): # can't cast to float test_data = { 'A': dict(zip(range(20), tm.makeStringIndex(20))), - 'B': dict(zip(range(15), randn(15))) + 'B': dict(zip(range(15), np.random.randn(15))) } frame = DataFrame(test_data, dtype=float) assert len(frame) == 20 @@ -605,7 +603,7 @@ def test_constructor_period(self): def test_nested_dict_frame_constructor(self): rng = pd.period_range('1/1/2000', periods=5) - df = DataFrame(randn(10, 5), columns=rng) + df = DataFrame(np.random.randn(10, 5), columns=rng) data = {} for col in df.columns: @@ -812,7 +810,7 @@ def test_constructor_mrecarray(self): # call assert_frame_equal for all selections of 3 arrays for comb in itertools.combinations(arrays, 3): names, data = zip(*comb) - mrecs = mrecords.fromarrays(data, names=names) + mrecs = ma.mrecords.fromarrays(data, names=names) # fill the comb comb = {k: (v.filled() if hasattr(v, 'filled') else v) @@ -859,7 +857,7 @@ def test_constructor_scalar_inference(self): assert df['object'].dtype == np.object_ def test_constructor_arrays_and_scalars(self): - df = DataFrame({'a': randn(10), 'b': True}) + df = DataFrame({'a': np.random.randn(10), 'b': True}) exp = DataFrame({'a': df['a'].values, 'b': [True] * 10}) tm.assert_frame_equal(df, exp) @@ -875,11 +873,11 @@ def test_constructor_DataFrame(self): def test_constructor_more(self): # used to be in test_matrix.py - arr = randn(10) + arr = np.random.randn(10) dm = DataFrame(arr, columns=['A'], index=np.arange(10)) assert dm.values.ndim == 2 - arr = randn(0) + arr = np.random.randn(0) dm = DataFrame(arr) assert dm.values.ndim == 2 assert dm.values.ndim == 2 @@ -1140,8 +1138,8 @@ class CustomDict(dict): tm.assert_frame_equal(result, result_custom) def test_constructor_ragged(self): - data = {'A': randn(10), - 'B': randn(8)} + data = {'A': np.random.randn(10), + 'B': np.random.randn(8)} with pytest.raises(ValueError, match='arrays must all be same length'): DataFrame(data) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index f113140261aea..19b8ae4eb6e0f 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -6,8 +6,6 @@ from warnings import catch_warnings, simplefilter import numpy as np -from numpy import nan -from numpy.random import randn import pytest from pandas._libs.tslib import iNaT @@ -50,9 +48,9 @@ def test_getitem(self): self.frame['random'] df = self.frame.copy() - df['$10'] = randn(len(df)) + df['$10'] = np.random.randn(len(df)) - ad = randn(len(df)) + ad = np.random.randn(len(df)) df['@awesome_domain'] = ad with pytest.raises(KeyError): @@ -103,7 +101,7 @@ def test_getitem_listlike(self, idx_type, levels): frame, missing = self.frame, 'food' else: # MultiIndex columns - frame = DataFrame(randn(8, 3), + frame = DataFrame(np.random.randn(8, 3), columns=Index([('foo', 'bar'), ('baz', 'qux'), ('peek', 'aboo')], name=('sth', 'sth2'))) @@ -338,7 +336,7 @@ def _checkit(lst): _checkit([False, False, False]) def test_getitem_boolean_iadd(self): - arr = randn(5, 5) + arr = np.random.randn(5, 5) df = DataFrame(arr.copy(), columns=['A', 'B', 'C', 'D', 'E']) @@ -419,7 +417,8 @@ def test_getitem_setitem_ix_negative_integers(self): df.ix[:, [-1]] # #1942 - a = DataFrame(randn(20, 2), index=[chr(x + 65) for x in range(20)]) + a = DataFrame(np.random.randn(20, 2), + index=[chr(x + 65) for x in range(20)]) with catch_warnings(record=True): simplefilter("ignore", DeprecationWarning) a.ix[-1] = a.ix[-2] @@ -459,10 +458,10 @@ def test_setitem(self): tm.assert_series_equal(series, self.frame['col6'], check_names=False) with pytest.raises(KeyError): - self.frame[randn(len(self.frame) + 1)] = 1 + self.frame[np.random.randn(len(self.frame) + 1)] = 1 # set ndarray - arr = randn(len(self.frame)) + arr = np.random.randn(len(self.frame)) self.frame['col9'] = arr assert (self.frame['col9'] == arr).all() @@ -497,7 +496,7 @@ def test_setitem(self): @pytest.mark.parametrize("dtype", ["int32", "int64", "float32", "float64"]) def test_setitem_dtype(self, dtype): - arr = randn(len(self.frame)) + arr = np.random.randn(len(self.frame)) self.frame[dtype] = np.array(arr, dtype=dtype) assert self.frame[dtype].dtype.name == dtype @@ -511,7 +510,7 @@ def test_setitem_always_copy(self): s = self.frame['A'].copy() self.frame['E'] = s - self.frame['E'][5:10] = nan + self.frame['E'][5:10] = np.nan assert notna(s[5:10]).all() def test_setitem_boolean(self): @@ -554,8 +553,8 @@ def test_setitem_boolean(self): # index with DataFrame mask = df > np.abs(df) expected = df.copy() - df[df > np.abs(df)] = nan - expected.values[mask.values] = nan + df[df > np.abs(df)] = np.nan + expected.values[mask.values] = np.nan assert_frame_equal(df, expected) # set from DataFrame @@ -911,7 +910,7 @@ def test_setitem_fancy_2d(self): expected = frame.copy() subidx = self.frame.index[[5, 4, 1]] - values = randn(3, 2) + values = np.random.randn(3, 2) with catch_warnings(record=True): simplefilter("ignore", DeprecationWarning) @@ -937,7 +936,7 @@ def test_setitem_fancy_2d(self): with catch_warnings(record=True): simplefilter("ignore", DeprecationWarning) expected2 = self.frame.copy() - arr = randn(5, len(frame.columns)) + arr = np.random.randn(5, len(frame.columns)) frame.ix[5:10] = arr expected2.values[5:10] = arr assert_frame_equal(frame, expected2) @@ -958,7 +957,7 @@ def test_setitem_fancy_2d(self): frame2 = self.frame.copy() expected = self.frame.copy() - values = randn(5, 2) + values = np.random.randn(5, 2) frame.ix[:5, ['A', 'B']] = values expected['A'][:5] = values[:, 0] @@ -1128,8 +1127,8 @@ def test_setitem_fancy_mixed_2d(self): assert_frame_equal(df, expected) def test_ix_align(self): - b = Series(randn(10), name=0).sort_values() - df_orig = DataFrame(randn(10, 4)) + b = Series(np.random.randn(10), name=0).sort_values() + df_orig = DataFrame(np.random.randn(10, 4)) df = df_orig.copy() with catch_warnings(record=True): @@ -1174,7 +1173,7 @@ def test_ix_align(self): def test_ix_frame_align(self): b = DataFrame(np.random.randn(3, 4)) - df_orig = DataFrame(randn(10, 4)) + df_orig = DataFrame(np.random.randn(10, 4)) df = df_orig.copy() with catch_warnings(record=True): @@ -1355,7 +1354,7 @@ def test_setitem_fancy_1d(self): with catch_warnings(record=True): simplefilter("ignore", DeprecationWarning) - vals = randn(5) + vals = np.random.randn(5) expected.values[5:10, 2] = vals frame.ix[5:10, 2] = vals assert_frame_equal(frame, expected) @@ -1412,7 +1411,7 @@ def test_setitem_fancy_scalar(self): ts = f[col] # noqa for idx in f.index[::5]: i = f.index.get_loc(idx) - val = randn() + val = np.random.randn() expected.values[i, j] = val ix[idx, col] = val @@ -1588,15 +1587,16 @@ def test_getitem_setitem_float_labels(self): assert (result == 0).values.all() def test_setitem_single_column_mixed(self): - df = DataFrame(randn(5, 3), index=['a', 'b', 'c', 'd', 'e'], + df = DataFrame(np.random.randn(5, 3), index=['a', 'b', 'c', 'd', 'e'], columns=['foo', 'bar', 'baz']) df['str'] = 'qux' - df.loc[df.index[::2], 'str'] = nan - expected = np.array([nan, 'qux', nan, 'qux', nan], dtype=object) + df.loc[df.index[::2], 'str'] = np.nan + expected = np.array([np.nan, 'qux', np.nan, 'qux', np.nan], + dtype=object) assert_almost_equal(df['str'].values, expected) def test_setitem_single_column_mixed_datetime(self): - df = DataFrame(randn(5, 3), index=['a', 'b', 'c', 'd', 'e'], + df = DataFrame(np.random.randn(5, 3), index=['a', 'b', 'c', 'd', 'e'], columns=['foo', 'bar', 'baz']) df['timestamp'] = Timestamp('20010102') @@ -1611,17 +1611,17 @@ def test_setitem_single_column_mixed_datetime(self): assert isna(df.loc['b', 'timestamp']) # allow this syntax - df.loc['c', 'timestamp'] = nan + df.loc['c', 'timestamp'] = np.nan assert isna(df.loc['c', 'timestamp']) # allow this syntax - df.loc['d', :] = nan + df.loc['d', :] = np.nan assert not isna(df.loc['c', :]).all() # as of GH 3216 this will now work! # try to set with a list like item # pytest.raises( - # Exception, df.loc.__setitem__, ('d', 'timestamp'), [nan]) + # Exception, df.loc.__setitem__, ('d', 'timestamp'), [np.nan]) def test_setitem_mixed_datetime(self): # GH 9336 @@ -1852,7 +1852,8 @@ def test_set_value_resize(self): pytest.raises(ValueError, res3.set_value, 'foobar', 'baz', 'sam') def test_set_value_with_index_dtype_change(self): - df_orig = DataFrame(randn(3, 3), index=lrange(3), columns=list('ABC')) + df_orig = DataFrame(np.random.randn(3, 3), + index=lrange(3), columns=list('ABC')) # this is actually ambiguous as the 2 is interpreted as a positional # so column is not created @@ -2365,7 +2366,7 @@ def test_setitem_datetimeindex_tz(self, idxer, tz_naive_fixture): def test_at_time_between_time_datetimeindex(self): index = date_range("2012-01-01", "2012-01-05", freq='30min') - df = DataFrame(randn(len(index), 5), index=index) + df = DataFrame(np.random.randn(len(index), 5), index=index) akey = time(12, 0, 0) bkey = slice(time(13, 0, 0), time(14, 0, 0)) ainds = [24, 72, 120, 168] @@ -2463,7 +2464,7 @@ def test_xs_corner(self): assert_series_equal(result, expected) def test_xs_duplicates(self): - df = DataFrame(randn(5, 2), index=['b', 'b', 'c', 'b', 'a']) + df = DataFrame(np.random.randn(5, 2), index=['b', 'b', 'c', 'b', 'a']) cross = df.xs('c') exp = df.iloc[2] @@ -3119,7 +3120,7 @@ def test_mask_edge_case_1xN_frame(self): # GH4071 df = DataFrame([[1, 2]]) res = df.mask(DataFrame([[True, False]])) - expec = DataFrame([[nan, 2]]) + expec = DataFrame([[np.nan, 2]]) assert_frame_equal(res, expec) def test_mask_callable(self): diff --git a/pandas/tests/frame/test_missing.py b/pandas/tests/frame/test_missing.py index ac4b380034366..77a3d4785d295 100644 --- a/pandas/tests/frame/test_missing.py +++ b/pandas/tests/frame/test_missing.py @@ -7,7 +7,6 @@ import dateutil import numpy as np -from numpy import nan, random import pytest from pandas.compat import lrange @@ -39,8 +38,8 @@ class TestDataFrameMissingData(TestData): def test_dropEmptyRows(self): N = len(self.frame.index) - mat = random.randn(N) - mat[:5] = nan + mat = np.random.randn(N) + mat[:5] = np.nan frame = DataFrame({'foo': mat}, index=self.frame.index) original = Series(mat, index=self.frame.index, name='foo') @@ -61,8 +60,8 @@ def test_dropEmptyRows(self): def test_dropIncompleteRows(self): N = len(self.frame.index) - mat = random.randn(N) - mat[:5] = nan + mat = np.random.randn(N) + mat[:5] = np.nan frame = DataFrame({'foo': mat}, index=self.frame.index) frame['bar'] = 5 @@ -86,7 +85,7 @@ def test_dropIncompleteRows(self): def test_dropna(self): df = DataFrame(np.random.randn(6, 4)) - df[2][:2] = nan + df[2][:2] = np.nan dropped = df.dropna(axis=1) expected = df.loc[:, [0, 1, 3]] @@ -134,7 +133,7 @@ def test_dropna(self): dropped = df.dropna(axis=1, how='all') assert_frame_equal(dropped, df) - df[2] = nan + df[2] = np.nan dropped = df.dropna(axis=1, how='all') expected = df.loc[:, [0, 1, 3]] assert_frame_equal(dropped, expected) @@ -209,8 +208,8 @@ def test_dropna_tz_aware_datetime(self): def test_fillna(self): tf = self.tsframe - tf.loc[tf.index[:5], 'A'] = nan - tf.loc[tf.index[-5:], 'A'] = nan + tf.loc[tf.index[:5], 'A'] = np.nan + tf.loc[tf.index[-5:], 'A'] = np.nan zero_filled = self.tsframe.fillna(0) assert (zero_filled.loc[zero_filled.index[:5], 'A'] == 0).all() @@ -222,8 +221,8 @@ def test_fillna(self): # mixed type mf = self.mixed_frame - mf.loc[mf.index[5:20], 'foo'] = nan - mf.loc[mf.index[-10:], 'A'] = nan + mf.loc[mf.index[5:20], 'foo'] = np.nan + mf.loc[mf.index[-10:], 'A'] = np.nan result = self.mixed_frame.fillna(value=0) result = self.mixed_frame.fillna(method='pad') @@ -232,7 +231,7 @@ def test_fillna(self): # mixed numeric (but no float16) mf = self.mixed_float.reindex(columns=['A', 'B', 'D']) - mf.loc[mf.index[-10:], 'A'] = nan + mf.loc[mf.index[-10:], 'A'] = np.nan result = mf.fillna(value=0) _check_mixed_float(result, dtype=dict(C=None)) @@ -453,15 +452,15 @@ def test_fillna_datetime_columns(self): tm.assert_frame_equal(result, expected) def test_ffill(self): - self.tsframe['A'][:5] = nan - self.tsframe['A'][-5:] = nan + self.tsframe['A'][:5] = np.nan + self.tsframe['A'][-5:] = np.nan assert_frame_equal(self.tsframe.ffill(), self.tsframe.fillna(method='ffill')) def test_bfill(self): - self.tsframe['A'][:5] = nan - self.tsframe['A'][-5:] = nan + self.tsframe['A'][:5] = np.nan + self.tsframe['A'][-5:] = np.nan assert_frame_equal(self.tsframe.bfill(), self.tsframe.fillna(method='bfill')) @@ -531,9 +530,9 @@ def test_fillna_inplace(self): tm.assert_frame_equal(df, expected) def test_fillna_dict_series(self): - df = DataFrame({'a': [nan, 1, 2, nan, nan], - 'b': [1, 2, 3, nan, nan], - 'c': [nan, 1, 2, 3, 4]}) + df = DataFrame({'a': [np.nan, 1, 2, np.nan, np.nan], + 'b': [1, 2, 3, np.nan, np.nan], + 'c': [np.nan, 1, 2, 3, 4]}) result = df.fillna({'a': 0, 'b': 5}) @@ -556,13 +555,13 @@ def test_fillna_dict_series(self): def test_fillna_dataframe(self): # GH 8377 - df = DataFrame({'a': [nan, 1, 2, nan, nan], - 'b': [1, 2, 3, nan, nan], - 'c': [nan, 1, 2, 3, 4]}, + df = DataFrame({'a': [np.nan, 1, 2, np.nan, np.nan], + 'b': [1, 2, 3, np.nan, np.nan], + 'c': [np.nan, 1, 2, 3, 4]}, index=list('VWXYZ')) # df2 may have different index and columns - df2 = DataFrame({'a': [nan, 10, 20, 30, 40], + df2 = DataFrame({'a': [np.nan, 10, 20, 30, 40], 'b': [50, 60, 70, 80, 90], 'foo': ['bar'] * 5}, index=list('VWXuZ')) @@ -570,9 +569,9 @@ def test_fillna_dataframe(self): result = df.fillna(df2) # only those columns and indices which are shared get filled - expected = DataFrame({'a': [nan, 1, 2, nan, 40], - 'b': [1, 2, 3, nan, 90], - 'c': [nan, 1, 2, 3, 4]}, + expected = DataFrame({'a': [np.nan, 1, 2, np.nan, 40], + 'b': [1, 2, 3, np.nan, 90], + 'c': [np.nan, 1, 2, 3, 4]}, index=list('VWXYZ')) assert_frame_equal(result, expected) @@ -611,8 +610,8 @@ def test_fillna_col_reordering(self): def test_fill_corner(self): mf = self.mixed_frame - mf.loc[mf.index[5:20], 'foo'] = nan - mf.loc[mf.index[-10:], 'A'] = nan + mf.loc[mf.index[5:20], 'foo'] = np.nan + mf.loc[mf.index[-10:], 'A'] = np.nan filled = self.mixed_frame.fillna(value=0) assert (filled.loc[filled.index[5:20], 'foo'] == 0).all() diff --git a/pandas/tests/frame/test_period.py b/pandas/tests/frame/test_period.py index d9392b68c8ce1..8b37d4ff2cf9e 100644 --- a/pandas/tests/frame/test_period.py +++ b/pandas/tests/frame/test_period.py @@ -1,7 +1,6 @@ from datetime import timedelta import numpy as np -from numpy.random import randn import pytest import pandas as pd @@ -19,7 +18,7 @@ class TestPeriodIndex(object): def test_as_frame_columns(self): rng = period_range('1/1/2000', periods=5) - df = DataFrame(randn(10, 5), columns=rng) + df = DataFrame(np.random.randn(10, 5), columns=rng) ts = df[rng[0]] tm.assert_series_equal(ts, df.iloc[:, 0]) @@ -32,7 +31,7 @@ def test_as_frame_columns(self): def test_frame_setitem(self): rng = period_range('1/1/2000', periods=5, name='index') - df = DataFrame(randn(5, 3), index=rng) + df = DataFrame(np.random.randn(5, 3), index=rng) df['Index'] = rng rs = Index(df['Index']) @@ -47,7 +46,7 @@ def test_frame_setitem(self): def test_frame_to_time_stamp(self): K = 5 index = period_range(freq='A', start='1/1/2001', end='12/1/2009') - df = DataFrame(randn(len(index), K), index=index) + df = DataFrame(np.random.randn(len(index), K), index=index) df['mix'] = 'a' exp_index = date_range('1/1/2001', end='12/31/2009', freq='A-DEC') diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py index 1e06d5cad1374..9c4d306ea5720 100644 --- a/pandas/tests/frame/test_query_eval.py +++ b/pandas/tests/frame/test_query_eval.py @@ -5,7 +5,6 @@ import operator import numpy as np -from numpy.random import randn import pytest from pandas.compat import StringIO, lrange, range, zip @@ -153,7 +152,7 @@ def test_query_empty_string(self): def test_eval_resolvers_as_list(self): # GH 14095 - df = DataFrame(randn(10, 2), columns=list('ab')) + df = DataFrame(np.random.randn(10, 2), columns=list('ab')) dict1 = {'a': 1} dict2 = {'b': 2} assert (df.eval('a + b', resolvers=[dict1, dict2]) == @@ -169,7 +168,7 @@ def test_query_with_named_multiindex(self, parser, engine): a = np.random.choice(['red', 'green'], size=10) b = np.random.choice(['eggs', 'ham'], size=10) index = MultiIndex.from_arrays([a, b], names=['color', 'food']) - df = DataFrame(randn(10, 2), index=index) + df = DataFrame(np.random.randn(10, 2), index=index) ind = Series(df.index.get_level_values('color').values, index=index, name='color') @@ -218,7 +217,7 @@ def test_query_with_unnamed_multiindex(self, parser, engine): a = np.random.choice(['red', 'green'], size=10) b = np.random.choice(['eggs', 'ham'], size=10) index = MultiIndex.from_arrays([a, b]) - df = DataFrame(randn(10, 2), index=index) + df = DataFrame(np.random.randn(10, 2), index=index) ind = Series(df.index.get_level_values(0).values, index=index) res1 = df.query('ilevel_0 == "red"', parser=parser, engine=engine) @@ -309,7 +308,7 @@ def test_query_with_partially_named_multiindex(self, parser, engine): b = np.arange(10) index = MultiIndex.from_arrays([a, b]) index.names = [None, 'rating'] - df = DataFrame(randn(10, 2), index=index) + df = DataFrame(np.random.randn(10, 2), index=index) res = df.query('rating == 1', parser=parser, engine=engine) ind = Series(df.index.get_level_values('rating').values, index=index, name='rating') @@ -379,7 +378,7 @@ def teardown_class(cls): def test_date_query_with_attribute_access(self): engine, parser = self.engine, self.parser skip_if_no_pandas_parser(parser) - df = DataFrame(randn(5, 3)) + df = DataFrame(np.random.randn(5, 3)) df['dates1'] = date_range('1/1/2012', periods=5) df['dates2'] = date_range('1/1/2013', periods=5) df['dates3'] = date_range('1/1/2014', periods=5) @@ -390,7 +389,7 @@ def test_date_query_with_attribute_access(self): def test_date_query_no_attribute_access(self): engine, parser = self.engine, self.parser - df = DataFrame(randn(5, 3)) + df = DataFrame(np.random.randn(5, 3)) df['dates1'] = date_range('1/1/2012', periods=5) df['dates2'] = date_range('1/1/2013', periods=5) df['dates3'] = date_range('1/1/2014', periods=5) @@ -402,7 +401,7 @@ def test_date_query_no_attribute_access(self): def test_date_query_with_NaT(self): engine, parser = self.engine, self.parser n = 10 - df = DataFrame(randn(n, 3)) + df = DataFrame(np.random.randn(n, 3)) df['dates1'] = date_range('1/1/2012', periods=n) df['dates2'] = date_range('1/1/2013', periods=n) df['dates3'] = date_range('1/1/2014', periods=n) @@ -416,7 +415,7 @@ def test_date_query_with_NaT(self): def test_date_index_query(self): engine, parser = self.engine, self.parser n = 10 - df = DataFrame(randn(n, 3)) + df = DataFrame(np.random.randn(n, 3)) df['dates1'] = date_range('1/1/2012', periods=n) df['dates3'] = date_range('1/1/2014', periods=n) df.set_index('dates1', inplace=True, drop=True) @@ -428,7 +427,7 @@ def test_date_index_query(self): def test_date_index_query_with_NaT(self): engine, parser = self.engine, self.parser n = 10 - df = DataFrame(randn(n, 3)) + df = DataFrame(np.random.randn(n, 3)) df['dates1'] = date_range('1/1/2012', periods=n) df['dates3'] = date_range('1/1/2014', periods=n) df.iloc[0, 0] = pd.NaT @@ -603,7 +602,7 @@ def test_local_syntax(self): skip_if_no_pandas_parser(self.parser) engine, parser = self.engine, self.parser - df = DataFrame(randn(100, 10), columns=list('abcdefghij')) + df = DataFrame(np.random.randn(100, 10), columns=list('abcdefghij')) b = 1 expect = df[df.a < b] result = df.query('a < @b', engine=engine, parser=parser) @@ -617,7 +616,7 @@ def test_chained_cmp_and_in(self): skip_if_no_pandas_parser(self.parser) engine, parser = self.engine, self.parser cols = list('abc') - df = DataFrame(randn(100, len(cols)), columns=cols) + df = DataFrame(np.random.randn(100, len(cols)), columns=cols) res = df.query('a < b < c and a not in b not in c', engine=engine, parser=parser) ind = (df.a < df.b) & (df.b < df.c) & ~df.b.isin(df.a) & ~df.c.isin(df.b) # noqa @@ -712,7 +711,7 @@ def setup_class(cls): def test_date_query_no_attribute_access(self): engine, parser = self.engine, self.parser - df = DataFrame(randn(5, 3)) + df = DataFrame(np.random.randn(5, 3)) df['dates1'] = date_range('1/1/2012', periods=5) df['dates2'] = date_range('1/1/2013', periods=5) df['dates3'] = date_range('1/1/2014', periods=5) @@ -724,7 +723,7 @@ def test_date_query_no_attribute_access(self): def test_date_query_with_NaT(self): engine, parser = self.engine, self.parser n = 10 - df = DataFrame(randn(n, 3)) + df = DataFrame(np.random.randn(n, 3)) df['dates1'] = date_range('1/1/2012', periods=n) df['dates2'] = date_range('1/1/2013', periods=n) df['dates3'] = date_range('1/1/2014', periods=n) @@ -738,7 +737,7 @@ def test_date_query_with_NaT(self): def test_date_index_query(self): engine, parser = self.engine, self.parser n = 10 - df = DataFrame(randn(n, 3)) + df = DataFrame(np.random.randn(n, 3)) df['dates1'] = date_range('1/1/2012', periods=n) df['dates3'] = date_range('1/1/2014', periods=n) df.set_index('dates1', inplace=True, drop=True) @@ -750,7 +749,7 @@ def test_date_index_query(self): def test_date_index_query_with_NaT(self): engine, parser = self.engine, self.parser n = 10 - df = DataFrame(randn(n, 3)) + df = DataFrame(np.random.randn(n, 3)) df['dates1'] = date_range('1/1/2012', periods=n) df['dates3'] = date_range('1/1/2014', periods=n) df.iloc[0, 0] = pd.NaT @@ -763,7 +762,7 @@ def test_date_index_query_with_NaT(self): def test_date_index_query_with_NaT_duplicates(self): engine, parser = self.engine, self.parser n = 10 - df = DataFrame(randn(n, 3)) + df = DataFrame(np.random.randn(n, 3)) df['dates1'] = date_range('1/1/2012', periods=n) df['dates3'] = date_range('1/1/2014', periods=n) df.loc[np.random.rand(n) > 0.5, 'dates1'] = pd.NaT @@ -845,7 +844,7 @@ def test_query_builtin(self): class TestDataFrameQueryStrings(object): def test_str_query_method(self, parser, engine): - df = DataFrame(randn(10, 1), columns=['b']) + df = DataFrame(np.random.randn(10, 1), columns=['b']) df['strings'] = Series(list('aabbccddee')) expect = df[df.strings == 'a'] @@ -881,7 +880,7 @@ def test_str_query_method(self, parser, engine): assert_frame_equal(res, df[~df.strings.isin(['a'])]) def test_str_list_query_method(self, parser, engine): - df = DataFrame(randn(10, 1), columns=['b']) + df = DataFrame(np.random.randn(10, 1), columns=['b']) df['strings'] = Series(list('aabbccddee')) expect = df[df.strings.isin(['a', 'b'])] @@ -1017,7 +1016,7 @@ def test_query_string_scalar_variable(self, parser, engine): class TestDataFrameEvalWithFrame(object): def setup_method(self, method): - self.frame = DataFrame(randn(10, 3), columns=list('abc')) + self.frame = DataFrame(np.random.randn(10, 3), columns=list('abc')) def teardown_method(self, method): del self.frame diff --git a/pandas/tests/frame/test_rank.py b/pandas/tests/frame/test_rank.py index 0a9801ea8ed61..10c42e0d1a1cf 100644 --- a/pandas/tests/frame/test_rank.py +++ b/pandas/tests/frame/test_rank.py @@ -3,7 +3,6 @@ from distutils.version import LooseVersion import numpy as np -from numpy import nan import pytest from pandas import DataFrame, Series @@ -13,16 +12,16 @@ class TestRank(TestData): - s = Series([1, 3, 4, 2, nan, 2, 1, 5, nan, 3]) + s = Series([1, 3, 4, 2, np.nan, 2, 1, 5, np.nan, 3]) df = DataFrame({'A': s, 'B': s}) results = { - 'average': np.array([1.5, 5.5, 7.0, 3.5, nan, - 3.5, 1.5, 8.0, nan, 5.5]), - 'min': np.array([1, 5, 7, 3, nan, 3, 1, 8, nan, 5]), - 'max': np.array([2, 6, 7, 4, nan, 4, 2, 8, nan, 6]), - 'first': np.array([1, 5, 7, 3, nan, 4, 2, 8, nan, 6]), - 'dense': np.array([1, 3, 4, 2, nan, 2, 1, 5, nan, 3]), + 'average': np.array([1.5, 5.5, 7.0, 3.5, np.nan, + 3.5, 1.5, 8.0, np.nan, 5.5]), + 'min': np.array([1, 5, 7, 3, np.nan, 3, 1, 8, np.nan, 5]), + 'max': np.array([2, 6, 7, 4, np.nan, 4, 2, 8, np.nan, 6]), + 'first': np.array([1, 5, 7, 3, np.nan, 4, 2, 8, np.nan, 6]), + 'dense': np.array([1, 3, 4, 2, np.nan, 2, 1, 5, np.nan, 3]), } @pytest.fixture(params=['average', 'min', 'max', 'first', 'dense']) @@ -87,27 +86,27 @@ def test_rank2(self): tm.assert_frame_equal(result, expected) df = DataFrame([['b', np.nan, 'a'], ['a', 'c', 'b']]) - expected = DataFrame([[2.0, nan, 1.0], [1.0, 3.0, 2.0]]) + expected = DataFrame([[2.0, np.nan, 1.0], [1.0, 3.0, 2.0]]) result = df.rank(1, numeric_only=False) tm.assert_frame_equal(result, expected) - expected = DataFrame([[2.0, nan, 1.0], [1.0, 1.0, 2.0]]) + expected = DataFrame([[2.0, np.nan, 1.0], [1.0, 1.0, 2.0]]) result = df.rank(0, numeric_only=False) tm.assert_frame_equal(result, expected) # 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) # check the rank - expected = DataFrame([[2., nan, 1.], + expected = DataFrame([[2., np.nan, 1.], [2., 3., 1.]]) result = df.rank(1, numeric_only=False, ascending=True) tm.assert_frame_equal(result, expected) - expected = DataFrame([[1., nan, 2.], + expected = DataFrame([[1., np.nan, 2.], [2., 1., 3.]]) result = df.rank(1, numeric_only=False, ascending=False) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/frame/test_replace.py b/pandas/tests/frame/test_replace.py index 87fd5f2e74a9a..219f7a1585fc2 100644 --- a/pandas/tests/frame/test_replace.py +++ b/pandas/tests/frame/test_replace.py @@ -6,7 +6,6 @@ import re import numpy as np -from numpy import nan import pytest from pandas.compat import StringIO, lrange, range, zip @@ -20,24 +19,24 @@ class TestDataFrameReplace(TestData): def test_replace_inplace(self): - self.tsframe['A'][:5] = nan - self.tsframe['A'][-5:] = nan + self.tsframe['A'][:5] = np.nan + self.tsframe['A'][-5:] = np.nan tsframe = self.tsframe.copy() - tsframe.replace(nan, 0, inplace=True) + tsframe.replace(np.nan, 0, inplace=True) assert_frame_equal(tsframe, self.tsframe.fillna(0)) # mixed type mf = self.mixed_frame - mf.iloc[5:20, mf.columns.get_loc('foo')] = nan - mf.iloc[-10:, mf.columns.get_loc('A')] = nan + mf.iloc[5:20, mf.columns.get_loc('foo')] = np.nan + mf.iloc[-10:, mf.columns.get_loc('A')] = np.nan result = self.mixed_frame.replace(np.nan, 0) expected = self.mixed_frame.fillna(value=0) assert_frame_equal(result, expected) tsframe = self.tsframe.copy() - tsframe.replace([nan], [0], inplace=True) + tsframe.replace([np.nan], [0], inplace=True) assert_frame_equal(tsframe, self.tsframe.fillna(0)) def test_regex_replace_scalar(self): @@ -49,11 +48,11 @@ def test_regex_replace_scalar(self): # simplest cases # regex -> value # obj frame - res = dfobj.replace(r'\s*\.\s*', nan, regex=True) + res = dfobj.replace(r'\s*\.\s*', np.nan, regex=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed - res = dfmix.replace(r'\s*\.\s*', nan, regex=True) + res = dfmix.replace(r'\s*\.\s*', np.nan, regex=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex @@ -72,11 +71,11 @@ def test_regex_replace_scalar(self): assert_frame_equal(res, expec) # everything with compiled regexs as well - res = dfobj.replace(re.compile(r'\s*\.\s*'), nan, regex=True) + res = dfobj.replace(re.compile(r'\s*\.\s*'), np.nan, regex=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed - res = dfmix.replace(re.compile(r'\s*\.\s*'), nan, regex=True) + res = dfmix.replace(re.compile(r'\s*\.\s*'), np.nan, regex=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex @@ -116,12 +115,12 @@ def test_regex_replace_scalar_inplace(self): # regex -> value # obj frame res = dfobj.copy() - res.replace(r'\s*\.\s*', nan, regex=True, inplace=True) + res.replace(r'\s*\.\s*', np.nan, regex=True, inplace=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.copy() - res.replace(r'\s*\.\s*', nan, regex=True, inplace=True) + res.replace(r'\s*\.\s*', np.nan, regex=True, inplace=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex @@ -143,12 +142,12 @@ def test_regex_replace_scalar_inplace(self): # everything with compiled regexs as well res = dfobj.copy() - res.replace(re.compile(r'\s*\.\s*'), nan, regex=True, inplace=True) + res.replace(re.compile(r'\s*\.\s*'), np.nan, regex=True, inplace=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.copy() - res.replace(re.compile(r'\s*\.\s*'), nan, regex=True, inplace=True) + res.replace(re.compile(r'\s*\.\s*'), np.nan, regex=True, inplace=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex @@ -171,12 +170,12 @@ def test_regex_replace_scalar_inplace(self): assert_frame_equal(res, expec) res = dfobj.copy() - res.replace(regex=r'\s*\.\s*', value=nan, inplace=True) + res.replace(regex=r'\s*\.\s*', value=np.nan, inplace=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.copy() - res.replace(regex=r'\s*\.\s*', value=nan, inplace=True) + res.replace(regex=r'\s*\.\s*', value=np.nan, inplace=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex @@ -198,12 +197,12 @@ def test_regex_replace_scalar_inplace(self): # everything with compiled regexs as well res = dfobj.copy() - res.replace(regex=re.compile(r'\s*\.\s*'), value=nan, inplace=True) + res.replace(regex=re.compile(r'\s*\.\s*'), value=np.nan, inplace=True) assert_frame_equal(dfobj, res.fillna('.')) # mixed res = dfmix.copy() - res.replace(regex=re.compile(r'\s*\.\s*'), value=nan, inplace=True) + res.replace(regex=re.compile(r'\s*\.\s*'), value=np.nan, inplace=True) assert_frame_equal(dfmix, res.fillna('.')) # regex -> regex @@ -232,9 +231,9 @@ def test_regex_replace_list_obj(self): # lists of regexes and values # list of [re1, re2, ..., reN] -> [v1, v2, ..., vN] to_replace_res = [r'\s*\.\s*', r'e|f|g'] - values = [nan, 'crap'] + values = [np.nan, 'crap'] res = dfobj.replace(to_replace_res, values, regex=True) - expec = DataFrame({'a': ['a', 'b', nan, nan], 'b': ['crap'] * 3 + + expec = DataFrame({'a': ['a', 'b', np.nan, np.nan], 'b': ['crap'] * 3 + ['h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) @@ -276,10 +275,10 @@ def test_regex_replace_list_obj_inplace(self): # lists of regexes and values # list of [re1, re2, ..., reN] -> [v1, v2, ..., vN] to_replace_res = [r'\s*\.\s*', r'e|f|g'] - values = [nan, 'crap'] + values = [np.nan, 'crap'] res = dfobj.copy() res.replace(to_replace_res, values, inplace=True, regex=True) - expec = DataFrame({'a': ['a', 'b', nan, nan], 'b': ['crap'] * 3 + + expec = DataFrame({'a': ['a', 'b', np.nan, np.nan], 'b': ['crap'] * 3 + ['h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) @@ -323,11 +322,11 @@ def test_regex_replace_list_mixed(self): # lists of regexes and values # list of [re1, re2, ..., reN] -> [v1, v2, ..., vN] to_replace_res = [r'\s*\.\s*', r'a'] - values = [nan, 'crap'] + values = [np.nan, 'crap'] mix2 = {'a': lrange(4), 'b': list('ab..'), 'c': list('halo')} dfmix2 = DataFrame(mix2) res = dfmix2.replace(to_replace_res, values, regex=True) - expec = DataFrame({'a': mix2['a'], 'b': ['crap', 'b', nan, nan], + expec = DataFrame({'a': mix2['a'], 'b': ['crap', 'b', np.nan, np.nan], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) @@ -361,10 +360,10 @@ def test_regex_replace_list_mixed_inplace(self): # lists of regexes and values # list of [re1, re2, ..., reN] -> [v1, v2, ..., vN] to_replace_res = [r'\s*\.\s*', r'a'] - values = [nan, 'crap'] + values = [np.nan, 'crap'] res = dfmix.copy() res.replace(to_replace_res, values, inplace=True, regex=True) - expec = DataFrame({'a': mix['a'], 'b': ['crap', 'b', nan, nan]}) + expec = DataFrame({'a': mix['a'], 'b': ['crap', 'b', np.nan, np.nan]}) assert_frame_equal(res, expec) # list of [re1, re2, ..., reN] -> [re1, re2, .., reN] @@ -394,7 +393,7 @@ def test_regex_replace_list_mixed_inplace(self): assert_frame_equal(res, expec) def test_regex_replace_dict_mixed(self): - mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} + mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']} dfmix = DataFrame(mix) # dicts @@ -403,10 +402,11 @@ def test_regex_replace_dict_mixed(self): # list of dicts {re1: v1, re2: v2, ..., re3: v3}, search the whole # frame - res = dfmix.replace({'b': r'\s*\.\s*'}, {'b': nan}, regex=True) + res = dfmix.replace({'b': r'\s*\.\s*'}, {'b': np.nan}, regex=True) res2 = dfmix.copy() - res2.replace({'b': r'\s*\.\s*'}, {'b': nan}, inplace=True, regex=True) - expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c': + res2.replace({'b': r'\s*\.\s*'}, {'b': np.nan}, + inplace=True, regex=True) + expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', np.nan, np.nan], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) @@ -433,33 +433,33 @@ def test_regex_replace_dict_mixed(self): # scalar -> dict # to_replace regex, {value: value} - expec = DataFrame({'a': mix['a'], 'b': [nan, 'b', '.', '.'], 'c': + expec = DataFrame({'a': mix['a'], 'b': [np.nan, 'b', '.', '.'], 'c': mix['c']}) - res = dfmix.replace('a', {'b': nan}, regex=True) + res = dfmix.replace('a', {'b': np.nan}, regex=True) res2 = dfmix.copy() - res2.replace('a', {'b': nan}, regex=True, inplace=True) + res2.replace('a', {'b': np.nan}, regex=True, inplace=True) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) - res = dfmix.replace('a', {'b': nan}, regex=True) + res = dfmix.replace('a', {'b': np.nan}, regex=True) res2 = dfmix.copy() - res2.replace(regex='a', value={'b': nan}, inplace=True) - expec = DataFrame({'a': mix['a'], 'b': [nan, 'b', '.', '.'], 'c': + res2.replace(regex='a', value={'b': np.nan}, inplace=True) + expec = DataFrame({'a': mix['a'], 'b': [np.nan, 'b', '.', '.'], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) def test_regex_replace_dict_nested(self): # nested dicts will not work until this is implemented for Series - mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} + mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']} dfmix = DataFrame(mix) - res = dfmix.replace({'b': {r'\s*\.\s*': nan}}, regex=True) + res = dfmix.replace({'b': {r'\s*\.\s*': np.nan}}, regex=True) res2 = dfmix.copy() res4 = dfmix.copy() - res2.replace({'b': {r'\s*\.\s*': nan}}, inplace=True, regex=True) - res3 = dfmix.replace(regex={'b': {r'\s*\.\s*': nan}}) - res4.replace(regex={'b': {r'\s*\.\s*': nan}}, inplace=True) - expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c': + res2.replace({'b': {r'\s*\.\s*': np.nan}}, inplace=True, regex=True) + res3 = dfmix.replace(regex={'b': {r'\s*\.\s*': np.nan}}) + res4.replace(regex={'b': {r'\s*\.\s*': np.nan}}, inplace=True) + expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', np.nan, np.nan], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) @@ -473,23 +473,23 @@ def test_regex_replace_dict_nested_gh4115(self): assert_frame_equal(result, expected) def test_regex_replace_list_to_scalar(self): - mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} + mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']} df = DataFrame(mix) - expec = DataFrame({'a': mix['a'], 'b': np.array([nan] * 4), - 'c': [nan, nan, nan, 'd']}) + expec = DataFrame({'a': mix['a'], 'b': np.array([np.nan] * 4), + 'c': [np.nan, np.nan, np.nan, 'd']}) - res = df.replace([r'\s*\.\s*', 'a|b'], nan, regex=True) + res = df.replace([r'\s*\.\s*', 'a|b'], np.nan, regex=True) res2 = df.copy() res3 = df.copy() - res2.replace([r'\s*\.\s*', 'a|b'], nan, regex=True, inplace=True) - res3.replace(regex=[r'\s*\.\s*', 'a|b'], value=nan, inplace=True) + res2.replace([r'\s*\.\s*', 'a|b'], np.nan, regex=True, inplace=True) + res3.replace(regex=[r'\s*\.\s*', 'a|b'], value=np.nan, inplace=True) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) assert_frame_equal(res3, expec) def test_regex_replace_str_to_numeric(self): # what happens when you try to replace a numeric value with a regex? - mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} + mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']} df = DataFrame(mix) res = df.replace(r'\s*\.\s*', 0, regex=True) res2 = df.copy() @@ -503,7 +503,7 @@ def test_regex_replace_str_to_numeric(self): assert_frame_equal(res3, expec) def test_regex_replace_regex_list_to_numeric(self): - mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} + mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']} df = DataFrame(mix) res = df.replace([r'\s*\.\s*', 'b'], 0, regex=True) res2 = df.copy() @@ -511,30 +511,30 @@ def test_regex_replace_regex_list_to_numeric(self): res3 = df.copy() res3.replace(regex=[r'\s*\.\s*', 'b'], value=0, inplace=True) expec = DataFrame({'a': mix['a'], 'b': ['a', 0, 0, 0], 'c': ['a', 0, - nan, + np.nan, 'd']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) assert_frame_equal(res3, expec) def test_regex_replace_series_of_regexes(self): - mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} + mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']} df = DataFrame(mix) s1 = Series({'b': r'\s*\.\s*'}) - s2 = Series({'b': nan}) + s2 = Series({'b': np.nan}) res = df.replace(s1, s2, regex=True) res2 = df.copy() res2.replace(s1, s2, inplace=True, regex=True) res3 = df.copy() res3.replace(regex=s1, value=s2, inplace=True) - expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c': + expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', np.nan, np.nan], 'c': mix['c']}) assert_frame_equal(res, expec) assert_frame_equal(res2, expec) assert_frame_equal(res3, expec) def test_regex_replace_numeric_to_object_conversion(self): - mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} + mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']} df = DataFrame(mix) expec = DataFrame({'a': ['a', 1, 2, 3], 'b': mix['b'], 'c': mix['c']}) res = df.replace(0, 'a') @@ -549,15 +549,15 @@ def test_replace_regex_metachar(self, metachar): assert_frame_equal(result, expected) def test_replace(self): - self.tsframe['A'][:5] = nan - self.tsframe['A'][-5:] = nan + self.tsframe['A'][:5] = np.nan + self.tsframe['A'][-5:] = np.nan - zero_filled = self.tsframe.replace(nan, -1e8) + zero_filled = self.tsframe.replace(np.nan, -1e8) assert_frame_equal(zero_filled, self.tsframe.fillna(-1e8)) - assert_frame_equal(zero_filled.replace(-1e8, nan), self.tsframe) + assert_frame_equal(zero_filled.replace(-1e8, np.nan), self.tsframe) - self.tsframe['A'][:5] = nan - self.tsframe['A'][-5:] = nan + self.tsframe['A'][:5] = np.nan + self.tsframe['A'][-5:] = np.nan self.tsframe['B'][:5] = -1e8 # empty @@ -580,9 +580,9 @@ def test_replace_list(self): # lists of regexes and values # list of [v1, v2, ..., vN] -> [v1, v2, ..., vN] to_replace_res = [r'.', r'e'] - values = [nan, 'crap'] + values = [np.nan, 'crap'] res = dfobj.replace(to_replace_res, values) - expec = DataFrame({'a': ['a', 'b', nan, nan], + expec = DataFrame({'a': ['a', 'b', np.nan, np.nan], 'b': ['crap', 'f', 'g', 'h'], 'c': ['h', 'crap', 'l', 'o']}) assert_frame_equal(res, expec) @@ -644,18 +644,18 @@ def test_replace_convert(self): def test_replace_mixed(self): mf = self.mixed_frame - mf.iloc[5:20, mf.columns.get_loc('foo')] = nan - mf.iloc[-10:, mf.columns.get_loc('A')] = nan + mf.iloc[5:20, mf.columns.get_loc('foo')] = np.nan + mf.iloc[-10:, mf.columns.get_loc('A')] = np.nan result = self.mixed_frame.replace(np.nan, -18) expected = self.mixed_frame.fillna(value=-18) assert_frame_equal(result, expected) - assert_frame_equal(result.replace(-18, nan), self.mixed_frame) + assert_frame_equal(result.replace(-18, np.nan), self.mixed_frame) result = self.mixed_frame.replace(np.nan, -1e8) expected = self.mixed_frame.fillna(value=-1e8) assert_frame_equal(result, expected) - assert_frame_equal(result.replace(-1e8, nan), self.mixed_frame) + assert_frame_equal(result.replace(-1e8, np.nan), self.mixed_frame) # int block upcasting df = DataFrame({'A': Series([1.0, 2.0], dtype='float64'), @@ -726,14 +726,14 @@ def test_replace_value_is_none(self): orig_value = self.tsframe.iloc[0, 0] orig2 = self.tsframe.iloc[1, 0] - self.tsframe.iloc[0, 0] = nan + self.tsframe.iloc[0, 0] = np.nan self.tsframe.iloc[1, 0] = 1 - result = self.tsframe.replace(to_replace={nan: 0}) - expected = self.tsframe.T.replace(to_replace={nan: 0}).T + result = self.tsframe.replace(to_replace={np.nan: 0}) + expected = self.tsframe.T.replace(to_replace={np.nan: 0}).T assert_frame_equal(result, expected) - result = self.tsframe.replace(to_replace={nan: 0, 1: -1e8}) + result = self.tsframe.replace(to_replace={np.nan: 0, 1: -1e8}) tsframe = self.tsframe.copy() tsframe.iloc[0, 0] = 0 tsframe.iloc[1, 0] = -1e8 @@ -746,19 +746,19 @@ def test_replace_for_new_dtypes(self): # dtypes tsframe = self.tsframe.copy().astype(np.float32) - tsframe['A'][:5] = nan - tsframe['A'][-5:] = nan + tsframe['A'][:5] = np.nan + tsframe['A'][-5:] = np.nan - zero_filled = tsframe.replace(nan, -1e8) + zero_filled = tsframe.replace(np.nan, -1e8) assert_frame_equal(zero_filled, tsframe.fillna(-1e8)) - assert_frame_equal(zero_filled.replace(-1e8, nan), tsframe) + assert_frame_equal(zero_filled.replace(-1e8, np.nan), tsframe) - tsframe['A'][:5] = nan - tsframe['A'][-5:] = nan + tsframe['A'][:5] = np.nan + tsframe['A'][-5:] = np.nan tsframe['B'][:5] = -1e8 b = tsframe['B'] - b[b == -1e8] = nan + b[b == -1e8] = np.nan tsframe['B'] = b result = tsframe.fillna(method='bfill') assert_frame_equal(result, tsframe.fillna(method='bfill')) @@ -875,10 +875,10 @@ def test_replace_series_no_regex(self): assert_series_equal(result, expected) def test_replace_dict_tuple_list_ordering_remains_the_same(self): - df = DataFrame(dict(A=[nan, 1])) - res1 = df.replace(to_replace={nan: 0, 1: -1e8}) - res2 = df.replace(to_replace=(1, nan), value=[-1e8, 0]) - res3 = df.replace(to_replace=[1, nan], value=[-1e8, 0]) + df = DataFrame(dict(A=[np.nan, 1])) + res1 = df.replace(to_replace={np.nan: 0, 1: -1e8}) + res2 = df.replace(to_replace=(1, np.nan), value=[-1e8, 0]) + res3 = df.replace(to_replace=[1, np.nan], value=[-1e8, 0]) expected = DataFrame({'A': [0, -1e8]}) assert_frame_equal(res1, res2) @@ -1062,7 +1062,7 @@ def test_replace_datetimetz(self): def test_replace_with_empty_dictlike(self): # GH 15289 - mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']} + mix = {'a': lrange(4), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']} df = DataFrame(mix) assert_frame_equal(df, df.replace({})) assert_frame_equal(df, df.replace(Series([]))) @@ -1072,31 +1072,31 @@ def test_replace_with_empty_dictlike(self): @pytest.mark.parametrize("to_replace, method, expected", [ (0, 'bfill', {'A': [1, 1, 2], - 'B': [5, nan, 7], + 'B': [5, np.nan, 7], 'C': ['a', 'b', 'c']}), - (nan, 'bfill', {'A': [0, 1, 2], - 'B': [5.0, 7.0, 7.0], - 'C': ['a', 'b', 'c']}), + (np.nan, 'bfill', {'A': [0, 1, 2], + 'B': [5.0, 7.0, 7.0], + 'C': ['a', 'b', 'c']}), ('d', 'ffill', {'A': [0, 1, 2], - 'B': [5, nan, 7], + 'B': [5, np.nan, 7], 'C': ['a', 'b', 'c']}), ([0, 2], 'bfill', {'A': [1, 1, 2], - 'B': [5, nan, 7], + 'B': [5, np.nan, 7], 'C': ['a', 'b', 'c']}), ([1, 2], 'pad', {'A': [0, 0, 0], - 'B': [5, nan, 7], + 'B': [5, np.nan, 7], 'C': ['a', 'b', 'c']}), ((1, 2), 'bfill', {'A': [0, 2, 2], - 'B': [5, nan, 7], + 'B': [5, np.nan, 7], 'C': ['a', 'b', 'c']}), (['b', 'c'], 'ffill', {'A': [0, 1, 2], - 'B': [5, nan, 7], + 'B': [5, np.nan, 7], 'C': ['a', 'a', 'a']}), ]) def test_replace_method(self, to_replace, method, expected): # GH 19632 df = DataFrame({'A': [0, 1, 2], - 'B': [5, nan, 7], + 'B': [5, np.nan, 7], 'C': ['a', 'b', 'c']}) result = df.replace(to_replace=to_replace, value=None, method=method) diff --git a/pandas/tests/frame/test_reshape.py b/pandas/tests/frame/test_reshape.py index f2f6944a21e03..28222a82945be 100644 --- a/pandas/tests/frame/test_reshape.py +++ b/pandas/tests/frame/test_reshape.py @@ -7,8 +7,6 @@ from warnings import catch_warnings, simplefilter import numpy as np -from numpy import nan -from numpy.random import randn import pytest from pandas.compat import u @@ -388,7 +386,7 @@ def test_stack_mixed_levels(self): ('A', 'dog', 'short'), ('B', 'dog', 'short')], names=['exp', 'animal', 'hair_length'] ) - df = DataFrame(randn(4, 4), columns=columns) + df = DataFrame(np.random.randn(4, 4), columns=columns) animal_hair_stacked = df.stack(level=['animal', 'hair_length']) exp_hair_stacked = df.stack(level=['exp', 'hair_length']) @@ -420,7 +418,7 @@ def test_stack_int_level_names(self): ('A', 'dog', 'short'), ('B', 'dog', 'short')], names=['exp', 'animal', 'hair_length'] ) - df = DataFrame(randn(4, 4), columns=columns) + df = DataFrame(np.random.randn(4, 4), columns=columns) exp_animal_stacked = df.stack(level=['exp', 'animal']) animal_hair_stacked = df.stack(level=['animal', 'hair_length']) @@ -634,7 +632,6 @@ def test_unstack_unused_level(self, cols): def test_unstack_nan_index(self): # GH7466 cast = lambda val: '{0:1}'.format('' if val != val else val) - nan = np.nan def verify(df): mk_list = lambda a: list(a) if isinstance(a, tuple) else [a] @@ -645,7 +642,7 @@ def verify(df): right = sorted(list(map(cast, right))) assert left == right - df = DataFrame({'jim': ['a', 'b', nan, 'd'], + df = DataFrame({'jim': ['a', 'b', np.nan, 'd'], 'joe': ['w', 'x', 'y', 'z'], 'jolie': ['a.w', 'b.x', ' .y', 'd.z']}) @@ -660,10 +657,10 @@ def verify(df): assert udf.notna().values.sum() == len(df) verify(udf['jolie']) - df = DataFrame({'1st': ['d'] * 3 + [nan] * 5 + ['a'] * 2 + + df = DataFrame({'1st': ['d'] * 3 + [np.nan] * 5 + ['a'] * 2 + ['c'] * 3 + ['e'] * 2 + ['b'] * 5, - '2nd': ['y'] * 2 + ['w'] * 3 + [nan] * 3 + - ['z'] * 4 + [nan] * 3 + ['x'] * 3 + [nan] * 2, + '2nd': ['y'] * 2 + ['w'] * 3 + [np.nan] * 3 + + ['z'] * 4 + [np.nan] * 3 + ['x'] * 3 + [np.nan] * 2, '3rd': [67, 39, 53, 72, 57, 80, 31, 18, 11, 30, 59, 50, 62, 59, 76, 52, 14, 53, 60, 51]}) @@ -685,10 +682,10 @@ def verify(df): df.iloc[3, 1] = np.NaN left = df.set_index(['A', 'B']).unstack(0) - vals = [[3, 0, 1, 2, nan, nan, nan, nan], - [nan, nan, nan, nan, 4, 5, 6, 7]] + vals = [[3, 0, 1, 2, np.nan, np.nan, np.nan, np.nan], + [np.nan, np.nan, np.nan, np.nan, 4, 5, 6, 7]] vals = list(map(list, zip(*vals))) - idx = Index([nan, 0, 1, 2, 4, 5, 6, 7], name='B') + idx = Index([np.nan, 0, 1, 2, 4, 5, 6, 7], name='B') cols = MultiIndex(levels=[['C'], ['a', 'b']], codes=[[0, 0], [0, 1]], names=[None, 'A']) @@ -701,11 +698,11 @@ def verify(df): df.iloc[2, 1] = np.NaN left = df.set_index(['A', 'B']).unstack(0) - vals = [[2, nan], [0, 4], [1, 5], [nan, 6], [3, 7]] + vals = [[2, np.nan], [0, 4], [1, 5], [np.nan, 6], [3, 7]] cols = MultiIndex(levels=[['C'], ['a', 'b']], codes=[[0, 0], [0, 1]], names=[None, 'A']) - idx = Index([nan, 0, 1, 2, 3], name='B') + idx = Index([np.nan, 0, 1, 2, 3], name='B') right = DataFrame(vals, columns=cols, index=idx) assert_frame_equal(left, right) @@ -714,11 +711,11 @@ def verify(df): df.iloc[3, 1] = np.NaN left = df.set_index(['A', 'B']).unstack(0) - vals = [[3, nan], [0, 4], [1, 5], [2, 6], [nan, 7]] + vals = [[3, np.nan], [0, 4], [1, 5], [2, 6], [np.nan, 7]] cols = MultiIndex(levels=[['C'], ['a', 'b']], codes=[[0, 0], [0, 1]], names=[None, 'A']) - idx = Index([nan, 0, 1, 2, 3], name='B') + idx = Index([np.nan, 0, 1, 2, 3], name='B') right = DataFrame(vals, columns=cols, index=idx) assert_frame_equal(left, right) @@ -731,7 +728,7 @@ def verify(df): df.iloc[3, 1] = np.NaN left = df.set_index(['A', 'B']).unstack() - vals = np.array([[3, 0, 1, 2, nan, 4], [nan, 5, 6, 7, 8, 9]]) + vals = np.array([[3, 0, 1, 2, np.nan, 4], [np.nan, 5, 6, 7, 8, 9]]) idx = Index(['a', 'b'], name='A') cols = MultiIndex(levels=[['C'], date_range('2012-01-01', periods=5)], codes=[[0, 0, 0, 0, 0, 0], [-1, 0, 1, 2, 3, 4]], @@ -741,9 +738,9 @@ def verify(df): assert_frame_equal(left, right) # GH4862 - vals = [['Hg', nan, nan, 680585148], - ['U', 0.0, nan, 680585148], - ['Pb', 7.07e-06, nan, 680585148], + vals = [['Hg', np.nan, np.nan, 680585148], + ['U', 0.0, np.nan, 680585148], + ['Pb', 7.07e-06, np.nan, 680585148], ['Sn', 2.3614e-05, 0.0133, 680607017], ['Ag', 0.0, 0.0133, 680607017], ['Hg', -0.00015, 0.0133, 680607017]] @@ -752,8 +749,8 @@ def verify(df): left = df.copy().set_index(['s_id', 'dosage', 'agent']).unstack() - vals = [[nan, nan, 7.07e-06, nan, 0.0], - [0.0, -0.00015, nan, 2.3614e-05, nan]] + vals = [[np.nan, np.nan, 7.07e-06, np.nan, 0.0], + [0.0, -0.00015, np.nan, 2.3614e-05, np.nan]] idx = MultiIndex(levels=[[680585148, 680607017], [0.0133]], codes=[[0, 1], [-1, 0]], @@ -777,8 +774,8 @@ def verify(df): 'joe': (np.random.randn(6) * 10).round(2)}) df['3rd'] = df['2nd'] - pd.Timestamp('2014-02-02') - df.loc[1, '2nd'] = df.loc[3, '2nd'] = nan - df.loc[1, '3rd'] = df.loc[4, '3rd'] = nan + df.loc[1, '2nd'] = df.loc[3, '2nd'] = np.nan + df.loc[1, '3rd'] = df.loc[4, '3rd'] = np.nan left = df.set_index(['1st', '2nd', '3rd']).unstack(['2nd', '3rd']) assert left.notna().values.sum() == 2 * len(df) @@ -845,7 +842,7 @@ def _test_stack_with_multiindex(multiindex): df = DataFrame(np.arange(6).reshape(2, 3), columns=full_multiindex[[0, 1, 3]]) result = df.stack(dropna=False) - expected = DataFrame([[0, 2], [1, nan], [3, 5], [4, nan]], + expected = DataFrame([[0, 2], [1, np.nan], [3, 5], [4, np.nan]], index=MultiIndex( levels=[[0, 1], ['u', 'x', 'y', 'z']], codes=[[0, 0, 1, 1], diff --git a/pandas/tests/frame/test_timeseries.py b/pandas/tests/frame/test_timeseries.py index 75a8c834e3af6..bc37317f72802 100644 --- a/pandas/tests/frame/test_timeseries.py +++ b/pandas/tests/frame/test_timeseries.py @@ -5,7 +5,6 @@ from datetime import datetime, time import numpy as np -from numpy.random import randn import pytest from pandas.compat import product @@ -530,7 +529,7 @@ def test_asfreq_fillvalue(self): def test_first_last_valid(self, data, idx, expected_first, expected_last): N = len(self.frame.index) - mat = randn(N) + mat = np.random.randn(N) mat[:5] = np.nan mat[-5:] = np.nan @@ -812,7 +811,7 @@ def test_frame_to_period(self): dr = date_range('1/1/2000', '1/1/2001') pr = period_range('1/1/2000', '1/1/2001') - df = DataFrame(randn(len(dr), K), index=dr) + df = DataFrame(np.random.randn(len(dr), K), index=dr) df['mix'] = 'a' pts = df.to_period()