Exemple #1
0
def _get_range_edges(first, last, offset, closed='left', base=0):
    if isinstance(offset, compat.string_types):
        offset = to_offset(offset)

    if isinstance(offset, Tick):
        is_day = isinstance(offset, Day)
        day_nanos = _delta_to_nanoseconds(timedelta(1))

        # #1165
        if (is_day and day_nanos % offset.nanos == 0) or not is_day:
            return _adjust_dates_anchored(first,
                                          last,
                                          offset,
                                          closed=closed,
                                          base=base)

    if not isinstance(offset, Tick):  # and first.time() != last.time():
        # hack!
        first = first.normalize()
        last = last.normalize()

    if closed == 'left':
        first = Timestamp(offset.rollback(first))
    else:
        first = Timestamp(first - offset)

    last = Timestamp(last + offset)

    return first, last
Exemple #2
0
def _get_range_edges(axis, offset, closed='left', base=0):
    if isinstance(offset, basestring):
        offset = to_offset(offset)

    if isinstance(offset, Tick):
        day_nanos = _delta_to_nanoseconds(timedelta(1))
        # #1165
        if (day_nanos % offset.nanos) == 0:
            return _adjust_dates_anchored(axis[0],
                                          axis[-1],
                                          offset,
                                          closed=closed,
                                          base=base)

    first, last = axis[0], axis[-1]
    if not isinstance(offset, Tick):  # and first.time() != last.time():
        # hack!
        first = tools.normalize_date(first)
        last = tools.normalize_date(last)

    if closed == 'left':
        first = Timestamp(offset.rollback(first))
    else:
        first = Timestamp(first - offset)

    last = Timestamp(last + offset)

    return first, last
Exemple #3
0
 def _add_delta(self, delta):
     if isinstance(delta, (Tick, timedelta)):
         inc = offsets._delta_to_nanoseconds(delta)
         new_values = (self.asi8 + inc).view(_NS_DTYPE)
     else:
         new_values = self.astype('O') + delta
     return DatetimeIndex(new_values, tz=self.tz, freq='infer')
Exemple #4
0
def _get_range_edges(axis, begin, end, offset, closed='left',
                     base=0):
    from pandas.tseries.offsets import Tick, _delta_to_nanoseconds
    if isinstance(offset, basestring):
        offset = to_offset(offset)

    if not isinstance(offset, DateOffset):
        raise ValueError("Rule not a recognized offset")

    if isinstance(offset, Tick):
        day_nanos = _delta_to_nanoseconds(timedelta(1))
        # #1165
        if ((day_nanos % offset.nanos) == 0 and begin is None
            and end is None):
            return _adjust_dates_anchored(axis[0], axis[-1], offset,
                                          closed=closed, base=base)

    if begin is None:
        if closed == 'left':
            first = Timestamp(offset.rollback(axis[0]))
        else:
            first = Timestamp(axis[0] - offset)
    else:
        first = Timestamp(offset.rollback(begin))

    if end is None:
        last = Timestamp(axis[-1] + offset)
    else:
        last = Timestamp(offset.rollforward(end))

    return first, last
Exemple #5
0
def _get_range_edges(axis, offset, closed='left', base=0):
    if isinstance(offset, basestring):
        offset = to_offset(offset)

    if isinstance(offset, Tick):
        day_nanos = _delta_to_nanoseconds(timedelta(1))
        # #1165
        if (day_nanos % offset.nanos) == 0:
            return _adjust_dates_anchored(axis[0], axis[-1], offset,
                                          closed=closed, base=base)

    first, last = axis[0], axis[-1]
    if not isinstance(offset, Tick):  # and first.time() != last.time():
        # hack!
        first = tools.normalize_date(first)
        last = tools.normalize_date(last)

    if closed == 'left':
        first = Timestamp(offset.rollback(first))
    else:
        first = Timestamp(first - offset)

    last = Timestamp(last + offset)

    return first, last
Exemple #6
0
def _get_range_edges(first, last, offset, closed='left', base=0):
    if isinstance(offset, compat.string_types):
        offset = to_offset(offset)

    if isinstance(offset, Tick):
        is_day = isinstance(offset, Day)
        day_nanos = _delta_to_nanoseconds(timedelta(1))

        # #1165
        if (is_day and day_nanos % offset.nanos == 0) or not is_day:
            return _adjust_dates_anchored(first, last, offset,
                                          closed=closed, base=base)

    if not isinstance(offset, Tick):  # and first.time() != last.time():
        # hack!
        first = first.normalize()
        last = last.normalize()

    if closed == 'left':
        first = Timestamp(offset.rollback(first))
    else:
        first = Timestamp(first - offset)

    last = Timestamp(last + offset)

    return first, last
Exemple #7
0
 def _add_delta(self, delta):
     if isinstance(delta, (Tick, timedelta)):
         inc = offsets._delta_to_nanoseconds(delta)
         new_values = (self.asi8 + inc).view("M8[ns]")
     else:
         new_values = self.astype("O") + delta
     return DatetimeIndex(new_values, tz=self.tz, freq="infer")
Exemple #8
0
 def _add_delta(self, delta):
     if isinstance(delta, (Tick, timedelta)):
         inc = offsets._delta_to_nanoseconds(delta)
         new_values = (self.asi8 + inc).view(_NS_DTYPE)
     else:
         new_values = self.astype('O') + delta
     return DatetimeIndex(new_values, tz=self.tz, freq='infer')
Exemple #9
0
def _get_range_edges(axis, begin, end, offset, closed='left', base=0):
    if isinstance(offset, basestring):
        offset = to_offset(offset)

    if not isinstance(offset, DateOffset):
        raise ValueError("Rule not a recognized offset")

    if isinstance(offset, Tick):
        day_nanos = _delta_to_nanoseconds(timedelta(1))
        # #1165
        if ((day_nanos % offset.nanos) == 0 and begin is None and end is None):
            return _adjust_dates_anchored(axis[0],
                                          axis[-1],
                                          offset,
                                          closed=closed,
                                          base=base)

    if begin is None:
        if closed == 'left':
            first = Timestamp(offset.rollback(axis[0]))
        else:
            first = Timestamp(axis[0] - offset)
    else:
        first = Timestamp(offset.rollback(begin))

    if end is None:
        last = Timestamp(axis[-1] + offset)
    else:
        last = Timestamp(offset.rollforward(end))

    return first, last
Exemple #10
0
    def _get_time_bins(self, axis):
        assert (isinstance(axis, DatetimeIndex))

        if len(axis) == 0:
            binner = labels = DatetimeIndex(data=[], freq=self.freq)
            return binner, [], labels

        first, last = _get_range_edges(axis,
                                       self.begin,
                                       self.end,
                                       self.freq,
                                       closed=self.closed,
                                       base=self.base)
        binner = labels = DatetimeIndex(freq=self.freq, start=first, end=last)

        # a little hack
        trimmed = False
        if len(binner) > 2 and binner[-2] == axis[-1]:
            binner = binner[:-1]
            trimmed = True

        ax_values = axis.asi8
        bin_edges = binner.asi8

        # Some hacks for > daily data, see #1471, #1458
        if self.freq != 'D' and is_superperiod(self.freq, 'D'):
            day_nanos = _delta_to_nanoseconds(timedelta(1))
            if self.closed == 'right':
                bin_edges = bin_edges + day_nanos - 1
            else:
                bin_edges = bin_edges + day_nanos

            # intraday values on last day
            if bin_edges[-2] > ax_values[-1]:
                bin_edges = bin_edges[:-1]
                binner = binner[:-1]

        # general version, knowing nothing about relative frequencies
        bins = lib.generate_bins_dt64(ax_values, bin_edges, self.closed)

        if self.closed == 'right':
            labels = binner
            if self.label == 'right':
                labels = labels[1:]
            elif not trimmed:
                labels = labels[:-1]
        else:
            if self.label == 'right':
                labels = labels[1:]
            elif not trimmed:
                labels = labels[:-1]

        return binner, bins, labels
Exemple #11
0
    def _get_time_bins(self, axis):
        assert(isinstance(axis, DatetimeIndex))

        if len(axis) == 0:
            binner = labels = DatetimeIndex(data=[], freq=self.freq)
            return binner, [], labels

        first, last = _get_range_edges(axis, self.begin, self.end, self.freq,
                                       closed=self.closed, base=self.base)
        binner = labels = DatetimeIndex(freq=self.freq, start=first, end=last)

        # a little hack
        trimmed = False
        if len(binner) > 2 and binner[-2] == axis[-1]:
            binner = binner[:-1]
            trimmed = True

        ax_values = axis.asi8
        bin_edges = binner.asi8

        # Some hacks for > daily data, see #1471, #1458
        if self.freq != 'D' and is_superperiod(self.freq, 'D'):
            day_nanos = _delta_to_nanoseconds(timedelta(1))
            if self.closed == 'right':
                bin_edges = bin_edges + day_nanos - 1
            else:
                bin_edges = bin_edges + day_nanos

            # intraday values on last day
            if bin_edges[-2] > ax_values[-1]:
                bin_edges = bin_edges[:-1]
                binner = binner[:-1]

        # general version, knowing nothing about relative frequencies
        bins = lib.generate_bins_dt64(ax_values, bin_edges, self.closed)

        if self.closed == 'right':
            labels = binner
            if self.label == 'right':
                labels = labels[1:]
            elif not trimmed:
                labels = labels[:-1]
        else:
            if self.label == 'right':
                labels = labels[1:]
            elif not trimmed:
                labels = labels[:-1]

        return binner, bins, labels
Exemple #12
0
    def _adjust_bin_edges(self, binner, ax_values):
        # Some hacks for > daily data, see #1471, #1458, #1483

        bin_edges = binner.asi8

        if self.freq != 'D' and is_superperiod(self.freq, 'D'):
            day_nanos = _delta_to_nanoseconds(timedelta(1))
            if self.closed == 'right':
                bin_edges = bin_edges + day_nanos - 1

            # intraday values on last day
            if bin_edges[-2] > ax_values.max():
                bin_edges = bin_edges[:-1]
                binner = binner[:-1]

        return binner, bin_edges
Exemple #13
0
    def _adjust_bin_edges(self, binner, ax_values):
        # Some hacks for > daily data, see #1471, #1458, #1483

        bin_edges = binner.asi8

        if self.freq != 'D' and is_superperiod(self.freq, 'D'):
            day_nanos = _delta_to_nanoseconds(timedelta(1))
            if self.closed == 'right':
                bin_edges = bin_edges + day_nanos - 1

            # intraday values on last day
            if bin_edges[-2] > ax_values[-1]:
                bin_edges = bin_edges[:-1]
                binner = binner[:-1]

        return binner, bin_edges
Exemple #14
0
def _get_range_edges(axis, offset, closed="left", base=0):
    if isinstance(offset, basestring):
        offset = to_offset(offset)

    if isinstance(offset, Tick):
        day_nanos = _delta_to_nanoseconds(timedelta(1))
        # #1165
        if (day_nanos % offset.nanos) == 0:
            return _adjust_dates_anchored(axis[0], axis[-1], offset, closed=closed, base=base)

    if closed == "left":
        first = Timestamp(offset.rollback(axis[0]))
    else:
        first = Timestamp(axis[0] - offset)

    last = Timestamp(axis[-1] + offset)

    return first, last
Exemple #15
0
def _get_range_edges(axis, offset, closed='left', base=0):
    if isinstance(offset, basestring):
        offset = to_offset(offset)

    if isinstance(offset, Tick):
        day_nanos = _delta_to_nanoseconds(timedelta(1))
        # #1165
        if (day_nanos % offset.nanos) == 0:
            return _adjust_dates_anchored(axis[0],
                                          axis[-1],
                                          offset,
                                          closed=closed,
                                          base=base)

    if closed == 'left':
        first = Timestamp(offset.rollback(axis[0]))
    else:
        first = Timestamp(axis[0] - offset)

    last = Timestamp(axis[-1] + offset)

    return first, last