microsoft/qlib · error · ValueError

Empty data from dataset, please check your dataset config.

Error message

Empty data from dataset, please check your dataset config.

What it means

The TS LSTM fit() prepares 'train' and 'valid' as TSDataSampler-style handlers (data_key=DK_L) and requires both non-empty before configuring fillna and building DataLoaders. If either is empty it raises ValueError('Empty data from dataset, please check your dataset config.') — commonly caused by step_len/windowing producing no samples or segment ranges outside the data.

Source

Thrown at qlib/contrib/model/pytorch_lstm_ts.py:205

            loss = self.loss_fn(pred, label, weight.to(self.device))
            losses.append(loss.item())

            score = self.metric_fn(pred, label)
            scores.append(score.item())

        return np.mean(losses), np.mean(scores)

    def fit(
        self,
        dataset,
        evals_result=dict(),
        save_path=None,
        reweighter=None,
    ):
        dl_train = dataset.prepare("train", col_set=["feature", "label"], data_key=DataHandlerLP.DK_L)
        dl_valid = dataset.prepare("valid", col_set=["feature", "label"], data_key=DataHandlerLP.DK_L)
        if dl_train.empty or dl_valid.empty:
            raise ValueError("Empty data from dataset, please check your dataset config.")

        dl_train.config(fillna_type="ffill+bfill")  # process nan brought by dataloader
        dl_valid.config(fillna_type="ffill+bfill")  # process nan brought by dataloader

        if reweighter is None:
            wl_train = np.ones(len(dl_train))
            wl_valid = np.ones(len(dl_valid))
        elif isinstance(reweighter, Reweighter):
            wl_train = reweighter.reweight(dl_train)
            wl_valid = reweighter.reweight(dl_valid)
        else:
            raise ValueError("Unsupported reweighter type.")

        train_loader = DataLoader(
            ConcatDataset(dl_train, wl_train),
            batch_size=self.batch_size,
            shuffle=True,
            num_workers=self.n_jobs,

View on GitHub (pinned to 79633dd950)

Solutions

  1. Check both prepared handlers' lengths (len(dataset.prepare('train', col_set=['feature','label'], data_key='learn'))) to identify which is empty.
  2. Realign segment windows with the handler's actual date coverage (inspect the handler's underlying index).
  3. Ensure the data handler itself has data (fetch and check shape) and the instruments were not all filtered out.
  4. For TS datasets, confirm the window/step_len leaves at least one sample per segment.

Example fix

# before
dataset = DatasetH(handler, segments={'train': ('2017-01-01','2017-12-31'), 'valid': ('2018-01-01','2018-06-30')})
# data only starts 2019 -> dl_train.empty
model.fit(dataset)  # ValueError: Empty data

# after
dataset = DatasetH(handler, segments={'train': ('2019-01-01','2019-10-31'), 'valid': ('2019-11-01','2019-12-31')})
model.fit(dataset)
Defensive patterns

Strategy: validation

Validate before calling

for seg in ("train", "valid"):
    dl = dataset.prepare(seg, col_set=["feature", "label"], data_key="learn")
    if getattr(dl, "empty", False) or len(dl) == 0:
        raise ValueError(f"'{seg}' prepared 0 samples; fix segments/windowing config")

Try / catch

try:
    model.fit(dataset)
except ValueError as e:
    if "Empty data" in str(e):
        # inspect prepare('train')/prepare('valid') sizes; fix date windows
        raise
    raise

Prevention

When it happens

Trigger: model.fit(dataset) where dataset.prepare('train'|'valid', col_set=['feature','label'], data_key=DK_L).empty is True — segment dates disjoint from the data calendar, handler with no data, or time-series windowing yielding zero samples.

Common situations: Segments beyond the handler's end date; dataset built on a calendar where the valid window contains no trading days; all instruments dropped from the handler; misconfigured start/end times in the underlying data handler.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/85e198f12963e6b5. Report an issue: GitHub.