def test_tslib_tz_convert(self): def compare_utc_to_local(tz_didx, utc_didx): f = lambda x: tslib.tz_convert_single(x, 'UTC', tz_didx.tz) result = tslib.tz_convert(tz_didx.asi8, 'UTC', tz_didx.tz) result_single = np.vectorize(f)(tz_didx.asi8) self.assert_numpy_array_equal(result, result_single) def compare_local_to_utc(tz_didx, utc_didx): f = lambda x: tslib.tz_convert_single(x, tz_didx.tz, 'UTC') result = tslib.tz_convert(utc_didx.asi8, tz_didx.tz, 'UTC') result_single = np.vectorize(f)(utc_didx.asi8) self.assert_numpy_array_equal(result, result_single) for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern', 'Europe/Moscow']: # US: 2014-03-09 - 2014-11-11 # MOSCOW: 2014-10-26 / 2014-12-31 tz_didx = date_range('2014-03-01', '2015-01-10', freq='H', tz=tz) utc_didx = date_range('2014-03-01', '2015-01-10', freq='H') compare_utc_to_local(tz_didx, utc_didx) # local tz to UTC can be differ in hourly (or higher) freqs because # of DST compare_local_to_utc(tz_didx, utc_didx) tz_didx = date_range('2000-01-01', '2020-01-01', freq='D', tz=tz) utc_didx = date_range('2000-01-01', '2020-01-01', freq='D') compare_utc_to_local(tz_didx, utc_didx) compare_local_to_utc(tz_didx, utc_didx) tz_didx = date_range('2000-01-01', '2100-01-01', freq='A', tz=tz) utc_didx = date_range('2000-01-01', '2100-01-01', freq='A') compare_utc_to_local(tz_didx, utc_didx) compare_local_to_utc(tz_didx, utc_didx) # Check empty array result = tslib.tz_convert(np.array([], dtype=np.int64), tslib.maybe_get_tz('US/Eastern'), tslib.maybe_get_tz('Asia/Tokyo')) self.assert_numpy_array_equal(result, np.array([], dtype=np.int64)) # Check all-NaT array result = tslib.tz_convert(np.array([tslib.iNaT], dtype=np.int64), tslib.maybe_get_tz('US/Eastern'), tslib.maybe_get_tz('Asia/Tokyo')) self.assert_numpy_array_equal(result, np.array([tslib.iNaT], dtype=np.int64))
def test_tslib_tz_convert(self): def compare_utc_to_local(tz_didx, utc_didx): f = lambda x: tslib.tz_convert_single(x, 'UTC', tz_didx.tz) result = tslib.tz_convert(tz_didx.asi8, 'UTC', tz_didx.tz) result_single = np.vectorize(f)(tz_didx.asi8) self.assert_numpy_array_equal(result, result_single) def compare_local_to_utc(tz_didx, utc_didx): f = lambda x: tslib.tz_convert_single(x, tz_didx.tz, 'UTC') result = tslib.tz_convert(utc_didx.asi8, tz_didx.tz, 'UTC') result_single = np.vectorize(f)(utc_didx.asi8) self.assert_numpy_array_equal(result, result_single) for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern', 'Europe/Moscow']: # US: 2014-03-09 - 2014-11-11 # MOSCOW: 2014-10-26 / 2014-12-31 tz_didx = date_range('2014-03-01', '2015-01-10', freq='H', tz=tz) utc_didx = date_range('2014-03-01', '2015-01-10', freq='H') compare_utc_to_local(tz_didx, utc_didx) # local tz to UTC can be differ in hourly (or higher) freqs because # of DST compare_local_to_utc(tz_didx, utc_didx) tz_didx = date_range('2000-01-01', '2020-01-01', freq='D', tz=tz) utc_didx = date_range('2000-01-01', '2020-01-01', freq='D') compare_utc_to_local(tz_didx, utc_didx) compare_local_to_utc(tz_didx, utc_didx) tz_didx = date_range('2000-01-01', '2100-01-01', freq='A', tz=tz) utc_didx = date_range('2000-01-01', '2100-01-01', freq='A') compare_utc_to_local(tz_didx, utc_didx) compare_local_to_utc(tz_didx, utc_didx) # Check empty array result = tslib.tz_convert(np.array([], dtype=np.int64), tslib.maybe_get_tz('US/Eastern'), tslib.maybe_get_tz('Asia/Tokyo')) self.assert_numpy_array_equal(result, np.array([], dtype=np.int64)) # Check all-NaT array result = tslib.tz_convert(np.array([tslib.iNaT], dtype=np.int64), tslib.maybe_get_tz('US/Eastern'), tslib.maybe_get_tz('Asia/Tokyo')) self.assert_numpy_array_equal(result, np.array( [tslib.iNaT], dtype=np.int64))
def test_tslib_tz_convert(self): def compare_utc_to_local(tz_didx, utc_didx): f = lambda x: tslib.tz_convert_single(x, "UTC", tz_didx.tz) result = tslib.tz_convert(tz_didx.asi8, "UTC", tz_didx.tz) result_single = np.vectorize(f)(tz_didx.asi8) self.assert_numpy_array_equal(result, result_single) def compare_local_to_utc(tz_didx, utc_didx): f = lambda x: tslib.tz_convert_single(x, tz_didx.tz, "UTC") result = tslib.tz_convert(utc_didx.asi8, tz_didx.tz, "UTC") result_single = np.vectorize(f)(utc_didx.asi8) self.assert_numpy_array_equal(result, result_single) for tz in ["UTC", "Asia/Tokyo", "US/Eastern", "Europe/Moscow"]: # US: 2014-03-09 - 2014-11-11 # MOSCOW: 2014-10-26 / 2014-12-31 tz_didx = date_range("2014-03-01", "2015-01-10", freq="H", tz=tz) utc_didx = date_range("2014-03-01", "2015-01-10", freq="H") compare_utc_to_local(tz_didx, utc_didx) # local tz to UTC can be differ in hourly (or higher) freqs because # of DST compare_local_to_utc(tz_didx, utc_didx) tz_didx = date_range("2000-01-01", "2020-01-01", freq="D", tz=tz) utc_didx = date_range("2000-01-01", "2020-01-01", freq="D") compare_utc_to_local(tz_didx, utc_didx) compare_local_to_utc(tz_didx, utc_didx) tz_didx = date_range("2000-01-01", "2100-01-01", freq="A", tz=tz) utc_didx = date_range("2000-01-01", "2100-01-01", freq="A") compare_utc_to_local(tz_didx, utc_didx) compare_local_to_utc(tz_didx, utc_didx) # Check empty array result = tslib.tz_convert( np.array([], dtype=np.int64), tslib.maybe_get_tz("US/Eastern"), tslib.maybe_get_tz("Asia/Tokyo") ) self.assert_numpy_array_equal(result, np.array([], dtype=np.int64)) # Check all-NaT array result = tslib.tz_convert( np.array([tslib.iNaT], dtype=np.int64), tslib.maybe_get_tz("US/Eastern"), tslib.maybe_get_tz("Asia/Tokyo") ) self.assert_numpy_array_equal(result, np.array([tslib.iNaT], dtype=np.int64))
def _tz_convert_with_transitions(values, to_tz, from_tz): """ convert i8 values from the specificed timezone to the to_tz zone, taking into account DST transitions """ # vectorization is slow, so tests if we can do this via the faster tz_convert f = lambda x: tslib.tz_convert_single(x, to_tz, from_tz) if len(values) > 2: first_slow, last_slow = f(values[0]),f(values[-1]) first_fast, last_fast = tslib.tz_convert(np.array([values[0],values[-1]],dtype='i8'),to_tz,from_tz) # don't cross a DST, so ok if first_fast == first_slow and last_fast == last_slow: return tslib.tz_convert(values,to_tz,from_tz) return np.vectorize(f)(values)
def _tz_convert_with_transitions(values, to_tz, from_tz): """ convert i8 values from the specificed timezone to the to_tz zone, taking into account DST transitions """ # vectorization is slow, so tests if we can do this via the faster tz_convert f = lambda x: tslib.tz_convert_single(x, to_tz, from_tz) if len(values) > 2: first_slow, last_slow = f(values[0]), f(values[-1]) first_fast, last_fast = tslib.tz_convert( np.array([values[0], values[-1]], dtype='i8'), to_tz, from_tz) # don't cross a DST, so ok if first_fast == first_slow and last_fast == last_slow: return tslib.tz_convert(values, to_tz, from_tz) return np.vectorize(f)(values)
def __init__(self, index, warn=True): self.index = index self.values = np.asarray(index).view('i8') if index.tz is not None: self.values = tslib.tz_convert(self.values, 'UTC', index.tz) self.warn = warn if len(index) < 3: raise ValueError('Need at least 3 dates to infer frequency') self.is_monotonic = self.index.is_monotonic
def __init__(self, index, warn=True): self.index = index self.values = np.asarray(index).view('i8') # This moves the values, which are implicitly in UTC, to the # the timezone so they are in local time if hasattr(index,'tz'): if index.tz is not None: self.values = tslib.tz_convert(self.values, 'UTC', index.tz) self.warn = warn if len(index) < 3: raise ValueError('Need at least 3 dates to infer frequency') self.is_monotonic = self.index.is_monotonic
def __init__(self, index, warn=True): self.index = index self.values = np.asarray(index).view('i8') # This moves the values, which are implicitly in UTC, to the # the timezone so they are in local time if hasattr(index, 'tz'): if index.tz is not None: self.values = tslib.tz_convert(self.values, 'UTC', index.tz) self.warn = warn if len(index) < 3: raise ValueError('Need at least 3 dates to infer frequency') self.is_monotonic = self.index.is_monotonic
def __init__(self, index, warn=True): self.index = index self.values = np.asarray(index).view("i8") # This moves the values, which are implicitly in UTC, to the # the timezone so they are in local time if hasattr(index, "tz"): if index.tz is not None: self.values = tslib.tz_convert(self.values, "UTC", index.tz) self.warn = warn if len(index) < 3: raise ValueError("Need at least 3 dates to infer frequency") self.is_monotonic = self.index.is_monotonic_increasing or self.index.is_monotonic_decreasing
def compare_local_to_utc(tz_didx, utc_didx): f = lambda x: tslib.tz_convert_single(x, tz_didx.tz, 'UTC') result = tslib.tz_convert(utc_didx.asi8, tz_didx.tz, 'UTC') result_single = np.vectorize(f)(utc_didx.asi8) self.assert_numpy_array_equal(result, result_single)