From 761b85bbdfada6211ad64e9fd9c9cdff30f479ee Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 12:19:27 -0800 Subject: [PATCH 1/8] CLN: runtime imports --- pandas/tests/scalar/timestamp/test_timestamp.py | 4 ---- pandas/tests/scalar/timestamp/test_timezones.py | 6 ++---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 652dd34ca7ce2..f9fa80644d4b9 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -202,8 +202,6 @@ def test_constructor(self): base_expected = 1404205200000000000 # confirm base representation is correct - import calendar - assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected tests = [ @@ -275,8 +273,6 @@ def test_constructor_with_stringoffset(self): base_expected = 1404205200000000000 # confirm base representation is correct - import calendar - assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected tests = [ diff --git a/pandas/tests/scalar/timestamp/test_timezones.py b/pandas/tests/scalar/timestamp/test_timezones.py index 424b0c9abdef8..a56cdf0b98ee7 100644 --- a/pandas/tests/scalar/timestamp/test_timezones.py +++ b/pandas/tests/scalar/timestamp/test_timezones.py @@ -306,15 +306,13 @@ def test_astimezone(self, tzstr): @td.skip_if_windows def test_tz_convert_utc_with_system_utc(self): - from pandas._libs.tslibs.timezones import maybe_get_tz - # from system utc to real utc - ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC")) + ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc()) # from system utc to real utc - ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC")) + ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc()) From e537b78f8d17877b02c38e0b6ca8f06c9d409688 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 15:43:43 -0800 Subject: [PATCH 2/8] parametrize --- pandas/tests/arithmetic/test_datetime64.py | 87 ++++----------------- pandas/tests/arithmetic/test_timedelta64.py | 25 +++--- 2 files changed, 27 insertions(+), 85 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index d239687a37757..b47764f667c2f 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -26,7 +26,9 @@ Timestamp, date_range, ) +import pandas.core.arrays.datetimelike as dtl from pandas.core.indexes.datetimes import _to_M8 +from pandas.core.ops import roperator import pandas.util.testing as tm @@ -2257,13 +2259,6 @@ def test_timedelta64_equal_timedelta_supported_ops(self, op): intervals = ["D", "h", "m", "s", "us"] - # TODO: unused - # npy16_mappings = {'D': 24 * 60 * 60 * 1000000, - # 'h': 60 * 60 * 1000000, - # 'm': 60 * 1000000, - # 's': 1000000, - # 'us': 1} - def timedelta64(*args): # see casting notes in NumPy gh-12927 return np.sum(list(starmap(np.timedelta64, zip(args, intervals)))) @@ -2406,82 +2401,30 @@ def test_dti_add_series(self, tz, names): result4 = index + ser.values tm.assert_index_equal(result4, expected) + @pytest.mark.parametrize("other_box", [pd.Index, Series]) + @pytest.mark.parametrize("op", [operator.add, roperator.radd, operator.sub]) @pytest.mark.parametrize( "names", [(None, None, None), ("foo", "bar", None), ("foo", "foo", "foo")] ) - def test_dti_add_offset_index(self, tz_naive_fixture, names): + def test_dti_addsub_offset_arraylike(self, tz_naive_fixture, names, op, other_box): # GH#18849, GH#19744 - tz = tz_naive_fixture - dti = pd.date_range("2017-01-01", periods=2, tz=tz, name=names[0]) - other = pd.Index([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)], name=names[1]) - - with tm.assert_produces_warning( - PerformanceWarning, clear=[pd.core.arrays.datetimelike] - ): - res = dti + other - expected = DatetimeIndex( - [dti[n] + other[n] for n in range(len(dti))], name=names[2], freq="infer" - ) - tm.assert_index_equal(res, expected) - - with tm.assert_produces_warning( - PerformanceWarning, clear=[pd.core.arrays.datetimelike] - ): - res2 = other + dti - tm.assert_index_equal(res2, expected) - - @pytest.mark.parametrize( - "names", [(None, None, None), ("foo", "bar", None), ("foo", "foo", "foo")] - ) - def test_dti_sub_offset_index(self, tz_naive_fixture, names): - # GH#18824, GH#19744 - tz = tz_naive_fixture - dti = pd.date_range("2017-01-01", periods=2, tz=tz, name=names[0]) - other = pd.Index([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)], name=names[1]) + box = pd.Index + from .test_timedelta64 import get_upcast_box - with tm.assert_produces_warning( - PerformanceWarning, clear=[pd.core.arrays.datetimelike] - ): - res = dti - other - expected = DatetimeIndex( - [dti[n] - other[n] for n in range(len(dti))], name=names[2], freq="infer" - ) - tm.assert_index_equal(res, expected) - - @pytest.mark.parametrize( - "names", [(None, None, None), ("foo", "bar", None), ("foo", "foo", "foo")] - ) - def test_dti_with_offset_series(self, tz_naive_fixture, names): - # GH#18849 tz = tz_naive_fixture dti = pd.date_range("2017-01-01", periods=2, tz=tz, name=names[0]) - other = Series([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)], name=names[1]) + other = other_box([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)], name=names[1]) - expected_add = Series( - [dti[n] + other[n] for n in range(len(dti))], name=names[2] - ) - - with tm.assert_produces_warning( - PerformanceWarning, clear=[pd.core.arrays.datetimelike] - ): - res = dti + other - tm.assert_series_equal(res, expected_add) + xbox = get_upcast_box(box, other) - with tm.assert_produces_warning( - PerformanceWarning, clear=[pd.core.arrays.datetimelike] - ): - res2 = other + dti - tm.assert_series_equal(res2, expected_add) + with tm.assert_produces_warning(PerformanceWarning, clear=[dtl]): + res = op(dti, other) - expected_sub = Series( - [dti[n] - other[n] for n in range(len(dti))], name=names[2] + expected = DatetimeIndex( + [op(dti[n], other[n]) for n in range(len(dti))], name=names[2], freq="infer" ) - - with tm.assert_produces_warning( - PerformanceWarning, clear=[pd.core.arrays.datetimelike] - ): - res3 = dti - other - tm.assert_series_equal(res3, expected_sub) + expected = tm.box_expected(expected, xbox) + tm.assert_equal(res, expected) @pytest.mark.parametrize("years", [-1, 0, 1]) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index ecb07fa49036a..996d221b9190e 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -362,10 +362,6 @@ def test_dti_tdi_numeric_ops(self): tdi = TimedeltaIndex(["1 days", pd.NaT, "2 days"], name="foo") dti = pd.date_range("20130101", periods=3, name="bar") - # TODO(wesm): unused? - # td = Timedelta('1 days') - # dt = Timestamp('20130101') - result = tdi - tdi expected = TimedeltaIndex(["0 days", pd.NaT, "0 days"], name="foo") tm.assert_index_equal(result, expected) @@ -896,11 +892,16 @@ def test_td64arr_add_timestamp(self, box_with_array, tz_naive_fixture): result = other + idx tm.assert_equal(result, expected) - def test_td64arr_add_sub_timestamp(self, box_with_array): + @pytest.mark.parametrize( + "ts", + [ + Timestamp("2012-01-01"), + Timestamp("2012-01-01").to_pydatetime(), + Timestamp("2012-01-01").to_datetime64(), + ], + ) + def test_td64arr_add_sub_timestamp(self, ts, box_with_array): # GH#11925 - ts = Timestamp("2012-01-01") - # TODO: parametrize over types of datetime scalar? - tdi = timedelta_range("1 day", periods=3) expected = pd.date_range("2012-01-02", periods=3) @@ -2028,10 +2029,10 @@ def test_td64arr_mul_int_series(self, box_df_fail, names): dtype="timedelta64[ns]", name=exname, ) + xbox = get_upcast_box(box, ser) tdi = tm.box_expected(tdi, box) - box = Series if (box is pd.Index or box is tm.to_array) else box - expected = tm.box_expected(expected, box) + expected = tm.box_expected(expected, xbox) result = ser * tdi tm.assert_equal(result, expected) @@ -2066,9 +2067,7 @@ def test_float_series_rdiv_td64arr(self, box_with_array, names): name=xname, ) - xbox = box - if box in [pd.Index, tm.to_array] and type(ser) is Series: - xbox = Series + xbox = get_upcast_box(box, ser) tdi = tm.box_expected(tdi, box) expected = tm.box_expected(expected, xbox) From e11973869ccffdd7a0ed1ca4d47ad76c2b519af3 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 16:01:15 -0800 Subject: [PATCH 3/8] parametrize tests --- pandas/tests/arithmetic/test_datetime64.py | 91 +++++++++------------- 1 file changed, 36 insertions(+), 55 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index b47764f667c2f..45f95bca02e27 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -104,19 +104,24 @@ def test_compare_zerodim(self, tz_naive_fixture, box_with_array): expected = tm.box_expected(expected, xbox) tm.assert_equal(result, expected) - def test_dt64arr_cmp_date_invalid(self, tz_naive_fixture, box_with_array): - # GH#19800, GH#19301 datetime.date comparison raises to - # match DatetimeIndex/Timestamp. This also matches the behavior - # of stdlib datetime.datetime - tz = tz_naive_fixture - - dti = pd.date_range("20010101", periods=10, tz=tz) - date = dti[0].to_pydatetime().date() - - dtarr = tm.box_expected(dti, box_with_array) - assert_invalid_comparison(dtarr, date, box_with_array) - - @pytest.mark.parametrize("other", ["foo", -1, 99, 4.0, object(), timedelta(days=2)]) + @pytest.mark.parametrize( + "other", + [ + "foo", + -1, + 99, + 4.0, + object(), + timedelta(days=2), + # GH#19800, GH#19301 datetime.date comparison raises to + # match DatetimeIndex/Timestamp. This also matches the behavior + # of stdlib datetime.datetime + datetime(2001, 1, 1).date(), + # GH#19301 None and NaN are *not* cast to NaT for comparisons + None, + np.nan, + ], + ) def test_dt64arr_cmp_scalar_invalid(self, other, tz_naive_fixture, box_with_array): # GH#22074, GH#15966 tz = tz_naive_fixture @@ -125,16 +130,6 @@ def test_dt64arr_cmp_scalar_invalid(self, other, tz_naive_fixture, box_with_arra dtarr = tm.box_expected(rng, box_with_array) assert_invalid_comparison(dtarr, other, box_with_array) - @pytest.mark.parametrize("other", [None, np.nan]) - def test_dt64arr_cmp_na_scalar_invalid( - self, other, tz_naive_fixture, box_with_array - ): - # GH#19301 - tz = tz_naive_fixture - dti = pd.date_range("2016-01-01", periods=2, tz=tz) - dtarr = tm.box_expected(dti, box_with_array) - assert_invalid_comparison(dtarr, other, box_with_array) - def test_dt64arr_nat_comparison(self, tz_naive_fixture, box_with_array): # GH#22242, GH#22163 DataFrame considered NaT == ts incorrectly tz = tz_naive_fixture @@ -260,15 +255,10 @@ def test_nat_comparisons_scalar(self, dtype, data, box_with_array): tm.assert_equal(left >= NaT, expected) tm.assert_equal(NaT <= left, expected) - def test_series_comparison_scalars(self): + @pytest.mark.parametrize("val", [datetime(2000, 1, 4), datetime(2000, 1, 5)]) + def test_series_comparison_scalars(self, val): series = Series(date_range("1/1/2000", periods=10)) - val = datetime(2000, 1, 4) - result = series > val - expected = Series([x > val for x in series]) - tm.assert_series_equal(result, expected) - - val = series[5] result = series > val expected = Series([x > val for x in series]) tm.assert_series_equal(result, expected) @@ -1022,9 +1012,18 @@ def test_dt64arr_add_timestamp_raises(self, box_with_array): # ------------------------------------------------------------- # 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_array): - dti = DatetimeIndex(["2011-01-01", "2011-01-02"], freq="D") + @pytest.mark.parametrize( + "other", + [ + 3.14, + np.array([2.0, 3.0]), + # GH#13078 datetime +/- Period is invalid + pd.Period("2011-01-01", freq="D"), + ], + ) + @pytest.mark.parametrize("dti_freq", [None, "D"]) + def test_dt64arr_add_sub_invalid(self, dti_freq, other, box_with_array): + dti = DatetimeIndex(["2011-01-01", "2011-01-02"], freq=dti_freq) dtarr = tm.box_expected(dti, box_with_array) msg = "|".join( [ @@ -1070,24 +1069,6 @@ def test_dt64arr_add_sub_parr( with pytest.raises(TypeError, match=msg): parr - dtarr - @pytest.mark.parametrize("dti_freq", [None, "D"]) - 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_array) - msg = "|".join(["unsupported operand type", "cannot (add|subtract)"]) - with pytest.raises(TypeError, match=msg): - dtarr + per - with pytest.raises(TypeError, match=msg): - per + dtarr - with pytest.raises(TypeError, match=msg): - dtarr - per - with pytest.raises(TypeError, match=msg): - per - dtarr - class TestDatetime64DateOffsetArithmetic: @@ -1408,7 +1389,7 @@ def test_dt64arr_add_mixed_offset_array(self, box_with_array): s = tm.box_expected(s, box_with_array) warn = None if box_with_array is pd.DataFrame else PerformanceWarning - with tm.assert_produces_warning(warn, clear=[pd.core.arrays.datetimelike]): + with tm.assert_produces_warning(warn, clear=[dtl]): other = pd.Index([pd.offsets.DateOffset(years=1), pd.offsets.MonthEnd()]) other = tm.box_expected(other, box_with_array) result = s + other @@ -1437,7 +1418,7 @@ def test_dt64arr_add_sub_offset_ndarray(self, tz_naive_fixture, box_with_array): other = np.array([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)]) warn = None if box_with_array is pd.DataFrame else PerformanceWarning - with tm.assert_produces_warning(warn, clear=[pd.core.arrays.datetimelike]): + with tm.assert_produces_warning(warn, clear=[dtl]): res = dtarr + other expected = DatetimeIndex( [dti[n] + other[n] for n in range(len(dti))], name=dti.name, freq="infer" @@ -1445,11 +1426,11 @@ def test_dt64arr_add_sub_offset_ndarray(self, tz_naive_fixture, box_with_array): expected = tm.box_expected(expected, box_with_array) tm.assert_equal(res, expected) - with tm.assert_produces_warning(warn, clear=[pd.core.arrays.datetimelike]): + with tm.assert_produces_warning(warn, clear=[dtl]): res2 = other + dtarr tm.assert_equal(res2, expected) - with tm.assert_produces_warning(warn, clear=[pd.core.arrays.datetimelike]): + with tm.assert_produces_warning(warn, clear=[dtl]): res = dtarr - other expected = DatetimeIndex( [dti[n] - other[n] for n in range(len(dti))], name=dti.name, freq="infer" From 0fa415fd8adfaf68ebe8c8005dcffb85aef48555 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 16:24:15 -0800 Subject: [PATCH 4/8] revert test that will need bugfix --- pandas/tests/arithmetic/test_timedelta64.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 996d221b9190e..41a0e86730fec 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -892,16 +892,11 @@ def test_td64arr_add_timestamp(self, box_with_array, tz_naive_fixture): result = other + idx tm.assert_equal(result, expected) - @pytest.mark.parametrize( - "ts", - [ - Timestamp("2012-01-01"), - Timestamp("2012-01-01").to_pydatetime(), - Timestamp("2012-01-01").to_datetime64(), - ], - ) - def test_td64arr_add_sub_timestamp(self, ts, box_with_array): + def test_td64arr_add_sub_timestamp(self, box_with_array): # GH#11925 + ts = Timestamp("2012-01-01") + # TODO: parametrize over types of datetime scalar? + tdi = timedelta_range("1 day", periods=3) expected = pd.date_range("2012-01-02", periods=3) From e3a7a2f3498d85494779c1b505a0cdd2b0d83b97 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 17:01:16 -0800 Subject: [PATCH 5/8] define constant --- pandas/tests/arithmetic/test_period.py | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index ed693d873efb8..0c4488c9684ee 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -342,28 +342,29 @@ def test_pi_comp_period(self): idx = PeriodIndex( ["2011-01", "2011-02", "2011-03", "2011-04"], freq="M", name="idx" ) + per = pd.Period("2011-03", freq="M") - f = lambda x: x == pd.Period("2011-03", freq="M") + f = lambda x: x == per exp = np.array([False, False, True, False], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: pd.Period("2011-03", freq="M") == x + f = lambda x: per == x self._check(idx, f, exp) - f = lambda x: x != pd.Period("2011-03", freq="M") + f = lambda x: x != per exp = np.array([True, True, False, True], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: pd.Period("2011-03", freq="M") != x + f = lambda x: per != x self._check(idx, f, exp) - f = lambda x: pd.Period("2011-03", freq="M") >= x + f = lambda x: per >= x exp = np.array([True, True, True, False], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: x > pd.Period("2011-03", freq="M") + f = lambda x: x > per exp = np.array([False, False, False, True], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: pd.Period("2011-03", freq="M") >= x + f = lambda x: per >= x exp = np.array([True, True, True, False], dtype=np.bool) self._check(idx, f, exp) @@ -371,11 +372,12 @@ def test_pi_comp_period_nat(self): idx = PeriodIndex( ["2011-01", "NaT", "2011-03", "2011-04"], freq="M", name="idx" ) + per = pd.Period("2011-03", freq="M") - f = lambda x: x == pd.Period("2011-03", freq="M") + f = lambda x: x == per exp = np.array([False, False, True, False], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: pd.Period("2011-03", freq="M") == x + f = lambda x: per == x self._check(idx, f, exp) f = lambda x: x == pd.NaT @@ -384,10 +386,10 @@ def test_pi_comp_period_nat(self): f = lambda x: pd.NaT == x self._check(idx, f, exp) - f = lambda x: x != pd.Period("2011-03", freq="M") + f = lambda x: x != per exp = np.array([True, True, False, True], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: pd.Period("2011-03", freq="M") != x + f = lambda x: per != x self._check(idx, f, exp) f = lambda x: x != pd.NaT @@ -396,11 +398,11 @@ def test_pi_comp_period_nat(self): f = lambda x: pd.NaT != x self._check(idx, f, exp) - f = lambda x: pd.Period("2011-03", freq="M") >= x + f = lambda x: per >= x exp = np.array([True, False, True, False], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: x < pd.Period("2011-03", freq="M") + f = lambda x: x < per exp = np.array([True, False, False, False], dtype=np.bool) self._check(idx, f, exp) From c95f0c2d5c238124485bf8bd2146ecb3fe9dcdd2 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 17:13:44 -0800 Subject: [PATCH 6/8] isolate non-scalar tests --- pandas/tests/arithmetic/test_period.py | 25 ++++++++++++++++++- pandas/tests/scalar/period/test_period.py | 30 +++++++++-------------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 0c4488c9684ee..9b6fe51d92ecc 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -10,7 +10,7 @@ from pandas.errors import PerformanceWarning import pandas as pd -from pandas import Period, PeriodIndex, Series, period_range +from pandas import Period, PeriodIndex, Series, Timestamp, period_range from pandas.core import ops from pandas.core.arrays import TimedeltaArray import pandas.util.testing as tm @@ -1045,6 +1045,29 @@ def test_parr_add_sub_index(self): expected = pi - pi tm.assert_index_equal(result, expected) + @pytest.mark.parametrize("lbox", [Series, pd.Index]) + @pytest.mark.parametrize("rbox", [Series, pd.Index]) + def test_period_add_timestamp_raises(self, rbox, lbox): + # GH#17983, see also scalar version of this test in tests.scalar.period + ts = Timestamp("2017") + per = Period("2017", freq="M") + + # We may get a different message depending on which class raises + # the error. + msg = ( + r"cannot add|unsupported operand|" + r"can only operate on a|incompatible type|" + r"ufunc add cannot use operands" + ) + with pytest.raises(TypeError, match=msg): + lbox([ts]) + rbox([per]) + + with pytest.raises(TypeError, match=msg): + lbox([per]) + rbox([ts]) + + with pytest.raises(TypeError, match=msg): + lbox([per]) + rbox([per]) + class TestPeriodSeriesArithmetic: def test_ops_series_timedelta(self): diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 3bdf91cbf838b..028d582b54990 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -14,7 +14,6 @@ from pandas._libs.tslibs.timezones import dateutil_gettz, maybe_get_tz from pandas.compat.numpy import np_datetime64_compat -import pandas as pd from pandas import NaT, Period, Timedelta, Timestamp, offsets import pandas.util.testing as tm @@ -1062,31 +1061,22 @@ def test_add_invalid(self): with pytest.raises(TypeError, match=msg): per1 + per2 - boxes = [lambda x: x, lambda x: pd.Series([x]), lambda x: pd.Index([x])] - ids = ["identity", "Series", "Index"] - - @pytest.mark.parametrize("lbox", boxes, ids=ids) - @pytest.mark.parametrize("rbox", boxes, ids=ids) - def test_add_timestamp_raises(self, rbox, lbox): - # GH#17983 + def test_period_add_timestamp_raises(self): + # GH#17983, see also boxed version of this test + # in tests.arithmetic.test_period ts = Timestamp("2017") per = Period("2017", freq="M") - # We may get a different message depending on which class raises - # the error. - msg = ( - r"cannot add|unsupported operand|" - r"can only operate on a|incompatible type|" - r"ufunc add cannot use operands" - ) + msg = "unsupported operand" + with pytest.raises(TypeError, match=msg): - lbox(ts) + rbox(per) + ts + per with pytest.raises(TypeError, match=msg): - lbox(per) + rbox(ts) + per + ts with pytest.raises(TypeError, match=msg): - lbox(per) + rbox(per) + per + per def test_sub(self): per1 = Period("2011-01-01", freq="D") @@ -1284,6 +1274,7 @@ def test_add_offset_nat(self): # freq is DateOffset for freq in ["A", "2A", "3A"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [offsets.YearEnd(2)]: assert p + o is NaT assert o + p is NaT @@ -1300,6 +1291,7 @@ def test_add_offset_nat(self): for freq in ["M", "2M", "3M"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]: assert p + o is NaT assert o + p is NaT @@ -1317,6 +1309,7 @@ def test_add_offset_nat(self): # freq is Tick for freq in ["D", "2D", "3D"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [ offsets.Day(5), offsets.Hour(24), @@ -1340,6 +1333,7 @@ def test_add_offset_nat(self): for freq in ["H", "2H", "3H"]: p = Period("NaT", freq=freq) + assert p is NaT for o in [ offsets.Day(2), offsets.Hour(3), From 0005bf3f0bd9666005c5b3eb0b8cf48e3672dfab Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 18:07:54 -0800 Subject: [PATCH 7/8] revert --- pandas/tests/arithmetic/test_period.py | 53 +++++-------------- pandas/tests/arithmetic/test_timedelta64.py | 12 +++-- pandas/tests/scalar/period/test_period.py | 30 ++++++----- .../tests/scalar/timestamp/test_timestamp.py | 4 ++ .../tests/scalar/timestamp/test_timezones.py | 6 ++- 5 files changed, 49 insertions(+), 56 deletions(-) diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 9b6fe51d92ecc..ed693d873efb8 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -10,7 +10,7 @@ from pandas.errors import PerformanceWarning import pandas as pd -from pandas import Period, PeriodIndex, Series, Timestamp, period_range +from pandas import Period, PeriodIndex, Series, period_range from pandas.core import ops from pandas.core.arrays import TimedeltaArray import pandas.util.testing as tm @@ -342,29 +342,28 @@ def test_pi_comp_period(self): idx = PeriodIndex( ["2011-01", "2011-02", "2011-03", "2011-04"], freq="M", name="idx" ) - per = pd.Period("2011-03", freq="M") - f = lambda x: x == per + f = lambda x: x == pd.Period("2011-03", freq="M") exp = np.array([False, False, True, False], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: per == x + f = lambda x: pd.Period("2011-03", freq="M") == x self._check(idx, f, exp) - f = lambda x: x != per + f = lambda x: x != pd.Period("2011-03", freq="M") exp = np.array([True, True, False, True], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: per != x + f = lambda x: pd.Period("2011-03", freq="M") != x self._check(idx, f, exp) - f = lambda x: per >= x + f = lambda x: pd.Period("2011-03", freq="M") >= x exp = np.array([True, True, True, False], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: x > per + f = lambda x: x > pd.Period("2011-03", freq="M") exp = np.array([False, False, False, True], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: per >= x + f = lambda x: pd.Period("2011-03", freq="M") >= x exp = np.array([True, True, True, False], dtype=np.bool) self._check(idx, f, exp) @@ -372,12 +371,11 @@ def test_pi_comp_period_nat(self): idx = PeriodIndex( ["2011-01", "NaT", "2011-03", "2011-04"], freq="M", name="idx" ) - per = pd.Period("2011-03", freq="M") - f = lambda x: x == per + f = lambda x: x == pd.Period("2011-03", freq="M") exp = np.array([False, False, True, False], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: per == x + f = lambda x: pd.Period("2011-03", freq="M") == x self._check(idx, f, exp) f = lambda x: x == pd.NaT @@ -386,10 +384,10 @@ def test_pi_comp_period_nat(self): f = lambda x: pd.NaT == x self._check(idx, f, exp) - f = lambda x: x != per + f = lambda x: x != pd.Period("2011-03", freq="M") exp = np.array([True, True, False, True], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: per != x + f = lambda x: pd.Period("2011-03", freq="M") != x self._check(idx, f, exp) f = lambda x: x != pd.NaT @@ -398,11 +396,11 @@ def test_pi_comp_period_nat(self): f = lambda x: pd.NaT != x self._check(idx, f, exp) - f = lambda x: per >= x + f = lambda x: pd.Period("2011-03", freq="M") >= x exp = np.array([True, False, True, False], dtype=np.bool) self._check(idx, f, exp) - f = lambda x: x < per + f = lambda x: x < pd.Period("2011-03", freq="M") exp = np.array([True, False, False, False], dtype=np.bool) self._check(idx, f, exp) @@ -1045,29 +1043,6 @@ def test_parr_add_sub_index(self): expected = pi - pi tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("lbox", [Series, pd.Index]) - @pytest.mark.parametrize("rbox", [Series, pd.Index]) - def test_period_add_timestamp_raises(self, rbox, lbox): - # GH#17983, see also scalar version of this test in tests.scalar.period - ts = Timestamp("2017") - per = Period("2017", freq="M") - - # We may get a different message depending on which class raises - # the error. - msg = ( - r"cannot add|unsupported operand|" - r"can only operate on a|incompatible type|" - r"ufunc add cannot use operands" - ) - with pytest.raises(TypeError, match=msg): - lbox([ts]) + rbox([per]) - - with pytest.raises(TypeError, match=msg): - lbox([per]) + rbox([ts]) - - with pytest.raises(TypeError, match=msg): - lbox([per]) + rbox([per]) - class TestPeriodSeriesArithmetic: def test_ops_series_timedelta(self): diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 41a0e86730fec..ecb07fa49036a 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -362,6 +362,10 @@ def test_dti_tdi_numeric_ops(self): tdi = TimedeltaIndex(["1 days", pd.NaT, "2 days"], name="foo") dti = pd.date_range("20130101", periods=3, name="bar") + # TODO(wesm): unused? + # td = Timedelta('1 days') + # dt = Timestamp('20130101') + result = tdi - tdi expected = TimedeltaIndex(["0 days", pd.NaT, "0 days"], name="foo") tm.assert_index_equal(result, expected) @@ -2024,10 +2028,10 @@ def test_td64arr_mul_int_series(self, box_df_fail, names): dtype="timedelta64[ns]", name=exname, ) - xbox = get_upcast_box(box, ser) tdi = tm.box_expected(tdi, box) - expected = tm.box_expected(expected, xbox) + box = Series if (box is pd.Index or box is tm.to_array) else box + expected = tm.box_expected(expected, box) result = ser * tdi tm.assert_equal(result, expected) @@ -2062,7 +2066,9 @@ def test_float_series_rdiv_td64arr(self, box_with_array, names): name=xname, ) - xbox = get_upcast_box(box, ser) + xbox = box + if box in [pd.Index, tm.to_array] and type(ser) is Series: + xbox = Series tdi = tm.box_expected(tdi, box) expected = tm.box_expected(expected, xbox) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 028d582b54990..3bdf91cbf838b 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -14,6 +14,7 @@ from pandas._libs.tslibs.timezones import dateutil_gettz, maybe_get_tz from pandas.compat.numpy import np_datetime64_compat +import pandas as pd from pandas import NaT, Period, Timedelta, Timestamp, offsets import pandas.util.testing as tm @@ -1061,22 +1062,31 @@ def test_add_invalid(self): with pytest.raises(TypeError, match=msg): per1 + per2 - def test_period_add_timestamp_raises(self): - # GH#17983, see also boxed version of this test - # in tests.arithmetic.test_period + boxes = [lambda x: x, lambda x: pd.Series([x]), lambda x: pd.Index([x])] + ids = ["identity", "Series", "Index"] + + @pytest.mark.parametrize("lbox", boxes, ids=ids) + @pytest.mark.parametrize("rbox", boxes, ids=ids) + def test_add_timestamp_raises(self, rbox, lbox): + # GH#17983 ts = Timestamp("2017") per = Period("2017", freq="M") - msg = "unsupported operand" - + # We may get a different message depending on which class raises + # the error. + msg = ( + r"cannot add|unsupported operand|" + r"can only operate on a|incompatible type|" + r"ufunc add cannot use operands" + ) with pytest.raises(TypeError, match=msg): - ts + per + lbox(ts) + rbox(per) with pytest.raises(TypeError, match=msg): - per + ts + lbox(per) + rbox(ts) with pytest.raises(TypeError, match=msg): - per + per + lbox(per) + rbox(per) def test_sub(self): per1 = Period("2011-01-01", freq="D") @@ -1274,7 +1284,6 @@ def test_add_offset_nat(self): # freq is DateOffset for freq in ["A", "2A", "3A"]: p = Period("NaT", freq=freq) - assert p is NaT for o in [offsets.YearEnd(2)]: assert p + o is NaT assert o + p is NaT @@ -1291,7 +1300,6 @@ def test_add_offset_nat(self): for freq in ["M", "2M", "3M"]: p = Period("NaT", freq=freq) - assert p is NaT for o in [offsets.MonthEnd(2), offsets.MonthEnd(12)]: assert p + o is NaT assert o + p is NaT @@ -1309,7 +1317,6 @@ def test_add_offset_nat(self): # freq is Tick for freq in ["D", "2D", "3D"]: p = Period("NaT", freq=freq) - assert p is NaT for o in [ offsets.Day(5), offsets.Hour(24), @@ -1333,7 +1340,6 @@ def test_add_offset_nat(self): for freq in ["H", "2H", "3H"]: p = Period("NaT", freq=freq) - assert p is NaT for o in [ offsets.Day(2), offsets.Hour(3), diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index f9fa80644d4b9..652dd34ca7ce2 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -202,6 +202,8 @@ def test_constructor(self): base_expected = 1404205200000000000 # confirm base representation is correct + import calendar + assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected tests = [ @@ -273,6 +275,8 @@ def test_constructor_with_stringoffset(self): base_expected = 1404205200000000000 # confirm base representation is correct + import calendar + assert calendar.timegm(base_dt.timetuple()) * 1000000000 == base_expected tests = [ diff --git a/pandas/tests/scalar/timestamp/test_timezones.py b/pandas/tests/scalar/timestamp/test_timezones.py index a56cdf0b98ee7..424b0c9abdef8 100644 --- a/pandas/tests/scalar/timestamp/test_timezones.py +++ b/pandas/tests/scalar/timestamp/test_timezones.py @@ -306,13 +306,15 @@ def test_astimezone(self, tzstr): @td.skip_if_windows def test_tz_convert_utc_with_system_utc(self): + from pandas._libs.tslibs.timezones import maybe_get_tz + # from system utc to real utc - ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC")) + ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc()) # from system utc to real utc - ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC")) + ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc()) From 42674a221f5884f0658381eaa027bba7a341c65c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 18:14:19 -0800 Subject: [PATCH 8/8] parametrize --- pandas/tests/arithmetic/test_datetime64.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 45f95bca02e27..4d3d6e2df35db 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -2151,16 +2151,16 @@ def test_dti_isub_tdi(self, tz_naive_fixture): ids=lambda x: type(x).__name__, ) @pytest.mark.parametrize("tz", [None, "US/Eastern"]) - def test_add_datetimelike_and_dti(self, addend, tz): + def test_add_datetimelike_and_dtarr(self, box_with_array, addend, tz): # GH#9631 dti = DatetimeIndex(["2011-01-01", "2011-01-02"]).tz_localize(tz) - msg = ( - "cannot add DatetimeArray and {0}".format(type(addend).__name__) - ).replace("DatetimeIndex", "DatetimeArray") + dtarr = tm.box_expected(dti, box_with_array) + msg = "cannot add DatetimeArray and" + with pytest.raises(TypeError, match=msg): - dti + addend + dtarr + addend with pytest.raises(TypeError, match=msg): - addend + dti + addend + dtarr # -------------------------------------------------------------