|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import sys |
| 3 | +from datetime import datetime |
| 4 | +import operator |
| 5 | + |
| 6 | +import pytest |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from dateutil.tz import tzutc |
| 10 | +from pytz import utc |
| 11 | + |
| 12 | +from pandas.compat import long |
| 13 | +from pandas import Timestamp |
| 14 | + |
| 15 | + |
| 16 | +class TestTimestampComparison(object): |
| 17 | + def test_comparison_object_array(self): |
| 18 | + # GH#15183 |
| 19 | + ts = Timestamp('2011-01-03 00:00:00-0500', tz='US/Eastern') |
| 20 | + other = Timestamp('2011-01-01 00:00:00-0500', tz='US/Eastern') |
| 21 | + naive = Timestamp('2011-01-01 00:00:00') |
| 22 | + |
| 23 | + arr = np.array([other, ts], dtype=object) |
| 24 | + res = arr == ts |
| 25 | + expected = np.array([False, True], dtype=bool) |
| 26 | + assert (res == expected).all() |
| 27 | + |
| 28 | + # 2D case |
| 29 | + arr = np.array([[other, ts], |
| 30 | + [ts, other]], |
| 31 | + dtype=object) |
| 32 | + res = arr != ts |
| 33 | + expected = np.array([[True, False], [False, True]], dtype=bool) |
| 34 | + assert res.shape == expected.shape |
| 35 | + assert (res == expected).all() |
| 36 | + |
| 37 | + # tzaware mismatch |
| 38 | + arr = np.array([naive], dtype=object) |
| 39 | + with pytest.raises(TypeError): |
| 40 | + arr < ts |
| 41 | + |
| 42 | + def test_comparison(self): |
| 43 | + # 5-18-2012 00:00:00.000 |
| 44 | + stamp = long(1337299200000000000) |
| 45 | + |
| 46 | + val = Timestamp(stamp) |
| 47 | + |
| 48 | + assert val == val |
| 49 | + assert not val != val |
| 50 | + assert not val < val |
| 51 | + assert val <= val |
| 52 | + assert not val > val |
| 53 | + assert val >= val |
| 54 | + |
| 55 | + other = datetime(2012, 5, 18) |
| 56 | + assert val == other |
| 57 | + assert not val != other |
| 58 | + assert not val < other |
| 59 | + assert val <= other |
| 60 | + assert not val > other |
| 61 | + assert val >= other |
| 62 | + |
| 63 | + other = Timestamp(stamp + 100) |
| 64 | + |
| 65 | + assert val != other |
| 66 | + assert val != other |
| 67 | + assert val < other |
| 68 | + assert val <= other |
| 69 | + assert other > val |
| 70 | + assert other >= val |
| 71 | + |
| 72 | + def test_compare_invalid(self): |
| 73 | + # GH 8058 |
| 74 | + val = Timestamp('20130101 12:01:02') |
| 75 | + assert not val == 'foo' |
| 76 | + assert not val == 10.0 |
| 77 | + assert not val == 1 |
| 78 | + assert not val == long(1) |
| 79 | + assert not val == [] |
| 80 | + assert not val == {'foo': 1} |
| 81 | + assert not val == np.float64(1) |
| 82 | + assert not val == np.int64(1) |
| 83 | + |
| 84 | + assert val != 'foo' |
| 85 | + assert val != 10.0 |
| 86 | + assert val != 1 |
| 87 | + assert val != long(1) |
| 88 | + assert val != [] |
| 89 | + assert val != {'foo': 1} |
| 90 | + assert val != np.float64(1) |
| 91 | + assert val != np.int64(1) |
| 92 | + |
| 93 | + def test_cant_compare_tz_naive_w_aware(self): |
| 94 | + # see gh-1404 |
| 95 | + a = Timestamp('3/12/2012') |
| 96 | + b = Timestamp('3/12/2012', tz='utc') |
| 97 | + |
| 98 | + pytest.raises(Exception, a.__eq__, b) |
| 99 | + pytest.raises(Exception, a.__ne__, b) |
| 100 | + pytest.raises(Exception, a.__lt__, b) |
| 101 | + pytest.raises(Exception, a.__gt__, b) |
| 102 | + pytest.raises(Exception, b.__eq__, a) |
| 103 | + pytest.raises(Exception, b.__ne__, a) |
| 104 | + pytest.raises(Exception, b.__lt__, a) |
| 105 | + pytest.raises(Exception, b.__gt__, a) |
| 106 | + |
| 107 | + if sys.version_info < (3, 3): |
| 108 | + pytest.raises(Exception, a.__eq__, b.to_pydatetime()) |
| 109 | + pytest.raises(Exception, a.to_pydatetime().__eq__, b) |
| 110 | + else: |
| 111 | + assert not a == b.to_pydatetime() |
| 112 | + assert not a.to_pydatetime() == b |
| 113 | + |
| 114 | + def test_cant_compare_tz_naive_w_aware_explicit_pytz(self): |
| 115 | + # see gh-1404 |
| 116 | + a = Timestamp('3/12/2012') |
| 117 | + b = Timestamp('3/12/2012', tz=utc) |
| 118 | + |
| 119 | + pytest.raises(Exception, a.__eq__, b) |
| 120 | + pytest.raises(Exception, a.__ne__, b) |
| 121 | + pytest.raises(Exception, a.__lt__, b) |
| 122 | + pytest.raises(Exception, a.__gt__, b) |
| 123 | + pytest.raises(Exception, b.__eq__, a) |
| 124 | + pytest.raises(Exception, b.__ne__, a) |
| 125 | + pytest.raises(Exception, b.__lt__, a) |
| 126 | + pytest.raises(Exception, b.__gt__, a) |
| 127 | + |
| 128 | + if sys.version_info < (3, 3): |
| 129 | + pytest.raises(Exception, a.__eq__, b.to_pydatetime()) |
| 130 | + pytest.raises(Exception, a.to_pydatetime().__eq__, b) |
| 131 | + else: |
| 132 | + assert not a == b.to_pydatetime() |
| 133 | + assert not a.to_pydatetime() == b |
| 134 | + |
| 135 | + def test_cant_compare_tz_naive_w_aware_dateutil(self): |
| 136 | + # see gh-1404 |
| 137 | + a = Timestamp('3/12/2012') |
| 138 | + b = Timestamp('3/12/2012', tz=tzutc()) |
| 139 | + |
| 140 | + pytest.raises(Exception, a.__eq__, b) |
| 141 | + pytest.raises(Exception, a.__ne__, b) |
| 142 | + pytest.raises(Exception, a.__lt__, b) |
| 143 | + pytest.raises(Exception, a.__gt__, b) |
| 144 | + pytest.raises(Exception, b.__eq__, a) |
| 145 | + pytest.raises(Exception, b.__ne__, a) |
| 146 | + pytest.raises(Exception, b.__lt__, a) |
| 147 | + pytest.raises(Exception, b.__gt__, a) |
| 148 | + |
| 149 | + if sys.version_info < (3, 3): |
| 150 | + pytest.raises(Exception, a.__eq__, b.to_pydatetime()) |
| 151 | + pytest.raises(Exception, a.to_pydatetime().__eq__, b) |
| 152 | + else: |
| 153 | + assert not a == b.to_pydatetime() |
| 154 | + assert not a.to_pydatetime() == b |
| 155 | + |
| 156 | + def test_timestamp_compare_scalars(self): |
| 157 | + # case where ndim == 0 |
| 158 | + lhs = np.datetime64(datetime(2013, 12, 6)) |
| 159 | + rhs = Timestamp('now') |
| 160 | + nat = Timestamp('nat') |
| 161 | + |
| 162 | + ops = {'gt': 'lt', |
| 163 | + 'lt': 'gt', |
| 164 | + 'ge': 'le', |
| 165 | + 'le': 'ge', |
| 166 | + 'eq': 'eq', |
| 167 | + 'ne': 'ne'} |
| 168 | + |
| 169 | + for left, right in ops.items(): |
| 170 | + left_f = getattr(operator, left) |
| 171 | + right_f = getattr(operator, right) |
| 172 | + expected = left_f(lhs, rhs) |
| 173 | + |
| 174 | + result = right_f(rhs, lhs) |
| 175 | + assert result == expected |
| 176 | + |
| 177 | + expected = left_f(rhs, nat) |
| 178 | + result = right_f(nat, rhs) |
| 179 | + assert result == expected |
| 180 | + |
| 181 | + def test_timestamp_compare_with_early_datetime(self): |
| 182 | + # e.g. datetime.min |
| 183 | + stamp = Timestamp('2012-01-01') |
| 184 | + |
| 185 | + assert not stamp == datetime.min |
| 186 | + assert not stamp == datetime(1600, 1, 1) |
| 187 | + assert not stamp == datetime(2700, 1, 1) |
| 188 | + assert stamp != datetime.min |
| 189 | + assert stamp != datetime(1600, 1, 1) |
| 190 | + assert stamp != datetime(2700, 1, 1) |
| 191 | + assert stamp > datetime(1600, 1, 1) |
| 192 | + assert stamp >= datetime(1600, 1, 1) |
| 193 | + assert stamp < datetime(2700, 1, 1) |
| 194 | + assert stamp <= datetime(2700, 1, 1) |
0 commit comments