From 66be10c8b44ab18e3db2f748fee238d3c265b5f0 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 26 Aug 2019 17:37:54 -0700 Subject: [PATCH 1/2] BUG: fix+test Timestamp with int array --- pandas/_libs/tslibs/c_timestamp.pyx | 16 ++++++ .../tests/scalar/timestamp/test_arithmetic.py | 53 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/pandas/_libs/tslibs/c_timestamp.pyx b/pandas/_libs/tslibs/c_timestamp.pyx index 906dabba09486..70b7fd7f1e9ee 100644 --- a/pandas/_libs/tslibs/c_timestamp.pyx +++ b/pandas/_libs/tslibs/c_timestamp.pyx @@ -251,6 +251,14 @@ cdef class _Timestamp(datetime): result = result.normalize() return result + elif is_array(other): + if other.dtype.kind in ['i', 'u']: + maybe_integer_op_deprecated(self) + if self.freq is None: + raise ValueError("Cannot add integer-dtype array " + "to Timestamp without freq.") + return self.freq * other + self + # index/series like elif hasattr(other, '_typ'): return NotImplemented @@ -268,6 +276,14 @@ cdef class _Timestamp(datetime): neg_other = -other return self + neg_other + elif is_array(other): + if other.dtype.kind in ['i', 'u']: + maybe_integer_op_deprecated(self) + if self.freq is None: + raise ValueError("Cannot subtract integer-dtype array " + "from Timestamp without freq.") + return self - self.freq * other + typ = getattr(other, '_typ', None) # a Timestamp-DatetimeIndex -> yields a negative TimedeltaIndex diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index 58bd03129f2df..e349399fbc151 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -151,3 +151,56 @@ def test_timestamp_add_timedelta64_unit(self, other, expected_difference): result = ts + other valdiff = result.value - ts.value assert valdiff == expected_difference + + @pytest.mark.parametrize("ts", [Timestamp.now(), Timestamp.now("utc")]) + @pytest.mark.parametrize( + "other", + [ + 1, + np.int64(1), + np.array([1, 2], dtype=np.int32), + np.array([3, 4], dtype=np.uint64), + ], + ) + def test_add_int_no_freq(self, ts, other): + with pytest.raises(ValueError, match="without freq"): + ts + other + with pytest.raises(ValueError, match="without freq"): + other + ts + + with pytest.raises(ValueError, match="without freq"): + ts - other + with pytest.raises(TypeError): + other - ts + + @pytest.mark.parametrize( + "ts", + [ + Timestamp("1776-07-04", freq="D"), + Timestamp("1776-07-04", tz="UTC", freq="D"), + ], + ) + @pytest.mark.parametrize( + "other", + [ + 1, + np.int64(1), + np.array([1, 2], dtype=np.int32), + np.array([3, 4], dtype=np.uint64), + ], + ) + def test_add_int_with_freq(self, ts, other): + with tm.assert_produces_warning(FutureWarning): + result1 = ts + other + with tm.assert_produces_warning(FutureWarning): + result2 = other + ts + + assert np.all(result1 == result2) + + with tm.assert_produces_warning(FutureWarning): + result = result1 - other + + assert np.all(result == ts) + + with pytest.raises(TypeError): + other - ts From 3276aac874baae35d35be436930ee401ea808230 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 26 Aug 2019 18:11:10 -0700 Subject: [PATCH 2/2] add _raises to test name --- pandas/tests/scalar/timestamp/test_arithmetic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index e349399fbc151..2ef4fe79eeacf 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -162,7 +162,7 @@ def test_timestamp_add_timedelta64_unit(self, other, expected_difference): np.array([3, 4], dtype=np.uint64), ], ) - def test_add_int_no_freq(self, ts, other): + def test_add_int_no_freq_raises(self, ts, other): with pytest.raises(ValueError, match="without freq"): ts + other with pytest.raises(ValueError, match="without freq"):