From 86f38103dff5d7d9d70697239ef68c4257795e8d Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Thu, 15 Nov 2018 17:28:26 -0800 Subject: [PATCH 1/3] implement tm.to_array, maximally-specific fixtures --- pandas/tests/arithmetic/conftest.py | 90 ++++++++++++++++----- pandas/tests/arithmetic/test_datetime64.py | 48 +++++------ pandas/tests/arithmetic/test_period.py | 29 +++---- pandas/tests/arithmetic/test_timedelta64.py | 11 ++- pandas/util/testing.py | 38 ++++++++- 5 files changed, 141 insertions(+), 75 deletions(-) diff --git a/pandas/tests/arithmetic/conftest.py b/pandas/tests/arithmetic/conftest.py index 9ee5e05638978..604cddd4165cc 100644 --- a/pandas/tests/arithmetic/conftest.py +++ b/pandas/tests/arithmetic/conftest.py @@ -5,9 +5,22 @@ import pandas as pd from pandas.compat import long -from pandas.core.arrays import PeriodArray, DatetimeArrayMixin as DatetimeArray +import pandas.util.testing as tm +# ------------------------------------------------------------------ +# Helper Functions + +def id_func(x): + if isinstance(x, tuple): + assert len(x) == 2 + return x[0].__name__ + '-' + str(x[1]) + else: + return x.__name__ + + +# ------------------------------------------------------------------ + @pytest.fixture(params=[1, np.array(1, dtype=np.int64)]) def one(request): # zero-dim integer array behaves like an integer @@ -137,7 +150,7 @@ def mismatched_freq(request): # ------------------------------------------------------------------ @pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame], - ids=lambda x: x.__name__) + ids=id_func) def box(request): """ Several array-like containers that should have effectively identical @@ -150,7 +163,7 @@ def box(request): pd.Series, pytest.param(pd.DataFrame, marks=pytest.mark.xfail(strict=True))], - ids=lambda x: x.__name__) + ids=id_func) def box_df_fail(request): """ Fixture equivalent to `box` fixture but xfailing the DataFrame case. @@ -158,34 +171,73 @@ def box_df_fail(request): return request.param -@pytest.fixture(params=[(pd.Index, False), - (pd.Series, False), +@pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, tm.to_array], + ids=id_func) +def box4(request): + """ + Fixture to test on each of Index, Series, DataFrame, and pandas Array + classes. + """ + return request.param + + +@pytest.fixture(params=[pd.Index, + pd.Series, (pd.DataFrame, False), - pytest.param((pd.DataFrame, True), - marks=pytest.mark.xfail(strict=True))], - ids=lambda x: x[0].__name__ + '-' + str(x[1])) -def box_transpose_fail(request): + (pd.DataFrame, True), + tm.to_array], + ids=id_func) +def box5(request): """ - Fixture similar to `box` but testing both transpose cases for DataFrame, - with the tranpose=True case xfailed. + Like `box4`, but the DataFrame case with box transpose=True and + transpose=False """ # GH#23620 return request.param -@pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, PeriodArray], - ids=lambda x: x.__name__) -def box_with_period(request): +@pytest.fixture(params=[pd.Index, + pd.Series, + (pd.DataFrame, False), + pytest.param((pd.DataFrame, True), + marks=pytest.mark.xfail(strict=True)), + tm.to_array], + ids=id_func) +def box5_tfail(request): """ - Like `box`, but specific to PeriodDtype for also testing PeriodArray + Like `box5`, but xfailing the transposed DataFrame case """ + # GH#23620 return request.param -@pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, DatetimeArray], - ids=lambda x: x.__name__) -def box_with_datetime(request): +# Construct tuples of the form (box, tz) over all our box classes and +# the timezones in tz_naive_fixture. We then xfail the cases where the box +# is a DataFrame-with-transpose and the timezone is not tz-naive (i.e. None) +boxes = [pd.Index, + pd.Series, + (pd.DataFrame, False), + (pd.DataFrame, True), + tm.to_array] + +# copied from pandas.conftest tz_naive_fixture +TIMEZONES = [None, 'UTC', 'US/Eastern', 'Asia/Tokyo', 'dateutil/US/Pacific', + 'dateutil/Asia/Singapore'] + +params = [(x, y) for x in boxes for y in TIMEZONES] +for n in range(len(params)): + tup = params[n] + if isinstance(tup[0], tuple) and tup[0][1] is True and tup[1] is not None: + # i.e. (DataFrame, True, tzinfo) excluding the no-tzinfo case + param = pytest.param(tup, marks=pytest.mark.xfail(strict=True)) + params[n] = param + + +@pytest.fixture(params=params) +def box5_and_tz(request): """ - Like `box`, but specific to datetime64 for also testing DatetimeArray + Fixture to test over Index, Series, DataFrame, and pandas Array, also + providing timezones, xfailing timezone-aware cases with transposed + DataFrame """ return request.param diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index b25e9a9a485c2..28a54a8d8a4d1 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1037,10 +1037,10 @@ def test_dti_add_sub_float(self, op, other): with pytest.raises(TypeError): op(dti, other) - def test_dti_add_timestamp_raises(self, box_with_datetime): + def test_dti_add_timestamp_raises(self, box4): # 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, box4) msg = "cannot add" with pytest.raises(TypeError, match=msg): idx + Timestamp('2011-01-01') @@ -1152,21 +1152,17 @@ def test_dti_add_intarray_no_freq(self, box): # ------------------------------------------------------------- # Binary operations DatetimeIndex and timedelta-like - def test_dti_add_timedeltalike(self, tz_naive_fixture, two_hours, - box_with_datetime): + def test_dti_add_timedeltalike(self, two_hours, box5_and_tz): # GH#22005, GH#22163 check DataFrame doesn't raise TypeError - box = box_with_datetime - - tz = tz_naive_fixture + box, tz = box5_and_tz rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) - # FIXME: calling with transpose=True raises ValueError - rng = tm.box_expected(rng, box, transpose=False) + rng = tm.box_expected(rng, box) 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) + expected = tm.box_expected(expected, box) tm.assert_equal(result, expected) def test_dti_iadd_timedeltalike(self, tz_naive_fixture, two_hours): @@ -1193,18 +1189,16 @@ 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): + def test_dt64arr_add_sub_td64_nat(self, box5_and_tz): # GH#23320 special handling for timedelta64("NaT") - tz = tz_naive_fixture + box, tz = box5_and_tz 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) + obj = tm.box_expected(dti, box) + expected = tm.box_expected(expected, box) result = obj + other tm.assert_equal(result, expected) @@ -1439,13 +1433,13 @@ def test_sub_dti_dti(self): tm.assert_index_equal(result, expected) @pytest.mark.parametrize('freq', [None, 'D']) - def test_sub_period(self, freq, box_with_datetime): + def test_sub_period(self, freq, box5): # GH#13078 # not supported, check TypeError p = pd.Period('2011-01-01', freq='D') idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=freq) - idx = tm.box_expected(idx, box_with_datetime) + idx = tm.box_expected(idx, box5) with pytest.raises(TypeError): idx - p @@ -1785,24 +1779,22 @@ 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, box5_and_tz): # GH#21610, GH#22163 ensure DataFrame doesn't return object-dtype - box = box_with_datetime + box, tz = box5_and_tz - timezone = tz_aware_fixture - if timezone == 'US/Pacific': - dates = date_range('2012-11-01', periods=3, tz=timezone) + if tz == 'US/Pacific': + dates = date_range('2012-11-01', periods=3, tz=tz) offset = dates + pd.offsets.Hour(5) assert dates[0] + pd.offsets.Hour(5) == offset[0] dates = date_range('2010-11-01 00:00', - periods=3, tz=timezone, freq='H') + periods=3, tz=tz, freq='H') expected = DatetimeIndex(['2010-11-01 05:00', '2010-11-01 06:00', - '2010-11-01 07:00'], freq='H', tz=timezone) + '2010-11-01 07:00'], freq='H', tz=tz) - # 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) + expected = tm.box_expected(expected, box) # TODO: parametrize over the scalar being added? radd? sub? offset = dates + pd.offsets.Hour(5) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index a26a11cb6be9e..a0c1c92a71281 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -553,18 +553,16 @@ def test_pi_sub_isub_offset(self): rng -= pd.offsets.MonthEnd(5) tm.assert_index_equal(rng, expected) - def test_pi_add_offset_n_gt1(self, box_transpose_fail): + def test_pi_add_offset_n_gt1(self, box5_tfail): # GH#23215 # add offset to PeriodIndex with freq.n > 1 - box, transpose = box_transpose_fail - per = pd.Period('2016-01', freq='2M') pi = pd.PeriodIndex([per]) expected = pd.PeriodIndex(['2016-03'], freq='2M') - pi = tm.box_expected(pi, box, transpose=transpose) - expected = tm.box_expected(expected, box, transpose=transpose) + pi = tm.box_expected(pi, box5_tfail) + expected = tm.box_expected(expected, box5_tfail) result = pi + per.freq tm.assert_equal(result, expected) @@ -572,17 +570,14 @@ 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, box5_tfail): # GH#23215 # PeriodIndex with freq.n > 1 add offset with offset.n % freq.n != 0 - box = box_with_period - pi = pd.PeriodIndex(['2016-01'], freq='2M') expected = pd.PeriodIndex(['2016-04'], freq='2M') - # FIXME: with transposing these tests fail - pi = tm.box_expected(pi, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) + pi = tm.box_expected(pi, box5_tfail) + expected = tm.box_expected(expected, box5_tfail) result = pi + to_offset('3M') tm.assert_equal(result, expected) @@ -796,16 +791,14 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self, with pytest.raises(period.IncompatibleFrequency, match=msg): rng -= other - def test_parr_add_sub_td64_nat(self, box_transpose_fail): + def test_parr_add_sub_td64_nat(self, box5_tfail): # GH#23320 special handling for timedelta64("NaT") - box, transpose = box_transpose_fail - pi = pd.period_range("1994-04-01", periods=9, freq="19D") other = np.timedelta64("NaT") expected = pd.PeriodIndex(["NaT"] * 9, freq="19D") - obj = tm.box_expected(pi, box, transpose=transpose) - expected = tm.box_expected(expected, box, transpose=transpose) + obj = tm.box_expected(pi, box5_tfail) + expected = tm.box_expected(expected, box5_tfail) result = obj + other tm.assert_equal(result, expected) @@ -898,10 +891,10 @@ 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_pi_ops_errors(self, ng, box4): 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, box4) msg = r"unsupported operand type\(s\)" with pytest.raises(TypeError, match=msg): diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 58c8b3b07f723..f94869675558c 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -413,19 +413,18 @@ def test_td64arr_sub_timestamp_raises(self, box): with pytest.raises(TypeError, match=msg): idx - Timestamp('2011-01-01') - def test_td64arr_add_timestamp(self, box, tz_naive_fixture): + def test_td64arr_add_timestamp(self, box5_and_tz): # GH#23215 # TODO: parametrize over scalar datetime types? - tz = tz_naive_fixture + box, tz = box5_and_tz + other = Timestamp('2011-01-01', tz=tz) idx = TimedeltaIndex(['1 day', '2 day']) expected = DatetimeIndex(['2011-01-02', '2011-01-03'], tz=tz) - # FIXME: fails with transpose=True because of tz-aware DataFrame - # transpose bug - idx = tm.box_expected(idx, box, transpose=False) - expected = tm.box_expected(expected, box, transpose=False) + idx = tm.box_expected(idx, box) + expected = tm.box_expected(expected, box) result = idx + other tm.assert_equal(result, expected) diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 1fa77f5321038..494322ee970de 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -24,9 +24,10 @@ map, raise_with_traceback, range, string_types, u, unichr, zip) from pandas.core.dtypes.common import ( - is_bool, is_categorical_dtype, is_datetimelike_v_numeric, - is_datetimelike_v_object, is_extension_array_dtype, is_interval_dtype, - is_list_like, is_number, is_sequence, needs_i8_conversion) + is_bool, is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype, + is_datetimelike_v_numeric, is_datetimelike_v_object, + is_extension_array_dtype, is_interval_dtype, is_list_like, is_number, + is_period_dtype, is_sequence, is_timedelta64_dtype, needs_i8_conversion) from pandas.core.dtypes.missing import array_equivalent import pandas as pd @@ -37,7 +38,7 @@ from pandas.core.algorithms import take_1d from pandas.core.arrays import ( DatetimeArrayMixin as DatetimeArray, ExtensionArray, IntervalArray, - PeriodArray, period_array) + PeriodArray, TimedeltaArrayMixin as TimedeltaArray, period_array) import pandas.core.common as com from pandas.io.common import urlopen @@ -1080,6 +1081,13 @@ def assert_datetime_array_equal(left, right, obj='DatetimeArray'): assert_attr_equal('tz', left, right, obj=obj) +def assert_timedelta_array_equal(left, right, obj='TimedeltaArray'): + _check_isinstance(left, right, TimedeltaArray) + assert_numpy_array_equal(left._data, right._data, + obj='{obj}._data'.format(obj=obj)) + assert_attr_equal('freq', left, right, obj=obj) + + def raise_assert_detail(obj, message, left, right, diff=None): __tracebackhide__ = True @@ -1579,6 +1587,8 @@ def assert_equal(left, right, **kwargs): assert_period_array_equal(left, right, **kwargs) elif isinstance(left, DatetimeArray): assert_datetime_array_equal(left, right, **kwargs) + elif isinstance(left, TimedeltaArray): + assert_timedelta_array_equal(left, right, **kwargs) elif isinstance(left, ExtensionArray): assert_extension_array_equal(left, right, **kwargs) elif isinstance(left, np.ndarray): @@ -1611,6 +1621,12 @@ def box_expected(expected, box_cls, transpose=True): # not a single-column, in order to operate against non-DataFrame # vectors of the same length. expected = expected.T + elif isinstance(box_cls, tuple): + # compat for passing transpose in the same fixture as box without + # unpacking the tuple in every test + assert len(box_cls) == 2 + box_cls, transpose = box_cls + return box_expected(expected, box_cls, transpose=transpose) elif box_cls is PeriodArray: # the PeriodArray constructor is not as flexible as period_array expected = period_array(expected) @@ -1618,11 +1634,25 @@ def box_expected(expected, box_cls, transpose=True): expected = DatetimeArray(expected) elif box_cls is np.ndarray: expected = np.array(expected) + elif box_cls is to_array: + expected = to_array(expected) else: raise NotImplementedError(box_cls) return expected +def to_array(obj): + # temporary implementation until we get pd.array in place + if is_period_dtype(obj): + return period_array(obj) + elif is_datetime64_dtype(obj) or is_datetime64tz_dtype(obj): + return DatetimeArray(obj) + elif is_timedelta64_dtype(obj): + return TimedeltaArray(obj) + else: + return np.array(obj) + + # ----------------------------------------------------------------------------- # Sparse From f8a3988bdcd00ba721869d83bc025534416944c5 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 16 Nov 2018 09:47:43 -0800 Subject: [PATCH 2/3] implement box4b, box5b --- pandas/tests/arithmetic/conftest.py | 5 +++++ pandas/tests/arithmetic/test_datetime64.py | 20 ++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/pandas/tests/arithmetic/conftest.py b/pandas/tests/arithmetic/conftest.py index 604cddd4165cc..c5b48435abdc4 100644 --- a/pandas/tests/arithmetic/conftest.py +++ b/pandas/tests/arithmetic/conftest.py @@ -241,3 +241,8 @@ def box5_and_tz(request): DataFrame """ return request.param + + +# aliases so we can use the same fixture twice in a test +box4b = box4 +box5b = box5 diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 2770073c2ebbf..37251067d4465 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -159,16 +159,14 @@ 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, box5): # GH#19800 datetime.date comparison raises to # match DatetimeIndex/Timestamp. This also matches the behavior # of stdlib datetime.datetime - box = box_with_datetime - 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, box5) assert not (ser == date).any() assert (ser != date).all() with pytest.raises(TypeError): @@ -230,13 +228,12 @@ 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, box4): # GH#11034 - box = box_with_datetime - xbox = box if box not in [pd.Index, DatetimeArray] else np.ndarray + xbox = box4 if box4 not in [pd.Index, DatetimeArray] else np.ndarray ser = pd.Series([pd.Timestamp('2000-01-29 01:59:00'), 'NaT']) - ser = tm.box_expected(ser, box) + ser = tm.box_expected(ser, box4) result = ser != ser expected = tm.box_expected([False, True], xbox) @@ -1486,14 +1483,13 @@ def test_dt64arr_add_sub_period(self, dti_freq, box5): @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_dti_add_sub_pi(self, dti_freq, pi_freq, box4, box4b): # 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, box4) + parr = tm.box_expected(pi, box4b) with pytest.raises(TypeError): dtarr + parr From 9c07638dfb81fe9ff99555add61703123b73e7c9 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 17 Nov 2018 15:13:59 -0800 Subject: [PATCH 3/3] better names --- pandas/tests/arithmetic/conftest.py | 12 +++++------ pandas/tests/arithmetic/test_datetime64.py | 24 ++++++++++----------- pandas/tests/arithmetic/test_period.py | 18 ++++++++-------- pandas/tests/arithmetic/test_timedelta64.py | 4 ++-- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/pandas/tests/arithmetic/conftest.py b/pandas/tests/arithmetic/conftest.py index 6a9e390f9100a..2becf8d776bda 100644 --- a/pandas/tests/arithmetic/conftest.py +++ b/pandas/tests/arithmetic/conftest.py @@ -177,7 +177,7 @@ def box_df_fail(request): (pd.DataFrame, True), tm.to_array], ids=id_func) -def box5(request): +def box_and_transpose(request): """ Like `box4`, but the DataFrame case with box transpose=True and transpose=False @@ -193,9 +193,9 @@ def box5(request): marks=pytest.mark.xfail(strict=True)), tm.to_array], ids=id_func) -def box5_tfail(request): +def box_transpose_fail(request): """ - Like `box5`, but xfailing the transposed DataFrame case + Like `box_and_transpose`, but xfailing the transposed DataFrame case """ # GH#23620 return request.param @@ -224,7 +224,7 @@ def box5_tfail(request): @pytest.fixture(params=params) -def box5_and_tz(request): +def box_transpose_and_tz(request): """ Fixture to test over Index, Series, DataFrame, and pandas Array, also providing timezones, xfailing timezone-aware cases with transposed @@ -244,5 +244,5 @@ def box_with_array(request): # aliases so we can use the same fixture twice in a test -box_with_arrayb = box_with_array -box5b = box5 +box_with_array2 = box_with_array +box_and_transpose2 = box_and_transpose diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 1bffef9822790..6e0142a3272c9 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -159,14 +159,14 @@ 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, box5): + def test_dt64ser_cmp_date_invalid(self, box_and_transpose): # GH#19800 datetime.date comparison raises to # match DatetimeIndex/Timestamp. This also matches the behavior # of stdlib datetime.datetime ser = pd.date_range('20010101', periods=10) date = ser.iloc[0].to_pydatetime().date() - ser = tm.box_expected(ser, box5) + ser = tm.box_expected(ser, box_and_transpose) assert not (ser == date).any() assert (ser != date).all() with pytest.raises(TypeError): @@ -1183,9 +1183,9 @@ def test_dti_add_intarray_no_freq(self, box): # ------------------------------------------------------------- # Binary operations DatetimeIndex and timedelta-like - def test_dti_add_timedeltalike(self, two_hours, box5_and_tz): + def test_dti_add_timedeltalike(self, two_hours, box_transpose_and_tz): # GH#22005, GH#22163 check DataFrame doesn't raise TypeError - box, tz = box5_and_tz + box, tz = box_transpose_and_tz rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) rng = tm.box_expected(rng, box) @@ -1220,9 +1220,9 @@ 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, box5_and_tz): + def test_dt64arr_add_sub_td64_nat(self, box_transpose_and_tz): # GH#23320 special handling for timedelta64("NaT") - box, tz = box5_and_tz + box, tz = box_transpose_and_tz dti = pd.date_range("1994-04-01", periods=9, tz=tz, freq="QS") other = np.timedelta64("NaT") @@ -1464,13 +1464,13 @@ def test_sub_dti_dti(self): tm.assert_index_equal(result, expected) @pytest.mark.parametrize('dti_freq', [None, 'D']) - def test_dt64arr_add_sub_period(self, dti_freq, box5): + def test_dt64arr_add_sub_period(self, dti_freq, box_and_transpose): # 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, box5) + idx = tm.box_expected(idx, box_and_transpose) with pytest.raises(TypeError): idx + p @@ -1484,13 +1484,13 @@ def test_dt64arr_add_sub_period(self, dti_freq, box5): @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_array, box_with_arrayb): + 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_array) - parr = tm.box_expected(pi, box_with_arrayb) + parr = tm.box_expected(pi, box_with_array2) with pytest.raises(TypeError): dtarr + parr @@ -1819,9 +1819,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, box5_and_tz): + def test_dti_add_offset_tzaware(self, box_transpose_and_tz): # GH#21610, GH#22163 ensure DataFrame doesn't return object-dtype - box, tz = box5_and_tz + box, tz = box_transpose_and_tz if tz == 'US/Pacific': dates = date_range('2012-11-01', periods=3, tz=tz) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 3dcc21cb5e902..23d5c593cab6a 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -553,7 +553,7 @@ def test_pi_sub_isub_offset(self): rng -= pd.offsets.MonthEnd(5) tm.assert_index_equal(rng, expected) - def test_pi_add_offset_n_gt1(self, box5_tfail): + def test_pi_add_offset_n_gt1(self, box_transpose_fail): # GH#23215 # add offset to PeriodIndex with freq.n > 1 per = pd.Period('2016-01', freq='2M') @@ -561,8 +561,8 @@ def test_pi_add_offset_n_gt1(self, box5_tfail): expected = pd.PeriodIndex(['2016-03'], freq='2M') - pi = tm.box_expected(pi, box5_tfail) - expected = tm.box_expected(expected, box5_tfail) + pi = tm.box_expected(pi, box_transpose_fail) + expected = tm.box_expected(expected, box_transpose_fail) result = pi + per.freq tm.assert_equal(result, expected) @@ -570,14 +570,14 @@ def test_pi_add_offset_n_gt1(self, box5_tfail): result = per.freq + pi tm.assert_equal(result, expected) - def test_pi_add_offset_n_gt1_not_divisible(self, box5_tfail): + def test_pi_add_offset_n_gt1_not_divisible(self, box_transpose_fail): # GH#23215 # PeriodIndex with freq.n > 1 add offset with offset.n % freq.n != 0 pi = pd.PeriodIndex(['2016-01'], freq='2M') expected = pd.PeriodIndex(['2016-04'], freq='2M') - pi = tm.box_expected(pi, box5_tfail) - expected = tm.box_expected(expected, box5_tfail) + pi = tm.box_expected(pi, box_transpose_fail) + expected = tm.box_expected(expected, box_transpose_fail) result = pi + to_offset('3M') tm.assert_equal(result, expected) @@ -791,14 +791,14 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self, with pytest.raises(period.IncompatibleFrequency, match=msg): rng -= other - def test_parr_add_sub_td64_nat(self, box5_tfail): + def test_parr_add_sub_td64_nat(self, box_transpose_fail): # GH#23320 special handling for timedelta64("NaT") pi = pd.period_range("1994-04-01", periods=9, freq="19D") other = np.timedelta64("NaT") expected = pd.PeriodIndex(["NaT"] * 9, freq="19D") - obj = tm.box_expected(pi, box5_tfail) - expected = tm.box_expected(expected, box5_tfail) + obj = tm.box_expected(pi, box_transpose_fail) + expected = tm.box_expected(expected, box_transpose_fail) result = obj + other tm.assert_equal(result, expected) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index f94869675558c..207bc6ed6f60a 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -413,10 +413,10 @@ def test_td64arr_sub_timestamp_raises(self, box): with pytest.raises(TypeError, match=msg): idx - Timestamp('2011-01-01') - def test_td64arr_add_timestamp(self, box5_and_tz): + def test_td64arr_add_timestamp(self, box_transpose_and_tz): # GH#23215 # TODO: parametrize over scalar datetime types? - box, tz = box5_and_tz + box, tz = box_transpose_and_tz other = Timestamp('2011-01-01', tz=tz)