From 327fc3f70a19a5cf66e2af14f701f301dc2634eb Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 16 Nov 2018 16:43:47 -0800 Subject: [PATCH 01/16] avoid non-standard import of nan --- pandas/tests/arithmetic/test_datetime64.py | 4 +- pandas/tests/arithmetic/test_numeric.py | 11 +++++ pandas/tests/frame/test_block_internals.py | 7 ++- pandas/tests/frame/test_quantile.py | 14 +++--- pandas/tests/indexes/test_numeric.py | 15 ++----- pandas/tests/io/parser/test_textreader.py | 7 ++- pandas/tests/io/test_excel.py | 11 +++-- pandas/tests/reshape/merge/test_multi.py | 9 ++-- pandas/tests/reshape/test_reshape.py | 15 +++---- pandas/tests/series/test_analytics.py | 52 ++++++++++------------ pandas/tests/series/test_constructors.py | 19 ++++---- pandas/tests/series/test_rank.py | 15 +++---- pandas/tests/test_algos.py | 8 ++-- pandas/tests/test_multilevel.py | 11 +++-- pandas/tests/test_sorting.py | 6 +-- 15 files changed, 96 insertions(+), 108 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index cd28e60b62562..883b281466540 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1392,10 +1392,8 @@ def test_dti_sub_dt64_array_naive(self): result = dtarr - dti tm.assert_index_equal(result, expected) - def test_dti_sub_dt64_array_aware_raises(self, tz_naive_fixture): + def test_dti_sub_dt64_array_aware_raises(self, tz_aware_fixture): tz = tz_naive_fixture - if tz is None: - return dti = pd.date_range('2016-01-01', periods=3, tz=tz) dtarr = dti.values diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 9163f2e1a3d1c..f1023148aaf1c 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -806,6 +806,17 @@ def check(series, other): class TestUFuncCompat(object): + + @pytest.mark.parametrize('holder', [pd.Int64Index, pd.UInt64Index, + pd.Float64Index, pd.Series]) + def test_ufunc_compat(self, holder): + box = pd.Series if holder is pd.Series else pd.Index + + idx = holder(np.arange(5, dtype='int64')) + result = np.sin(idx) + expected = box(np.sin(np.arange(5, dtype='int64'))) + tm.assert_equal(result, expected) + @pytest.mark.parametrize('holder', [pd.Int64Index, pd.UInt64Index, pd.Float64Index, pd.Series]) def test_ufunc_coercions(self, holder): diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index 224e56777f6b4..19c4deab1b831 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -7,7 +7,6 @@ from datetime import datetime, timedelta import itertools -from numpy import nan import numpy as np from pandas import (DataFrame, Series, Timestamp, date_range, compat, @@ -202,7 +201,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) @@ -544,7 +543,7 @@ 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): @@ -555,7 +554,7 @@ def test_strange_column_corruption_issue(self): 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_quantile.py b/pandas/tests/frame/test_quantile.py index a7c91dd36b2d2..a6757e0060a70 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -19,15 +19,14 @@ class TestDataFrameQuantile(TestData): def test_quantile(self): - from numpy import percentile q = self.tsframe.quantile(0.1, axis=0) - assert q['A'] == percentile(self.tsframe['A'], 10) + assert q['A'] == np.percentile(self.tsframe['A'], 10) tm.assert_index_equal(q.index, self.tsframe.columns) q = self.tsframe.quantile(0.9, axis=1) assert (q['2000-01-17'] == - percentile(self.tsframe.loc['2000-01-17'], 90)) + np.percentile(self.tsframe.loc['2000-01-17'], 90)) tm.assert_index_equal(q.index, self.tsframe.index) # test degenerate case @@ -53,7 +52,7 @@ def test_quantile(self): # We may want to break API in the future to change this # so that we exclude non-numeric along the same axis - # See GH #7312 + # See GH#7312 df = DataFrame([[1, 2, 3], ['a', 'b', 4]]) result = df.quantile(.5, axis=1) @@ -101,14 +100,13 @@ def test_quantile_axis_parameter(self): pytest.raises(ValueError, df.quantile, 0.1, axis="column") def test_quantile_interpolation(self): - # see gh-10174 - from numpy import percentile + # see GH#10174 # interpolation = linear (default case) q = self.tsframe.quantile(0.1, axis=0, interpolation='linear') - assert q['A'] == percentile(self.tsframe['A'], 10) + assert q['A'] == np.percentile(self.tsframe['A'], 10) q = self.intframe.quantile(0.1) - assert q['A'] == percentile(self.intframe['A'], 10) + assert q['A'] == np.percentile(self.intframe['A'], 10) # test with and without interpolation keyword q1 = self.intframe.quantile(0.1) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index c125db16bcbff..273e1d6ac30a6 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -49,12 +49,6 @@ def test_explicit_conversions(self): result = a - fidx tm.assert_index_equal(result, expected) - def test_ufunc_compat(self): - idx = self._holder(np.arange(5, dtype='int64')) - result = np.sin(idx) - expected = Float64Index(np.sin(np.arange(5, dtype='int64'))) - tm.assert_index_equal(result, expected) - def test_index_groupby(self): int_idx = Index(range(6)) float_idx = Index(np.arange(0, 0.6, 0.1)) @@ -488,11 +482,10 @@ def test_join_non_unique(self): exp_ridx = np.array([2, 3, 2, 3, 0, 1, 0, 1], dtype=np.intp) tm.assert_numpy_array_equal(ridx, exp_ridx) - def test_join_self(self): - kinds = 'outer', 'inner', 'left', 'right' - for kind in kinds: - joined = self.index.join(self.index, how=kind) - assert self.index is joined + @pytest.mark.parametrize('kind', ['outer', 'inner', 'left', 'right']) + def test_join_self(self, kind): + joined = self.index.join(self.index, how=kind) + assert self.index is joined def test_union_noncomparable(self): from datetime import datetime, timedelta diff --git a/pandas/tests/io/parser/test_textreader.py b/pandas/tests/io/parser/test_textreader.py index 93c115ae0a57b..77c869fcaeda1 100644 --- a/pandas/tests/io/parser/test_textreader.py +++ b/pandas/tests/io/parser/test_textreader.py @@ -9,7 +9,6 @@ import sys import numpy as np -from numpy import nan import pytest import pandas._libs.parsers as parser @@ -317,12 +316,12 @@ def test_empty_field_eof(self): assert_array_dicts_equal(result, expected) # GH5664 - a = DataFrame([['b'], [nan]], columns=['a'], index=['a', 'c']) + a = DataFrame([['b'], [np.nan]], columns=['a'], index=['a', 'c']) b = DataFrame([[1, 1, 1, 0], [1, 1, 1, 0]], columns=list('abcd'), index=[1, 1]) - c = DataFrame([[1, 2, 3, 4], [6, nan, nan, nan], - [8, 9, 10, 11], [13, 14, nan, nan]], + c = DataFrame([[1, 2, 3, 4], [6, np.nan, np.nan, np.nan], + [8, 9, 10, 11], [13, 14, np.nan, np.nan]], columns=list('abcd'), index=[0, 5, 7, 12]) diff --git a/pandas/tests/io/test_excel.py b/pandas/tests/io/test_excel.py index a097e0adbeb7a..8d4e022d0270a 100644 --- a/pandas/tests/io/test_excel.py +++ b/pandas/tests/io/test_excel.py @@ -9,7 +9,6 @@ import numpy as np import pytest -from numpy import nan import pandas as pd import pandas.util.testing as tm @@ -1205,7 +1204,7 @@ def test_excel_writer_context_manager(self, *_): tm.assert_frame_equal(found_df2, self.frame2) def test_roundtrip(self, merge_cells, engine, ext): - self.frame['A'][:5] = nan + self.frame['A'][:5] = np.nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) @@ -1267,7 +1266,7 @@ def test_ts_frame(self, *_): tm.assert_frame_equal(df, recons) def test_basics_with_nan(self, merge_cells, engine, ext): - self.frame['A'][:5] = nan + self.frame['A'][:5] = np.nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) self.frame.to_excel(self.path, 'test1', header=False) @@ -1332,7 +1331,7 @@ def test_inf_roundtrip(self, *_): tm.assert_frame_equal(frame, recons) def test_sheets(self, merge_cells, engine, ext): - self.frame['A'][:5] = nan + self.frame['A'][:5] = np.nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) @@ -1354,7 +1353,7 @@ def test_sheets(self, merge_cells, engine, ext): assert 'test2' == reader.sheet_names[1] def test_colaliases(self, merge_cells, engine, ext): - self.frame['A'][:5] = nan + self.frame['A'][:5] = np.nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) @@ -1371,7 +1370,7 @@ def test_colaliases(self, merge_cells, engine, ext): tm.assert_frame_equal(xp, rs) def test_roundtrip_indexlabels(self, merge_cells, engine, ext): - self.frame['A'][:5] = nan + self.frame['A'][:5] = np.nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) diff --git a/pandas/tests/reshape/merge/test_multi.py b/pandas/tests/reshape/merge/test_multi.py index a1158201844b0..3ec9b4e656c29 100644 --- a/pandas/tests/reshape/merge/test_multi.py +++ b/pandas/tests/reshape/merge/test_multi.py @@ -3,7 +3,6 @@ from collections import OrderedDict import numpy as np -from numpy import nan from numpy.random import randn import pytest @@ -295,11 +294,11 @@ def test_left_join_index_multi_match_multiindex(self): expected = DataFrame([ ['X', 'Y', 'C', 'a', 6], ['X', 'Y', 'C', 'a', 9], - ['W', 'Y', 'C', 'e', nan], + ['W', 'Y', 'C', 'e', np.nan], ['V', 'Q', 'A', 'h', -3], ['V', 'R', 'D', 'i', 2], ['V', 'R', 'D', 'i', -1], - ['X', 'Y', 'D', 'b', nan], + ['X', 'Y', 'D', 'b', np.nan], ['X', 'Y', 'A', 'c', 1], ['X', 'Y', 'A', 'c', 4], ['W', 'Q', 'B', 'f', 3], @@ -348,10 +347,10 @@ def test_left_join_index_multi_match(self): ['c', 0, 'x'], ['c', 0, 'r'], ['c', 0, 's'], - ['b', 1, nan], + ['b', 1, np.nan], ['a', 2, 'v'], ['a', 2, 'z'], - ['b', 3, nan]], + ['b', 3, np.nan]], columns=['tag', 'val', 'char'], index=[2, 2, 2, 2, 0, 1, 1, 3]) diff --git a/pandas/tests/reshape/test_reshape.py b/pandas/tests/reshape/test_reshape.py index d8b3d9588f2f1..38598d11d2ef3 100644 --- a/pandas/tests/reshape/test_reshape.py +++ b/pandas/tests/reshape/test_reshape.py @@ -8,7 +8,6 @@ from pandas.core.sparse.api import SparseDtype, SparseArray import pandas as pd -from numpy import nan import numpy as np from pandas.util.testing import assert_frame_equal @@ -149,18 +148,18 @@ def test_include_na(self, sparse, dtype): # Sparse dataframes do not allow nan labelled columns, see #GH8822 res_na = get_dummies(s, dummy_na=True, sparse=sparse, dtype=dtype) - exp_na = DataFrame({nan: [0, 0, 1], + exp_na = DataFrame({np.nan: [0, 0, 1], 'a': [1, 0, 0], 'b': [0, 1, 0]}, dtype=self.effective_dtype(dtype)) - exp_na = exp_na.reindex(['a', 'b', nan], axis=1) + exp_na = exp_na.reindex(['a', 'b', np.nan], axis=1) # hack (NaN handling in assert_index_equal) exp_na.columns = res_na.columns assert_frame_equal(res_na, exp_na) - res_just_na = get_dummies([nan], dummy_na=True, + res_just_na = get_dummies([np.nan], dummy_na=True, sparse=sparse, dtype=dtype) - exp_just_na = DataFrame(Series(1, index=[0]), columns=[nan], + exp_just_na = DataFrame(Series(1, index=[0]), columns=[np.nan], dtype=self.effective_dtype(dtype)) tm.assert_numpy_array_equal(res_just_na.values, exp_just_na.values) @@ -444,13 +443,13 @@ def test_basic_drop_first_NA(self, sparse): sparse=sparse) exp_na = DataFrame( {'b': [0, 1, 0], - nan: [0, 0, 1]}, - dtype=np.uint8).reindex(['b', nan], axis=1) + np.nan: [0, 0, 1]}, + dtype=np.uint8).reindex(['b', np.nan], axis=1) if sparse: exp_na = exp_na.to_sparse(fill_value=0, kind='integer') assert_frame_equal(res_na, exp_na) - res_just_na = get_dummies([nan], dummy_na=True, drop_first=True, + res_just_na = get_dummies([np.nan], dummy_na=True, drop_first=True, sparse=sparse) exp_just_na = DataFrame(index=np.arange(1)) assert_frame_equal(res_just_na, exp_just_na) diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index a5a7cc2217864..3651d2c8a00b6 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -6,7 +6,6 @@ import operator import numpy as np -from numpy import nan import pytest from pandas.compat import PY35, lrange, range @@ -15,10 +14,8 @@ import pandas as pd from pandas import ( Categorical, CategoricalIndex, DataFrame, Series, bdate_range, compat, - date_range, isna, notna) -from pandas.core.index import MultiIndex -from pandas.core.indexes.datetimes import Timestamp -from pandas.core.indexes.timedeltas import Timedelta + date_range, isna, notna, MultiIndex, Timedelta, Timestamp) + import pandas.core.nanops as nanops import pandas.util.testing as tm from pandas.util.testing import ( @@ -505,7 +502,7 @@ def test_npdiff(self): s = Series(np.arange(5)) r = np.diff(s) - assert_series_equal(Series([nan, 0, 0, 0, nan]), r) + assert_series_equal(Series([np.nan, 0, 0, 0, np.nan]), r) def _check_stat_op(self, name, alternate, string_series_, check_objects=False, check_allna=False): @@ -530,7 +527,7 @@ def _check_stat_op(self, name, alternate, string_series_, assert_almost_equal(f(nona), alternate(nona.values)) assert_almost_equal(f(string_series_), alternate(nona.values)) - allna = string_series_ * nan + allna = string_series_ * np.nan if check_allna: assert np.isnan(f(allna)) @@ -706,15 +703,15 @@ def test_modulo(self): result2 = p['second'] % p['first'] assert not result.equals(result2) - # GH 9144 + # GH#9144 s = Series([0, 1]) result = s % 0 - expected = Series([nan, nan]) + expected = Series([np.nan, np.nan]) assert_series_equal(result, expected) result = 0 % s - expected = Series([nan, 0.0]) + expected = Series([np.nan, 0.0]) assert_series_equal(result, expected) @td.skip_if_no_scipy @@ -856,14 +853,14 @@ def test_count(self, datetime_series): assert datetime_series.count() == np.isfinite(datetime_series).sum() - mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, nan, 1, 2]]) + mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, np.nan, 1, 2]]) ts = Series(np.arange(len(mi)), index=mi) left = ts.count(level=1) - right = Series([2, 3, 1], index=[1, 2, nan]) + right = Series([2, 3, 1], index=[1, 2, np.nan]) assert_series_equal(left, right) - ts.iloc[[0, 3, 5]] = nan + ts.iloc[[0, 3, 5]] = np.nan assert_series_equal(ts.count(level=1), right - 1) def test_dot(self): @@ -1072,11 +1069,11 @@ def cummax(x): result = getattr(s, method)() assert_series_equal(result, expected) - e = pd.Series([False, True, nan, False]) - cse = pd.Series([0, 1, nan, 1], dtype=object) - cpe = pd.Series([False, 0, nan, 0]) - cmin = pd.Series([False, False, nan, False]) - cmax = pd.Series([False, True, nan, True]) + e = pd.Series([False, True, np.nan, False]) + cse = pd.Series([0, 1, np.nan, 1], dtype=object) + cpe = pd.Series([False, 0, np.nan, 0]) + cmin = pd.Series([False, False, np.nan, False]) + cmax = pd.Series([False, True, np.nan, True]) expecteds = {'cumsum': cse, 'cumprod': cpe, 'cummin': cmin, @@ -1219,7 +1216,7 @@ def test_idxmin(self, string_series): nona.values.argmin()) # all NaNs - allna = string_series * nan + allna = string_series * np.nan assert isna(allna.idxmin()) # datetime64[ns] @@ -1275,7 +1272,7 @@ def test_idxmax(self, string_series): nona.values.argmax()) # all NaNs - allna = string_series * nan + allna = string_series * np.nan assert isna(allna.idxmax()) from pandas import date_range @@ -1501,7 +1498,7 @@ def test_shift_int(self, datetime_series): assert_series_equal(shifted, expected) def test_shift_categorical(self): - # GH 9416 + # GH#9416 s = pd.Series(['a', 'b', 'c', 'd'], dtype='category') assert_series_equal(s.iloc[:-1], s.shift(1).shift(-1).dropna()) @@ -1520,15 +1517,13 @@ def test_shift_categorical(self): assert_index_equal(s.values.categories, sn2.values.categories) def test_unstack(self): - from numpy import nan - index = MultiIndex(levels=[['bar', 'foo'], ['one', 'three', 'two']], labels=[[1, 1, 0, 0], [0, 1, 0, 2]]) s = Series(np.arange(4.), index=index) unstacked = s.unstack() - expected = DataFrame([[2., nan, 3.], [0., 1., nan]], + expected = DataFrame([[2., np.nan, 3.], [0., 1., np.nan]], index=['bar', 'foo'], columns=['one', 'three', 'two']) @@ -1552,17 +1547,18 @@ def test_unstack(self): idx = pd.MultiIndex.from_arrays([[101, 102], [3.5, np.nan]]) ts = pd.Series([1, 2], index=idx) left = ts.unstack() - right = DataFrame([[nan, 1], [2, nan]], index=[101, 102], - columns=[nan, 3.5]) + right = DataFrame([[np.nan, 1], [2, np.nan]], index=[101, 102], + columns=[np.nan, 3.5]) assert_frame_equal(left, right) idx = pd.MultiIndex.from_arrays([['cat', 'cat', 'cat', 'dog', 'dog' ], ['a', 'a', 'b', 'a', 'b'], [1, 2, 1, 1, np.nan]]) ts = pd.Series([1.0, 1.1, 1.2, 1.3, 1.4], index=idx) - right = DataFrame([[1.0, 1.3], [1.1, nan], [nan, 1.4], [1.2, nan]], + right = DataFrame([[1.0, 1.3], [1.1, np.nan], + [np.nan, 1.4], [1.2, np.nan]], columns=['cat', 'dog']) - tpls = [('a', 1), ('a', 2), ('b', nan), ('b', 1)] + tpls = [('a', 1), ('a', 2), ('b', np.nan), ('b', 1)] right.index = pd.MultiIndex.from_tuples(tpls) assert_frame_equal(ts.unstack(level=0), right) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index ce0cf0d5c089e..1714392667b05 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -5,7 +5,6 @@ from datetime import datetime, timedelta import numpy as np -from numpy import nan import numpy.ma as ma import pytest @@ -372,14 +371,14 @@ def test_unordered_compare_equal(self): def test_constructor_maskedarray(self): data = ma.masked_all((3, ), dtype=float) result = Series(data) - expected = Series([nan, nan, nan]) + expected = Series([np.nan, np.nan, np.nan]) assert_series_equal(result, expected) data[0] = 0.0 data[2] = 2.0 index = ['a', 'b', 'c'] result = Series(data, index=index) - expected = Series([0.0, nan, 2.0], index=index) + expected = Series([0.0, np.nan, 2.0], index=index) assert_series_equal(result, expected) data[1] = 1.0 @@ -389,14 +388,14 @@ def test_constructor_maskedarray(self): data = ma.masked_all((3, ), dtype=int) result = Series(data) - expected = Series([nan, nan, nan], dtype=float) + expected = Series([np.nan, np.nan, np.nan], dtype=float) assert_series_equal(result, expected) data[0] = 0 data[2] = 2 index = ['a', 'b', 'c'] result = Series(data, index=index) - expected = Series([0, nan, 2], index=index, dtype=float) + expected = Series([0, np.nan, 2], index=index, dtype=float) assert_series_equal(result, expected) data[1] = 1 @@ -406,14 +405,14 @@ def test_constructor_maskedarray(self): data = ma.masked_all((3, ), dtype=bool) result = Series(data) - expected = Series([nan, nan, nan], dtype=object) + expected = Series([np.nan, np.nan, np.nan], dtype=object) assert_series_equal(result, expected) data[0] = True data[2] = False index = ['a', 'b', 'c'] result = Series(data, index=index) - expected = Series([True, nan, False], index=index, dtype=object) + expected = Series([True, np.nan, False], index=index, dtype=object) assert_series_equal(result, expected) data[1] = True @@ -632,14 +631,14 @@ def test_constructor_dtype_datetime64(self): s = Series(iNaT, index=lrange(5)) assert not isna(s).all() - s = Series(nan, dtype='M8[ns]', index=lrange(5)) + s = Series(np.nan, dtype='M8[ns]', index=lrange(5)) assert isna(s).all() s = Series([datetime(2001, 1, 2, 0, 0), iNaT], dtype='M8[ns]') assert isna(s[1]) assert s.dtype == 'M8[ns]' - s = Series([datetime(2001, 1, 2, 0, 0), nan], dtype='M8[ns]') + s = Series([datetime(2001, 1, 2, 0, 0), np.nan], dtype='M8[ns]') assert isna(s[1]) assert s.dtype == 'M8[ns]' @@ -885,7 +884,7 @@ def test_constructor_periodindex(self): def test_constructor_dict(self): d = {'a': 0., 'b': 1., 'c': 2.} result = Series(d, index=['b', 'c', 'd', 'a']) - expected = Series([1, 2, nan, 0], index=['b', 'c', 'd', 'a']) + expected = Series([1, 2, np.nan, 0], index=['b', 'c', 'd', 'a']) assert_series_equal(result, expected) pidx = tm.makePeriodIndex(100) diff --git a/pandas/tests/series/test_rank.py b/pandas/tests/series/test_rank.py index 5b0ea37a0bfcf..66cc9149fb43e 100644 --- a/pandas/tests/series/test_rank.py +++ b/pandas/tests/series/test_rank.py @@ -3,7 +3,6 @@ from itertools import chain import numpy as np -from numpy import nan import pytest from pandas._libs.algos import Infinity, NegInfinity @@ -20,15 +19,15 @@ class TestSeriesRank(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]) 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]), } def test_rank(self): diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index ff505f2986b1a..e5cc75d08fe4b 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -4,7 +4,7 @@ import pytest from numpy.random import RandomState -from numpy import nan + from datetime import datetime from itertools import permutations import struct @@ -1435,11 +1435,11 @@ def _check(arr): result = libalgos.rank_1d_float64(arr) arr[mask] = np.inf exp = rankdata(arr) - exp[mask] = nan + exp[mask] = np.nan assert_almost_equal(result, exp) - _check(np.array([nan, nan, 5., 5., 5., nan, 1, 2, 3, nan])) - _check(np.array([4., nan, 5., 5., 5., nan, 1, 2, 4., nan])) + _check(np.array([np.nan, np.nan, 5., 5., 5., np.nan, 1, 2, 3, np.nan])) + _check(np.array([4., np.nan, 5., 5., 5., np.nan, 1, 2, 4., np.nan])) def test_basic(self): exp = np.array([1, 2], dtype=np.float64) diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 2717b92e05a29..da6d24bcbb0b1 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -2058,21 +2058,20 @@ def test_indexing_over_hashtable_size_cutoff(self): def test_multiindex_na_repr(self): # only an issue with long columns - from numpy import nan df3 = DataFrame({ 'A' * 30: {('A', 'A0006000', 'nuit'): 'A0006000'}, - 'B' * 30: {('A', 'A0006000', 'nuit'): nan}, - 'C' * 30: {('A', 'A0006000', 'nuit'): nan}, - 'D' * 30: {('A', 'A0006000', 'nuit'): nan}, + 'B' * 30: {('A', 'A0006000', 'nuit'): np.nan}, + 'C' * 30: {('A', 'A0006000', 'nuit'): np.nan}, + 'D' * 30: {('A', 'A0006000', 'nuit'): np.nan}, 'E' * 30: {('A', 'A0006000', 'nuit'): 'A'}, - 'F' * 30: {('A', 'A0006000', 'nuit'): nan}, + 'F' * 30: {('A', 'A0006000', 'nuit'): np.nan}, }) idf = df3.set_index(['A' * 30, 'C' * 30]) repr(idf) def test_assign_index_sequences(self): - # #2200 + # GH#2200 df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}).set_index(["a", "b"]) diff --git a/pandas/tests/test_sorting.py b/pandas/tests/test_sorting.py index 22e758a0e59a7..525c95d33e330 100644 --- a/pandas/tests/test_sorting.py +++ b/pandas/tests/test_sorting.py @@ -5,7 +5,7 @@ from datetime import datetime import numpy as np -from numpy import nan + from pandas.core import common as com from pandas import DataFrame, MultiIndex, merge, concat, Series, compat from pandas.util import testing as tm @@ -100,7 +100,7 @@ def aggr(func): assert_frame_equal(gr.median(), aggr(np.median)) def test_lexsort_indexer(self): - keys = [[nan] * 5 + list(range(100)) + [nan] * 5] + keys = [[np.nan] * 5 + list(range(100)) + [np.nan] * 5] # orders=True, na_position='last' result = lexsort_indexer(keys, orders=True, na_position='last') exp = list(range(5, 105)) + list(range(5)) + list(range(105, 110)) @@ -123,7 +123,7 @@ def test_lexsort_indexer(self): def test_nargsort(self): # np.argsort(items) places NaNs last - items = [nan] * 5 + list(range(100)) + [nan] * 5 + items = [np.nan] * 5 + list(range(100)) + [np.nan] * 5 # np.argsort(items2) may not place NaNs first items2 = np.array(items, dtype='O') From bb56802b097d96ec2fa6e3a3f4e692fce386765c Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 16 Nov 2018 19:34:10 -0800 Subject: [PATCH 02/16] box tests --- pandas/tests/arithmetic/test_datetime64.py | 118 ++++++++++---------- pandas/tests/arithmetic/test_period.py | 21 ++++ pandas/tests/arithmetic/test_timedelta64.py | 4 +- 3 files changed, 86 insertions(+), 57 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 883b281466540..20fb519f12392 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -53,21 +53,26 @@ def test_dt64_nat_comparison(self): class TestDatetime64SeriesComparison(object): # TODO: moved from tests.series.test_operators; needs cleanup - def test_comparison_invalid(self): + def test_comparison_invalid(self, box): # GH#4968 # invalid date/int comparisons + xbox = box if box not in [pd.Index, DatetimeArray] else np.ndarray + ser = Series(range(5)) ser2 = Series(pd.date_range('20010101', periods=5)) + ser = tm.box_expected(ser, box) + ser2 = tm.box_expected(ser2, box) + for (x, y) in [(ser, ser2), (ser2, ser)]: result = x == y - expected = Series([False] * 5) - tm.assert_series_equal(result, expected) + expected = tm.box_expected([False] * 5, xbox) + tm.assert_equal(result, expected) result = x != y - expected = Series([True] * 5) - tm.assert_series_equal(result, expected) + expected = tm.box_expected([True] * 5, xbox) + tm.assert_equal(result, expected) with pytest.raises(TypeError): x >= y @@ -84,27 +89,33 @@ def test_comparison_invalid(self): [Period('2011-01', freq='M'), NaT, Period('2011-03', freq='M')] ]) @pytest.mark.parametrize('dtype', [None, object]) - def test_nat_comparisons_scalar(self, dtype, data): - left = Series(data, dtype=dtype) - - expected = Series([False, False, False]) - tm.assert_series_equal(left == NaT, expected) - tm.assert_series_equal(NaT == left, expected) - - expected = Series([True, True, True]) - tm.assert_series_equal(left != NaT, expected) - tm.assert_series_equal(NaT != left, expected) + def test_nat_comparisons_scalar(self, dtype, data, box): + xbox = box if box is not pd.Index else np.ndarray - expected = Series([False, False, False]) - tm.assert_series_equal(left < NaT, expected) - tm.assert_series_equal(NaT > left, expected) - tm.assert_series_equal(left <= NaT, expected) - tm.assert_series_equal(NaT >= left, expected) - - tm.assert_series_equal(left > NaT, expected) - tm.assert_series_equal(NaT < left, expected) - tm.assert_series_equal(left >= NaT, expected) - tm.assert_series_equal(NaT <= left, expected) + left = Series(data, dtype=dtype) + left = tm.box_expected(left, box) + + expected = [False, False, False] + expected = tm.box_expected(expected, xbox) + tm.assert_equal(left == NaT, expected) + tm.assert_equal(NaT == left, expected) + + expected = [True, True, True] + expected = tm.box_expected(expected, xbox) + tm.assert_equal(left != NaT, expected) + tm.assert_equal(NaT != left, expected) + + expected = [False, False, False] + expected = tm.box_expected(expected, xbox) + tm.assert_equal(left < NaT, expected) + tm.assert_equal(NaT > left, expected) + tm.assert_equal(left <= NaT, expected) + tm.assert_equal(NaT >= left, expected) + + tm.assert_equal(left > NaT, expected) + tm.assert_equal(NaT < left, expected) + tm.assert_equal(left >= NaT, expected) + tm.assert_equal(NaT <= left, expected) def test_series_comparison_scalars(self): series = Series(date_range('1/1/2000', periods=10)) @@ -180,18 +191,6 @@ def test_dt64ser_cmp_date_invalid(self, box_with_datetime): with pytest.raises(TypeError): ser <= date - def test_dt64ser_cmp_period_scalar(self): - ser = Series(pd.period_range('2000-01-01', periods=10, freq='D')) - val = Period('2000-01-04', freq='D') - result = ser > val - expected = Series([x > val for x in ser]) - tm.assert_series_equal(result, expected) - - val = ser[5] - result = ser > val - expected = Series([x > val for x in ser]) - tm.assert_series_equal(result, expected) - @pytest.mark.parametrize("left,right", [ ("lt", "gt"), ("le", "ge"), @@ -649,14 +648,16 @@ def test_dti_cmp_object_dtype(self): # Arithmetic class TestFrameArithmetic(object): - def test_dt64arr_sub_timestamp(self, box): + + @pytest.mark.parametrize('ts', [ + pd.Timestamp('2013-01-01'), + pd.Timestamp('2013-01-01').to_pydatetime(), + pd.Timestamp('2013-01-01').to_datetime64()]) + def test_dt64arr_sub_dtscalar(self, box, ts): # GH#8554, GH#22163 DataFrame op should _not_ return dt64 dtype idx = pd.date_range('2013-01-01', periods=3) idx = tm.box_expected(idx, box) - ts = pd.Timestamp('2013-01-01') - # TODO: parametrize over scalar types - expected = pd.TimedeltaIndex(['0 Days', '1 Day', '2 Days']) expected = tm.box_expected(expected, box) @@ -772,7 +773,7 @@ def test_sub_datetime64_not_ns(self, box): tm.assert_equal(result, -expected) def test_sub_single_tz(self): - # GH12290 + # GH#12290 s1 = Series([pd.Timestamp('2016-02-10', tz='America/Sao_Paulo')]) s2 = Series([pd.Timestamp('2016-02-08', tz='America/Sao_Paulo')]) result = s1 - s2 @@ -822,7 +823,7 @@ def test_dt64_series_addsub_timedelta(self): tm.assert_series_equal(result2, expected) def test_dt64_series_add_tick_DateOffset(self): - # GH 4532 + # GH#4532 # operate with pd.offsets ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) expected = Series([Timestamp('20130101 9:01:05'), @@ -835,7 +836,7 @@ def test_dt64_series_add_tick_DateOffset(self): tm.assert_series_equal(result2, expected) def test_dt64_series_sub_tick_DateOffset(self): - # GH 4532 + # GH#4532 # operate with pd.offsets ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) expected = Series([Timestamp('20130101 9:00:55'), @@ -853,7 +854,7 @@ def test_dt64_series_sub_tick_DateOffset(self): @pytest.mark.parametrize('cls_name', ['Day', 'Hour', 'Minute', 'Second', 'Milli', 'Micro', 'Nano']) def test_dt64_series_add_tick_DateOffset_smoke(self, cls_name): - # GH 4532 + # GH#4532 # smoke tests for valid DateOffsets ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) @@ -862,7 +863,7 @@ def test_dt64_series_add_tick_DateOffset_smoke(self, cls_name): offset_cls(5) + ser def test_dt64_series_add_mixed_tick_DateOffset(self): - # GH 4532 + # GH#4532 # operate with pd.offsets s = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) @@ -878,22 +879,27 @@ def test_dt64_series_add_mixed_tick_DateOffset(self): Timestamp('20130101 9:07:00.005')]) tm.assert_series_equal(result, expected) - def test_dt64_series_sub_NaT(self): + def test_dt64arr_sub_NaT(self, box): # GH#18808 dti = pd.DatetimeIndex([pd.NaT, pd.Timestamp('19900315')]) - ser = pd.Series(dti) - res = ser - pd.NaT + ser = tm.box_expected(dti, box, transpose=False) + + result = ser - pd.NaT expected = pd.Series([pd.NaT, pd.NaT], dtype='timedelta64[ns]') - tm.assert_series_equal(res, expected) + # FIXME: raises ValueError with ranspose + expected = tm.box_expected(expected, box, transpose=False) + tm.assert_equal(result, expected) dti_tz = dti.tz_localize('Asia/Tokyo') - ser_tz = pd.Series(dti_tz) - res = ser_tz - pd.NaT + ser_tz = tm.box_expected(dti_tz, box, transpose=False) + + result = ser_tz - pd.NaT expected = pd.Series([pd.NaT, pd.NaT], dtype='timedelta64[ns]') - tm.assert_series_equal(res, expected) + expected = tm.box_expected(expected, box, transpose=False) + tm.assert_equal(result, expected) def test_dt64_series_arith_overflow(self): - # GH#12534, fixed by #19024 + # GH#12534, fixed by GH#19024 dt = pd.Timestamp('1700-01-31') td = pd.Timedelta('20000 Days') dti = pd.date_range('1949-09-30', freq='100Y', periods=4) @@ -924,7 +930,7 @@ def test_dt64_series_arith_overflow(self): tm.assert_series_equal(res, -expected) def test_datetime64_ops_nat(self): - # GH 11349 + # GH#11349 datetime_series = Series([NaT, Timestamp('19900315')]) nat_series_dtype_timestamp = Series([NaT, NaT], dtype='datetime64[ns]') single_nat_dtype_datetime = Series([NaT], dtype='datetime64[ns]') @@ -1393,7 +1399,7 @@ def test_dti_sub_dt64_array_naive(self): tm.assert_index_equal(result, expected) def test_dti_sub_dt64_array_aware_raises(self, tz_aware_fixture): - tz = tz_naive_fixture + tz = tz_aware_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) dtarr = dti.values diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index a26a11cb6be9e..d44c4f31b15c6 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -29,6 +29,27 @@ def test_pi_cmp_period(self): exp = idx.values < idx.values[10] tm.assert_numpy_array_equal(result, exp) + # TODO: moved from test_datetime64; de-duplicate with version below + def test_parr_cmp_period_scalar2(self, box): + xbox = box if box is not pd.Index else np.ndarray + + pi = pd.period_range('2000-01-01', periods=10, freq='D') + + val = Period('2000-01-04', freq='D') + expected = [x > val for x in pi] + + ser = tm.box_expected(pi, box) + expected = tm.box_expected(expected, xbox) + result = ser > val + tm.assert_equal(result, expected) + + val = pi[5] + result = ser > val + expected = [x > val for x in pi] + expected = tm.box_expected(expected, xbox) + tm.assert_equal(result, expected) + + @pytest.mark.parametrize('freq', ['M', '2M', '3M']) def test_parr_cmp_period_scalar(self, freq, box): # GH#13200 diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 58c8b3b07f723..4d12a86e27e64 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -25,7 +25,7 @@ class TestTimedelta64ArrayComparisons(object): # TODO: All of these need to be parametrized over box def test_compare_timedelta_series(self): - # regresssion test for GH5963 + # regresssion test for GH#5963 s = pd.Series([timedelta(days=1), timedelta(days=2)]) actual = s > timedelta(days=1) expected = pd.Series([False, True]) @@ -40,9 +40,11 @@ def test_tdi_cmp_str_invalid(self): left > right with pytest.raises(TypeError): + # FIXME: Shouldn't this return all-False? left == right with pytest.raises(TypeError): + # FIXME: Shouldn't this return all-True? left != right @pytest.mark.parametrize('dtype', [None, object]) From 0a7bb2b67e855d174f67dd1b73e0452bd1c79deb Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 07:58:56 -0800 Subject: [PATCH 03/16] box more tests --- pandas/tests/arithmetic/test_datetime64.py | 52 +++++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 20fb519f12392..bb2d9454e8968 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1378,57 +1378,85 @@ def test_add_datetimelike_and_dti(self, addend, tz): # ------------------------------------------------------------- # __add__/__sub__ with ndarray[datetime64] and ndarray[timedelta64] - def test_dti_add_dt64_array_raises(self, tz_naive_fixture): + def test_dti_add_dt64_array_raises(self, tz_naive_fixture, + box_with_datetime): + if box_with_datetime is pd.DataFrame: + pytest.xfail("FIXME: ValueError with transpose; " + "alignment error without") + tz = tz_naive_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) dtarr = dti.values + dti = tm.box_expected(dti, box_with_datetime) + with pytest.raises(TypeError): dti + dtarr with pytest.raises(TypeError): dtarr + dti - def test_dti_sub_dt64_array_naive(self): + def test_dti_sub_dt64_array_naive(self, box_with_datetime): dti = pd.date_range('2016-01-01', periods=3, tz=None) dtarr = dti.values + dti = tm.box_expected(dti, box_with_datetime) + expected = dti - dti result = dti - dtarr - tm.assert_index_equal(result, expected) + tm.assert_equal(result, expected) result = dtarr - dti - tm.assert_index_equal(result, expected) + tm.assert_equal(result, expected) + + def test_dti_sub_dt64_array_aware_raises(self, tz_aware_fixture, + box_with_datetime): + if box_with_datetime is pd.DataFrame: + pytest.xfail("FIXME: ValueError with transpose; " + "alignment error without") - def test_dti_sub_dt64_array_aware_raises(self, tz_aware_fixture): tz = tz_aware_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) dtarr = dti.values + dti = tm.box_expected(dti, box_with_datetime) + with pytest.raises(TypeError): dti - dtarr with pytest.raises(TypeError): dtarr - dti - def test_dti_add_td64_array(self, tz_naive_fixture): + def test_dti_add_td64_array(self, tz_naive_fixture, box_with_datetime): + if box_with_datetime is pd.DataFrame: + pytest.xfail("FIXME: ValueError with transpose; " + "alignment error without") + tz = tz_naive_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day']) tdarr = tdi.values + dti = tm.box_expected(dti, box_with_datetime) + expected = dti + tdi result = dti + tdarr - tm.assert_index_equal(result, expected) + tm.assert_equal(result, expected) result = tdarr + dti - tm.assert_index_equal(result, expected) + tm.assert_equal(result, expected) + + def test_dti_sub_td64_array(self, tz_naive_fixture, box_with_datetime): + if box_with_datetime is pd.DataFrame: + pytest.xfail("FIXME: ValueError with transpose; " + "alignment error without") - def test_dti_sub_td64_array(self, tz_naive_fixture): tz = tz_naive_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day']) tdarr = tdi.values + dti = tm.box_expected(dti, box_with_datetime) + expected = dti - tdi result = dti - tdarr - tm.assert_index_equal(result, expected) + tm.assert_equal(result, expected) with pytest.raises(TypeError): tdarr - dti @@ -1496,8 +1524,8 @@ def test_dt64arr_add_sub_period(self, dti_freq, box_with_datetime): @pytest.mark.parametrize('pi_freq', ['D', 'W', 'Q', 'H']) @pytest.mark.parametrize('dti_freq', [None, 'D']) - def test_dti_add_sub_pi(self, dti_freq, pi_freq, - box_with_datetime, box_with_period): + def test_dt64arr_add_sub_parr(self, dti_freq, pi_freq, + box_with_datetime, box_with_period): # GH#20049 subtracting PeriodIndex should raise TypeError dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq) pi = dti.to_period(pi_freq) From c0f82bae1a69a546a3b0ff5d4e96440e5a0358ec Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 12:17:37 -0800 Subject: [PATCH 04/16] parametrize more tests over box --- pandas/tests/arithmetic/test_datetime64.py | 83 +++++++++++----------- pandas/tests/arithmetic/test_period.py | 70 +++++++++--------- 2 files changed, 77 insertions(+), 76 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index bb2d9454e8968..4d37dc0481bea 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -647,7 +647,12 @@ def test_dti_cmp_object_dtype(self): # ------------------------------------------------------------------ # Arithmetic -class TestFrameArithmetic(object): +class TestDatetime64Arithmetic(object): + # This class is intended for "finished" tests that are fully parametrized + # over DataFrame/Series/Index/DatetimeArray + + # ----------------------------------------------------------------- + # Subtraction of datetime-like scalars @pytest.mark.parametrize('ts', [ pd.Timestamp('2013-01-01'), @@ -667,21 +672,20 @@ def test_dt64arr_sub_dtscalar(self, box, ts): def test_dt64arr_sub_datetime64_not_ns(self, box): # GH#7996, GH#22163 ensure non-nano datetime64 is converted to nano # for DataFrame operation + dt64 = np.datetime64('2013-01-01') + assert dt64.dtype == 'datetime64[D]' dti = pd.date_range('20130101', periods=3) dtarr = tm.box_expected(dti, box) - dt64 = np.datetime64('2013-01-01') - assert dt64.dtype == 'datetime64[D]' - expected = pd.TimedeltaIndex(['0 Days', '1 Day', '2 Days']) expected = tm.box_expected(expected, box) result = dtarr - dt64 tm.assert_equal(result, expected) - -class TestTimestampSeriesArithmetic(object): + result = dt64 - dtarr + tm.assert_equal(result, -expected) def test_dt64arr_sub_timestamp(self, box): ser = pd.date_range('2014-03-17', periods=2, freq='D', @@ -698,6 +702,38 @@ def test_dt64arr_sub_timestamp(self, box): tm.assert_equal(ser - ts, expected) tm.assert_equal(ts - ser, -expected) + # ------------------------------------------------------------- + # Addition of datetime-like others (invalid) + + def test_dt64arr_add_timestamp_raises(self, box_with_datetime): + # GH#22163 ensure DataFrame doesn't cast Timestamp to i8 + idx = DatetimeIndex(['2011-01-01', '2011-01-02']) + idx = tm.box_expected(idx, box_with_datetime) + msg = "cannot add" + with pytest.raises(TypeError, match=msg): + idx + Timestamp('2011-01-01') + with pytest.raises(TypeError, match=msg): + Timestamp('2011-01-01') + idx + + # ------------------------------------------------------------- + # Other Invalid Addition/Subtraction + + @pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])]) + def test_dt64arr_add_sub_float(self, other, box_with_datetime): + dti = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') + dtarr = tm.box_expected(dti, box_with_datetime) + with pytest.raises(TypeError): + dtarr + other + with pytest.raises(TypeError): + other + dtarr + with pytest.raises(TypeError): + dtarr - other + with pytest.raises(TypeError): + other - dtarr + + +class TestTimestampSeriesArithmetic(object): + def test_dt64ser_sub_datetime_dtype(self): ts = Timestamp(datetime(1993, 1, 7, 13, 30, 00)) dt = datetime(1993, 6, 22, 13, 30) @@ -754,24 +790,6 @@ def check(get_ser, test_ser): if op_str not in ['__add__', '__radd__', '__sub__', '__rsub__']: check(dt2, td2) - def test_sub_datetime64_not_ns(self, box): - # GH#7996 operation with non-nano datetime64 scalar - dt64 = np.datetime64('2013-01-01') - assert dt64.dtype == 'datetime64[D]' - - obj = date_range('20130101', periods=3) - obj = tm.box_expected(obj, box) - - expected = TimedeltaIndex([Timedelta(days=0), Timedelta(days=1), - Timedelta(days=2)]) - expected = tm.box_expected(expected, box) - - result = obj - dt64 - tm.assert_equal(result, expected) - - result = dt64 - obj - tm.assert_equal(result, -expected) - def test_sub_single_tz(self): # GH#12290 s1 = Series([pd.Timestamp('2016-02-10', tz='America/Sao_Paulo')]) @@ -1069,23 +1087,6 @@ class TestDatetimeIndexArithmetic(object): # ------------------------------------------------------------- # Invalid Operations - @pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])]) - @pytest.mark.parametrize('op', [operator.add, ops.radd, - operator.sub, ops.rsub]) - def test_dti_add_sub_float(self, op, other): - dti = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') - with pytest.raises(TypeError): - op(dti, other) - - def test_dti_add_timestamp_raises(self, box_with_datetime): - # GH#22163 ensure DataFrame doesn't cast Timestamp to i8 - idx = DatetimeIndex(['2011-01-01', '2011-01-02']) - idx = tm.box_expected(idx, box_with_datetime) - msg = "cannot add" - with pytest.raises(TypeError, match=msg): - idx + Timestamp('2011-01-01') - with pytest.raises(TypeError, match=msg): - Timestamp('2011-01-01') + idx # ------------------------------------------------------------- # Binary operations DatetimeIndex and int diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index d44c4f31b15c6..7cbc20ec2ed2c 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -10,9 +10,9 @@ import pandas as pd import pandas.util.testing as tm +from pandas._libs.tslibs.period import IncompatibleFrequency from pandas.errors import PerformanceWarning -import pandas.core.indexes.period as period from pandas.core import ops from pandas import Period, PeriodIndex, period_range, Series from pandas.tseries.frequencies import to_offset @@ -136,27 +136,27 @@ def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box): base = tm.box_expected(base, box) msg = "Input has different freq=A-DEC from " - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): base <= Period('2011', freq='A') - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): Period('2011', freq='A') >= base # TODO: Could parametrize over boxes for idx? idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='A') - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): base <= idx # Different frequency msg = "Input has different freq=4M from " - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): base <= Period('2011', freq='4M') - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): Period('2011', freq='4M') >= base idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='4M') - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): base <= idx @pytest.mark.parametrize('freq', ['M', '2M', '3M']) @@ -208,10 +208,10 @@ def test_pi_cmp_nat_mismatched_freq_raises(self, freq): diff = PeriodIndex(['2011-02', '2011-01', '2011-04', 'NaT'], freq='4M') msg = "Input has different freq=4M from PeriodIndex" - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): idx1 > diff - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): idx1 == diff # TODO: De-duplicate with test_pi_cmp_nat @@ -368,7 +368,7 @@ def test_parr_sub_pi_mismatched_freq(self, box): # TODO: parametrize over boxes for other? rng = tm.box_expected(rng, box) - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): rng - other # ------------------------------------------------------------- @@ -423,12 +423,12 @@ def test_pi_add_sub_td64_array_non_tick_raises(self): tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day']) tdarr = tdi.values - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): rng + tdarr - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): tdarr + rng - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): rng - tdarr with pytest.raises(TypeError): tdarr - rng @@ -486,10 +486,10 @@ def test_pi_add_offset_array(self, box): pd.offsets.Minute(n=-2)]) # addition/subtraction ops with incompatible offsets should issue # a PerformanceWarning and _then_ raise a TypeError. - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): with tm.assert_produces_warning(PerformanceWarning): pi + unanchored - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): with tm.assert_produces_warning(PerformanceWarning): unanchored + pi @@ -510,10 +510,10 @@ def test_pi_sub_offset_array(self, box): # addition/subtraction ops with anchored offsets should issue # a PerformanceWarning and _then_ raise a TypeError. - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): with tm.assert_produces_warning(PerformanceWarning): pi - anchored - with pytest.raises(period.IncompatibleFrequency): + with pytest.raises(IncompatibleFrequency): with tm.assert_produces_warning(PerformanceWarning): anchored - pi @@ -724,13 +724,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_daily(self, not_daily): other = not_daily rng = pd.period_range('2014-05-01', '2014-05-15', freq='D') msg = 'Input has different freq(=.+)? from Period.*?\\(freq=D\\)' - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng + other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng += other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng - other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng -= other def test_pi_add_iadd_timedeltalike_hourly(self, two_hours): @@ -750,10 +750,10 @@ def test_pi_add_timedeltalike_mismatched_freq_hourly(self, not_hourly): rng = pd.period_range('2014-01-01 10:00', '2014-01-05 10:00', freq='H') msg = 'Input has different freq(=.+)? from Period.*?\\(freq=H\\)' - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng + other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng += other def test_pi_sub_isub_timedeltalike_hourly(self, two_hours): @@ -784,13 +784,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_annual(self, rng = pd.period_range('2014', '2024', freq='A') msg = ('Input has different freq(=.+)? ' 'from Period.*?\\(freq=A-DEC\\)') - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng + other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng += other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng - other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng -= other def test_pi_add_iadd_timedeltalike_M(self): @@ -808,13 +808,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self, other = mismatched_freq rng = pd.period_range('2014-01', '2016-12', freq='M') msg = 'Input has different freq(=.+)? from Period.*?\\(freq=M\\)' - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng + other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng += other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng - other - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): rng -= other def test_parr_add_sub_td64_nat(self, box_transpose_fail): @@ -1027,17 +1027,17 @@ def test_pi_offset_errors(self): # from Period msg = r"Input has different freq from Period.*?\(freq=D\)" for obj in [idx, ser]: - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): obj + pd.offsets.Hour(2) - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): pd.offsets.Hour(2) + obj - with pytest.raises(period.IncompatibleFrequency, match=msg): + with pytest.raises(IncompatibleFrequency, match=msg): obj - pd.offsets.Hour(2) def test_pi_sub_period(self): - # GH 13071 + # GH#13071 idx = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], freq='M', name='idx') From c3d3d4bfdf9c35e816a4c7799813e34a408a8d9b Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 12:29:10 -0800 Subject: [PATCH 05/16] parametrize more tests over box --- pandas/tests/arithmetic/test_datetime64.py | 113 +++++++++++---------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 4d37dc0481bea..cfe54b47b3ecc 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -20,8 +20,6 @@ from pandas._libs.tslibs.conversion import localize_pydatetime from pandas._libs.tslibs.offsets import shift_months -from pandas.core import ops - from pandas import ( Timestamp, Timedelta, Period, Series, date_range, NaT, DatetimeIndex, TimedeltaIndex) @@ -651,6 +649,9 @@ class TestDatetime64Arithmetic(object): # This class is intended for "finished" tests that are fully parametrized # over DataFrame/Series/Index/DatetimeArray + # ------------------------------------------------------------- + # Addition/Subtraction of timedelta-like + # ----------------------------------------------------------------- # Subtraction of datetime-like scalars @@ -702,6 +703,21 @@ def test_dt64arr_sub_timestamp(self, box): tm.assert_equal(ser - ts, expected) tm.assert_equal(ts - ser, -expected) + # ------------------------------------------------------------- + # Subtraction of datetime-like array-like + + def test_dt64arr_sub_dt64ndarray_naive(self, box_with_datetime): + dti = pd.date_range('2016-01-01', periods=3, tz=None) + dt64vals = dti.values + + dtarr = tm.box_expected(dti, box_with_datetime) + + expected = dtarr - dtarr + result = dtarr - dt64vals + tm.assert_equal(result, expected) + result = dt64vals - dtarr + tm.assert_equal(result, expected) + # ------------------------------------------------------------- # Addition of datetime-like others (invalid) @@ -731,6 +747,44 @@ def test_dt64arr_add_sub_float(self, other, box_with_datetime): with pytest.raises(TypeError): other - dtarr + @pytest.mark.parametrize('pi_freq', ['D', 'W', 'Q', 'H']) + @pytest.mark.parametrize('dti_freq', [None, 'D']) + def test_dt64arr_add_sub_parr(self, dti_freq, pi_freq, + box_with_datetime, box_with_period): + # GH#20049 subtracting PeriodIndex should raise TypeError + dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq) + pi = dti.to_period(pi_freq) + + dtarr = tm.box_expected(dti, box_with_datetime) + parr = tm.box_expected(pi, box_with_period) + + with pytest.raises(TypeError): + dtarr + parr + with pytest.raises(TypeError): + parr + dtarr + with pytest.raises(TypeError): + dtarr - parr + with pytest.raises(TypeError): + parr - dtarr + + @pytest.mark.parametrize('dti_freq', [None, 'D']) + def test_dt64arr_add_sub_period_scalar(self, dti_freq, box_with_datetime): + # GH#13078 + # not supported, check TypeError + per = pd.Period('2011-01-01', freq='D') + + idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq) + dtarr = tm.box_expected(idx, box_with_datetime) + + with pytest.raises(TypeError): + dtarr + per + with pytest.raises(TypeError): + per + dtarr + with pytest.raises(TypeError): + dtarr - per + with pytest.raises(TypeError): + per - dtarr + class TestTimestampSeriesArithmetic(object): @@ -1084,10 +1138,6 @@ def test_operators_datetimelike_with_timezones(self): class TestDatetimeIndexArithmetic(object): - # ------------------------------------------------------------- - # Invalid Operations - - # ------------------------------------------------------------- # Binary operations DatetimeIndex and int @@ -1258,6 +1308,7 @@ def test_dt64arr_add_sub_td64_nat(self, box, tz_naive_fixture): # ------------------------------------------------------------- # Binary operations DatetimeIndex and TimedeltaIndex/array + def test_dti_add_tdi(self, tz_naive_fixture): # GH#17558 tz = tz_naive_fixture @@ -1396,18 +1447,6 @@ def test_dti_add_dt64_array_raises(self, tz_naive_fixture, with pytest.raises(TypeError): dtarr + dti - def test_dti_sub_dt64_array_naive(self, box_with_datetime): - dti = pd.date_range('2016-01-01', periods=3, tz=None) - dtarr = dti.values - - dti = tm.box_expected(dti, box_with_datetime) - - expected = dti - dti - result = dti - dtarr - tm.assert_equal(result, expected) - result = dtarr - dti - tm.assert_equal(result, expected) - def test_dti_sub_dt64_array_aware_raises(self, tz_aware_fixture, box_with_datetime): if box_with_datetime is pd.DataFrame: @@ -1505,44 +1544,6 @@ def test_sub_dti_dti(self): result = dti2 - dti1 tm.assert_index_equal(result, expected) - @pytest.mark.parametrize('dti_freq', [None, 'D']) - def test_dt64arr_add_sub_period(self, dti_freq, box_with_datetime): - # GH#13078 - # not supported, check TypeError - p = pd.Period('2011-01-01', freq='D') - - idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq) - idx = tm.box_expected(idx, box_with_datetime) - - with pytest.raises(TypeError): - idx + p - with pytest.raises(TypeError): - p + idx - with pytest.raises(TypeError): - idx - p - with pytest.raises(TypeError): - p - idx - - @pytest.mark.parametrize('pi_freq', ['D', 'W', 'Q', 'H']) - @pytest.mark.parametrize('dti_freq', [None, 'D']) - def test_dt64arr_add_sub_parr(self, dti_freq, pi_freq, - box_with_datetime, box_with_period): - # GH#20049 subtracting PeriodIndex should raise TypeError - dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq) - pi = dti.to_period(pi_freq) - - dtarr = tm.box_expected(dti, box_with_datetime) - parr = tm.box_expected(pi, box_with_period) - - with pytest.raises(TypeError): - dtarr + parr - with pytest.raises(TypeError): - parr + dtarr - with pytest.raises(TypeError): - dtarr - parr - with pytest.raises(TypeError): - parr - dtarr - # ------------------------------------------------------------------- # TODO: Most of this block is moved from series or frame tests, needs # cleanup, box-parametrization, and de-duplication From 310616253b18888c9f4f637b13d2feaf5d844fa3 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 12:40:32 -0800 Subject: [PATCH 06/16] keep parametrizing --- pandas/tests/arithmetic/test_datetime64.py | 96 ++++++++++++---------- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index cfe54b47b3ecc..bf3175a3a56f9 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -652,6 +652,30 @@ class TestDatetime64Arithmetic(object): # ------------------------------------------------------------- # Addition/Subtraction of timedelta-like + def test_dt64arr_add_sub_td64_nat(self, box_with_datetime, + tz_naive_fixture): + # GH#23320 special handling for timedelta64("NaT") + tz = tz_naive_fixture + box = box_with_datetime + + dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS") + other = np.timedelta64("NaT") + expected = pd.DatetimeIndex(["NaT"] * 9, tz=tz) + + # FIXME: fails with transpose=True due to tz-aware DataFrame + # transpose bug + obj = tm.box_expected(dti, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) + + result = obj + other + tm.assert_equal(result, expected) + result = other + obj + tm.assert_equal(result, expected) + result = obj - other + tm.assert_equal(result, expected) + with pytest.raises(TypeError): + other - obj + # ----------------------------------------------------------------- # Subtraction of datetime-like scalars @@ -1284,28 +1308,6 @@ def test_dti_isub_timedeltalike(self, tz_naive_fixture, two_hours): rng -= two_hours tm.assert_index_equal(rng, expected) - def test_dt64arr_add_sub_td64_nat(self, box, tz_naive_fixture): - # GH#23320 special handling for timedelta64("NaT") - tz = tz_naive_fixture - - dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS") - other = np.timedelta64("NaT") - expected = pd.DatetimeIndex(["NaT"] * 9, tz=tz) - - # FIXME: fails with transpose=True due to tz-aware DataFrame - # transpose bug - obj = tm.box_expected(dti, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) - - result = obj + other - tm.assert_equal(result, expected) - result = other + obj - tm.assert_equal(result, expected) - result = obj - other - tm.assert_equal(result, expected) - with pytest.raises(TypeError): - other - obj - # ------------------------------------------------------------- # Binary operations DatetimeIndex and TimedeltaIndex/array @@ -1430,41 +1432,42 @@ def test_add_datetimelike_and_dti(self, addend, tz): # ------------------------------------------------------------- # __add__/__sub__ with ndarray[datetime64] and ndarray[timedelta64] - def test_dti_add_dt64_array_raises(self, tz_naive_fixture, - box_with_datetime): + def test_dt64arr_add_dt64ndarray_raises(self, tz_naive_fixture, + box_with_datetime): if box_with_datetime is pd.DataFrame: pytest.xfail("FIXME: ValueError with transpose; " "alignment error without") tz = tz_naive_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) - dtarr = dti.values + dt64vals = dti.values - dti = tm.box_expected(dti, box_with_datetime) + dtarr = tm.box_expected(dti, box_with_datetime) with pytest.raises(TypeError): - dti + dtarr + dtarr + dt64vals with pytest.raises(TypeError): - dtarr + dti + dt64vals + dtarr - def test_dti_sub_dt64_array_aware_raises(self, tz_aware_fixture, - box_with_datetime): + def test_dt64arr_aware_sub_dt64ndarray_raises(self, tz_aware_fixture, + box_with_datetime): if box_with_datetime is pd.DataFrame: pytest.xfail("FIXME: ValueError with transpose; " "alignment error without") tz = tz_aware_fixture dti = pd.date_range('2016-01-01', periods=3, tz=tz) - dtarr = dti.values + dt64vals = dti.values - dti = tm.box_expected(dti, box_with_datetime) + dtarr = tm.box_expected(dti, box_with_datetime) with pytest.raises(TypeError): - dti - dtarr + dtarr - dt64vals with pytest.raises(TypeError): - dtarr - dti + dt64vals - dtarr - def test_dti_add_td64_array(self, tz_naive_fixture, box_with_datetime): + def test_dt64arr_add_td64ndarray(self, tz_naive_fixture, + box_with_datetime): if box_with_datetime is pd.DataFrame: pytest.xfail("FIXME: ValueError with transpose; " "alignment error without") @@ -1474,15 +1477,18 @@ def test_dti_add_td64_array(self, tz_naive_fixture, box_with_datetime): tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day']) tdarr = tdi.values - dti = tm.box_expected(dti, box_with_datetime) + expected = pd.date_range('2015-12-31', periods=3, tz=tz) + + dtarr = tm.box_expected(dti, box_with_datetime) + expected = tm.box_expected(expected, box_with_datetime) - expected = dti + tdi - result = dti + tdarr + result = dtarr + tdarr tm.assert_equal(result, expected) - result = tdarr + dti + result = tdarr + dtarr tm.assert_equal(result, expected) - def test_dti_sub_td64_array(self, tz_naive_fixture, box_with_datetime): + def test_dt64arr_sub_td64ndarray(self, tz_naive_fixture, + box_with_datetime): if box_with_datetime is pd.DataFrame: pytest.xfail("FIXME: ValueError with transpose; " "alignment error without") @@ -1492,14 +1498,16 @@ def test_dti_sub_td64_array(self, tz_naive_fixture, box_with_datetime): tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day']) tdarr = tdi.values - dti = tm.box_expected(dti, box_with_datetime) + expected = pd.date_range('2016-01-02', periods=3, tz=tz) + + dtarr = tm.box_expected(dti, box_with_datetime) + expected = tm.box_expected(expected, box_with_datetime) - expected = dti - tdi - result = dti - tdarr + result = dtarr - tdarr tm.assert_equal(result, expected) with pytest.raises(TypeError): - tdarr - dti + tdarr - dtarr # ------------------------------------------------------------- From 16ecc717ddfe5b3993e7979b67c04deff97c2439 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 13:39:44 -0800 Subject: [PATCH 07/16] parametrize more tests over box --- pandas/tests/arithmetic/test_datetime64.py | 228 +++++++++++---------- 1 file changed, 118 insertions(+), 110 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index bf3175a3a56f9..9ab701084cb99 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -676,6 +676,36 @@ def test_dt64arr_add_sub_td64_nat(self, box_with_datetime, with pytest.raises(TypeError): other - obj + def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, + box_with_datetime): + if box_with_datetime is pd.DataFrame: + pytest.xfail("FIXME: ValueError with transpose; " + "alignment error without") + + tz = tz_naive_fixture + dti = pd.date_range('2016-01-01', periods=3, tz=tz) + tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day']) + tdarr = tdi.values + + expected = pd.date_range('2015-12-31', periods=3, tz=tz) + + dtarr = tm.box_expected(dti, box_with_datetime) + expected = tm.box_expected(expected, box_with_datetime) + + result = dtarr + tdarr + tm.assert_equal(result, expected) + result = tdarr + dtarr + tm.assert_equal(result, expected) + + expected = pd.date_range('2016-01-02', periods=3, tz=tz) + expected = tm.box_expected(expected, box_with_datetime) + + result = dtarr - tdarr + tm.assert_equal(result, expected) + + with pytest.raises(TypeError): + tdarr - dtarr + # ----------------------------------------------------------------- # Subtraction of datetime-like scalars @@ -727,10 +757,29 @@ def test_dt64arr_sub_timestamp(self, box): tm.assert_equal(ser - ts, expected) tm.assert_equal(ts - ser, -expected) + def test_dt64arr_sub_NaT(self, box): + # GH#18808 + dti = pd.DatetimeIndex([pd.NaT, pd.Timestamp('19900315')]) + ser = tm.box_expected(dti, box, transpose=False) + + result = ser - pd.NaT + expected = pd.Series([pd.NaT, pd.NaT], dtype='timedelta64[ns]') + # FIXME: raises ValueError with transpose + expected = tm.box_expected(expected, box, transpose=False) + tm.assert_equal(result, expected) + + dti_tz = dti.tz_localize('Asia/Tokyo') + ser_tz = tm.box_expected(dti_tz, box, transpose=False) + + result = ser_tz - pd.NaT + expected = pd.Series([pd.NaT, pd.NaT], dtype='timedelta64[ns]') + expected = tm.box_expected(expected, box, transpose=False) + tm.assert_equal(result, expected) + # ------------------------------------------------------------- # Subtraction of datetime-like array-like - def test_dt64arr_sub_dt64ndarray_naive(self, box_with_datetime): + def test_dt64arr_naive_sub_dt64ndarray(self, box_with_datetime): dti = pd.date_range('2016-01-01', periods=3, tz=None) dt64vals = dti.values @@ -742,9 +791,43 @@ def test_dt64arr_sub_dt64ndarray_naive(self, box_with_datetime): result = dt64vals - dtarr tm.assert_equal(result, expected) + def test_dt64arr_aware_sub_dt64ndarray_raises(self, tz_aware_fixture, + box_with_datetime): + if box_with_datetime is pd.DataFrame: + pytest.xfail("FIXME: ValueError with transpose; " + "alignment error without") + + tz = tz_aware_fixture + dti = pd.date_range('2016-01-01', periods=3, tz=tz) + dt64vals = dti.values + + dtarr = tm.box_expected(dti, box_with_datetime) + + with pytest.raises(TypeError): + dtarr - dt64vals + with pytest.raises(TypeError): + dt64vals - dtarr + # ------------------------------------------------------------- # Addition of datetime-like others (invalid) + def test_dt64arr_add_dt64ndarray_raises(self, tz_naive_fixture, + box_with_datetime): + if box_with_datetime is pd.DataFrame: + pytest.xfail("FIXME: ValueError with transpose; " + "alignment error without") + + tz = tz_naive_fixture + dti = pd.date_range('2016-01-01', periods=3, tz=tz) + dt64vals = dti.values + + dtarr = tm.box_expected(dti, box_with_datetime) + + with pytest.raises(TypeError): + dtarr + dt64vals + with pytest.raises(TypeError): + dt64vals + dtarr + def test_dt64arr_add_timestamp_raises(self, box_with_datetime): # GH#22163 ensure DataFrame doesn't cast Timestamp to i8 idx = DatetimeIndex(['2011-01-01', '2011-01-02']) @@ -975,25 +1058,6 @@ def test_dt64_series_add_mixed_tick_DateOffset(self): Timestamp('20130101 9:07:00.005')]) tm.assert_series_equal(result, expected) - def test_dt64arr_sub_NaT(self, box): - # GH#18808 - dti = pd.DatetimeIndex([pd.NaT, pd.Timestamp('19900315')]) - ser = tm.box_expected(dti, box, transpose=False) - - result = ser - pd.NaT - expected = pd.Series([pd.NaT, pd.NaT], dtype='timedelta64[ns]') - # FIXME: raises ValueError with ranspose - expected = tm.box_expected(expected, box, transpose=False) - tm.assert_equal(result, expected) - - dti_tz = dti.tz_localize('Asia/Tokyo') - ser_tz = tm.box_expected(dti_tz, box, transpose=False) - - result = ser_tz - pd.NaT - expected = pd.Series([pd.NaT, pd.NaT], dtype='timedelta64[ns]') - expected = tm.box_expected(expected, box, transpose=False) - tm.assert_equal(result, expected) - def test_dt64_series_arith_overflow(self): # GH#12534, fixed by GH#19024 dt = pd.Timestamp('1700-01-31') @@ -1271,42 +1335,66 @@ def test_dti_add_timedeltalike(self, tz_naive_fixture, two_hours, box_with_datetime): # GH#22005, GH#22163 check DataFrame doesn't raise TypeError box = box_with_datetime - tz = tz_naive_fixture + rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) + expected = pd.date_range('2000-01-01 02:00', + '2000-02-01 02:00', tz=tz) # FIXME: calling with transpose=True raises ValueError rng = tm.box_expected(rng, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) result = rng + two_hours - expected = pd.date_range('2000-01-01 02:00', - '2000-02-01 02:00', tz=tz) - expected = tm.box_expected(expected, box, transpose=False) tm.assert_equal(result, expected) - def test_dti_iadd_timedeltalike(self, tz_naive_fixture, two_hours): + def test_dti_iadd_timedeltalike(self, tz_naive_fixture, two_hours, + box_with_datetime): + box = box_with_datetime tz = tz_naive_fixture + rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) expected = pd.date_range('2000-01-01 02:00', '2000-02-01 02:00', tz=tz) + + # FIXME: calling with transpose=True raises ValueError + rng = tm.box_expected(rng, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) + rng += two_hours - tm.assert_index_equal(rng, expected) + tm.assert_equal(rng, expected) - def test_dti_sub_timedeltalike(self, tz_naive_fixture, two_hours): + def test_dti_sub_timedeltalike(self, tz_naive_fixture, two_hours, + box_with_datetime): + box = box_with_datetime tz = tz_naive_fixture + rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) expected = pd.date_range('1999-12-31 22:00', '2000-01-31 22:00', tz=tz) + + # FIXME: calling with transpose=True raises ValueError + rng = tm.box_expected(rng, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) + result = rng - two_hours - tm.assert_index_equal(result, expected) + tm.assert_equal(result, expected) - def test_dti_isub_timedeltalike(self, tz_naive_fixture, two_hours): + def test_dti_isub_timedeltalike(self, tz_naive_fixture, two_hours, + box_with_datetime): + box = box_with_datetime tz = tz_naive_fixture + rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) expected = pd.date_range('1999-12-31 22:00', '2000-01-31 22:00', tz=tz) + + # FIXME: calling with transpose=True raises ValueError + rng = tm.box_expected(rng, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) + rng -= two_hours - tm.assert_index_equal(rng, expected) + tm.assert_equal(rng, expected) # ------------------------------------------------------------- # Binary operations DatetimeIndex and TimedeltaIndex/array @@ -1429,86 +1517,6 @@ def test_add_datetimelike_and_dti(self, addend, tz): with pytest.raises(TypeError, match=msg): addend + dti - # ------------------------------------------------------------- - # __add__/__sub__ with ndarray[datetime64] and ndarray[timedelta64] - - def test_dt64arr_add_dt64ndarray_raises(self, tz_naive_fixture, - box_with_datetime): - if box_with_datetime is pd.DataFrame: - pytest.xfail("FIXME: ValueError with transpose; " - "alignment error without") - - tz = tz_naive_fixture - dti = pd.date_range('2016-01-01', periods=3, tz=tz) - dt64vals = dti.values - - dtarr = tm.box_expected(dti, box_with_datetime) - - with pytest.raises(TypeError): - dtarr + dt64vals - with pytest.raises(TypeError): - dt64vals + dtarr - - def test_dt64arr_aware_sub_dt64ndarray_raises(self, tz_aware_fixture, - box_with_datetime): - if box_with_datetime is pd.DataFrame: - pytest.xfail("FIXME: ValueError with transpose; " - "alignment error without") - - tz = tz_aware_fixture - dti = pd.date_range('2016-01-01', periods=3, tz=tz) - dt64vals = dti.values - - dtarr = tm.box_expected(dti, box_with_datetime) - - with pytest.raises(TypeError): - dtarr - dt64vals - with pytest.raises(TypeError): - dt64vals - dtarr - - def test_dt64arr_add_td64ndarray(self, tz_naive_fixture, - box_with_datetime): - if box_with_datetime is pd.DataFrame: - pytest.xfail("FIXME: ValueError with transpose; " - "alignment error without") - - tz = tz_naive_fixture - dti = pd.date_range('2016-01-01', periods=3, tz=tz) - tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day']) - tdarr = tdi.values - - expected = pd.date_range('2015-12-31', periods=3, tz=tz) - - dtarr = tm.box_expected(dti, box_with_datetime) - expected = tm.box_expected(expected, box_with_datetime) - - result = dtarr + tdarr - tm.assert_equal(result, expected) - result = tdarr + dtarr - tm.assert_equal(result, expected) - - def test_dt64arr_sub_td64ndarray(self, tz_naive_fixture, - box_with_datetime): - if box_with_datetime is pd.DataFrame: - pytest.xfail("FIXME: ValueError with transpose; " - "alignment error without") - - tz = tz_naive_fixture - dti = pd.date_range('2016-01-01', periods=3, tz=tz) - tdi = pd.TimedeltaIndex(['-1 Day', '-1 Day', '-1 Day']) - tdarr = tdi.values - - expected = pd.date_range('2016-01-02', periods=3, tz=tz) - - dtarr = tm.box_expected(dti, box_with_datetime) - expected = tm.box_expected(expected, box_with_datetime) - - result = dtarr - tdarr - tm.assert_equal(result, expected) - - with pytest.raises(TypeError): - tdarr - dtarr - # ------------------------------------------------------------- def test_sub_dti_dti(self): From 09e10a804973508600b31535d5dc30a73d6f281b Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 13:49:13 -0800 Subject: [PATCH 08/16] collect/parametrize tests more --- pandas/tests/arithmetic/test_datetime64.py | 177 +++++++++++---------- 1 file changed, 90 insertions(+), 87 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 9ab701084cb99..199987ec8f64d 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -652,6 +652,96 @@ class TestDatetime64Arithmetic(object): # ------------------------------------------------------------- # Addition/Subtraction of timedelta-like + def test_dt64arr_add_timedeltalike_scalar(self, tz_naive_fixture, + two_hours, box_with_datetime): + # GH#22005, GH#22163 check DataFrame doesn't raise TypeError + box = box_with_datetime + tz = tz_naive_fixture + + rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) + expected = pd.date_range('2000-01-01 02:00', + '2000-02-01 02:00', tz=tz) + + # FIXME: calling with transpose=True raises ValueError + rng = tm.box_expected(rng, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) + + result = rng + two_hours + tm.assert_equal(result, expected) + + def test_dt64arr_iadd_timedeltalike_scalar(self, tz_naive_fixture, + two_hours, box_with_datetime): + box = box_with_datetime + tz = tz_naive_fixture + + rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) + expected = pd.date_range('2000-01-01 02:00', + '2000-02-01 02:00', tz=tz) + + # FIXME: calling with transpose=True raises ValueError + rng = tm.box_expected(rng, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) + + rng += two_hours + tm.assert_equal(rng, expected) + + def test_dt64arr_sub_timedeltalike_scalar(self, tz_naive_fixture, + two_hours, box_with_datetime): + box = box_with_datetime + tz = tz_naive_fixture + + rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) + expected = pd.date_range('1999-12-31 22:00', + '2000-01-31 22:00', tz=tz) + + # FIXME: calling with transpose=True raises ValueError + rng = tm.box_expected(rng, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) + + result = rng - two_hours + tm.assert_equal(result, expected) + + def test_dt64arr_isub_timedeltalike_scalar(self, tz_naive_fixture, + two_hours, box_with_datetime): + box = box_with_datetime + tz = tz_naive_fixture + + rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) + expected = pd.date_range('1999-12-31 22:00', + '2000-01-31 22:00', tz=tz) + + # FIXME: calling with transpose=True raises ValueError + rng = tm.box_expected(rng, box, transpose=False) + expected = tm.box_expected(expected, box, transpose=False) + + rng -= two_hours + tm.assert_equal(rng, expected) + + def test_dt64arr_add_td64_scalar(self, box_with_datetime): + # scalar timedeltas/np.timedelta64 objects + # operate with np.timedelta64 correctly + ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) + + expected = Series([Timestamp('20130101 9:01:01'), + Timestamp('20130101 9:02:01')]) + + dtarr = tm.box_expected(ser, box_with_datetime) + expected = tm.box_expected(expected, box_with_datetime) + + result = dtarr + np.timedelta64(1, 's') + tm.assert_equal(result, expected) + result = np.timedelta64(1, 's') + dtarr + tm.assert_equal(result, expected) + + expected = Series([Timestamp('20130101 9:01:00.005'), + Timestamp('20130101 9:02:00.005')]) + expected = tm.box_expected(expected, box_with_datetime) + + result = dtarr + np.timedelta64(5, 'ms') + tm.assert_equal(result, expected) + result2 = np.timedelta64(5, 'ms') + dtarr + tm.assert_equal(result, expected) + def test_dt64arr_add_sub_td64_nat(self, box_with_datetime, tz_naive_fixture): # GH#23320 special handling for timedelta64("NaT") @@ -982,25 +1072,6 @@ def test_sub_datetime_compat(self): tm.assert_series_equal(s - dt, exp) tm.assert_series_equal(s - Timestamp(dt), exp) - def test_dt64_series_addsub_timedelta(self): - # scalar timedeltas/np.timedelta64 objects - # operate with np.timedelta64 correctly - s = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) - - result = s + np.timedelta64(1, 's') - result2 = np.timedelta64(1, 's') + s - expected = Series([Timestamp('20130101 9:01:01'), - Timestamp('20130101 9:02:01')]) - tm.assert_series_equal(result, expected) - tm.assert_series_equal(result2, expected) - - result = s + np.timedelta64(5, 'ms') - result2 = np.timedelta64(5, 'ms') + s - expected = Series([Timestamp('20130101 9:01:00.005'), - Timestamp('20130101 9:02:00.005')]) - tm.assert_series_equal(result, expected) - tm.assert_series_equal(result2, expected) - def test_dt64_series_add_tick_DateOffset(self): # GH#4532 # operate with pd.offsets @@ -1328,74 +1399,6 @@ def test_dti_add_intarray_no_freq(self, box): with pytest.raises(TypeError): other - dti - # ------------------------------------------------------------- - # Binary operations DatetimeIndex and timedelta-like - - def test_dti_add_timedeltalike(self, tz_naive_fixture, two_hours, - box_with_datetime): - # GH#22005, GH#22163 check DataFrame doesn't raise TypeError - box = box_with_datetime - tz = tz_naive_fixture - - rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) - expected = pd.date_range('2000-01-01 02:00', - '2000-02-01 02:00', tz=tz) - - # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) - - result = rng + two_hours - tm.assert_equal(result, expected) - - def test_dti_iadd_timedeltalike(self, tz_naive_fixture, two_hours, - box_with_datetime): - box = box_with_datetime - tz = tz_naive_fixture - - rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) - expected = pd.date_range('2000-01-01 02:00', - '2000-02-01 02:00', tz=tz) - - # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) - - rng += two_hours - tm.assert_equal(rng, expected) - - def test_dti_sub_timedeltalike(self, tz_naive_fixture, two_hours, - box_with_datetime): - box = box_with_datetime - tz = tz_naive_fixture - - rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) - expected = pd.date_range('1999-12-31 22:00', - '2000-01-31 22:00', tz=tz) - - # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) - - result = rng - two_hours - tm.assert_equal(result, expected) - - def test_dti_isub_timedeltalike(self, tz_naive_fixture, two_hours, - box_with_datetime): - box = box_with_datetime - tz = tz_naive_fixture - - rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) - expected = pd.date_range('1999-12-31 22:00', - '2000-01-31 22:00', tz=tz) - - # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) - - rng -= two_hours - tm.assert_equal(rng, expected) - # ------------------------------------------------------------- # Binary operations DatetimeIndex and TimedeltaIndex/array From 4464755088a77183bd13844983008a3c771b9bda Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 13:56:08 -0800 Subject: [PATCH 09/16] box more tests --- pandas/tests/arithmetic/test_period.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 7cbc20ec2ed2c..32eb64e225d96 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -49,7 +49,6 @@ def test_parr_cmp_period_scalar2(self, box): expected = tm.box_expected(expected, xbox) tm.assert_equal(result, expected) - @pytest.mark.parametrize('freq', ['M', '2M', '3M']) def test_parr_cmp_period_scalar(self, freq, box): # GH#13200 @@ -320,12 +319,12 @@ class TestPeriodIndexArithmetic(object): # PeriodIndex - other is defined for integers, timedelta-like others, # and PeriodIndex (with matching freq) - def test_parr_add_iadd_parr_raises(self, box): + def test_parr_add_iadd_parr_raises(self, box_with_period): rng = pd.period_range('1/1/2000', freq='D', periods=5) other = pd.period_range('1/6/2000', freq='D', periods=5) # TODO: parametrize over boxes for other? - rng = tm.box_expected(rng, box) + rng = tm.box_expected(rng, box_with_period) # An earlier implementation of PeriodIndex addition performed # a set operation (union). This has since been changed to # raise a TypeError. See GH#14164 and GH#13077 for historical @@ -362,12 +361,12 @@ def test_pi_sub_pi_with_nat(self): expected = pd.Index([pd.NaT, 0 * off, 0 * off, 0 * off, 0 * off]) tm.assert_index_equal(result, expected) - def test_parr_sub_pi_mismatched_freq(self, box): + def test_parr_sub_pi_mismatched_freq(self, box_with_period): rng = pd.period_range('1/1/2000', freq='D', periods=5) other = pd.period_range('1/6/2000', freq='H', periods=5) # TODO: parametrize over boxes for other? - rng = tm.box_expected(rng, box) + rng = tm.box_expected(rng, box_with_period) with pytest.raises(IncompatibleFrequency): rng - other @@ -377,19 +376,20 @@ def test_parr_sub_pi_mismatched_freq(self, box): @pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])]) @pytest.mark.parametrize('op', [operator.add, ops.radd, operator.sub, ops.rsub]) - def test_pi_add_sub_float(self, op, other, box): + def test_parr_add_sub_float_raises(self, op, other, box_with_period): dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') pi = dti.to_period('D') - pi = tm.box_expected(pi, box) + pi = tm.box_expected(pi, box_with_period) with pytest.raises(TypeError): op(pi, other) @pytest.mark.parametrize('other', [pd.Timestamp.now(), pd.Timestamp.now().to_pydatetime(), pd.Timestamp.now().to_datetime64()]) - def test_pi_add_sub_datetime(self, other): + def test_parr_add_sub_datetime_scalar(self, other, box_with_period): # GH#23215 rng = pd.period_range('1/1/2000', freq='D', periods=3) + rng = tm.box_expected(rng, box_with_period) with pytest.raises(TypeError): rng + other @@ -403,11 +403,13 @@ def test_pi_add_sub_datetime(self, other): # ----------------------------------------------------------------- # __add__/__sub__ with ndarray[datetime64] and ndarray[timedelta64] - def test_pi_add_sub_dt64_array_raises(self): + def test_parr_add_sub_dt64_array_raises(self, box_with_period): rng = pd.period_range('1/1/2000', freq='D', periods=3) dti = pd.date_range('2016-01-01', periods=3) dtarr = dti.values + rng = tm.box_expected(rng, box_with_period) + with pytest.raises(TypeError): rng + dtarr with pytest.raises(TypeError): @@ -840,7 +842,7 @@ def test_parr_add_sub_td64_nat(self, box_transpose_fail): class TestPeriodSeriesArithmetic(object): def test_ops_series_timedelta(self): - # GH 13043 + # GH#13043 ser = pd.Series([pd.Period('2015-01-01', freq='D'), pd.Period('2015-01-02', freq='D')], name='xxx') assert ser.dtype == 'Period[D]' @@ -861,7 +863,7 @@ def test_ops_series_timedelta(self): tm.assert_series_equal(result, expected) def test_ops_series_period(self): - # GH 13043 + # GH#13043 ser = pd.Series([pd.Period('2015-01-01', freq='D'), pd.Period('2015-01-02', freq='D')], name='xxx') assert ser.dtype == "Period[D]" @@ -919,7 +921,7 @@ def test_pi_ops(self): tm.assert_index_equal(result, exp) @pytest.mark.parametrize('ng', ["str", 1.5]) - def test_pi_ops_errors(self, ng, box_with_period): + def test_parr_ops_errors(self, ng, box_with_period): idx = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], freq='M', name='idx') obj = tm.box_expected(idx, box_with_period) From 2fa9679fcda9e55bf0338e26b1bcc9191c229437 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 13:59:55 -0800 Subject: [PATCH 10/16] GH references --- pandas/tests/arithmetic/test_datetime64.py | 2 +- pandas/tests/arithmetic/test_period.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 199987ec8f64d..c78b0a9b2acaa 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1065,7 +1065,7 @@ def test_dt64tz_series_sub_dtitz(self): tm.assert_series_equal(res, expected) def test_sub_datetime_compat(self): - # see gh-14088 + # see GH#14088 s = Series([datetime(2016, 8, 23, 12, tzinfo=pytz.utc), pd.NaT]) dt = datetime(2016, 8, 22, 12, tzinfo=pytz.utc) exp = Series([Timedelta('1 days'), pd.NaT]) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 32eb64e225d96..2ae4940211fd4 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -1063,7 +1063,7 @@ def test_pi_sub_period(self): tm.assert_index_equal(pd.Period('NaT', freq='M') - idx, exp) def test_pi_sub_pdnat(self): - # GH 13071 + # GH#13071 idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'], freq='M', name='idx') exp = pd.TimedeltaIndex([pd.NaT] * 4, name='idx') @@ -1071,7 +1071,7 @@ def test_pi_sub_pdnat(self): tm.assert_index_equal(idx - pd.NaT, exp) def test_pi_sub_period_nat(self): - # GH 13071 + # GH#13071 idx = PeriodIndex(['2011-01', 'NaT', '2011-03', '2011-04'], freq='M', name='idx') From 19b08e13dc01387bcc3062ed02a7f129c7d745ff Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 14:48:50 -0800 Subject: [PATCH 11/16] param over box --- pandas/tests/arithmetic/test_datetime64.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index c78b0a9b2acaa..6fbc7ab1446f2 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1072,31 +1072,37 @@ def test_sub_datetime_compat(self): tm.assert_series_equal(s - dt, exp) tm.assert_series_equal(s - Timestamp(dt), exp) - def test_dt64_series_add_tick_DateOffset(self): + def test_dt64_series_add_tick_DateOffset(self, box_with_datetime): # GH#4532 # operate with pd.offsets ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) expected = Series([Timestamp('20130101 9:01:05'), Timestamp('20130101 9:02:05')]) + ser = tm.box_expected(ser, box_with_datetime) + expected = tm.box_expected(expected, box_with_datetime) + result = ser + pd.offsets.Second(5) - tm.assert_series_equal(result, expected) + tm.assert_equal(result, expected) result2 = pd.offsets.Second(5) + ser - tm.assert_series_equal(result2, expected) + tm.assert_equal(result2, expected) - def test_dt64_series_sub_tick_DateOffset(self): + def test_dt64_series_sub_tick_DateOffset(self, box_with_datetime): # GH#4532 # operate with pd.offsets ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) expected = Series([Timestamp('20130101 9:00:55'), Timestamp('20130101 9:01:55')]) + ser = tm.box_expected(ser, box_with_datetime) + expected = tm.box_expected(expected, box_with_datetime) + result = ser - pd.offsets.Second(5) - tm.assert_series_equal(result, expected) + tm.assert_equal(result, expected) result2 = -pd.offsets.Second(5) + ser - tm.assert_series_equal(result2, expected) + tm.assert_equal(result2, expected) with pytest.raises(TypeError): pd.offsets.Second(5) - ser From 6973bf4b2341aa3b613db73875f9062b9f796209 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 15:07:22 -0800 Subject: [PATCH 12/16] use box_with_array --- pandas/tests/arithmetic/conftest.py | 31 +------ pandas/tests/arithmetic/test_datetime64.py | 96 +++++++++++----------- pandas/tests/arithmetic/test_period.py | 28 +++---- 3 files changed, 66 insertions(+), 89 deletions(-) diff --git a/pandas/tests/arithmetic/conftest.py b/pandas/tests/arithmetic/conftest.py index e5d9c85691b7a..5777e042cf709 100644 --- a/pandas/tests/arithmetic/conftest.py +++ b/pandas/tests/arithmetic/conftest.py @@ -189,33 +189,6 @@ def box_transpose_fail(request): return request.param -@pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, PeriodArray], - ids=id_func) -def box_with_period(request): - """ - Like `box`, but specific to PeriodDtype for also testing PeriodArray - """ - return request.param - - -@pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, DatetimeArray], - ids=id_func) -def box_with_datetime(request): - """ - Like `box`, but specific to datetime64 for also testing DatetimeArray - """ - return request.param - - -@pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, TimedeltaArray], - ids=id_func) -def box_with_timedelta(request): - """ - Like `box`, but specific to timedelta64 for also testing TimedeltaArray - """ - return request.param - - @pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, tm.to_array], ids=id_func) def box_with_array(request): @@ -224,3 +197,7 @@ def box_with_array(request): classes """ return request.param + + +# alias so we can use the same fixture for multiple parameters in a test +box_with_array2 = box_with_array diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 6fbc7ab1446f2..642d53a50e86a 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -168,11 +168,11 @@ def test_dt64_ser_cmp_date_warning(self): assert "a TypeError will be raised" in str(m[0].message) @pytest.mark.skip(reason="GH#21359") - def test_dt64ser_cmp_date_invalid(self, box_with_datetime): + def test_dt64ser_cmp_date_invalid(self, box_with_array): # GH#19800 datetime.date comparison raises to # match DatetimeIndex/Timestamp. This also matches the behavior # of stdlib datetime.datetime - box = box_with_datetime + box = box_with_array ser = pd.date_range('20010101', periods=10) date = ser.iloc[0].to_pydatetime().date() @@ -227,9 +227,9 @@ def test_timestamp_compare_series(self, left, right): result = right_f(pd.Timestamp("nat"), s_nat) tm.assert_series_equal(result, expected) - def test_dt64arr_timestamp_equality(self, box_with_datetime): + def test_dt64arr_timestamp_equality(self, box_with_array): # GH#11034 - box = box_with_datetime + box = box_with_array xbox = box if box not in [pd.Index, DatetimeArray] else np.ndarray ser = pd.Series([pd.Timestamp('2000-01-29 01:59:00'), 'NaT']) @@ -653,9 +653,9 @@ class TestDatetime64Arithmetic(object): # Addition/Subtraction of timedelta-like def test_dt64arr_add_timedeltalike_scalar(self, tz_naive_fixture, - two_hours, box_with_datetime): + two_hours, box_with_array): # GH#22005, GH#22163 check DataFrame doesn't raise TypeError - box = box_with_datetime + box = box_with_array tz = tz_naive_fixture rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) @@ -670,8 +670,8 @@ def test_dt64arr_add_timedeltalike_scalar(self, tz_naive_fixture, tm.assert_equal(result, expected) def test_dt64arr_iadd_timedeltalike_scalar(self, tz_naive_fixture, - two_hours, box_with_datetime): - box = box_with_datetime + two_hours, box_with_array): + box = box_with_array tz = tz_naive_fixture rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) @@ -686,8 +686,8 @@ def test_dt64arr_iadd_timedeltalike_scalar(self, tz_naive_fixture, tm.assert_equal(rng, expected) def test_dt64arr_sub_timedeltalike_scalar(self, tz_naive_fixture, - two_hours, box_with_datetime): - box = box_with_datetime + two_hours, box_with_array): + box = box_with_array tz = tz_naive_fixture rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) @@ -702,8 +702,8 @@ def test_dt64arr_sub_timedeltalike_scalar(self, tz_naive_fixture, tm.assert_equal(result, expected) def test_dt64arr_isub_timedeltalike_scalar(self, tz_naive_fixture, - two_hours, box_with_datetime): - box = box_with_datetime + two_hours, box_with_array): + box = box_with_array tz = tz_naive_fixture rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) @@ -717,7 +717,7 @@ def test_dt64arr_isub_timedeltalike_scalar(self, tz_naive_fixture, rng -= two_hours tm.assert_equal(rng, expected) - def test_dt64arr_add_td64_scalar(self, box_with_datetime): + def test_dt64arr_add_td64_scalar(self, box_with_array): # scalar timedeltas/np.timedelta64 objects # operate with np.timedelta64 correctly ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) @@ -725,8 +725,8 @@ def test_dt64arr_add_td64_scalar(self, box_with_datetime): expected = Series([Timestamp('20130101 9:01:01'), Timestamp('20130101 9:02:01')]) - dtarr = tm.box_expected(ser, box_with_datetime) - expected = tm.box_expected(expected, box_with_datetime) + dtarr = tm.box_expected(ser, box_with_array) + expected = tm.box_expected(expected, box_with_array) result = dtarr + np.timedelta64(1, 's') tm.assert_equal(result, expected) @@ -735,18 +735,18 @@ def test_dt64arr_add_td64_scalar(self, box_with_datetime): expected = Series([Timestamp('20130101 9:01:00.005'), Timestamp('20130101 9:02:00.005')]) - expected = tm.box_expected(expected, box_with_datetime) + expected = tm.box_expected(expected, box_with_array) result = dtarr + np.timedelta64(5, 'ms') tm.assert_equal(result, expected) result2 = np.timedelta64(5, 'ms') + dtarr tm.assert_equal(result, expected) - def test_dt64arr_add_sub_td64_nat(self, box_with_datetime, + def test_dt64arr_add_sub_td64_nat(self, box_with_array, tz_naive_fixture): # GH#23320 special handling for timedelta64("NaT") tz = tz_naive_fixture - box = box_with_datetime + box = box_with_array dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS") other = np.timedelta64("NaT") @@ -767,8 +767,8 @@ def test_dt64arr_add_sub_td64_nat(self, box_with_datetime, other - obj def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, - box_with_datetime): - if box_with_datetime is pd.DataFrame: + box_with_array): + if box_with_array is pd.DataFrame: pytest.xfail("FIXME: ValueError with transpose; " "alignment error without") @@ -779,8 +779,8 @@ def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, expected = pd.date_range('2015-12-31', periods=3, tz=tz) - dtarr = tm.box_expected(dti, box_with_datetime) - expected = tm.box_expected(expected, box_with_datetime) + dtarr = tm.box_expected(dti, box_with_array) + expected = tm.box_expected(expected, box_with_array) result = dtarr + tdarr tm.assert_equal(result, expected) @@ -788,7 +788,7 @@ def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, tm.assert_equal(result, expected) expected = pd.date_range('2016-01-02', periods=3, tz=tz) - expected = tm.box_expected(expected, box_with_datetime) + expected = tm.box_expected(expected, box_with_array) result = dtarr - tdarr tm.assert_equal(result, expected) @@ -869,11 +869,11 @@ def test_dt64arr_sub_NaT(self, box): # ------------------------------------------------------------- # Subtraction of datetime-like array-like - def test_dt64arr_naive_sub_dt64ndarray(self, box_with_datetime): + def test_dt64arr_naive_sub_dt64ndarray(self, box_with_array): dti = pd.date_range('2016-01-01', periods=3, tz=None) dt64vals = dti.values - dtarr = tm.box_expected(dti, box_with_datetime) + dtarr = tm.box_expected(dti, box_with_array) expected = dtarr - dtarr result = dtarr - dt64vals @@ -882,8 +882,8 @@ def test_dt64arr_naive_sub_dt64ndarray(self, box_with_datetime): tm.assert_equal(result, expected) def test_dt64arr_aware_sub_dt64ndarray_raises(self, tz_aware_fixture, - box_with_datetime): - if box_with_datetime is pd.DataFrame: + box_with_array): + if box_with_array is pd.DataFrame: pytest.xfail("FIXME: ValueError with transpose; " "alignment error without") @@ -891,7 +891,7 @@ def test_dt64arr_aware_sub_dt64ndarray_raises(self, tz_aware_fixture, dti = pd.date_range('2016-01-01', periods=3, tz=tz) dt64vals = dti.values - dtarr = tm.box_expected(dti, box_with_datetime) + dtarr = tm.box_expected(dti, box_with_array) with pytest.raises(TypeError): dtarr - dt64vals @@ -902,8 +902,8 @@ def test_dt64arr_aware_sub_dt64ndarray_raises(self, tz_aware_fixture, # Addition of datetime-like others (invalid) def test_dt64arr_add_dt64ndarray_raises(self, tz_naive_fixture, - box_with_datetime): - if box_with_datetime is pd.DataFrame: + box_with_array): + if box_with_array is pd.DataFrame: pytest.xfail("FIXME: ValueError with transpose; " "alignment error without") @@ -911,17 +911,17 @@ def test_dt64arr_add_dt64ndarray_raises(self, tz_naive_fixture, dti = pd.date_range('2016-01-01', periods=3, tz=tz) dt64vals = dti.values - dtarr = tm.box_expected(dti, box_with_datetime) + dtarr = tm.box_expected(dti, box_with_array) with pytest.raises(TypeError): dtarr + dt64vals with pytest.raises(TypeError): dt64vals + dtarr - def test_dt64arr_add_timestamp_raises(self, box_with_datetime): + def test_dt64arr_add_timestamp_raises(self, box_with_array): # GH#22163 ensure DataFrame doesn't cast Timestamp to i8 idx = DatetimeIndex(['2011-01-01', '2011-01-02']) - idx = tm.box_expected(idx, box_with_datetime) + idx = tm.box_expected(idx, box_with_array) msg = "cannot add" with pytest.raises(TypeError, match=msg): idx + Timestamp('2011-01-01') @@ -932,9 +932,9 @@ def test_dt64arr_add_timestamp_raises(self, box_with_datetime): # Other Invalid Addition/Subtraction @pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])]) - def test_dt64arr_add_sub_float(self, other, box_with_datetime): + def test_dt64arr_add_sub_float(self, other, box_with_array): dti = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') - dtarr = tm.box_expected(dti, box_with_datetime) + dtarr = tm.box_expected(dti, box_with_array) with pytest.raises(TypeError): dtarr + other with pytest.raises(TypeError): @@ -947,13 +947,13 @@ def test_dt64arr_add_sub_float(self, other, box_with_datetime): @pytest.mark.parametrize('pi_freq', ['D', 'W', 'Q', 'H']) @pytest.mark.parametrize('dti_freq', [None, 'D']) def test_dt64arr_add_sub_parr(self, dti_freq, pi_freq, - box_with_datetime, box_with_period): + box_with_array, box_with_array2): # GH#20049 subtracting PeriodIndex should raise TypeError dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq) pi = dti.to_period(pi_freq) - dtarr = tm.box_expected(dti, box_with_datetime) - parr = tm.box_expected(pi, box_with_period) + dtarr = tm.box_expected(dti, box_with_array) + parr = tm.box_expected(pi, box_with_array2) with pytest.raises(TypeError): dtarr + parr @@ -965,13 +965,13 @@ def test_dt64arr_add_sub_parr(self, dti_freq, pi_freq, parr - dtarr @pytest.mark.parametrize('dti_freq', [None, 'D']) - def test_dt64arr_add_sub_period_scalar(self, dti_freq, box_with_datetime): + def test_dt64arr_add_sub_period_scalar(self, dti_freq, box_with_array): # GH#13078 # not supported, check TypeError per = pd.Period('2011-01-01', freq='D') idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=dti_freq) - dtarr = tm.box_expected(idx, box_with_datetime) + dtarr = tm.box_expected(idx, box_with_array) with pytest.raises(TypeError): dtarr + per @@ -1072,15 +1072,15 @@ def test_sub_datetime_compat(self): tm.assert_series_equal(s - dt, exp) tm.assert_series_equal(s - Timestamp(dt), exp) - def test_dt64_series_add_tick_DateOffset(self, box_with_datetime): + def test_dt64_series_add_tick_DateOffset(self, box_with_array): # GH#4532 # operate with pd.offsets ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) expected = Series([Timestamp('20130101 9:01:05'), Timestamp('20130101 9:02:05')]) - ser = tm.box_expected(ser, box_with_datetime) - expected = tm.box_expected(expected, box_with_datetime) + ser = tm.box_expected(ser, box_with_array) + expected = tm.box_expected(expected, box_with_array) result = ser + pd.offsets.Second(5) tm.assert_equal(result, expected) @@ -1088,15 +1088,15 @@ def test_dt64_series_add_tick_DateOffset(self, box_with_datetime): result2 = pd.offsets.Second(5) + ser tm.assert_equal(result2, expected) - def test_dt64_series_sub_tick_DateOffset(self, box_with_datetime): + def test_dt64_series_sub_tick_DateOffset(self, box_with_array): # GH#4532 # operate with pd.offsets ser = Series([Timestamp('20130101 9:01'), Timestamp('20130101 9:02')]) expected = Series([Timestamp('20130101 9:00:55'), Timestamp('20130101 9:01:55')]) - ser = tm.box_expected(ser, box_with_datetime) - expected = tm.box_expected(expected, box_with_datetime) + ser = tm.box_expected(ser, box_with_array) + expected = tm.box_expected(expected, box_with_array) result = ser - pd.offsets.Second(5) tm.assert_equal(result, expected) @@ -1887,9 +1887,9 @@ def test_dti_with_offset_series(self, tz_naive_fixture, names): res3 = dti - other tm.assert_series_equal(res3, expected_sub) - def test_dti_add_offset_tzaware(self, tz_aware_fixture, box_with_datetime): + def test_dti_add_offset_tzaware(self, tz_aware_fixture, box_with_array): # GH#21610, GH#22163 ensure DataFrame doesn't return object-dtype - box = box_with_datetime + box = box_with_array timezone = tz_aware_fixture if timezone == 'US/Pacific': diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 2ae4940211fd4..af3aeaaff0479 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -319,12 +319,12 @@ class TestPeriodIndexArithmetic(object): # PeriodIndex - other is defined for integers, timedelta-like others, # and PeriodIndex (with matching freq) - def test_parr_add_iadd_parr_raises(self, box_with_period): + def test_parr_add_iadd_parr_raises(self, box_with_array): rng = pd.period_range('1/1/2000', freq='D', periods=5) other = pd.period_range('1/6/2000', freq='D', periods=5) # TODO: parametrize over boxes for other? - rng = tm.box_expected(rng, box_with_period) + rng = tm.box_expected(rng, box_with_array) # An earlier implementation of PeriodIndex addition performed # a set operation (union). This has since been changed to # raise a TypeError. See GH#14164 and GH#13077 for historical @@ -361,12 +361,12 @@ def test_pi_sub_pi_with_nat(self): expected = pd.Index([pd.NaT, 0 * off, 0 * off, 0 * off, 0 * off]) tm.assert_index_equal(result, expected) - def test_parr_sub_pi_mismatched_freq(self, box_with_period): + def test_parr_sub_pi_mismatched_freq(self, box_with_array): rng = pd.period_range('1/1/2000', freq='D', periods=5) other = pd.period_range('1/6/2000', freq='H', periods=5) # TODO: parametrize over boxes for other? - rng = tm.box_expected(rng, box_with_period) + rng = tm.box_expected(rng, box_with_array) with pytest.raises(IncompatibleFrequency): rng - other @@ -376,20 +376,20 @@ def test_parr_sub_pi_mismatched_freq(self, box_with_period): @pytest.mark.parametrize('other', [3.14, np.array([2.0, 3.0])]) @pytest.mark.parametrize('op', [operator.add, ops.radd, operator.sub, ops.rsub]) - def test_parr_add_sub_float_raises(self, op, other, box_with_period): + def test_parr_add_sub_float_raises(self, op, other, box_with_array): dti = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') pi = dti.to_period('D') - pi = tm.box_expected(pi, box_with_period) + pi = tm.box_expected(pi, box_with_array) with pytest.raises(TypeError): op(pi, other) @pytest.mark.parametrize('other', [pd.Timestamp.now(), pd.Timestamp.now().to_pydatetime(), pd.Timestamp.now().to_datetime64()]) - def test_parr_add_sub_datetime_scalar(self, other, box_with_period): + def test_parr_add_sub_datetime_scalar(self, other, box_with_array): # GH#23215 rng = pd.period_range('1/1/2000', freq='D', periods=3) - rng = tm.box_expected(rng, box_with_period) + rng = tm.box_expected(rng, box_with_array) with pytest.raises(TypeError): rng + other @@ -403,12 +403,12 @@ def test_parr_add_sub_datetime_scalar(self, other, box_with_period): # ----------------------------------------------------------------- # __add__/__sub__ with ndarray[datetime64] and ndarray[timedelta64] - def test_parr_add_sub_dt64_array_raises(self, box_with_period): + def test_parr_add_sub_dt64_array_raises(self, box_with_array): rng = pd.period_range('1/1/2000', freq='D', periods=3) dti = pd.date_range('2016-01-01', periods=3) dtarr = dti.values - rng = tm.box_expected(rng, box_with_period) + rng = tm.box_expected(rng, box_with_array) with pytest.raises(TypeError): rng + dtarr @@ -595,10 +595,10 @@ def test_pi_add_offset_n_gt1(self, box_transpose_fail): result = per.freq + pi tm.assert_equal(result, expected) - def test_pi_add_offset_n_gt1_not_divisible(self, box_with_period): + def test_pi_add_offset_n_gt1_not_divisible(self, box_with_array): # GH#23215 # PeriodIndex with freq.n > 1 add offset with offset.n % freq.n != 0 - box = box_with_period + box = box_with_array pi = pd.PeriodIndex(['2016-01'], freq='2M') expected = pd.PeriodIndex(['2016-04'], freq='2M') @@ -921,10 +921,10 @@ def test_pi_ops(self): tm.assert_index_equal(result, exp) @pytest.mark.parametrize('ng', ["str", 1.5]) - def test_parr_ops_errors(self, ng, box_with_period): + def test_parr_ops_errors(self, ng, box_with_array): idx = PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'], freq='M', name='idx') - obj = tm.box_expected(idx, box_with_period) + obj = tm.box_expected(idx, box_with_array) msg = r"unsupported operand type\(s\)" with pytest.raises(TypeError, match=msg): From 5625fd2cff9be1ac2861da1c814f284794f7c4ec Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 15:21:45 -0800 Subject: [PATCH 13/16] revert non-central changes --- pandas/tests/frame/test_block_internals.py | 7 +-- pandas/tests/frame/test_quantile.py | 14 +++--- pandas/tests/io/parser/test_textreader.py | 7 +-- pandas/tests/io/test_excel.py | 11 ++--- pandas/tests/reshape/merge/test_multi.py | 9 ++-- pandas/tests/reshape/test_reshape.py | 15 ++++--- pandas/tests/series/test_analytics.py | 52 ++++++++++++---------- pandas/tests/series/test_constructors.py | 19 ++++---- pandas/tests/series/test_rank.py | 15 ++++--- pandas/tests/test_algos.py | 8 ++-- pandas/tests/test_multilevel.py | 11 ++--- pandas/tests/test_sorting.py | 6 +-- 12 files changed, 94 insertions(+), 80 deletions(-) diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index 19c4deab1b831..224e56777f6b4 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -7,6 +7,7 @@ from datetime import datetime, timedelta import itertools +from numpy import nan import numpy as np from pandas import (DataFrame, Series, Timestamp, date_range, compat, @@ -201,7 +202,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), np.nan, datetime(2001, 1, 2)], + data = [[datetime(2001, 1, 5), nan, datetime(2001, 1, 2)], [datetime(2000, 1, 2), datetime(2000, 1, 3), datetime(2000, 1, 1)]] df = DataFrame(data) @@ -543,7 +544,7 @@ 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] = np.nan + df[0] = nan wasCol = {} # uncommenting these makes the results match # for col in xrange(100, 200): @@ -554,7 +555,7 @@ def test_strange_column_corruption_issue(self): for col in range(100, 200): if col not in wasCol: wasCol[col] = 1 - df[col] = np.nan + df[col] = nan df[col][dt] = i myid = 100 diff --git a/pandas/tests/frame/test_quantile.py b/pandas/tests/frame/test_quantile.py index a6757e0060a70..a7c91dd36b2d2 100644 --- a/pandas/tests/frame/test_quantile.py +++ b/pandas/tests/frame/test_quantile.py @@ -19,14 +19,15 @@ class TestDataFrameQuantile(TestData): def test_quantile(self): + from numpy import percentile q = self.tsframe.quantile(0.1, axis=0) - assert q['A'] == np.percentile(self.tsframe['A'], 10) + assert q['A'] == percentile(self.tsframe['A'], 10) tm.assert_index_equal(q.index, self.tsframe.columns) q = self.tsframe.quantile(0.9, axis=1) assert (q['2000-01-17'] == - np.percentile(self.tsframe.loc['2000-01-17'], 90)) + percentile(self.tsframe.loc['2000-01-17'], 90)) tm.assert_index_equal(q.index, self.tsframe.index) # test degenerate case @@ -52,7 +53,7 @@ def test_quantile(self): # We may want to break API in the future to change this # so that we exclude non-numeric along the same axis - # See GH#7312 + # See GH #7312 df = DataFrame([[1, 2, 3], ['a', 'b', 4]]) result = df.quantile(.5, axis=1) @@ -100,13 +101,14 @@ def test_quantile_axis_parameter(self): pytest.raises(ValueError, df.quantile, 0.1, axis="column") def test_quantile_interpolation(self): - # see GH#10174 + # see gh-10174 + from numpy import percentile # interpolation = linear (default case) q = self.tsframe.quantile(0.1, axis=0, interpolation='linear') - assert q['A'] == np.percentile(self.tsframe['A'], 10) + assert q['A'] == percentile(self.tsframe['A'], 10) q = self.intframe.quantile(0.1) - assert q['A'] == np.percentile(self.intframe['A'], 10) + assert q['A'] == percentile(self.intframe['A'], 10) # test with and without interpolation keyword q1 = self.intframe.quantile(0.1) diff --git a/pandas/tests/io/parser/test_textreader.py b/pandas/tests/io/parser/test_textreader.py index 77c869fcaeda1..93c115ae0a57b 100644 --- a/pandas/tests/io/parser/test_textreader.py +++ b/pandas/tests/io/parser/test_textreader.py @@ -9,6 +9,7 @@ import sys import numpy as np +from numpy import nan import pytest import pandas._libs.parsers as parser @@ -316,12 +317,12 @@ def test_empty_field_eof(self): assert_array_dicts_equal(result, expected) # GH5664 - a = DataFrame([['b'], [np.nan]], columns=['a'], index=['a', 'c']) + a = DataFrame([['b'], [nan]], columns=['a'], index=['a', 'c']) b = DataFrame([[1, 1, 1, 0], [1, 1, 1, 0]], columns=list('abcd'), index=[1, 1]) - c = DataFrame([[1, 2, 3, 4], [6, np.nan, np.nan, np.nan], - [8, 9, 10, 11], [13, 14, np.nan, np.nan]], + c = DataFrame([[1, 2, 3, 4], [6, nan, nan, nan], + [8, 9, 10, 11], [13, 14, nan, nan]], columns=list('abcd'), index=[0, 5, 7, 12]) diff --git a/pandas/tests/io/test_excel.py b/pandas/tests/io/test_excel.py index 8d4e022d0270a..a097e0adbeb7a 100644 --- a/pandas/tests/io/test_excel.py +++ b/pandas/tests/io/test_excel.py @@ -9,6 +9,7 @@ import numpy as np import pytest +from numpy import nan import pandas as pd import pandas.util.testing as tm @@ -1204,7 +1205,7 @@ def test_excel_writer_context_manager(self, *_): tm.assert_frame_equal(found_df2, self.frame2) def test_roundtrip(self, merge_cells, engine, ext): - self.frame['A'][:5] = np.nan + self.frame['A'][:5] = nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) @@ -1266,7 +1267,7 @@ def test_ts_frame(self, *_): tm.assert_frame_equal(df, recons) def test_basics_with_nan(self, merge_cells, engine, ext): - self.frame['A'][:5] = np.nan + self.frame['A'][:5] = nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) self.frame.to_excel(self.path, 'test1', header=False) @@ -1331,7 +1332,7 @@ def test_inf_roundtrip(self, *_): tm.assert_frame_equal(frame, recons) def test_sheets(self, merge_cells, engine, ext): - self.frame['A'][:5] = np.nan + self.frame['A'][:5] = nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) @@ -1353,7 +1354,7 @@ def test_sheets(self, merge_cells, engine, ext): assert 'test2' == reader.sheet_names[1] def test_colaliases(self, merge_cells, engine, ext): - self.frame['A'][:5] = np.nan + self.frame['A'][:5] = nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) @@ -1370,7 +1371,7 @@ def test_colaliases(self, merge_cells, engine, ext): tm.assert_frame_equal(xp, rs) def test_roundtrip_indexlabels(self, merge_cells, engine, ext): - self.frame['A'][:5] = np.nan + self.frame['A'][:5] = nan self.frame.to_excel(self.path, 'test1') self.frame.to_excel(self.path, 'test1', columns=['A', 'B']) diff --git a/pandas/tests/reshape/merge/test_multi.py b/pandas/tests/reshape/merge/test_multi.py index 3ec9b4e656c29..a1158201844b0 100644 --- a/pandas/tests/reshape/merge/test_multi.py +++ b/pandas/tests/reshape/merge/test_multi.py @@ -3,6 +3,7 @@ from collections import OrderedDict import numpy as np +from numpy import nan from numpy.random import randn import pytest @@ -294,11 +295,11 @@ def test_left_join_index_multi_match_multiindex(self): expected = DataFrame([ ['X', 'Y', 'C', 'a', 6], ['X', 'Y', 'C', 'a', 9], - ['W', 'Y', 'C', 'e', np.nan], + ['W', 'Y', 'C', 'e', nan], ['V', 'Q', 'A', 'h', -3], ['V', 'R', 'D', 'i', 2], ['V', 'R', 'D', 'i', -1], - ['X', 'Y', 'D', 'b', np.nan], + ['X', 'Y', 'D', 'b', nan], ['X', 'Y', 'A', 'c', 1], ['X', 'Y', 'A', 'c', 4], ['W', 'Q', 'B', 'f', 3], @@ -347,10 +348,10 @@ def test_left_join_index_multi_match(self): ['c', 0, 'x'], ['c', 0, 'r'], ['c', 0, 's'], - ['b', 1, np.nan], + ['b', 1, nan], ['a', 2, 'v'], ['a', 2, 'z'], - ['b', 3, np.nan]], + ['b', 3, nan]], columns=['tag', 'val', 'char'], index=[2, 2, 2, 2, 0, 1, 1, 3]) diff --git a/pandas/tests/reshape/test_reshape.py b/pandas/tests/reshape/test_reshape.py index 38598d11d2ef3..d8b3d9588f2f1 100644 --- a/pandas/tests/reshape/test_reshape.py +++ b/pandas/tests/reshape/test_reshape.py @@ -8,6 +8,7 @@ from pandas.core.sparse.api import SparseDtype, SparseArray import pandas as pd +from numpy import nan import numpy as np from pandas.util.testing import assert_frame_equal @@ -148,18 +149,18 @@ def test_include_na(self, sparse, dtype): # Sparse dataframes do not allow nan labelled columns, see #GH8822 res_na = get_dummies(s, dummy_na=True, sparse=sparse, dtype=dtype) - exp_na = DataFrame({np.nan: [0, 0, 1], + exp_na = DataFrame({nan: [0, 0, 1], 'a': [1, 0, 0], 'b': [0, 1, 0]}, dtype=self.effective_dtype(dtype)) - exp_na = exp_na.reindex(['a', 'b', np.nan], axis=1) + exp_na = exp_na.reindex(['a', 'b', nan], axis=1) # hack (NaN handling in assert_index_equal) exp_na.columns = res_na.columns assert_frame_equal(res_na, exp_na) - res_just_na = get_dummies([np.nan], dummy_na=True, + res_just_na = get_dummies([nan], dummy_na=True, sparse=sparse, dtype=dtype) - exp_just_na = DataFrame(Series(1, index=[0]), columns=[np.nan], + exp_just_na = DataFrame(Series(1, index=[0]), columns=[nan], dtype=self.effective_dtype(dtype)) tm.assert_numpy_array_equal(res_just_na.values, exp_just_na.values) @@ -443,13 +444,13 @@ def test_basic_drop_first_NA(self, sparse): sparse=sparse) exp_na = DataFrame( {'b': [0, 1, 0], - np.nan: [0, 0, 1]}, - dtype=np.uint8).reindex(['b', np.nan], axis=1) + nan: [0, 0, 1]}, + dtype=np.uint8).reindex(['b', nan], axis=1) if sparse: exp_na = exp_na.to_sparse(fill_value=0, kind='integer') assert_frame_equal(res_na, exp_na) - res_just_na = get_dummies([np.nan], dummy_na=True, drop_first=True, + res_just_na = get_dummies([nan], dummy_na=True, drop_first=True, sparse=sparse) exp_just_na = DataFrame(index=np.arange(1)) assert_frame_equal(res_just_na, exp_just_na) diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index 3651d2c8a00b6..a5a7cc2217864 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -6,6 +6,7 @@ import operator import numpy as np +from numpy import nan import pytest from pandas.compat import PY35, lrange, range @@ -14,8 +15,10 @@ import pandas as pd from pandas import ( Categorical, CategoricalIndex, DataFrame, Series, bdate_range, compat, - date_range, isna, notna, MultiIndex, Timedelta, Timestamp) - + date_range, isna, notna) +from pandas.core.index import MultiIndex +from pandas.core.indexes.datetimes import Timestamp +from pandas.core.indexes.timedeltas import Timedelta import pandas.core.nanops as nanops import pandas.util.testing as tm from pandas.util.testing import ( @@ -502,7 +505,7 @@ def test_npdiff(self): s = Series(np.arange(5)) r = np.diff(s) - assert_series_equal(Series([np.nan, 0, 0, 0, np.nan]), r) + assert_series_equal(Series([nan, 0, 0, 0, nan]), r) def _check_stat_op(self, name, alternate, string_series_, check_objects=False, check_allna=False): @@ -527,7 +530,7 @@ def _check_stat_op(self, name, alternate, string_series_, assert_almost_equal(f(nona), alternate(nona.values)) assert_almost_equal(f(string_series_), alternate(nona.values)) - allna = string_series_ * np.nan + allna = string_series_ * nan if check_allna: assert np.isnan(f(allna)) @@ -703,15 +706,15 @@ def test_modulo(self): result2 = p['second'] % p['first'] assert not result.equals(result2) - # GH#9144 + # GH 9144 s = Series([0, 1]) result = s % 0 - expected = Series([np.nan, np.nan]) + expected = Series([nan, nan]) assert_series_equal(result, expected) result = 0 % s - expected = Series([np.nan, 0.0]) + expected = Series([nan, 0.0]) assert_series_equal(result, expected) @td.skip_if_no_scipy @@ -853,14 +856,14 @@ def test_count(self, datetime_series): assert datetime_series.count() == np.isfinite(datetime_series).sum() - mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, np.nan, 1, 2]]) + mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, nan, 1, 2]]) ts = Series(np.arange(len(mi)), index=mi) left = ts.count(level=1) - right = Series([2, 3, 1], index=[1, 2, np.nan]) + right = Series([2, 3, 1], index=[1, 2, nan]) assert_series_equal(left, right) - ts.iloc[[0, 3, 5]] = np.nan + ts.iloc[[0, 3, 5]] = nan assert_series_equal(ts.count(level=1), right - 1) def test_dot(self): @@ -1069,11 +1072,11 @@ def cummax(x): result = getattr(s, method)() assert_series_equal(result, expected) - e = pd.Series([False, True, np.nan, False]) - cse = pd.Series([0, 1, np.nan, 1], dtype=object) - cpe = pd.Series([False, 0, np.nan, 0]) - cmin = pd.Series([False, False, np.nan, False]) - cmax = pd.Series([False, True, np.nan, True]) + e = pd.Series([False, True, nan, False]) + cse = pd.Series([0, 1, nan, 1], dtype=object) + cpe = pd.Series([False, 0, nan, 0]) + cmin = pd.Series([False, False, nan, False]) + cmax = pd.Series([False, True, nan, True]) expecteds = {'cumsum': cse, 'cumprod': cpe, 'cummin': cmin, @@ -1216,7 +1219,7 @@ def test_idxmin(self, string_series): nona.values.argmin()) # all NaNs - allna = string_series * np.nan + allna = string_series * nan assert isna(allna.idxmin()) # datetime64[ns] @@ -1272,7 +1275,7 @@ def test_idxmax(self, string_series): nona.values.argmax()) # all NaNs - allna = string_series * np.nan + allna = string_series * nan assert isna(allna.idxmax()) from pandas import date_range @@ -1498,7 +1501,7 @@ def test_shift_int(self, datetime_series): assert_series_equal(shifted, expected) def test_shift_categorical(self): - # GH#9416 + # GH 9416 s = pd.Series(['a', 'b', 'c', 'd'], dtype='category') assert_series_equal(s.iloc[:-1], s.shift(1).shift(-1).dropna()) @@ -1517,13 +1520,15 @@ def test_shift_categorical(self): assert_index_equal(s.values.categories, sn2.values.categories) def test_unstack(self): + from numpy import nan + index = MultiIndex(levels=[['bar', 'foo'], ['one', 'three', 'two']], labels=[[1, 1, 0, 0], [0, 1, 0, 2]]) s = Series(np.arange(4.), index=index) unstacked = s.unstack() - expected = DataFrame([[2., np.nan, 3.], [0., 1., np.nan]], + expected = DataFrame([[2., nan, 3.], [0., 1., nan]], index=['bar', 'foo'], columns=['one', 'three', 'two']) @@ -1547,18 +1552,17 @@ def test_unstack(self): idx = pd.MultiIndex.from_arrays([[101, 102], [3.5, np.nan]]) ts = pd.Series([1, 2], index=idx) left = ts.unstack() - right = DataFrame([[np.nan, 1], [2, np.nan]], index=[101, 102], - columns=[np.nan, 3.5]) + right = DataFrame([[nan, 1], [2, nan]], index=[101, 102], + columns=[nan, 3.5]) assert_frame_equal(left, right) idx = pd.MultiIndex.from_arrays([['cat', 'cat', 'cat', 'dog', 'dog' ], ['a', 'a', 'b', 'a', 'b'], [1, 2, 1, 1, np.nan]]) ts = pd.Series([1.0, 1.1, 1.2, 1.3, 1.4], index=idx) - right = DataFrame([[1.0, 1.3], [1.1, np.nan], - [np.nan, 1.4], [1.2, np.nan]], + right = DataFrame([[1.0, 1.3], [1.1, nan], [nan, 1.4], [1.2, nan]], columns=['cat', 'dog']) - tpls = [('a', 1), ('a', 2), ('b', np.nan), ('b', 1)] + tpls = [('a', 1), ('a', 2), ('b', nan), ('b', 1)] right.index = pd.MultiIndex.from_tuples(tpls) assert_frame_equal(ts.unstack(level=0), right) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 1714392667b05..ce0cf0d5c089e 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -5,6 +5,7 @@ from datetime import datetime, timedelta import numpy as np +from numpy import nan import numpy.ma as ma import pytest @@ -371,14 +372,14 @@ def test_unordered_compare_equal(self): def test_constructor_maskedarray(self): data = ma.masked_all((3, ), dtype=float) result = Series(data) - expected = Series([np.nan, np.nan, np.nan]) + expected = Series([nan, nan, nan]) assert_series_equal(result, expected) data[0] = 0.0 data[2] = 2.0 index = ['a', 'b', 'c'] result = Series(data, index=index) - expected = Series([0.0, np.nan, 2.0], index=index) + expected = Series([0.0, nan, 2.0], index=index) assert_series_equal(result, expected) data[1] = 1.0 @@ -388,14 +389,14 @@ def test_constructor_maskedarray(self): data = ma.masked_all((3, ), dtype=int) result = Series(data) - expected = Series([np.nan, np.nan, np.nan], dtype=float) + expected = Series([nan, nan, nan], dtype=float) assert_series_equal(result, expected) data[0] = 0 data[2] = 2 index = ['a', 'b', 'c'] result = Series(data, index=index) - expected = Series([0, np.nan, 2], index=index, dtype=float) + expected = Series([0, nan, 2], index=index, dtype=float) assert_series_equal(result, expected) data[1] = 1 @@ -405,14 +406,14 @@ def test_constructor_maskedarray(self): data = ma.masked_all((3, ), dtype=bool) result = Series(data) - expected = Series([np.nan, np.nan, np.nan], dtype=object) + expected = Series([nan, nan, nan], dtype=object) assert_series_equal(result, expected) data[0] = True data[2] = False index = ['a', 'b', 'c'] result = Series(data, index=index) - expected = Series([True, np.nan, False], index=index, dtype=object) + expected = Series([True, nan, False], index=index, dtype=object) assert_series_equal(result, expected) data[1] = True @@ -631,14 +632,14 @@ def test_constructor_dtype_datetime64(self): s = Series(iNaT, index=lrange(5)) assert not isna(s).all() - s = Series(np.nan, dtype='M8[ns]', index=lrange(5)) + s = Series(nan, dtype='M8[ns]', index=lrange(5)) assert isna(s).all() s = Series([datetime(2001, 1, 2, 0, 0), iNaT], dtype='M8[ns]') assert isna(s[1]) assert s.dtype == 'M8[ns]' - s = Series([datetime(2001, 1, 2, 0, 0), np.nan], dtype='M8[ns]') + s = Series([datetime(2001, 1, 2, 0, 0), nan], dtype='M8[ns]') assert isna(s[1]) assert s.dtype == 'M8[ns]' @@ -884,7 +885,7 @@ def test_constructor_periodindex(self): def test_constructor_dict(self): d = {'a': 0., 'b': 1., 'c': 2.} result = Series(d, index=['b', 'c', 'd', 'a']) - expected = Series([1, 2, np.nan, 0], index=['b', 'c', 'd', 'a']) + expected = Series([1, 2, nan, 0], index=['b', 'c', 'd', 'a']) assert_series_equal(result, expected) pidx = tm.makePeriodIndex(100) diff --git a/pandas/tests/series/test_rank.py b/pandas/tests/series/test_rank.py index 66cc9149fb43e..5b0ea37a0bfcf 100644 --- a/pandas/tests/series/test_rank.py +++ b/pandas/tests/series/test_rank.py @@ -3,6 +3,7 @@ from itertools import chain import numpy as np +from numpy import nan import pytest from pandas._libs.algos import Infinity, NegInfinity @@ -19,15 +20,15 @@ class TestSeriesRank(TestData): - s = Series([1, 3, 4, 2, np.nan, 2, 1, 5, np.nan, 3]) + s = Series([1, 3, 4, 2, nan, 2, 1, 5, nan, 3]) results = { - '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]), + '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]), } def test_rank(self): diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index e5cc75d08fe4b..ff505f2986b1a 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -4,7 +4,7 @@ import pytest from numpy.random import RandomState - +from numpy import nan from datetime import datetime from itertools import permutations import struct @@ -1435,11 +1435,11 @@ def _check(arr): result = libalgos.rank_1d_float64(arr) arr[mask] = np.inf exp = rankdata(arr) - exp[mask] = np.nan + exp[mask] = nan assert_almost_equal(result, exp) - _check(np.array([np.nan, np.nan, 5., 5., 5., np.nan, 1, 2, 3, np.nan])) - _check(np.array([4., np.nan, 5., 5., 5., np.nan, 1, 2, 4., np.nan])) + _check(np.array([nan, nan, 5., 5., 5., nan, 1, 2, 3, nan])) + _check(np.array([4., nan, 5., 5., 5., nan, 1, 2, 4., nan])) def test_basic(self): exp = np.array([1, 2], dtype=np.float64) diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index da6d24bcbb0b1..2717b92e05a29 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -2058,20 +2058,21 @@ def test_indexing_over_hashtable_size_cutoff(self): def test_multiindex_na_repr(self): # only an issue with long columns + from numpy import nan df3 = DataFrame({ 'A' * 30: {('A', 'A0006000', 'nuit'): 'A0006000'}, - 'B' * 30: {('A', 'A0006000', 'nuit'): np.nan}, - 'C' * 30: {('A', 'A0006000', 'nuit'): np.nan}, - 'D' * 30: {('A', 'A0006000', 'nuit'): np.nan}, + 'B' * 30: {('A', 'A0006000', 'nuit'): nan}, + 'C' * 30: {('A', 'A0006000', 'nuit'): nan}, + 'D' * 30: {('A', 'A0006000', 'nuit'): nan}, 'E' * 30: {('A', 'A0006000', 'nuit'): 'A'}, - 'F' * 30: {('A', 'A0006000', 'nuit'): np.nan}, + 'F' * 30: {('A', 'A0006000', 'nuit'): nan}, }) idf = df3.set_index(['A' * 30, 'C' * 30]) repr(idf) def test_assign_index_sequences(self): - # GH#2200 + # #2200 df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}).set_index(["a", "b"]) diff --git a/pandas/tests/test_sorting.py b/pandas/tests/test_sorting.py index 525c95d33e330..22e758a0e59a7 100644 --- a/pandas/tests/test_sorting.py +++ b/pandas/tests/test_sorting.py @@ -5,7 +5,7 @@ from datetime import datetime import numpy as np - +from numpy import nan from pandas.core import common as com from pandas import DataFrame, MultiIndex, merge, concat, Series, compat from pandas.util import testing as tm @@ -100,7 +100,7 @@ def aggr(func): assert_frame_equal(gr.median(), aggr(np.median)) def test_lexsort_indexer(self): - keys = [[np.nan] * 5 + list(range(100)) + [np.nan] * 5] + keys = [[nan] * 5 + list(range(100)) + [nan] * 5] # orders=True, na_position='last' result = lexsort_indexer(keys, orders=True, na_position='last') exp = list(range(5, 105)) + list(range(5)) + list(range(105, 110)) @@ -123,7 +123,7 @@ def test_lexsort_indexer(self): def test_nargsort(self): # np.argsort(items) places NaNs last - items = [np.nan] * 5 + list(range(100)) + [np.nan] * 5 + items = [nan] * 5 + list(range(100)) + [nan] * 5 # np.argsort(items2) may not place NaNs first items2 = np.array(items, dtype='O') From d8cd07308a76a512c5a7b15fee06ce66e74541d1 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 17:12:54 -0800 Subject: [PATCH 14/16] flake8 fixups --- pandas/tests/arithmetic/conftest.py | 3 --- pandas/tests/arithmetic/test_datetime64.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/arithmetic/conftest.py b/pandas/tests/arithmetic/conftest.py index 5777e042cf709..63a5c40a31f1d 100644 --- a/pandas/tests/arithmetic/conftest.py +++ b/pandas/tests/arithmetic/conftest.py @@ -5,9 +5,6 @@ import pandas as pd from pandas.compat import long -from pandas.core.arrays import ( - PeriodArray, DatetimeArrayMixin as DatetimeArray, - TimedeltaArrayMixin as TimedeltaArray) import pandas.util.testing as tm diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 642d53a50e86a..020d9bdf57f70 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -739,7 +739,7 @@ def test_dt64arr_add_td64_scalar(self, box_with_array): result = dtarr + np.timedelta64(5, 'ms') tm.assert_equal(result, expected) - result2 = np.timedelta64(5, 'ms') + dtarr + result = np.timedelta64(5, 'ms') + dtarr tm.assert_equal(result, expected) def test_dt64arr_add_sub_td64_nat(self, box_with_array, From 11eda45c7e14650114eb57b089c0bcc5a3638c53 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 17:18:55 -0800 Subject: [PATCH 15/16] whatspace improvement --- pandas/tests/arithmetic/test_datetime64.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 020d9bdf57f70..aeca8846a8bd8 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -742,8 +742,7 @@ def test_dt64arr_add_td64_scalar(self, box_with_array): result = np.timedelta64(5, 'ms') + dtarr tm.assert_equal(result, expected) - def test_dt64arr_add_sub_td64_nat(self, box_with_array, - tz_naive_fixture): + def test_dt64arr_add_sub_td64_nat(self, box_with_array, tz_naive_fixture): # GH#23320 special handling for timedelta64("NaT") tz = tz_naive_fixture box = box_with_array From d0ac7f25698134f5c1f44e8731ba355caae5113e Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 17:21:09 -0800 Subject: [PATCH 16/16] less verbose --- pandas/tests/arithmetic/test_datetime64.py | 34 +++++++++------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index aeca8846a8bd8..78af506978443 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -172,12 +172,11 @@ def test_dt64ser_cmp_date_invalid(self, box_with_array): # GH#19800 datetime.date comparison raises to # match DatetimeIndex/Timestamp. This also matches the behavior # of stdlib datetime.datetime - box = box_with_array ser = pd.date_range('20010101', periods=10) date = ser.iloc[0].to_pydatetime().date() - ser = tm.box_expected(ser, box) + ser = tm.box_expected(ser, box_with_array) assert not (ser == date).any() assert (ser != date).all() with pytest.raises(TypeError): @@ -655,7 +654,6 @@ class TestDatetime64Arithmetic(object): def test_dt64arr_add_timedeltalike_scalar(self, tz_naive_fixture, two_hours, box_with_array): # GH#22005, GH#22163 check DataFrame doesn't raise TypeError - box = box_with_array tz = tz_naive_fixture rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) @@ -663,15 +661,14 @@ def test_dt64arr_add_timedeltalike_scalar(self, tz_naive_fixture, '2000-02-01 02:00', tz=tz) # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) + rng = tm.box_expected(rng, box_with_array, transpose=False) + expected = tm.box_expected(expected, box_with_array, transpose=False) result = rng + two_hours tm.assert_equal(result, expected) def test_dt64arr_iadd_timedeltalike_scalar(self, tz_naive_fixture, two_hours, box_with_array): - box = box_with_array tz = tz_naive_fixture rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) @@ -679,15 +676,14 @@ def test_dt64arr_iadd_timedeltalike_scalar(self, tz_naive_fixture, '2000-02-01 02:00', tz=tz) # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) + rng = tm.box_expected(rng, box_with_array, transpose=False) + expected = tm.box_expected(expected, box_with_array, transpose=False) rng += two_hours tm.assert_equal(rng, expected) def test_dt64arr_sub_timedeltalike_scalar(self, tz_naive_fixture, two_hours, box_with_array): - box = box_with_array tz = tz_naive_fixture rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) @@ -695,15 +691,14 @@ def test_dt64arr_sub_timedeltalike_scalar(self, tz_naive_fixture, '2000-01-31 22:00', tz=tz) # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) + rng = tm.box_expected(rng, box_with_array, transpose=False) + expected = tm.box_expected(expected, box_with_array, transpose=False) result = rng - two_hours tm.assert_equal(result, expected) def test_dt64arr_isub_timedeltalike_scalar(self, tz_naive_fixture, two_hours, box_with_array): - box = box_with_array tz = tz_naive_fixture rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) @@ -711,8 +706,8 @@ def test_dt64arr_isub_timedeltalike_scalar(self, tz_naive_fixture, '2000-01-31 22:00', tz=tz) # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) + rng = tm.box_expected(rng, box_with_array, transpose=False) + expected = tm.box_expected(expected, box_with_array, transpose=False) rng -= two_hours tm.assert_equal(rng, expected) @@ -745,7 +740,6 @@ def test_dt64arr_add_td64_scalar(self, box_with_array): def test_dt64arr_add_sub_td64_nat(self, box_with_array, tz_naive_fixture): # GH#23320 special handling for timedelta64("NaT") tz = tz_naive_fixture - box = box_with_array dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS") other = np.timedelta64("NaT") @@ -753,8 +747,8 @@ def test_dt64arr_add_sub_td64_nat(self, box_with_array, tz_naive_fixture): # FIXME: fails with transpose=True due to tz-aware DataFrame # transpose bug - obj = tm.box_expected(dti, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) + obj = tm.box_expected(dti, box_with_array, transpose=False) + expected = tm.box_expected(expected, box_with_array, transpose=False) result = obj + other tm.assert_equal(result, expected) @@ -1888,8 +1882,6 @@ def test_dti_with_offset_series(self, tz_naive_fixture, names): def test_dti_add_offset_tzaware(self, tz_aware_fixture, box_with_array): # GH#21610, GH#22163 ensure DataFrame doesn't return object-dtype - box = box_with_array - timezone = tz_aware_fixture if timezone == 'US/Pacific': dates = date_range('2012-11-01', periods=3, tz=timezone) @@ -1902,8 +1894,8 @@ def test_dti_add_offset_tzaware(self, tz_aware_fixture, box_with_array): '2010-11-01 07:00'], freq='H', tz=timezone) # FIXME: these raise ValueError with transpose=True - dates = tm.box_expected(dates, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) + dates = tm.box_expected(dates, box_with_array, transpose=False) + expected = tm.box_expected(expected, box_with_array, transpose=False) # TODO: parametrize over the scalar being added? radd? sub? offset = dates + pd.offsets.Hour(5)