From c8f457afd890cdc9f26c96a485f8e9ab1036aa00 Mon Sep 17 00:00:00 2001 From: jreback Date: Tue, 19 Aug 2014 10:15:47 -0400 Subject: [PATCH] BUG: Bug in Timestamp comparisons with == and dtype of int64 (GH8058) --- doc/source/v0.15.0.txt | 2 +- pandas/tseries/tests/test_timeseries.py | 29 +++++++++++++++++++++++++ pandas/tslib.pyx | 6 +++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 3839d475b29ad..509d1c4a9b327 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -465,7 +465,7 @@ Bug Fixes - Bug in ``combine_first`` with ``PeriodIndex`` data raises ``TypeError`` (:issue:`3367`) - Bug in multi-index slicing with missing indexers (:issue:`7866`) - Regression in multi-index indexing with a non-scalar type object (:issue:`7914`) - +- Bug in Timestamp comparisons with ``==`` and dtype of int64 (:issue:`8058`) - Bug in pickles contains ``DateOffset`` may raise ``AttributeError`` when ``normalize`` attribute is reffered internally (:issue:`7748`) - Bug in pickle deserialization that failed for pre-0.14.1 containers with dup items trying to avoid ambiguity diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index 3da97074a93fd..63db28ca53cf1 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -3407,6 +3407,35 @@ def test_comparison(self): self.assertTrue(other > val) self.assertTrue(other >= val) + def test_compare_invalid(self): + + # GH 8058 + val = Timestamp('20130101 12:01:02') + self.assertFalse(val == 'foo') + self.assertFalse(val == 10.0) + self.assertFalse(val == 1) + self.assertFalse(val == long(1)) + self.assertFalse(val == []) + self.assertFalse(val == {'foo' : 1}) + self.assertFalse(val == np.float64(1)) + self.assertFalse(val == np.int64(1)) + + self.assertTrue(val != 'foo') + self.assertTrue(val != 10.0) + self.assertTrue(val != 1) + self.assertTrue(val != long(1)) + self.assertTrue(val != []) + self.assertTrue(val != {'foo' : 1}) + self.assertTrue(val != np.float64(1)) + self.assertTrue(val != np.int64(1)) + + # ops testing + df = DataFrame(randn(5,2)) + a = df[0] + b = Series(randn(5)) + b.name = Timestamp('2000-01-01') + tm.assert_series_equal(a / b, 1 / (b / a)) + def test_cant_compare_tz_naive_w_aware(self): tm._skip_if_no_pytz() # #1404 diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index 5a2352508d42f..36c40f8ca39af 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -710,6 +710,12 @@ cdef class _Timestamp(datetime): if isinstance(other, np.datetime64): other = Timestamp(other) else: + if op == Py_EQ: + return False + elif op == Py_NE: + return True + + # only allow ==, != ops raise TypeError('Cannot compare type %r with type %r' % (type(self).__name__, type(other).__name__))