Skip to content

Commit

Permalink
move functions is_in_valid_relative_index_types and is_in_valid_absol…
Browse files Browse the repository at this point in the history
…ute_index_types to utils/validation/series.py
  • Loading branch information
Stanislav Khrapov committed Apr 3, 2022
1 parent 5780916 commit e853c22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
23 changes: 6 additions & 17 deletions sktime/forecasting/base/_fh.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
)
from sktime.utils.validation.series import (
VALID_INDEX_TYPES,
is_in_valid_absolute_index_types,
is_in_valid_index_types,
is_integer_index,
is_in_valid_relative_index_types,
)

RELATIVE_TYPES = (pd.RangeIndex, pd.TimedeltaIndex)
ABSOLUTE_TYPES = (pd.RangeIndex, pd.DatetimeIndex, pd.PeriodIndex)
assert set(RELATIVE_TYPES).issubset(VALID_INDEX_TYPES)
assert set(ABSOLUTE_TYPES).issubset(VALID_INDEX_TYPES)
VALID_FORECASTING_HORIZON_TYPES = (int, list, np.ndarray, pd.Index)

DELEGATED_METHODS = (
Expand Down Expand Up @@ -59,14 +56,6 @@
)


def is_relative_fh_type(values) -> bool:
return type(values) in RELATIVE_TYPES or is_integer_index(values)


def is_absolute_fh_type(values) -> bool:
return type(values) in ABSOLUTE_TYPES or is_integer_index(values)


def _delegator(method):
"""Automatically decorate ForecastingHorizon class with pandas.Index methods.
Expand Down Expand Up @@ -194,17 +183,17 @@ def __init__(
# types inherit from each other, hence we check for type equality
error_msg = f"`values` type is not compatible with `is_relative={is_relative}`."
if is_relative is None:
if is_relative_fh_type(values):
if is_in_valid_relative_index_types(values):
is_relative = True
elif is_absolute_fh_type(values):
elif is_in_valid_absolute_index_types(values):
is_relative = False
else:
raise TypeError(f"{type(values)} is not a supported fh index type")
if is_relative:
if not is_relative_fh_type(values):
if not is_in_valid_relative_index_types(values):
raise TypeError(error_msg)
else:
if not is_absolute_fh_type(values):
if not is_in_valid_absolute_index_types(values):
raise TypeError(error_msg)

self._values = values
Expand Down
22 changes: 17 additions & 5 deletions sktime/utils/validation/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,30 @@
pd.DatetimeIndex,
pd.TimedeltaIndex,
)
RELATIVE_INDEX_TYPES = (pd.RangeIndex, pd.TimedeltaIndex)
ABSOLUTE_INDEX_TYPES = (pd.RangeIndex, pd.DatetimeIndex, pd.PeriodIndex)
assert set(RELATIVE_INDEX_TYPES).issubset(VALID_INDEX_TYPES)
assert set(ABSOLUTE_INDEX_TYPES).issubset(VALID_INDEX_TYPES)


def is_integer_index(x) -> bool:
"""Check that the input is an integer pd.Index."""
return isinstance(x, pd.Index) and x.is_integer()


def is_in_valid_index_types(x) -> bool:
"""Check that the input type belongs to the valid index types."""
return isinstance(x, VALID_INDEX_TYPES) or is_integer_index(x)


def is_in_valid_relative_index_types(x) -> bool:
return isinstance(x, RELATIVE_INDEX_TYPES) or is_integer_index(x)


def is_in_valid_absolute_index_types(x) -> bool:
return isinstance(x, ABSOLUTE_INDEX_TYPES) or is_integer_index(x)


def _check_is_univariate(y, var_name="input"):
"""Check if series is univariate."""
if isinstance(y, pd.DataFrame):
Expand Down Expand Up @@ -279,11 +296,6 @@ def check_equal_time_index(*ys, mode="equal"):
raise ValueError(msg)


def is_integer_index(x) -> bool:
"""Check that the input is an integer pd.Index."""
return isinstance(x, pd.Index) and x.is_integer()


def check_consistent_index_type(a, b):
"""Check that two indices have consistent types.
Expand Down

0 comments on commit e853c22

Please sign in to comment.