def _check_frequency()

in fclib/fclib/dataset/ojdata.py [0:0]


def _check_frequency(df, time_col_name, frequency, time_format, ts_id_col_names):
    """Check if the data frequency is valid.
    """
    try:
        df[time_col_name] = pd.to_datetime(df[time_col_name], format=time_format)
        timestamps_all = pd.date_range(min(df[time_col_name]), end=max(df[time_col_name]), freq=frequency)
    except Exception:
        raise ValueError(
            "Input data frequency is invalid. Please use the aliases in "
            + "https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases"
        )

    condition1 = (ts_id_col_names is None) and (not set(df[time_col_name]) <= set(timestamps_all))
    condition2 = (ts_id_col_names is not None) and (
        not all(df.groupby(ts_id_col_names).apply(lambda x: set(x[time_col_name]) <= set(timestamps_all)))
    )
    if condition1 or condition2:
        raise ValueError(
            "Timestamp(s) with irregular frequency in the input dataframe. Please make sure the frequency "
            + "of each time series is as what specified by 'frequency'."
        )