Beispiel #1
0
    def _init_dates(self, dates, freq):
        if dates is None:
            dates = self.data.row_labels

        if dates is not None:
            if (not datetools._is_datetime_index(dates) and
                    isinstance(self.data, data.PandasData)):
                try:
                    if is_numeric_dtype(dates):
                        raise ValueError
                    dates = to_datetime(dates)
                except ValueError:
                    raise ValueError("Given a pandas object and the index does "
                                     "not contain dates")
            if not freq:
                try:
                    freq = datetools._infer_freq(dates)
                except:
                    raise ValueError("Frequency inference failed. Use `freq` "
                                     "keyword.")

            if isinstance(dates[0], datetime.datetime):
                dates = DatetimeIndex(dates)
            else: # preserve PeriodIndex
                dates = PeriodIndex(dates)
        self.data.dates = dates
        self.data.freq = freq

        # Test for nanoseconds in early pandas versions
        if freq is not None and _freq_to_pandas[freq].freqstr == 'N':
            from distutils.version import LooseVersion
            from pandas import __version__ as pd_version
            if LooseVersion(pd_version) < '0.14':
                raise NotImplementedError('Nanosecond index not available in'
                                          ' Pandas < 0.14')
Beispiel #2
0
    def _init_dates(self, dates, freq):
        if dates is None:
            dates = self.data.row_labels

        if dates is not None:
            if (not datetools._is_datetime_index(dates) and
                    isinstance(self.data, data.PandasData)):
                try:
                    if is_numeric_dtype(dates):
                        raise ValueError
                    dates = to_datetime(dates)
                except ValueError:
                    raise ValueError("Given a pandas object and the index does "
                                     "not contain dates")
            if not freq:
                try:
                    freq = datetools._infer_freq(dates)
                except:
                    raise ValueError("Frequency inference failed. Use `freq` "
                                     "keyword.")

            if isinstance(dates[0], datetime.datetime):
                dates = DatetimeIndex(dates)
            else: # preserve PeriodIndex
                dates = PeriodIndex(dates)
        self.data.dates = dates
        self.data.freq = freq

        # Test for nanoseconds in early pandas versions
        if freq is not None and _freq_to_pandas[freq].freqstr == 'N':
            from distutils.version import LooseVersion
            from pandas import __version__ as pd_version
            if LooseVersion(pd_version) < '0.14':
                raise NotImplementedError('Nanosecond index not available in'
                                          ' Pandas < 0.14')
Beispiel #3
0
    def _init_dates(self, dates, freq):
        if dates is None:
            dates = self.data.row_labels

        if dates is not None:
            if (not datetools._is_datetime_index(dates) and
                    isinstance(self.data, data.PandasData)):
                try:
                    if is_numeric_dtype(dates):
                        raise ValueError
                    dates = to_datetime(dates)
                except ValueError:
                    raise ValueError("Given a pandas object and the index does "
                                     "not contain dates")
            if not freq:
                try:
                    freq = datetools._infer_freq(dates)
                except:
                    raise ValueError("Frequency inference failed. Use `freq` "
                                     "keyword.")

            if isinstance(dates[0], datetime.datetime):
                dates = DatetimeIndex(dates)
            else: # preserve PeriodIndex
                dates = PeriodIndex(dates)
        self.data.dates = dates
        self.data.freq = freq
Beispiel #4
0
    def _init_dates(self, dates, freq):
        if dates is None:
            dates = self.data.row_labels

        if dates is not None:
            if (not datetools._is_datetime_index(dates) and
                    isinstance(self.data, data.PandasData)):
                try:
                    dates = to_datetime(dates)
                except ValueError:
                    raise ValueError("Given a pandas object and the index does "
                                     "not contain dates")
            if not freq:
                try:
                    freq = datetools._infer_freq(dates)
                except:
                    raise ValueError("Frequency inference failed. Use `freq` "
                            "keyword.")
            dates = Index(dates)
        self.data.dates = dates
        if freq:
            try: #NOTE: Can drop this once we move to pandas >= 0.8.x
                _freq_to_pandas[freq]
            except:
                raise ValueError("freq %s not understood" % freq)
        self.data.freq = freq
Beispiel #5
0
    def _init_dates(self, dates, freq):
        if dates is None:
            dates = self.data.row_labels

        if dates is not None:
            if (not datetools._is_datetime_index(dates)
                    and isinstance(self.data, data.PandasData)):
                try:
                    if is_numeric_dtype(dates):
                        raise ValueError
                    dates = to_datetime(dates)
                except ValueError:
                    raise ValueError(
                        "Given a pandas object and the index does "
                        "not contain dates")
            if not freq:
                try:
                    freq = datetools._infer_freq(dates)
                except:
                    raise ValueError("Frequency inference failed. Use `freq` "
                                     "keyword.")

            if isinstance(dates[0], datetime.datetime):
                dates = DatetimeIndex(dates)
            else:  # preserve PeriodIndex
                dates = PeriodIndex(dates)
        self.data.dates = dates
        self.data.freq = freq
Beispiel #6
0
    def _init_dates(self, dates, freq):
        if dates is None:
            dates = self._data.row_labels

        if dates is not None:
            try:
                from scikits.timeseries import Date
                if not isinstance(dates[0], (datetime.datetime,Date)):
                    raise ValueError("dates must be of type datetime or "
                                     "scikits.timeseries.Date")
            except ImportError:
                if not isinstance(dates[0], (datetime.datetime)):
                    raise ValueError("dates must be of type datetime")
            if not freq:
                #if isinstance(dates, DateRange):
                #    freq = datetools.inferTimeRule(dates)
                #elif isinstance(dates, TimeSeries):
                #    freq = dates.freqstr
                try:
                    freq = datetools._infer_freq(dates)
                except:
                    raise ValueError("Frequency inference failed. Use `freq` "
                            "keyword.")
            dates = Index(dates)
        self._data.dates = dates
        self._data.freq = _check_freq(freq) #TODO: drop if can get info from dates
def test_period_index():
    # tests 1285
    if _pandas_08x:
        from pandas import PeriodIndex

        dates = PeriodIndex(start="1/1/1990", periods=20, freq="M")
        npt.assert_(_infer_freq(dates) == "M")
Beispiel #8
0
def _check_period_index(x, freq="M"):
    from pandas import PeriodIndex, DatetimeIndex
    if not isinstance(x.index, (DatetimeIndex, PeriodIndex)):
        raise ValueError("The index must be a DatetimeIndex or PeriodIndex")

    from statsmodels.tsa.base.datetools import _infer_freq
    inferred_freq = _infer_freq(x.index)
    if not inferred_freq.startswith(freq):
        raise ValueError("Expected frequency {}. Got {}".format(inferred_freq,
                                                                freq))
Beispiel #9
0
def _check_period_index(x, freq="M"):
    try:
        from pandas import PeriodIndex, DatetimeIndex
    except ImportError: # not sure min. version
        PeriodIndex = DatetimeIndex # HACK
    from statsmodels.tools.data import _is_using_pandas
    if not _is_using_pandas(x, None):
        raise ValueError("x must be a pandas object")
    if not isinstance(x.index, (DatetimeIndex, PeriodIndex)):
        raise ValueError("The index must be a DatetimeIndex or PeriodIndex")

    from statsmodels.tsa.base.datetools import _infer_freq
    inferred_freq = _infer_freq(x.index)
    if not inferred_freq.startswith(freq):
        raise ValueError("Expected frequency {}. Got {}".format(inferred_freq,
                                                                freq))
Beispiel #10
0
def _check_period_index(x, freq="M"):
    try:
        from pandas import PeriodIndex, DatetimeIndex
    except ImportError:  # not sure min. version
        PeriodIndex = DatetimeIndex  # HACK
    from statsmodels.tools.data import _is_using_pandas
    if not _is_using_pandas(x, None):
        raise ValueError("x must be a pandas object")
    if not isinstance(x.index, (DatetimeIndex, PeriodIndex)):
        raise ValueError("The index must be a DatetimeIndex or PeriodIndex")

    from statsmodels.tsa.base.datetools import _infer_freq
    inferred_freq = _infer_freq(x.index)
    if not inferred_freq.startswith(freq):
        raise ValueError("Expected frequency {}. Got {}".format(
            inferred_freq, freq))
Beispiel #11
0
    def _init_dates(self, dates, freq):
        if dates is None:
            dates = self.data.row_labels

        if dates is not None:
            if (not isinstance(dates[0], datetime.datetime) and
                    isinstance(self.data, data.PandasData)):
                raise ValueError("Given a pandas object and the index does "
                                 "not contain dates")
            if not freq:
                try:
                    freq = datetools._infer_freq(dates)
                except:
                    raise ValueError("Frequency inference failed. Use `freq` "
                            "keyword.")
            dates = Index(dates)
        self.data.dates = dates
        self.data.freq = _check_freq(freq) #TODO: drop if can get info from dates
Beispiel #12
0
def test_infer_freq():
    d1 = datetime(2008, 12, 31)
    d2 = datetime(2012, 9, 30)

    if _pandas_08x:
        b = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['B']).values
        d = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['D']).values
        w = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['W']).values
        m = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['M']).values
        a = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['A']).values
        q = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['Q']).values
        assert _infer_freq(w) == 'W-SUN'
        assert _infer_freq(a) == 'A-DEC'
        assert _infer_freq(q) == 'Q-DEC'
        assert _infer_freq(w[:3]) == 'W-SUN'
        assert _infer_freq(a[:3]) == 'A-DEC'
        assert _infer_freq(q[:3]) == 'Q-DEC'
    else:
        from pandas import DateRange

        b = DateRange(d1, d2, offset=_freq_to_pandas['B']).values
        d = DateRange(d1, d2, offset=_freq_to_pandas['D']).values
        w = DateRange(d1, d2, offset=_freq_to_pandas['W']).values
        m = DateRange(d1, d2, offset=_freq_to_pandas['M']).values
        a = DateRange(d1, d2, offset=_freq_to_pandas['A']).values
        q = DateRange(d1, d2, offset=_freq_to_pandas['Q']).values
        assert _infer_freq(w) == 'W'
        assert _infer_freq(a) == 'A'
        assert _infer_freq(q) == 'Q'
        assert _infer_freq(w[:3]) == 'W'
        assert _infer_freq(a[:3]) == 'A'
        assert _infer_freq(q[:3]) == 'Q'

    assert _infer_freq(b[2:5]) == 'B'
    assert _infer_freq(b[:3]) == 'D'
    assert _infer_freq(b) == 'B'
    assert _infer_freq(d) == 'D'
    assert _infer_freq(m) == 'M'
    assert _infer_freq(d[:3]) == 'D'
    assert _infer_freq(m[:3]) == 'M'
Beispiel #13
0
def test_infer_freq():
    from pandas import DateRange
    d1 = datetime(2008, 12, 31)
    d2 = datetime(2012, 9, 30)

    b = DateRange(d1, d2, offset=_freq_to_pandas['B']).values
    d = DateRange(d1, d2, offset=_freq_to_pandas['D']).values
    w = DateRange(d1, d2, offset=_freq_to_pandas['W']).values
    m = DateRange(d1, d2, offset=_freq_to_pandas['M']).values
    a = DateRange(d1, d2, offset=_freq_to_pandas['A']).values
    q = DateRange(d1, d2, offset=_freq_to_pandas['Q']).values

    assert _infer_freq(b[2:5]) == 'B'
    assert _infer_freq(b[:3]) == 'D'

    assert _infer_freq(b) == 'B'
    assert _infer_freq(d) == 'D'
    assert _infer_freq(w) == 'W'
    assert _infer_freq(m) == 'M'
    assert _infer_freq(a) == 'A'
    assert _infer_freq(q) == 'Q'
    assert _infer_freq(d[:3]) == 'D'
    assert _infer_freq(w[:3]) == 'W'
    assert _infer_freq(m[:3]) == 'M'
    assert _infer_freq(a[:3]) == 'A'
    assert _infer_freq(q[:3]) == 'Q'
Beispiel #14
0
def test_infer_freq():
    d1 = datetime(2008, 12, 31)
    d2 = datetime(2012, 9, 30)

    if _pandas_08x:
        b = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas["B"]).values
        d = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas["D"]).values
        w = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas["W"]).values
        m = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas["M"]).values
        a = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas["A"]).values
        q = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas["Q"]).values
        assert _infer_freq(w) == "W-SUN"
        assert _infer_freq(a) == "A-DEC"
        assert _infer_freq(q) == "Q-DEC"
        assert _infer_freq(w[:3]) == "W-SUN"
        assert _infer_freq(a[:3]) == "A-DEC"
        assert _infer_freq(q[:3]) == "Q-DEC"
    else:
        from pandas import DateRange

        b = DateRange(d1, d2, offset=_freq_to_pandas["B"]).values
        d = DateRange(d1, d2, offset=_freq_to_pandas["D"]).values
        w = DateRange(d1, d2, offset=_freq_to_pandas["W"]).values
        m = DateRange(d1, d2, offset=_freq_to_pandas["M"]).values
        a = DateRange(d1, d2, offset=_freq_to_pandas["A"]).values
        q = DateRange(d1, d2, offset=_freq_to_pandas["Q"]).values
        assert _infer_freq(w) == "W"
        assert _infer_freq(a) == "A"
        assert _infer_freq(q) == "Q"
        assert _infer_freq(w[:3]) == "W"
        assert _infer_freq(a[:3]) == "A"
        assert _infer_freq(q[:3]) == "Q"

    assert _infer_freq(b[2:5]) == "B"
    assert _infer_freq(b[:3]) == "D"
    assert _infer_freq(b) == "B"
    assert _infer_freq(d) == "D"
    assert _infer_freq(m) == "M"
    assert _infer_freq(d[:3]) == "D"
    assert _infer_freq(m[:3]) == "M"
Beispiel #15
0
def test_infer_freq():
    d1 = datetime(2008, 12, 31)
    d2 = datetime(2012, 9, 30)

    if _pandas_08x:
        b = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['B']).values
        d = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['D']).values
        w = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['W']).values
        m = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['M']).values
        a = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['A']).values
        q = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['Q']).values
    else:
        from pandas import DateRange

        b = DateRange(d1, d2, offset=_freq_to_pandas['B']).values
        d = DateRange(d1, d2, offset=_freq_to_pandas['D']).values
        w = DateRange(d1, d2, offset=_freq_to_pandas['W']).values
        m = DateRange(d1, d2, offset=_freq_to_pandas['M']).values
        a = DateRange(d1, d2, offset=_freq_to_pandas['A']).values
        q = DateRange(d1, d2, offset=_freq_to_pandas['Q']).values

    assert _infer_freq(b[2:5]) == 'B'
    assert _infer_freq(b[:3]) == 'D'

    assert _infer_freq(b) == 'B'
    assert _infer_freq(d) == 'D'
    assert _infer_freq(w) == 'W'
    assert _infer_freq(m) == 'M'
    assert _infer_freq(a) == 'A'
    assert _infer_freq(q) == 'Q'
    assert _infer_freq(d[:3]) == 'D'
    assert _infer_freq(w[:3]) == 'W'
    assert _infer_freq(m[:3]) == 'M'
    assert _infer_freq(a[:3]) == 'A'
    assert _infer_freq(q[:3]) == 'Q'
Beispiel #16
0
def test_period_index():
    # tests 1285
    from pandas import PeriodIndex
    dates = PeriodIndex(start="1/1/1990", periods=20, freq="M")
    npt.assert_(_infer_freq(dates) == "M")
Beispiel #17
0
def test_infer_freq():
    d1 = datetime(2008, 12, 31)
    d2 = datetime(2012, 9, 30)

    b = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['B']).values
    d = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['D']).values
    w = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['W']).values
    m = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['M']).values
    a = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['A']).values
    q = DatetimeIndex(start=d1, end=d2, freq=_freq_to_pandas['Q']).values

    npt.assert_(_infer_freq(w) == 'W-SUN')
    npt.assert_(_infer_freq(a) == 'A-DEC')
    npt.assert_(_infer_freq(q) == 'Q-DEC')
    npt.assert_(_infer_freq(w[:3]) == 'W-SUN')
    npt.assert_(_infer_freq(a[:3]) == 'A-DEC')
    npt.assert_(_infer_freq(q[:3]) == 'Q-DEC')
    npt.assert_(_infer_freq(b[2:5]) == 'B')
    npt.assert_(_infer_freq(b[:3]) == 'D')
    npt.assert_(_infer_freq(b) == 'B')
    npt.assert_(_infer_freq(d) == 'D')
    npt.assert_(_infer_freq(m) == 'M')
    npt.assert_(_infer_freq(d[:3]) == 'D')
    npt.assert_(_infer_freq(m[:3]) == 'M')
def test_infer_freq():
    from pandas import DateRange

    d1 = datetime(2008, 12, 31)
    d2 = datetime(2012, 9, 30)

    b = DateRange(d1, d2, offset=_freq_to_pandas["B"]).values
    d = DateRange(d1, d2, offset=_freq_to_pandas["D"]).values
    w = DateRange(d1, d2, offset=_freq_to_pandas["W"]).values
    m = DateRange(d1, d2, offset=_freq_to_pandas["M"]).values
    a = DateRange(d1, d2, offset=_freq_to_pandas["A"]).values
    q = DateRange(d1, d2, offset=_freq_to_pandas["Q"]).values

    assert _infer_freq(b[2:5]) == "B"
    assert _infer_freq(b[:3]) == "D"

    assert _infer_freq(b) == "B"
    assert _infer_freq(d) == "D"
    assert _infer_freq(w) == "W"
    assert _infer_freq(m) == "M"
    assert _infer_freq(a) == "A"
    assert _infer_freq(q) == "Q"
    assert _infer_freq(d[:3]) == "D"
    assert _infer_freq(w[:3]) == "W"
    assert _infer_freq(m[:3]) == "M"
    assert _infer_freq(a[:3]) == "A"
    assert _infer_freq(q[:3]) == "Q"
Beispiel #19
0
def test_infer_freq():
    from pandas import DateRange
    d1 = datetime(2008, 12, 31)
    d2 = datetime(2012, 9, 30)

    b = DateRange(d1, d2, offset=_freq_to_pandas['B']).values
    d = DateRange(d1, d2, offset=_freq_to_pandas['D']).values
    w = DateRange(d1, d2, offset=_freq_to_pandas['W']).values
    m = DateRange(d1, d2, offset=_freq_to_pandas['M']).values
    a = DateRange(d1, d2, offset=_freq_to_pandas['A']).values
    q = DateRange(d1, d2, offset=_freq_to_pandas['Q']).values

    npt.assert_string_equal(_infer_freq(b), 'B')
    npt.assert_string_equal(_infer_freq(d), 'D')
    npt.assert_string_equal(_infer_freq(w), 'W')
    npt.assert_string_equal(_infer_freq(m), 'M')
    npt.assert_string_equal(_infer_freq(a), 'A')
    npt.assert_string_equal(_infer_freq(q), 'Q')
    npt.assert_string_equal(_infer_freq(b[2:4]), 'B')
    npt.assert_string_equal(_infer_freq(b[:2]), 'D')
    npt.assert_string_equal(_infer_freq(d[:2]), 'D')
    npt.assert_string_equal(_infer_freq(w[:2]), 'W')
    npt.assert_string_equal(_infer_freq(m[:2]), 'M')
    npt.assert_string_equal(_infer_freq(a[:2]), 'A')
    npt.assert_string_equal(_infer_freq(q[:2]), 'Q')
def test_period_index():
    dates = PeriodIndex(start="1/1/1990", periods=20, freq="M")
    npt.assert_(_infer_freq(dates) == "M")