def _generate_regular_range(start, end, periods, offset): if com._count_not_none(start, end, periods) < 2: raise ValueError("Must specify two of start, end, or periods") if isinstance(offset, datetools.Tick): stride = offset.us_stride() if periods is None: b = Timestamp(start).value e = Timestamp(end).value e += stride - e % stride elif start is not None: b = Timestamp(start).value e = b + periods * stride elif end is not None: e = Timestamp(end).value + stride b = e - periods * stride else: raise NotImplementedError data = np.arange(b, e, stride, dtype=np.int64) data = data.view("M8[us]") else: xdr = datetools.generate_range(start=start, end=end, periods=periods, offset=offset) data = np.array(list(xdr), dtype="M8[us]") return data
def _cached_range(cls, start=None, end=None, periods=None, offset=None, name=None): if start is not None: start = Timestamp(start) if end is not None: end = Timestamp(end) if offset is None: raise Exception("Must provide a DateOffset!") drc = _daterange_cache if offset not in _daterange_cache: xdr = datetools.generate_range(offset=offset, start=_CACHE_START, end=_CACHE_END) arr = np.array(_to_m8_array(list(xdr)), dtype="M8[us]", copy=False) cachedRange = arr.view(DatetimeIndex) cachedRange.offset = offset cachedRange.tz = None cachedRange.name = None drc[offset] = cachedRange else: cachedRange = drc[offset] if start is None: if end is None: raise Exception("Must provide start or end date!") if periods is None: raise Exception("Must provide number of periods!") assert isinstance(end, Timestamp) end = offset.rollback(end) endLoc = cachedRange.get_loc(end) + 1 startLoc = endLoc - periods elif end is None: assert isinstance(start, Timestamp) start = offset.rollforward(start) startLoc = cachedRange.get_loc(start) if periods is None: raise Exception("Must provide number of periods!") endLoc = startLoc + periods else: if not offset.onOffset(start): start = offset.rollforward(start) if not offset.onOffset(end): end = offset.rollback(end) startLoc = cachedRange.get_loc(start) endLoc = cachedRange.get_loc(end) + 1 indexSlice = cachedRange[startLoc:endLoc] indexSlice.name = name indexSlice.offset = offset return indexSlice
def test_generate(self): rng1 = list(generate_range(START, END, offset=datetools.bday)) rng2 = list(generate_range(START, END, time_rule='B')) self.assert_(np.array_equal(rng1, rng2))
def eq_gen_range(kwargs, expected): rng = generate_range(**kwargs) assert(np.array_equal(list(rng), expected))