Skip to content

Commit a6fd608

Browse files
committed
BUG: raise exception in comparisons between tz-naive and tz-aware Timestamp objects, close #1404
1 parent b2b9388 commit a6fd608

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

pandas/src/datetime.pyx

+9-1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ cdef class _Timestamp(datetime):
341341
else:
342342
raise TypeError('Cannot compare Timestamp with %s' % str(other))
343343

344+
if self.tzinfo is None:
345+
if other.tzinfo is not None:
346+
raise Exception('Cannot compare tz-naive and tz-aware timestamps')
347+
elif other.tzinfo is None:
348+
raise Exception('Cannot compare tz-naive and tz-aware timestamps')
349+
344350
if op == 2: # ==
345351
return self.value == ots.value
346352
elif op == 3: # !=
@@ -464,14 +470,16 @@ cpdef convert_to_tsobject(object ts, object tz=None):
464470
if ts.tzinfo is not None:
465471
ts = tz.normalize(ts)
466472
obj.value = _pydatetime_to_dts(ts, &obj.dts)
473+
obj.tzinfo = ts.tzinfo
467474
elif tz is not pytz.utc:
468475
ts = tz.localize(ts)
469476
obj.value = _pydatetime_to_dts(ts, &obj.dts)
470477
obj.value -= _delta_to_nanoseconds(ts.tzinfo._utcoffset)
478+
obj.tzinfo = ts.tzinfo
471479
else:
472480
# UTC
473481
obj.value = _pydatetime_to_dts(ts, &obj.dts)
474-
obj.tzinfo = ts.tzinfo
482+
obj.tzinfo = tz
475483
else:
476484
obj.value = _pydatetime_to_dts(ts, &obj.dts)
477485
obj.tzinfo = ts.tzinfo

pandas/tseries/tests/test_timeseries.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -1494,22 +1494,28 @@ def test_comparison(self):
14941494
self.assert_(other > val)
14951495
self.assert_(other >= val)
14961496

1497+
def test_cant_compare_tz_naive_w_aware(self):
1498+
# #1404
1499+
a = Timestamp('3/12/2012')
1500+
b = Timestamp('3/12/2012', tz='utc')
1501+
1502+
self.assertRaises(Exception, a.__eq__, b)
1503+
self.assertRaises(Exception, a.__ne__, b)
1504+
self.assertRaises(Exception, a.__lt__, b)
1505+
self.assertRaises(Exception, a.__gt__, b)
1506+
self.assertRaises(Exception, b.__eq__, a)
1507+
self.assertRaises(Exception, b.__ne__, a)
1508+
self.assertRaises(Exception, b.__lt__, a)
1509+
self.assertRaises(Exception, b.__gt__, a)
1510+
1511+
self.assertRaises(Exception, a.__eq__, b.to_pydatetime())
1512+
self.assertRaises(Exception, a.to_pydatetime().__eq__, b)
1513+
14971514
def test_delta_preserve_nanos(self):
14981515
val = Timestamp(1337299200000000123L)
14991516
result = val + timedelta(1)
15001517
self.assert_(result.nanosecond == val.nanosecond)
15011518

1502-
def test_create_with_tz(self):
1503-
stamp = Timestamp('3/11/2012 05:00', tz='US/Eastern')
1504-
self.assertEquals(stamp.hour, 5)
1505-
1506-
rng = date_range('3/11/2012 04:00', periods=10, freq='H', tz='US/Eastern')
1507-
1508-
self.assertEquals(stamp, rng[1])
1509-
1510-
utc_stamp = Timestamp('3/11/2012 05:00', tz='utc')
1511-
self.assertEquals(utc_stamp.hour, 5)
1512-
15131519
def test_tz_convert_localize(self):
15141520
stamp = Timestamp('3/11/2012 04:00')
15151521

pandas/tseries/tests/test_timezones.py

+15
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ def test_tz_localize_dti(self):
100100
self.assertRaises(pytz.AmbiguousTimeError, dti.tz_localize,
101101
'US/Eastern')
102102

103+
def test_create_with_tz(self):
104+
stamp = Timestamp('3/11/2012 05:00', tz='US/Eastern')
105+
self.assertEquals(stamp.hour, 5)
106+
107+
rng = date_range('3/11/2012 04:00', periods=10, freq='H', tz='US/Eastern')
108+
109+
self.assertEquals(stamp, rng[1])
110+
111+
utc_stamp = Timestamp('3/11/2012 05:00', tz='utc')
112+
self.assert_(utc_stamp.tzinfo is pytz.utc)
113+
self.assertEquals(utc_stamp.hour, 5)
114+
115+
stamp = Timestamp('3/11/2012 05:00').tz_localize('utc')
116+
self.assertEquals(utc_stamp.hour, 5)
117+
103118
def test_date_range_localize(self):
104119
rng = date_range('3/11/2012 03:00', periods=15, freq='H', tz='US/Eastern')
105120
rng2 = DatetimeIndex(['3/11/2012 03:00', '3/11/2012 04:00'],

0 commit comments

Comments
 (0)