Exemplo n.º 1
0
    def shift(self, periods, offset=None, timeRule=None):
        """
        Analogous to DataFrame.shift
        """
        if timeRule is not None and offset is None:
            offset = datetools.getOffset(timeRule)

        new_series = {}
        if offset is None:
            new_index = self.index
            for col, s in self.iteritems():
                new_series[col] = s.shift(periods)
        else:
            new_index = self.index.shift(periods, offset)
            for col, s in self.iteritems():
                new_series[col] = SparseSeries(
                    s.sp_values, index=new_index, sparse_index=s.sp_index, fill_value=s.fill_value
                )

        return SparseDataFrame(
            new_series,
            index=new_index,
            columns=self.columns,
            default_fill_value=self.default_fill_value,
            default_kind=self.default_kind,
        )
Exemplo n.º 2
0
    def __init__(self, start=None, end=None, nPeriods=None,
                 offset=datetools.BDay(), timeRule=None):

        if timeRule is not None:
            offset = datetools.getOffset(timeRule)

        if timeRule is None:
            if offset in datetools._offsetNames:
                timeRule = datetools._offsetNames[offset]

        start = datetools.to_datetime(start)
        end = datetools.to_datetime(end)

        if start and not offset.onOffset(start):
            start = start + offset.__class__(n=1, **offset.kwds)
        if end and not offset.onOffset(end):
            end = end - offset.__class__(n=1, **offset.kwds)
            if nPeriods == None and end < start:
                end = None
                nPeriods = 0

        if end is None:
            end = start + (nPeriods - 1) * offset

        if start is None:
            start = end - (nPeriods - 1) * offset

        self.offset = offset
        self.timeRule = timeRule
        self.start = start
        self.end = end
        self.nPeriods = nPeriods
Exemplo n.º 3
0
    def shift(self, periods, offset=None, timeRule=None):
        """
        Analogous to DataFrame.shift
        """
        if timeRule is not None and offset is None:
            offset = datetools.getOffset(timeRule)

        new_series = {}
        if offset is None:
            new_index = self.index
            for col, s in self.iteritems():
                new_series[col] = s.shift(periods)
        else:
            new_index = self.index.shift(periods, offset)
            for col, s in self.iteritems():
                new_series[col] = SparseSeries(s.sp_values,
                                               index=new_index,
                                               sparse_index=s.sp_index,
                                               fill_value=s.fill_value)

        return SparseDataFrame(new_series,
                               index=new_index,
                               columns=self.columns,
                               default_fill_value=self.default_fill_value,
                               default_kind=self.default_kind)
Exemplo n.º 4
0
    def __new__(cls,
                start=None,
                end=None,
                periods=None,
                offset=datetools.bday,
                timeRule=None,
                **kwds):

        # Allow us to circumvent hitting the cache
        index = kwds.get('index')
        if index is None:
            if timeRule is not None:
                offset = datetools.getOffset(timeRule)

            if timeRule is None:
                if offset in datetools._offsetNames:
                    timeRule = datetools._offsetNames[offset]

            # Cachable
            if not start:
                start = kwds.get('begin')
            if not end:
                end = kwds.get('end')
            if not periods:
                periods = kwds.get('nPeriods')

            start = datetools.to_datetime(start)
            end = datetools.to_datetime(end)

            # inside cache range
            fromInside = start is not None and start > CACHE_START
            toInside = end is not None and end < CACHE_END

            useCache = fromInside and toInside

            if (useCache and offset.isAnchored()
                    and not isinstance(offset, datetools.Tick)):

                index = cls.getCachedRange(start,
                                           end,
                                           periods=periods,
                                           offset=offset,
                                           timeRule=timeRule)

            else:
                xdr = XDateRange(start=start,
                                 end=end,
                                 nPeriods=periods,
                                 offset=offset,
                                 timeRule=timeRule)

                index = np.array(list(xdr), dtype=object, copy=False)

                index = index.view(cls)
                index.offset = offset
        else:
            index = index.view(cls)

        return index
Exemplo n.º 5
0
    def _cached_range(cls,
                      start=None,
                      end=None,
                      periods=None,
                      offset=None,
                      time_rule=None,
                      name=None):

        # HACK: fix this dependency later
        if time_rule is not None:
            offset = datetools.getOffset(time_rule)

        if offset is None:
            raise Exception('Must provide a DateOffset!')

        if offset not in _daterange_cache:
            xdr = generate_range(_CACHE_START, _CACHE_END, offset=offset)
            arr = np.array(list(xdr), dtype=object, copy=False)

            cachedRange = arr.view(DateRange)
            cachedRange.offset = offset
            cachedRange.tzinfo = None
            cachedRange.name = None
            _daterange_cache[offset] = cachedRange
        else:
            cachedRange = _daterange_cache[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, datetime))

            end = offset.rollback(end)

            endLoc = cachedRange.get_loc(end) + 1
            startLoc = endLoc - periods
        elif end is None:
            assert (isinstance(start, datetime))
            start = offset.rollforward(start)

            startLoc = cachedRange.get_loc(start)
            if periods is None:
                raise Exception('Must provide number of periods!')

            endLoc = startLoc + periods
        else:
            start = offset.rollforward(start)
            end = offset.rollback(end)

            startLoc = cachedRange.get_loc(start)
            endLoc = cachedRange.get_loc(end) + 1

        indexSlice = cachedRange[startLoc:endLoc]
        indexSlice.name = name
        return indexSlice
Exemplo n.º 6
0
    def getCachedRange(cls,
                       start=None,
                       end=None,
                       periods=None,
                       offset=None,
                       timeRule=None):

        # HACK: fix this dependency later
        if timeRule is not None:
            offset = datetools.getOffset(timeRule)

        if offset is None:
            raise Exception('Must provide a DateOffset!')

        if offset not in cls._cache:
            xdr = XDateRange(CACHE_START, CACHE_END, offset=offset)
            arr = np.array(list(xdr), dtype=object, copy=False)

            cachedRange = DateRange.fromIndex(arr)
            cachedRange.offset = offset

            cls._cache[offset] = cachedRange
        else:
            cachedRange = cls._cache[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, datetime))

            end = offset.rollback(end)

            endLoc = cachedRange.indexMap[end] + 1
            startLoc = endLoc - periods
        elif end is None:
            assert (isinstance(start, datetime))
            start = offset.rollforward(start)

            startLoc = cachedRange.indexMap[start]
            if periods is None:
                raise Exception('Must provide number of periods!')

            endLoc = startLoc + periods
        else:
            start = offset.rollforward(start)
            end = offset.rollback(end)

            startLoc = cachedRange.indexMap[start]
            endLoc = cachedRange.indexMap[end] + 1

        indexSlice = cachedRange[startLoc:endLoc]
        indexSlice._parent = cachedRange

        return indexSlice
Exemplo n.º 7
0
    def shift(self, periods, offset=None, timeRule=None):
        """
        Shift the underlying series of the DataMatrix and Series objects within
        by given number (positive or negative) of periods.

        Parameters
        ----------
        periods : int (+ or -)
            Number of periods to move
        offset : DateOffset, optional
            Increment to use from datetools module
        timeRule : string
            Time rule to use by name

        Returns
        -------
        DataMatrix
        """
        if periods == 0:
            return self

        if timeRule is not None and offset is None:
            offset = datetools.getOffset(timeRule)

        N = len(self)

        if offset is None:
            newIndex = self.index
            indexer = np.zeros(N, dtype=int)
            if periods > 0:
                indexer[periods:] = np.arange(N - periods)
                newValues = self.values.take(indexer, axis=0)
                newValues[:periods] = NaN
            else:
                indexer[:periods] = np.arange(-periods, N)
                newValues = self.values.take(indexer, axis=0)
                newValues[periods:] = NaN
        else:
            newIndex = self.index.shift(periods, offset)
            newValues = self.values.copy()

        if self.objects is not None:
            shifted_objects = self.objects.shift(periods,
                                                 offset=offset,
                                                 timeRule=timeRule)

            shifted_objects.index = newIndex
        else:
            shifted_objects = None

        return DataMatrix(data=newValues,
                          index=newIndex,
                          columns=self.columns,
                          objects=shifted_objects)
Exemplo n.º 8
0
def test_getOffset():
    assert_raises(Exception, getOffset, "gibberish")

    assert getOffset("WEEKDAY") == BDay()
    assert getOffset("EOM") == BMonthEnd()
    assert getOffset("W@MON") == Week(weekday=0)
    assert getOffset("W@TUE") == Week(weekday=1)
    assert getOffset("W@WED") == Week(weekday=2)
    assert getOffset("W@THU") == Week(weekday=3)
    assert getOffset("W@FRI") == Week(weekday=4)
Exemplo n.º 9
0
    def __new__(cls, start=None, end=None, periods=None,
                offset=datetools.bday, time_rule=None,
                tzinfo=None, name=None, **kwds):

        time_rule = kwds.get('timeRule', time_rule)
        if time_rule is not None:
            offset = datetools.getOffset(time_rule)

        if time_rule is None:
            if offset in datetools._offsetNames:
                time_rule = datetools._offsetNames[offset]

        # Cachable
        if not start:
            start = kwds.get('begin')
        if not periods:
            periods = kwds.get('nPeriods')

        start = datetools.to_datetime(start)
        end = datetools.to_datetime(end)

        if start is not None and not isinstance(start, datetime):
            raise ValueError('Failed to convert %s to datetime' % start)

        if end is not None and not isinstance(end, datetime):
            raise ValueError('Failed to convert %s to datetime' % end)

        # inside cache range. Handle UTC case
        useCache = _will_use_cache(offset)

        start, end, tzinfo = _figure_out_timezone(start, end, tzinfo)
        useCache = useCache and _naive_in_cache_range(start, end)

        if useCache:
            index = cls._cached_range(start, end, periods=periods,
                                      offset=offset, time_rule=time_rule,
                                      name=name)
            if tzinfo is None:
                return index
        else:
            xdr = generate_range(start=start, end=end, periods=periods,
                                 offset=offset, time_rule=time_rule)
            index = list(xdr)

        if tzinfo is not None:
            index = [d.replace(tzinfo=tzinfo) for d in index]

        index = np.array(index, dtype=object, copy=False)
        index = index.view(cls)
        index.name = name
        index.offset = offset
        index.tzinfo = tzinfo
        return index
Exemplo n.º 10
0
def test_getOffset():
    assert_raises(Exception, getOffset, 'gibberish')

    assert getOffset('WEEKDAY') == BDay()
    assert getOffset('EOM') == BMonthEnd()
    assert getOffset('W@MON') == Week(weekday=0)
    assert getOffset('W@TUE') == Week(weekday=1)
    assert getOffset('W@WED') == Week(weekday=2)
    assert getOffset('W@THU') == Week(weekday=3)
    assert getOffset('W@FRI') == Week(weekday=4)
Exemplo n.º 11
0
    def _cached_range(cls, start=None, end=None, periods=None, offset=None,
                      time_rule=None, name=None):

        # HACK: fix this dependency later
        if time_rule is not None:
            offset = datetools.getOffset(time_rule)

        if offset is None:
            raise Exception('Must provide a DateOffset!')

        if offset not in _daterange_cache:
            xdr = generate_range(_CACHE_START, _CACHE_END, offset=offset)
            arr = np.array(list(xdr), dtype=object, copy=False)

            cachedRange = arr.view(DateRange)
            cachedRange.offset = offset
            cachedRange.tzinfo = None
            cachedRange.name = None
            _daterange_cache[offset] = cachedRange
        else:
            cachedRange = _daterange_cache[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, datetime))

            end = offset.rollback(end)

            endLoc = cachedRange.get_loc(end) + 1
            startLoc = endLoc - periods
        elif end is None:
            assert(isinstance(start, datetime))
            start = offset.rollforward(start)

            startLoc = cachedRange.get_loc(start)
            if periods is None:
                raise Exception('Must provide number of periods!')

            endLoc = startLoc + periods
        else:
            start = offset.rollforward(start)
            end = offset.rollback(end)

            startLoc = cachedRange.get_loc(start)
            endLoc = cachedRange.get_loc(end) + 1

        indexSlice = cachedRange[startLoc:endLoc]
        indexSlice.name = name
        return indexSlice
Exemplo n.º 12
0
    def getCachedRange(cls, start=None, end=None, periods=None, offset=None,
                       timeRule=None):

        # HACK: fix this dependency later
        if timeRule is not None:
            offset = datetools.getOffset(timeRule)

        if offset is None:
            raise Exception('Must provide a DateOffset!')

        if offset not in cls._cache:
            xdr = XDateRange(CACHE_START, CACHE_END, offset=offset)
            arr = np.array(list(xdr), dtype=object, copy=False)

            cachedRange = DateRange.fromIndex(arr)
            cachedRange.offset = offset

            cls._cache[offset] = cachedRange
        else:
            cachedRange = cls._cache[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, datetime))

            end = offset.rollback(end)

            endLoc = cachedRange.indexMap[end] + 1
            startLoc = endLoc - periods
        elif end is None:
            assert(isinstance(start, datetime))
            start = offset.rollforward(start)

            startLoc = cachedRange.indexMap[start]
            if periods is None:
                raise Exception('Must provide number of periods!')

            endLoc = startLoc + periods
        else:
            start = offset.rollforward(start)
            end = offset.rollback(end)

            startLoc = cachedRange.indexMap[start]
            endLoc = cachedRange.indexMap[end] + 1

        indexSlice = cachedRange[startLoc:endLoc]
        indexSlice._parent = cachedRange

        return indexSlice
Exemplo n.º 13
0
def test_getOffset():
    assert_raises(Exception, getOffset, 'gibberish')

    assert getOffset('WEEKDAY') == BDay()
    assert getOffset('EOM') == BMonthEnd()
    assert getOffset('W@MON') == Week(weekday=0)
    assert getOffset('W@TUE') == Week(weekday=1)
    assert getOffset('W@WED') == Week(weekday=2)
    assert getOffset('W@THU') == Week(weekday=3)
    assert getOffset('W@FRI') == Week(weekday=4)
Exemplo n.º 14
0
    def mdf_timestep(self, parameter_s=""):
        """
        Gets/sets the timestep used to advance the date when calling
        %mdf_advance or %mdf_evalto.

        %mdf_timestep [offset]

        eg: %mdf_timestep
        or: %mdf_timestep WEEKDAY
        """
        if parameter_s:
            self.__timestep = datetools.getOffset(parameter_s)
        return self.__timestep
Exemplo n.º 15
0
    def mdf_timestep(self, parameter_s=""):
        """
        Gets/sets the timestep used to advance the date when calling
        %mdf_advance or %mdf_evalto.

        %mdf_timestep [offset]

        eg: %mdf_timestep
        or: %mdf_timestep WEEKDAY
        """
        if parameter_s:
            self.__timestep = datetools.getOffset(parameter_s)
        return self.__timestep
Exemplo n.º 16
0
    def shift(self, periods, offset=None, timeRule=None):
        """
        Shift the underlying series of the DataMatrix and Series objects within
        by given number (positive or negative) of periods.

        Parameters
        ----------
        periods : int (+ or -)
            Number of periods to move
        offset : DateOffset, optional
            Increment to use from datetools module
        timeRule : string
            Time rule to use by name

        Returns
        -------
        DataMatrix
        """
        if periods == 0:
            return self

        if timeRule is not None and offset is None:
            offset = datetools.getOffset(timeRule)

        N = len(self)

        if offset is None:
            newIndex = self.index
            indexer = np.zeros(N, dtype=int)
            if periods > 0:
                indexer[periods:] = np.arange(N - periods)
                newValues = self.values.take(indexer, axis=0)
                newValues[:periods] = NaN
            else:
                indexer[:periods] = np.arange(-periods, N)
                newValues = self.values.take(indexer, axis=0)
                newValues[periods:] = NaN
        else:
            newIndex = self.index.shift(periods, offset)
            newValues = self.values.copy()

        if self.objects is not None:
            shifted_objects = self.objects.shift(periods, offset=offset,
                                                 timeRule=timeRule)

            shifted_objects.index = newIndex
        else:
            shifted_objects = None

        return DataMatrix(data=newValues, index=newIndex, columns=self.columns,
                          objects=shifted_objects)
Exemplo n.º 17
0
    def __new__(cls, start=None, end=None, periods=None,
                offset=datetools.bday, timeRule=None, **kwds):

        # Allow us to circumvent hitting the cache
        index = kwds.get('index')
        if index is None:
            if timeRule is not None:
                offset = datetools.getOffset(timeRule)

            if timeRule is None:
                if offset in datetools._offsetNames:
                    timeRule = datetools._offsetNames[offset]

            # Cachable
            if not start:
                start = kwds.get('begin')
            if not end:
                end = kwds.get('end')
            if not periods:
                periods = kwds.get('nPeriods')

            start = datetools.to_datetime(start)
            end = datetools.to_datetime(end)

            # inside cache range
            fromInside = start is not None and start > CACHE_START
            toInside = end is not None and end < CACHE_END

            useCache = fromInside and toInside

            if (useCache and offset.isAnchored() and
                not isinstance(offset, datetools.Tick)):

                index = cls.getCachedRange(start, end, periods=periods,
                                           offset=offset, timeRule=timeRule)

            else:
                xdr = XDateRange(start=start, end=end,
                                 nPeriods=periods, offset=offset,
                                 timeRule=timeRule)

                index = np.array(list(xdr), dtype=object, copy=False)

                index = index.view(cls)
                index.offset = offset
        else:
            index = index.view(cls)

        return index
Exemplo n.º 18
0
    def __new__(cls, start=None, end=None, periods=None,
                offset=datetools.bday, timeRule=None,
                tzinfo=None, **kwds):
        if timeRule is not None:
            offset = datetools.getOffset(timeRule)

        if timeRule is None:
            if offset in datetools._offsetNames:
                timeRule = datetools._offsetNames[offset]

        # Cachable
        if not start:
            start = kwds.get('begin')
        if not periods:
            periods = kwds.get('nPeriods')

        start = datetools.to_datetime(start)
        end = datetools.to_datetime(end)

        # inside cache range. Handle UTC case

        useCache = (offset.isAnchored() and
                    isinstance(offset, datetools.CacheableOffset))

        start, end, tzinfo = _figure_out_timezone(start, end, tzinfo)
        useCache = useCache and _naive_in_cache_range(start, end)

        if useCache:
            index = cls._cached_range(start, end, periods=periods,
                                      offset=offset, timeRule=timeRule)
            if tzinfo is None:
                return index
        else:
            xdr = generate_range(start=start, end=end, periods=periods,
                                 offset=offset, timeRule=timeRule)
            index = list(xdr)

        if tzinfo is not None:
            index = [d.replace(tzinfo=tzinfo) for d in index]

        index = np.array(index, dtype=object, copy=False)
        index = index.view(cls)
        index.offset = offset
        index.tzinfo = tzinfo
        return index
Exemplo n.º 19
0
    def shift(self, periods, offset=None, timeRule=None):
        """
        Analogous to Series.shift
        """
        # no special handling of fill values yet
        if not isnull(self.fill_value):
            dense_shifted = self.to_dense().shift(periods,
                                                  offset=offset,
                                                  timeRule=timeRule)
            return dense_shifted.to_sparse(fill_value=self.fill_value,
                                           kind=self.kind)

        if periods == 0:
            return self.copy()

        if timeRule is not None and offset is None:
            offset = datetools.getOffset(timeRule)

        if offset is not None:
            return SparseSeries(self.sp_values,
                                sparse_index=self.sp_index,
                                index=self.index.shift(periods, offset),
                                fill_value=self.fill_value)

        int_index = self.sp_index.to_int_index()
        new_indices = int_index.indices + periods
        start, end = new_indices.searchsorted([0, int_index.length])

        new_indices = new_indices[start:end]

        new_sp_index = IntIndex(len(self), new_indices)
        if isinstance(self.sp_index, BlockIndex):
            new_sp_index = new_sp_index.to_block_index()

        return SparseSeries(self.sp_values[start:end].copy(),
                            index=self.index,
                            sparse_index=new_sp_index,
                            fill_value=self.fill_value)
Exemplo n.º 20
0
    def shift(self, periods, offset=None, timeRule=None):
        """
        Shift the underlying series of the DataMatrix and Series objects within
        by given number (positive or negative) of business/weekdays.

        Parameters
        ----------
        periods : int (+ or -)
            Number of periods to move
        offset : DateOffset, optional
            Increment to use from datetools module
        timeRule : string
            time rule name to use by name (e.g. 'WEEKDAY')

        Returns
        -------
        TimeSeries
        """
        if periods == 0:
            return self.copy()

        if timeRule is not None and offset is None:
            offset = datetools.getOffset(timeRule)

        if offset is None:
            newValues = np.empty(len(self), dtype=self.dtype)

            if periods > 0:
                newValues[periods:] = self.values[:-periods]
                newValues[:periods] = np.NaN
            elif periods < 0:
                newValues[:periods] = self.values[-periods:]
                newValues[periods:] = np.NaN

            return Series(newValues, index=self.index)
        else:
            newIndex = self.index.shift(periods, offset)
            return Series(self, index=newIndex)
Exemplo n.º 21
0
    def shift(self, periods, offset=None, timeRule=None):
        """
        Shift the underlying series of the DataMatrix and Series objects within
        by given number (positive or negative) of business/weekdays.

        Parameters
        ----------
        periods : int (+ or -)
            Number of periods to move
        offset : DateOffset, optional
            Increment to use from datetools module
        timeRule : string
            time rule name to use by name (e.g. 'WEEKDAY')

        Returns
        -------
        TimeSeries
        """
        if periods == 0:
            return self.copy()

        if timeRule is not None and offset is None:
            offset = datetools.getOffset(timeRule)

        if offset is None:
            newValues = np.empty(len(self), dtype=self.dtype)

            if periods > 0:
                newValues[periods:] = self.values[:-periods]
                newValues[:periods] = np.NaN
            elif periods < 0:
                newValues[:periods] = self.values[-periods:]
                newValues[periods:] = np.NaN

            return Series(newValues, index=self.index)
        else:
            newIndex = self.index.shift(periods, offset)
            return Series(self, index=newIndex)
Exemplo n.º 22
0
    def shift(self, periods, offset=None, timeRule=None):
        """
        Analogous to Series.shift
        """
        # no special handling of fill values yet
        if not isnull(self.fill_value):
            dense_shifted = self.to_dense().shift(periods, offset=offset,
                                                  timeRule=timeRule)
            return dense_shifted.to_sparse(fill_value=self.fill_value,
                                           kind=self.kind)

        if periods == 0:
            return self.copy()

        if timeRule is not None and offset is None:
            offset = datetools.getOffset(timeRule)

        if offset is not None:
            return SparseSeries(self.sp_values,
                                sparse_index=self.sp_index,
                                index=self.index.shift(periods, offset),
                                fill_value=self.fill_value)

        int_index = self.sp_index.to_int_index()
        new_indices = int_index.indices + periods
        start, end = new_indices.searchsorted([0, int_index.length])

        new_indices = new_indices[start:end]

        new_sp_index = IntIndex(len(self), new_indices)
        if isinstance(self.sp_index, BlockIndex):
            new_sp_index = new_sp_index.to_block_index()

        return SparseSeries(self.sp_values[start:end].copy(),
                            index=self.index,
                            sparse_index=new_sp_index,
                            fill_value=self.fill_value)
Exemplo n.º 23
0
    def __init__(self,
                 start=None,
                 end=None,
                 nPeriods=None,
                 offset=datetools.BDay(),
                 timeRule=None):

        if timeRule is not None:
            offset = datetools.getOffset(timeRule)

        if timeRule is None:
            if offset in datetools._offsetNames:
                timeRule = datetools._offsetNames[offset]

        start = datetools.to_datetime(start)
        end = datetools.to_datetime(end)

        if start and not offset.onOffset(start):
            start = start + offset.__class__(n=1, **offset.kwds)
        if end and not offset.onOffset(end):
            end = end - offset.__class__(n=1, **offset.kwds)
            if nPeriods == None and end < start:
                end = None
                nPeriods = 0

        if end is None:
            end = start + (nPeriods - 1) * offset

        if start is None:
            start = end - (nPeriods - 1) * offset

        self.offset = offset
        self.timeRule = timeRule
        self.start = start
        self.end = end
        self.nPeriods = nPeriods
Exemplo n.º 24
0
def generate_range(start=None, end=None, periods=None,
                   offset=datetools.BDay(), time_rule=None):
    """
    Generates a sequence of dates corresponding to the specified time
    offset. Similar to dateutil.rrule except uses pandas DateOffset
    objects to represent time increments

    Parameters
    ----------
    start : datetime (default None)
    end : datetime (default None)
    periods : int, optional

    Note
    ----
    * This method is faster for generating weekdays than dateutil.rrule
    * At least two of (start, end, periods) must be specified.
    * If both start and end are specified, the returned dates will
    satisfy start <= date <= end.

    Returns
    -------
    dates : generator object

    See also
    --------
    DateRange, dateutil.rrule
    """

    if time_rule is not None:
        offset = datetools.getOffset(time_rule)

    if time_rule is None:
        if offset in datetools._offsetNames:
            time_rule = datetools._offsetNames[offset]

    start = datetools.to_datetime(start)
    end = datetools.to_datetime(end)

    if start and not offset.onOffset(start):
        start = offset.rollforward(start)

    if end and not offset.onOffset(end):
        end = offset.rollback(end)

        if periods is None and end < start:
            end = None
            periods = 0

    if end is None:
        end = start + (periods - 1) * offset

    if start is None:
        start = end - (periods - 1) * offset

    cur = start
    if offset._normalizeFirst:
        cur = datetools.normalize_date(cur)

    while cur <= end:
        yield cur

        # faster than cur + offset
        cur = offset.apply(cur)