{"id":15653177,"url":"https://github.com/fbruzzesi/timebasedcv","last_synced_at":"2025-07-23T09:05:39.415Z","repository":{"id":181867168,"uuid":"667551224","full_name":"FBruzzesi/timebasedcv","owner":"FBruzzesi","description":"Time based splits for cross validation","archived":false,"fork":false,"pushed_at":"2025-07-17T15:27:35.000Z","size":1775,"stargazers_count":38,"open_issues_count":4,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-17T17:52:06.732Z","etag":null,"topics":["cross-validation","data-science","python","time-series","time-series-analysis"],"latest_commit_sha":null,"homepage":"https://fbruzzesi.github.io/timebasedcv/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FBruzzesi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-07-17T19:08:41.000Z","updated_at":"2025-07-01T06:56:44.000Z","dependencies_parsed_at":"2024-01-05T16:26:18.929Z","dependency_job_id":"38ed90e2-7b6a-43c0-a125-bb819b46fad4","html_url":"https://github.com/FBruzzesi/timebasedcv","commit_stats":null,"previous_names":["fbruzzesi/timebased-cv","fbruzzesi/timebasedcv"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/FBruzzesi/timebasedcv","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FBruzzesi%2Ftimebasedcv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FBruzzesi%2Ftimebasedcv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FBruzzesi%2Ftimebasedcv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FBruzzesi%2Ftimebasedcv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FBruzzesi","download_url":"https://codeload.github.com/FBruzzesi/timebasedcv/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FBruzzesi%2Ftimebasedcv/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265644909,"owners_count":23804179,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-validation","data-science","python","time-series","time-series-analysis"],"created_at":"2024-10-03T12:44:53.033Z","updated_at":"2025-07-23T09:05:39.400Z","avatar_url":"https://github.com/FBruzzesi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"https://raw.githubusercontent.com/FBruzzesi/timebasedcv/main/docs/img/timebasedcv-logo.svg\" width=185 height=185 align=\"right\"\u003e\n\n![license-shield](https://img.shields.io/github/license/FBruzzesi/timebasedcv)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n![coverage-badge](https://raw.githubusercontent.com/FBruzzesi/timebasedcv/main/docs/img/coverage.svg)\n![versions-shield](https://img.shields.io/pypi/pyversions/timebasedcv)\n\n# Time based cross validation\n\n**timebasedcv** is a Python codebase that provides a cross validation strategy based on time.\n\n---\n\n[Documentation](https://fbruzzesi.github.io/timebasedcv) | [Repository](https://github.com/fbruzzesi/timebasedcv) | [Issue Tracker](https://github.com/fbruzzesi/timebasedcv/issues)\n\n---\n\n## Disclaimer ⚠️\n\nThis codebase is experimental and is working for my use cases. It is very probable that there are cases not entirely covered and for which it could break (badly). If you find them, please feel free to open an issue in the [issue page](https://github.com/FBruzzesi/timebasedcv/issues/new) of the repo.\n\n## Description ✨\n\nThe current implementation of [scikit-learn TimeSeriesSplit](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html) lacks the flexibility of having multiple samples within the same time period (or time unit).\n\n**timebasedcv** addresses such problem by providing a cross validation strategy based on a **time period** rather than the number of samples. This is useful when the data is time dependent, and the split should keep together samples within the same time window.\n\nTemporal data leakage is an issue and we want to prevent it from happening by providing splits that make sure the past and the future are well separated, so that data leakage does not spoil in a model cross validation.\n\nAgain, these splits points solely depend on the time period and not the number of observations.\n\n### Features 📜\n\nWe introduce two main classes:\n\n- [`TimeBasedSplit`](https://fbruzzesi.github.io/timebasedcv/api/timebasedcv/#timebasedcv.core.TimeBasedSplit) allows to define a split based on time unit (frequency), train size, test size, gap, stride, window type and mode. Remark that `TimeBasedSplit` is **not** compatible with [scikit-learn CV Splitters](https://scikit-learn.org/stable/common_pitfalls.html#id3). In fact, we have made the (opinioned) choice to:\n\n  - Return the sliced arrays from `.split(...)`, while scikit-learn CV Splitters return train and test indices of the split.\n  - Require to pass the time series as input to `.split(...)` method, while scikit-learn CV Splitters require to provide only `X, y, groups` to `.split(...)`.\n  - Such time series is used to generate the boolean masks with which we slice the original arrays into train and test for each split.\n\n- Considering the above choices, we also provide a scikit-learn compatible splitter: [`TimeBasedCVSplitter`](https://fbruzzesi.github.io/timebasedcv/api/sklearn/#timebasedcv.sklearn.TimeBasedCVSplitter). Considering the signature that `.split(...)` requires and the fact that CV Splitters need to know a priori the number of splits, `TimeBasedCVSplitter` is initialized with the time series containing the time information used to generate the train and test indices of each split.\n\n### Dataframe and array agnostic\n\n- Thanks to [Narwhals](https://narwhals-dev.github.io/narwhals/), `TimeBasedSplit` works out of the box with `pandas`, `polars`, `pyarrow` and any other dataframe library supported by Narwhals.\n- Thanks to the array API, `TimeBasedSplit` works out of the box with `numpy`, `cupy`, `dask.array` and any other array library that support slicing à la numpy.\n\n## Installation 💻\n\nTL;DR:\n\n```bash\npython -m pip install timebasedcv\n```\n\nFor further information, please refer to the dedicated [installation](https://fbruzzesi.github.io/timebasedcv/installation) section.\n\n## Quickstart 🏃\n\nThe following code snippet is all you need to get started, yet consider checking out the [getting started](https://fbruzzesi.github.io/timebasedcv/user-guide/getting-started/) section of the documentation for a detailed guide on how to use the library.\n\nThe main takeaway should be that `TimeBasedSplit` allows for a lot of flexibility at the cost of having to specify a long list of parameters. This is what makes the library so powerful and flexible to cover the large majority of use cases.\n\nFirst let's generate some data with different number of points per day:\n\n```python\nimport numpy as np\nimport pandas as pd\n\nRNG = np.random.default_rng(seed=42)\n\ndates = pd.Series(pd.date_range(\"2023-01-01\", \"2023-01-31\", freq=\"D\"))\nsize = len(dates)\n\ndf = (pd.concat([\n        pd.DataFrame({\n            \"time\": pd.date_range(start, end, periods=_size, inclusive=\"left\"),\n            \"a\": RNG.normal(size=_size-1),\n            \"b\": RNG.normal(size=_size-1),\n        })\n        for start, end, _size in zip(dates[:-1], dates[1:], RNG.integers(2, 24, size-1))\n    ])\n    .reset_index(drop=True)\n    .assign(y=lambda t: t[[\"a\", \"b\"]].sum(axis=1) + RNG.normal(size=t.shape[0])/25)\n)\n\ndf.set_index(\"time\").resample(\"D\").agg(count=(\"y\", np.size)).head(5)\n```\n\n```terminal\n            count\ntime\n2023-01-01      2\n2023-01-02     18\n2023-01-03     15\n2023-01-04     10\n2023-01-05     10\n```\n\nThen lets instantiate the `TimeBasedSplit` class:\n\n```python\nfrom timebasedcv import TimeBasedSplit\n\ntbs = TimeBasedSplit(\n    frequency=\"days\",\n    train_size=10,\n    forecast_horizon=5,\n    gap=1,\n    stride=3,\n    window=\"rolling\",\n    mode=\"forward\",\n)\n```\n\nNow let's run split the data with the provided `TimeBasedSplit` instance:\n\n```py title=\"Generate the splits\"\nX, y, time_series = df.loc[:, [\"a\", \"b\"]], df[\"y\"], df[\"time\"]\n\nfor X_train, X_forecast, y_train, y_forecast in tbs.split(X, y, time_series=time_series):\n    print(f\"Train: {X_train.shape}, Forecast: {X_forecast.shape}\")\n```\n\n```terminal\nTrain: (100, 2), Forecast: (51, 2)\nTrain: (114, 2), Forecast: (50, 2)\n...\nTrain: (124, 2), Forecast: (40, 2)\nTrain: (137, 2), Forecast: (22, 2)\n```\n\nAs we can see, each split does not necessarely have the same number of points, this is because the time series has a different number of points per day.\n\nA picture is worth a thousand words, let's visualize the splits (blue dots represent the train points, while the red dots represent the forecastng points):\n\n![cross-validation](docs/img/basic-cv-split.png)\n\n## Contributing ✌️\n\nPlease read the [Contributing guidelines](https://fbruzzesi.github.io/timebasedcv/contribute/) in the documentation site.\n\n## License 👀\n\nThe project has a [MIT Licence](https://github.com/FBruzzesi/timebasedcv/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffbruzzesi%2Ftimebasedcv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffbruzzesi%2Ftimebasedcv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffbruzzesi%2Ftimebasedcv/lists"}