def test_infer_tz(eastern, localize): utc = pytz.utc start_naive = datetime(2001, 1, 1) end_naive = datetime(2009, 1, 1) start = localize(eastern, start_naive) end = localize(eastern, end_naive) assert (timezones.infer_tzinfo(start, end) is tslib._localize_pydatetime( start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(start, None) is tslib._localize_pydatetime( start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(None, end) is tslib._localize_pydatetime( end_naive, eastern).tzinfo) start = utc.localize(start_naive) end = utc.localize(end_naive) assert timezones.infer_tzinfo(start, end) is utc end = tslib._localize_pydatetime(end_naive, eastern) with pytest.raises(Exception): timezones.infer_tzinfo(start, end) with pytest.raises(Exception): timezones.infer_tzinfo(end, start)
def test_with_tz(self, tz): # just want it to work start = datetime(2011, 3, 12, tzinfo=pytz.utc) dr = bdate_range(start, periods=50, freq=pd.offsets.Hour()) 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) assert central.tz is tz naive = central[0].to_pydatetime().replace(tzinfo=None) comp = tslib._localize_pydatetime(naive, tz).tzinfo assert central[0].tz is comp # compare vs a localized tz naive = dr[0].to_pydatetime().replace(tzinfo=None) comp = tslib._localize_pydatetime(naive, tz).tzinfo assert central[0].tz is comp # datetimes with tzinfo set dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), datetime(2009, 1, 1, tzinfo=pytz.utc)) with pytest.raises(Exception): bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009', tz=tz)
def test_infer_tz(eastern, localize): utc = pytz.utc start_naive = datetime(2001, 1, 1) end_naive = datetime(2009, 1, 1) start = localize(eastern, start_naive) end = localize(eastern, end_naive) assert (timezones.infer_tzinfo(start, end) is tslib._localize_pydatetime(start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(start, None) is tslib._localize_pydatetime(start_naive, eastern).tzinfo) assert (timezones.infer_tzinfo(None, end) is tslib._localize_pydatetime(end_naive, eastern).tzinfo) start = utc.localize(start_naive) end = utc.localize(end_naive) assert timezones.infer_tzinfo(start, end) is utc end = tslib._localize_pydatetime(end_naive, eastern) with pytest.raises(Exception): timezones.infer_tzinfo(start, end) with pytest.raises(Exception): timezones.infer_tzinfo(end, start)
def test_dti_cmp_datetimelike(self, other, tz): dti = pd.date_range('2016-01-01', periods=2, tz=tz) if tz is not None: if isinstance(other, np.datetime64): # no tzaware version available return elif isinstance(other, Timestamp): other = other.tz_localize(dti.tzinfo) else: other = tslib._localize_pydatetime(other, dti.tzinfo) result = dti == other expected = np.array([True, False]) tm.assert_numpy_array_equal(result, expected) result = dti > other expected = np.array([False, True]) tm.assert_numpy_array_equal(result, expected) result = dti >= other expected = np.array([True, True]) tm.assert_numpy_array_equal(result, expected) result = dti < other expected = np.array([False, False]) tm.assert_numpy_array_equal(result, expected) result = dti <= other expected = np.array([True, False]) tm.assert_numpy_array_equal(result, expected)
def test_getitem_pydatetime_tz(self, tzstr): tz = timezones.maybe_get_tz(tzstr) index = date_range(start='2012-12-24 16:00', end='2012-12-24 18:00', freq='H', tz=tzstr) ts = Series(index=index, data=index.hour) time_pandas = Timestamp('2012-12-24 17:00', tz=tzstr) dt = datetime(2012, 12, 24, 17, 0) time_datetime = tslib._localize_pydatetime(dt, tz) assert ts[time_pandas] == ts[time_datetime]
def test_dti_convert_tz_aware_datetime_datetime(self, tz): # GH#1581 dates = [datetime(2000, 1, 1), datetime(2000, 1, 2), datetime(2000, 1, 3)] dates_aware = [tslib._localize_pydatetime(x, tz) for x in dates] result = DatetimeIndex(dates_aware) assert timezones.tz_compare(result.tz, tz) converted = to_datetime(dates_aware, utc=True) ex_vals = np.array([Timestamp(x).value for x in dates_aware]) tm.assert_numpy_array_equal(converted.asi8, ex_vals) assert converted.tz is pytz.utc
def test_replace_across_dst(self, tz, normalize): # GH#18319 check that 1) timezone is correctly normalized and # 2) that hour is not incorrectly changed by this normalization ts_naive = Timestamp('2017-12-03 16:03:30') ts_aware = tslib._localize_pydatetime(ts_naive, tz) # Preliminary sanity-check assert ts_aware == normalize(ts_aware) # Replace across DST boundary ts2 = ts_aware.replace(month=6) # Check that `replace` preserves hour literal assert (ts2.hour, ts2.minute) == (ts_aware.hour, ts_aware.minute) # Check that post-replace object is appropriately normalized ts2b = normalize(ts2) assert ts2 == ts2b