Esempio n. 1
0
    def __new__(cls,
                start=None,
                end=None,
                periods=None,
                offset=datetools.bday,
                time_rule=None,
                tzinfo=None,
                name=None,
                **kwds):

        import warnings
        warnings.warn("DateRange is deprecated, use DatetimeIndex instead",
                      FutureWarning)

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

        return DatetimeIndex(start=start,
                             end=end,
                             periods=periods,
                             freq=offset,
                             tzinfo=tzinfo,
                             name=name,
                             **kwds)
Esempio n. 2
0
    def __new__(cls, start=None, end=None, periods=None,
                offset=datetools.bday, time_rule=None,
                tzinfo=None, name=None, **kwds):

        import warnings
        warnings.warn("DateRange is deprecated, use DatetimeIndex instead",
                      FutureWarning)

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

        return DatetimeIndex(start=start, end=end,
                             periods=periods, freq=offset,
                             tzinfo=tzinfo, name=name, **kwds)
Esempio n. 3
0
    def test_legacy_time_rules(self):
        rules = [
            ("WEEKDAY", "B"),
            ("EOM", "BM"),
            ("W@MON", "W-MON"),
            ("W@TUE", "W-TUE"),
            ("W@WED", "W-WED"),
            ("W@THU", "W-THU"),
            ("W@FRI", "W-FRI"),
            ("Q@JAN", "BQ-JAN"),
            ("Q@FEB", "BQ-FEB"),
            ("Q@MAR", "BQ-MAR"),
            ("A@JAN", "BA-JAN"),
            ("A@FEB", "BA-FEB"),
            ("A@MAR", "BA-MAR"),
            ("A@APR", "BA-APR"),
            ("A@MAY", "BA-MAY"),
            ("A@JUN", "BA-JUN"),
            ("A@JUL", "BA-JUL"),
            ("A@AUG", "BA-AUG"),
            ("A@SEP", "BA-SEP"),
            ("A@OCT", "BA-OCT"),
            ("A@NOV", "BA-NOV"),
            ("A@DEC", "BA-DEC"),
            ("WOM@1FRI", "WOM-1FRI"),
            ("WOM@2FRI", "WOM-2FRI"),
            ("WOM@3FRI", "WOM-3FRI"),
            ("WOM@4FRI", "WOM-4FRI"),
        ]

        start, end = "1/1/2000", "1/1/2010"

        for old_freq, new_freq in rules:
            old_rng = date_range(start, end, freq=old_freq)
            new_rng = date_range(start, end, freq=new_freq)
            self.assert_(old_rng.equals(new_rng))

            # test get_legacy_offset_name
            offset = datetools.get_offset(new_freq)
            old_name = datetools.get_legacy_offset_name(offset)
            self.assertEquals(old_name, old_freq)
Esempio n. 4
0
    def test_legacy_time_rules(self):
        rules = [('WEEKDAY', 'B'), ('EOM', 'BM'), ('W@MON', 'W-MON'),
                 ('W@TUE', 'W-TUE'), ('W@WED', 'W-WED'), ('W@THU', 'W-THU'),
                 ('W@FRI', 'W-FRI'), ('Q@JAN', 'BQ-JAN'), ('Q@FEB', 'BQ-FEB'),
                 ('Q@MAR', 'BQ-MAR'), ('A@JAN', 'BA-JAN'), ('A@FEB', 'BA-FEB'),
                 ('A@MAR', 'BA-MAR'), ('A@APR', 'BA-APR'), ('A@MAY', 'BA-MAY'),
                 ('A@JUN', 'BA-JUN'), ('A@JUL', 'BA-JUL'), ('A@AUG', 'BA-AUG'),
                 ('A@SEP', 'BA-SEP'), ('A@OCT', 'BA-OCT'), ('A@NOV', 'BA-NOV'),
                 ('A@DEC', 'BA-DEC'), ('WOM@1FRI', 'WOM-1FRI'),
                 ('WOM@2FRI', 'WOM-2FRI'), ('WOM@3FRI', 'WOM-3FRI'),
                 ('WOM@4FRI', 'WOM-4FRI')]

        start, end = '1/1/2000', '1/1/2010'

        for old_freq, new_freq in rules:
            old_rng = date_range(start, end, freq=old_freq)
            new_rng = date_range(start, end, freq=new_freq)
            self.assert_index_equal(old_rng, new_rng)

            # test get_legacy_offset_name
            offset = datetools.get_offset(new_freq)
            old_name = datetools.get_legacy_offset_name(offset)
            self.assertEqual(old_name, old_freq)
Esempio n. 5
0
    def test_legacy_time_rules(self):
        rules = [('WEEKDAY', 'B'), ('EOM', 'BM'), ('W@MON', 'W-MON'),
                 ('W@TUE', 'W-TUE'), ('W@WED', 'W-WED'), ('W@THU', 'W-THU'),
                 ('W@FRI', 'W-FRI'), ('Q@JAN', 'BQ-JAN'), ('Q@FEB', 'BQ-FEB'),
                 ('Q@MAR', 'BQ-MAR'), ('A@JAN', 'BA-JAN'), ('A@FEB', 'BA-FEB'),
                 ('A@MAR', 'BA-MAR'), ('A@APR', 'BA-APR'), ('A@MAY', 'BA-MAY'),
                 ('A@JUN', 'BA-JUN'), ('A@JUL', 'BA-JUL'), ('A@AUG', 'BA-AUG'),
                 ('A@SEP', 'BA-SEP'), ('A@OCT', 'BA-OCT'), ('A@NOV', 'BA-NOV'),
                 ('A@DEC', 'BA-DEC'), ('WOM@1FRI', 'WOM-1FRI'),
                 ('WOM@2FRI', 'WOM-2FRI'), ('WOM@3FRI', 'WOM-3FRI'),
                 ('WOM@4FRI', 'WOM-4FRI')]

        start, end = '1/1/2000', '1/1/2010'

        for old_freq, new_freq in rules:
            old_rng = date_range(start, end, freq=old_freq)
            new_rng = date_range(start, end, freq=new_freq)
            self.assert_index_equal(old_rng, new_rng)

            # test get_legacy_offset_name
            offset = datetools.get_offset(new_freq)
            old_name = datetools.get_legacy_offset_name(offset)
            self.assertEqual(old_name, old_freq)
Esempio n. 6
0
 def test_ms_vs_MS(self):
     left = datetools.get_offset('ms')
     right = datetools.get_offset('MS')
     self.assertEqual(left, datetools.Milli())
     self.assertEqual(right, datetools.MonthBegin())
Esempio n. 7
0
 def test_ms_vs_MS(self):
     left = datetools.get_offset('ms')
     right = datetools.get_offset('MS')
     self.assertEqual(left, datetools.Milli())
     self.assertEqual(right, datetools.MonthBegin())
Esempio n. 8
0
 def test_ms_vs_MS(self):
     left = datetools.get_offset("ms")
     right = datetools.get_offset("MS")
     self.assert_(left == datetools.Milli())
     self.assert_(right == datetools.MonthBegin())
Esempio n. 9
0
    def __new__(
        cls,
        data=None,
        freq=None,
        start=None,
        end=None,
        periods=None,
        dtype=None,
        copy=False,
        name=None,
        tz=None,
        verify_integrity=True,
        normalize=False,
        **kwds
    ):

        warn = False
        if "offset" in kwds and kwds["offset"]:
            freq = kwds["offset"]
            warn = True

        if not isinstance(freq, datetools.DateOffset):
            freq = datetools.to_offset(freq)

        if warn:
            import warnings

            warnings.warn("parameter 'offset' is deprecated, " "please use 'freq' instead", FutureWarning)
            if isinstance(freq, basestring):
                freq = datetools.get_offset(freq)
        else:
            if isinstance(freq, basestring):
                freq = datetools.to_offset(freq)

        offset = freq

        if data is None and offset is None:
            raise ValueError("Must provide freq argument if no data is " "supplied")

        if data is None:
            _normalized = True

            if start is not None:
                start = Timestamp(start)
                if not isinstance(start, Timestamp):
                    raise ValueError("Failed to convert %s to timestamp" % start)

                if normalize:
                    start = datetools.normalize_date(start)
                    _normalized = True
                else:
                    _normalized = _normalized and start.time() == _midnight

            if end is not None:
                end = Timestamp(end)
                if not isinstance(end, Timestamp):
                    raise ValueError("Failed to convert %s to timestamp" % end)

                if normalize:
                    end = datetools.normalize_date(end)
                    _normalized = True
                else:
                    _normalized = _normalized and end.time() == _midnight

            start, end, tz = tools._figure_out_timezone(start, end, tz)

            if (
                offset._should_cache()
                and not (offset._normalize_cache and not _normalized)
                and datetools._naive_in_cache_range(start, end)
            ):
                index = cls._cached_range(start, end, periods=periods, offset=offset, name=name)
            else:
                index = _generate_regular_range(start, end, periods, offset)

            index = index.view(cls)
            index.name = name
            index.offset = offset
            index.tz = tz

            return index

        if not isinstance(data, np.ndarray):
            if np.isscalar(data):
                raise ValueError(
                    "DatetimeIndex() must be called with a " "collection of some kind, %s was passed" % repr(data)
                )

            if isinstance(data, datetime):
                data = [data]

            # other iterable of some kind
            if not isinstance(data, (list, tuple)):
                data = list(data)

            data = np.asarray(data, dtype="O")

            # try a few ways to make it datetime64
            if lib.is_string_array(data):
                data = _str_to_dt_array(data)
            else:
                data = np.asarray(data, dtype="M8[us]")

        if issubclass(data.dtype.type, basestring):
            subarr = _str_to_dt_array(data)
        elif issubclass(data.dtype.type, np.integer):
            subarr = np.array(data, dtype="M8[us]", copy=copy)
        elif issubclass(data.dtype.type, np.datetime64):
            subarr = np.array(data, dtype="M8[us]", copy=copy)
        else:
            subarr = np.array(data, dtype="M8[us]", copy=copy)

        # TODO: this is horribly inefficient. If user passes data + offset, we
        # need to make sure data points conform. Punting on this

        if verify_integrity:
            if offset is not None:
                for i, ts in enumerate(subarr):
                    if not offset.onOffset(Timestamp(ts)):
                        val = Timestamp(offset.rollforward(ts)).value
                        subarr[i] = val

        subarr = subarr.view(cls)
        subarr.name = name
        subarr.offset = offset
        subarr.tz = tz

        return subarr