Fix TslibDataModule generating synthetic time indices instead of real ones - #2342
Open
ShreyanshGoyal wants to merge 1 commit into
Open
Fix TslibDataModule generating synthetic time indices instead of real ones#2342ShreyanshGoyal wants to merge 1 commit into
ShreyanshGoyal wants to merge 1 commit into
Conversation
… ones _TslibDataset.__getitem__ built history_time_idx/future_time_idx from torch.arange(context_length) and torch.arange(context_length, context_length + prediction_length) -- synthetic, always-zero-start positional indices -- instead of the real per-series timestep values already computed in processed_data["timestep"] (the line reading from it was present but commented out). For any window that doesn't start at series position 0, or for irregular/gapped time axes, this silently fed models the wrong temporal context: e.g. history [10, 20, 35] would report as [0, 1, 2]. Fixes sktime#2263. The real timestep values are numpy arrays (numpy.ndarray from TimeSeries, not necessarily plain integers -- datetime64 is a supported time column type), and torch.as_tensor rejects datetime64 arrays outright, so a small _time_values_to_tensor helper converts datetime64 to its underlying int64 ticks first. Added test_time_idx_reflects_real_timestamps, using the exact irregular, non-zero-start reproduction from the issue, asserting history/future time values are a real, contiguous, in-order slice of the dataset's own time axis (fails with the original synthetic-arange bug), and test_time_idx_supports_datetime_time_column, covering a datetime64 time column end-to-end. Note: test_multivariate_target in the same file already fails on an unrelated, pre-existing bug (list vs. tensor shape assertion) independent of this change, as flagged in the issue -- confirmed by reproducing the same failure on the unmodified file.
ShreyanshGoyal
requested review from
benHeid,
fkiraly,
jdb78 and
phoeenniixx
as code owners
July 13, 2026 07:22
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2342 +/- ##
=======================================
Coverage ? 86.92%
=======================================
Files ? 167
Lines ? 9783
Branches ? 0
=======================================
Hits ? 8504
Misses ? 1279
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2263.
_TslibDataset.__getitem__builthistory_time_idx/future_time_idxfromtorch.arange(context_length)andtorch.arange(context_length, context_length + prediction_length)— synthetic, always-zero-start positional indices — instead of the real per-series timestep values already computed inprocessed_data["timestep"]. The line reading from it was present in the source but commented out:For any window that doesn't start at series position 0, or for irregular/gapped time axes, this silently fed models the wrong temporal context. Using the issue's repro (
time_idx = [10, 20, 35, 50, 80, 120],context_length=3,prediction_length=2):Fix
Restored
history_time_idx/future_time_idxto read fromprocessed_data["timestep"]. One wrinkle:TimeSeries.__getitem__returnstas a plainnumpy.ndarray(not a tensor), and a time column can legitimately bedatetime64(seetest_multivariate_targetin the existing test file, which usespd.date_range) —torch.as_tensorrejectsdatetime64arrays outright. Added a small_time_values_to_tensorhelper that viewsdatetime64values as their underlying int64 ticks before conversion; plain int/float time columns pass through unchanged.Test plan
test_time_idx_reflects_real_timestamps, using the exact irregular, non-zero-start reproduction from the issue — assertshistory_time_idx/future_time_idxare a real, contiguous, in-order slice of the dataset's own time axis for every window. Confirmed this fails with the original synthetic-arangeoutput on the pre-fix code and passes after the fix.test_time_idx_supports_datetime_time_column, covering adatetime64time column end-to-end (this wouldTypeErroron a naivetorch.as_tensor(...)fix without the datetime handling).pytest pytorch_forecasting/data/tests/test_tslib_data_module.py -v— 15 passed, 1 failed. The failure (test_multivariate_target,AttributeError: 'list' object has no attribute 'shape') is the same pre-existing, unrelated failure flagged in the issue itself — confirmed by reproducing the identical failure against the unmodified file onmain.ruff check/ruff format --checkon both changed files — clean (left one unrelated pre-existing formatting drift elsewhere in the source file untouched, to keep the diff focused).