Exemple #1
0
def infer_freq(index, warn: bool = True) -> str | None:
    """
    Infer the most likely frequency given the input index.

    Parameters
    ----------
    index : DatetimeIndex or TimedeltaIndex
      If passed a Series will use the values of the series (NOT THE INDEX).
    warn : bool, default True
      .. deprecated:: 1.5.0

    Returns
    -------
    str or None
        None if no discernible frequency.

    Raises
    ------
    TypeError
        If the index is not datetime-like.
    ValueError
        If there are fewer than three values.

    Examples
    --------
    >>> idx = pd.date_range(start='2020/12/01', end='2020/12/30', periods=30)
    >>> pd.infer_freq(idx)
    'D'
    """
    from pandas.core.api import (
        DatetimeIndex,
        Float64Index,
        Index,
        Int64Index,
    )

    if isinstance(index, ABCSeries):
        values = index._values
        if not (is_datetime64_dtype(values) or is_timedelta64_dtype(values)
                or values.dtype == object):
            raise TypeError("cannot infer freq from a non-convertible dtype "
                            f"on a Series of {index.dtype}")
        index = values

    inferer: _FrequencyInferer

    if not hasattr(index, "dtype"):
        pass
    elif is_period_dtype(index.dtype):
        raise TypeError("PeriodIndex given. Check the `freq` attribute "
                        "instead of using infer_freq.")
    elif is_timedelta64_dtype(index.dtype):
        # Allow TimedeltaIndex and TimedeltaArray
        inferer = _TimedeltaFrequencyInferer(index, warn=warn)
        return inferer.get_freq()

    if isinstance(index, Index) and not isinstance(index, DatetimeIndex):
        if isinstance(index, (Int64Index, Float64Index)):
            raise TypeError(
                f"cannot infer freq from a non-convertible index type {type(index)}"
            )
        index = index._values

    if not isinstance(index, DatetimeIndex):
        index = DatetimeIndex(index)

    inferer = _FrequencyInferer(index, warn=warn)
    return inferer.get_freq()
Exemple #2
0
from datetime import datetime
import string

import numpy as np

from pandas.core.api import Series, DataFrame, DatetimeIndex
from pandas.stats.api import ols

N = 100

start = datetime(2009, 9, 2)
dateRange = DatetimeIndex(start, periods=N)


def makeDataFrame():
    data = DataFrame(np.random.randn(N, 7),
                     columns=list(string.ascii_uppercase[:7]),
                     index=dateRange)

    return data


def makeSeries():
    return Series(np.random.randn(N), index=dateRange)


#-------------------------------------------------------------------------------
# Standard rolling linear regression

X = makeDataFrame()
Y = makeSeries()