def test_with_tzinfo_ambiguous_times(self): _skip_if_no_pytz() tz = pytz.timezone('US/Eastern') # regular no problem self.assert_(self.rng.tz_validate()) # March 13, 2011, spring forward, skip from 2 AM to 3 AM dr = DateRange(datetime(2011, 3, 13, 1, 30), periods=3, offset=datetools.Hour(), tzinfo=tz) self.assert_(not dr.tz_validate()) # after dst transition dr = DateRange(datetime(2011, 3, 13, 3, 30), periods=3, offset=datetools.Hour(), tzinfo=tz) self.assert_(dr.tz_validate()) # November 6, 2011, fall back, repeat 2 AM hour dr = DateRange(datetime(2011, 11, 6, 1, 30), periods=3, offset=datetools.Hour(), tzinfo=tz) self.assert_(not dr.tz_validate()) # UTC is OK dr = DateRange(datetime(2011, 3, 13), periods=48, offset=datetools.Minute(30), tzinfo=pytz.utc) self.assert_(dr.tz_validate())
def test_with_tz_ambiguous_times(self): tz = pytz.timezone('US/Eastern') rng = bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1)) # March 13, 2011, spring forward, skip from 2 AM to 3 AM dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3, freq=datetools.Hour()) self.assertRaises(pytz.NonExistentTimeError, dr.tz_localize, tz) # after dst transition, it works dr = date_range(datetime(2011, 3, 13, 3, 30), periods=3, freq=datetools.Hour(), tz=tz) # November 6, 2011, fall back, repeat 2 AM hour dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3, freq=datetools.Hour()) self.assertRaises(pytz.AmbiguousTimeError, dr.tz_localize, tz) # UTC is OK dr = date_range(datetime(2011, 3, 13), periods=48, freq=datetools.Minute(30), tz=pytz.utc)
def test_infer_dst(self): # November 6, 2011, fall back, repeat 2 AM hour # With no repeated hours, we cannot infer the transition tz = self.tz('US/Eastern') dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=datetools.Hour()) self.assertRaises(pytz.AmbiguousTimeError, dr.tz_localize, tz, infer_dst=True) # With repeated hours, we can infer the transition dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=datetools.Hour(), tz=tz) di = DatetimeIndex([ '11/06/2011 00:00', '11/06/2011 01:00', '11/06/2011 01:00', '11/06/2011 02:00', '11/06/2011 03:00' ]) localized = di.tz_localize(tz, infer_dst=True) self.assert_numpy_array_equal(dr, localized) # When there is no dst transition, nothing special happens dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=datetools.Hour()) localized = dr.tz_localize(tz) localized_infer = dr.tz_localize(tz, infer_dst=True) self.assert_numpy_array_equal(localized, localized_infer)
def test_ambiguous_flags(self): # November 6, 2011, fall back, repeat 2 AM hour tz = self.tz('US/Eastern') # Pass in flags to determine right dst transition dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=datetools.Hour(), tz=tz) times = ['11/06/2011 00:00', '11/06/2011 01:00', '11/06/2011 01:00', '11/06/2011 02:00', '11/06/2011 03:00'] # Test tz_localize di = DatetimeIndex(times) is_dst = [1, 1, 0, 0, 0] localized = di.tz_localize(tz, ambiguous=is_dst) self.assert_numpy_array_equal(dr, localized) self.assert_numpy_array_equal(dr, DatetimeIndex(times, tz=tz, ambiguous=is_dst)) localized = di.tz_localize(tz, ambiguous=np.array(is_dst)) self.assert_numpy_array_equal(dr, localized) localized = di.tz_localize(tz, ambiguous=np.array(is_dst).astype('bool')) self.assert_numpy_array_equal(dr, localized) # Test constructor localized = DatetimeIndex(times, tz=tz, ambiguous=is_dst) self.assert_numpy_array_equal(dr, localized) # Test duplicate times where infer_dst fails times += times di = DatetimeIndex(times) # When the sizes are incompatible, make sure error is raised self.assertRaises(Exception, di.tz_localize, tz, ambiguous=is_dst) # When sizes are compatible and there are repeats ('infer' won't work) is_dst = np.hstack((is_dst, is_dst)) localized = di.tz_localize(tz, ambiguous=is_dst) dr = dr.append(dr) self.assert_numpy_array_equal(dr, localized) # When there is no dst transition, nothing special happens dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=datetools.Hour()) is_dst = np.array([1] * 10) localized = dr.tz_localize(tz) localized_is_dst = dr.tz_localize(tz, ambiguous=is_dst) self.assert_numpy_array_equal(localized, localized_is_dst)
def test_with_tz(self): tz = pytz.timezone('US/Central') # just want it to work start = datetime(2011, 3, 12, tzinfo=pytz.utc) dr = bdate_range(start, periods=50, freq=datetools.Hour()) self.assert_(dr.tz is pytz.utc) # DateRange with naive datetimes dr = bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc) dr = bdate_range('1/1/2005', '1/1/2009', tz=tz) # normalized central = dr.tz_convert(tz) self.assert_(central.tz is tz) self.assert_(central[0].tz is tz) # datetimes with tzinfo set dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009', tz=pytz.utc) self.assertRaises(Exception, bdate_range, datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009', tz=tz)
def test_with_tzinfo(self): _skip_if_no_pytz() tz = pytz.timezone('US/Central') # just want it to work start = datetime(2011, 3, 12, tzinfo=pytz.utc) dr = DateRange(start, periods=50, offset=datetools.Hour()) self.assert_(dr.tzinfo is not None) self.assert_(dr.tzinfo is start.tzinfo) # DateRange with naive datetimes dr = DateRange('1/1/2005', '1/1/2009', tzinfo=pytz.utc) dr = DateRange('1/1/2005', '1/1/2009', tzinfo=tz) # normalized central = dr.tz_normalize(tz) self.assert_(central.tzinfo is tz) self.assert_(central[0].tzinfo is tz) # datetimes with tzinfo set dr = DateRange(datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009', tzinfo=pytz.utc) self.assertRaises(Exception, DateRange, datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009', tzinfo=tz)
def test_with_tz(self): tz = self.tz('US/Central') # just want it to work start = datetime(2011, 3, 12, tzinfo=pytz.utc) dr = bdate_range(start, periods=50, freq=datetools.Hour()) self.assertIs(dr.tz, pytz.utc) # DateRange with naive datetimes dr = bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc) dr = bdate_range('1/1/2005', '1/1/2009', tz=tz) # normalized central = dr.tz_convert(tz) self.assertIs(central.tz, tz) comp = self.localize( tz, central[0].to_pydatetime().replace(tzinfo=None)).tzinfo self.assertIs(central[0].tz, comp) # compare vs a localized tz comp = self.localize(tz, dr[0].to_pydatetime().replace(tzinfo=None)).tzinfo self.assertIs(central[0].tz, comp) # datetimes with tzinfo set dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009', tz=pytz.utc) self.assertRaises(Exception, bdate_range, datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009', tz=tz)
start = start if start is None else start.replace(tzinfo=None) end = end if end is None else end.replace(tzinfo=None) return start, end, tz def _infer_tzinfo(start, end): def _infer(a, b): tz = a.tzinfo if b and b.tzinfo: assert(tz == b.tzinfo) return tz tz = None if start is not None: tz = _infer(start, end) elif end is not None: tz = _infer(end, start) return tz def _will_use_cache(offset): return (offset.isAnchored() and isinstance(offset, datetools.CacheableOffset)) if __name__ == '__main__': import pytz # just want it to work tz = pytz.timezone('US/Eastern') dr = DateRange(datetime(2011, 3, 12, tzinfo=pytz.utc), periods=50, offset=datetools.Hour()) dr2 = dr.tz_normalize(tz)
def _infer_tzinfo(start, end): def _infer(a, b): tz = a.tzinfo if b and b.tzinfo: assert (tz == b.tzinfo) return tz tz = None if start is not None: tz = _infer(start, end) elif end is not None: tz = _infer(end, start) return tz def _will_use_cache(offset): return (offset.isAnchored() and isinstance(offset, datetools.CacheableOffset)) if __name__ == '__main__': import pytz # just want it to work tz = pytz.timezone('US/Eastern') dr = DateRange(datetime(2011, 3, 12, tzinfo=pytz.utc), periods=50, offset=datetools.Hour()) dr2 = dr.tz_normalize(tz)