Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/sktime_mcp/data/adapters/file_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ def load(self) -> pd.DataFrame:
# Set frequency if specified
freq = self.config.get("frequency")
if freq:
with contextlib.suppress(Exception):
try:
df = df.asfreq(freq)
except Exception as e:
raise ValueError(f"Invalid frequency '{freq}': {e}") from e

self._data = df

Expand Down
5 changes: 3 additions & 2 deletions src/sktime_mcp/data/adapters/pandas_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
time index detection and validation.
"""

import contextlib
from typing import Any

import pandas as pd
Expand Down Expand Up @@ -72,8 +71,10 @@ def load(self) -> pd.DataFrame:
# Infer or set frequency
freq = self.config.get("frequency")
if freq:
with contextlib.suppress(Exception):
try:
df = df.asfreq(freq)
except Exception as e:
raise ValueError(f"Invalid frequency '{freq}': {e}") from e
elif isinstance(df.index, pd.DatetimeIndex) and df.index.freq is None:
# Try to infer frequency
inferred_freq = pd.infer_freq(df.index)
Expand Down
67 changes: 67 additions & 0 deletions tests/test_data_frequency_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Tests for explicit frequency validation in data adapters."""

from pathlib import Path

import pytest

from sktime_mcp.data.adapters.file_adapter import FileAdapter
from sktime_mcp.data.adapters.pandas_adapter import PandasAdapter


def test_pandas_adapter_rejects_invalid_explicit_frequency():
"""Invalid user-provided frequency should fail instead of being ignored."""
adapter = PandasAdapter(
{
"type": "pandas",
"data": {
"date": ["2020-01-01", "2020-01-02", "2020-01-03"],
"value": [1, 2, 3],
},
"time_column": "date",
"target_column": "value",
"frequency": "not_a_freq",
}
)

with pytest.raises(ValueError, match="Invalid frequency 'not_a_freq'"):
adapter.load()


def test_file_adapter_rejects_invalid_explicit_frequency(tmp_path: Path):
"""Invalid explicit frequency should fail for file-backed data too."""
path = tmp_path / "series.csv"
path.write_text("date,value\n2020-01-01,1\n2020-01-02,2\n2020-01-03,3\n")

adapter = FileAdapter(
{
"type": "file",
"path": str(path),
"time_column": "date",
"target_column": "value",
"frequency": "not_a_freq",
}
)

with pytest.raises(ValueError, match="Invalid frequency 'not_a_freq'"):
adapter.load()


def test_pandas_adapter_applies_valid_explicit_frequency():
"""Valid explicit frequency should still be applied normally."""
adapter = PandasAdapter(
{
"type": "pandas",
"data": {
"date": ["2020-01-01", "2020-01-03"],
"value": [1, 3],
},
"time_column": "date",
"target_column": "value",
"frequency": "D",
}
)

data = adapter.load()

assert len(data) == 3
assert data.index.freqstr == "D"