Example #1
0
    def wrapper(self, other):
        if other is tslib.NaT:
            return tslib.NaT
        elif isinstance(other, (timedelta, Tick, DateOffset)):
            # timedelta path
            return func(self, other)
        elif isinstance(other, (np.datetime64, datetime, date)):
            other = as_timestamp(other)

        tz = getattr(other, 'tzinfo', None)
        nano = getattr(other, 'nanosecond', 0)

        try:
            if self._adjust_dst and isinstance(other, Timestamp):
                other = other.tz_localize(None)

            result = func(self, other)
            if self._adjust_dst:
                result = tslib._localize_pydatetime(result, tz)

            result = Timestamp(result)
            if self.normalize:
                result = result.normalize()

            # nanosecond may be deleted depending on offset process
            if not self.normalize and nano != 0:
                if not isinstance(self, Nano) and result.nanosecond != nano:
                    if result.tz is not None:
                        # convert to UTC
                        value = tslib.tz_convert_single(
                            result.value, 'UTC', result.tz)
                    else:
                        value = result.value
                    result = Timestamp(value + nano)

            if tz is not None and result.tzinfo is None:
                result = tslib._localize_pydatetime(result, tz)

        except OutOfBoundsDatetime:
            result = func(self, as_datetime(other))

            if self.normalize:
                # normalize_date returns normal datetime
                result = normalize_date(result)

            if tz is not None and result.tzinfo is None:
                result = tslib._localize_pydatetime(result, tz)

        return result
Example #2
0
    def __init__(self, index, warn=True):
        self.index = index
        self.values = np.asarray(index).view('i8')

        if index.tz is not None:
            f = lambda x: tslib.tz_convert_single(x, 'UTC', index.tz)
            self.values = np.vectorize(f)(self.values)
            # This cant work, because of DST
            # 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
Example #3
0
    def __init__(self, index, warn=True):
        self.index = index
        self.values = np.asarray(index).view('i8')

        if index.tz is not None:
            f = lambda x: tslib.tz_convert_single(x, 'UTC', index.tz)
            self.values = np.vectorize(f)(self.values)
            # This cant work, because of DST
            # 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
Example #4
0
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)
Example #5
0
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)
Example #6
0
 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)
Example #7
0
 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)