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
Raised by TransformerTSModel.fit when either the 'train' or 'valid' segment prepared from the DatasetH is empty (zero rows) after calling dataset.prepare with col_set=['feature','label'] and DataHandlerLP.DK_L. The model cannot train on an empty dataframe, so it fails fast rather than crashing later inside the PyTorch training loop. It almost always points to a dataset/segment configuration problem, not a model problem.
Source
Thrown at qlib/contrib/model/pytorch_transformer_ts.py:147
loss = self.loss_fn(pred, label)
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: DatasetH,
evals_result=dict(),
save_path=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
train_loader = DataLoader(
dl_train, batch_size=self.batch_size, shuffle=True, num_workers=self.n_jobs, drop_last=True
)
valid_loader = DataLoader(
dl_valid, batch_size=self.batch_size, shuffle=False, num_workers=self.n_jobs, drop_last=True
)
save_path = get_or_create_path(save_path)
stop_steps = 0
train_loss = 0
best_score = -np.inf
best_epoch = 0
evals_result["train"] = []
View on GitHub (pinned to 79633dd950)
Solutions
- Inspect the segments: print(dataset.prepare('train', col_set=['feature','label'], data_key=DataHandlerLP.DK_L).shape) and the same for 'valid' to confirm which one is empty.
- Verify your date segments overlap the qlib calendar loaded at init (D.calendar()) and that the local bin data covers those dates.
- Check the instruments file / market expression used when creating the dataset actually resolves to instruments with data in the segment window.
- Ensure the data handler was set up with the standard train/valid/test split (DatasetH with segments covering all three) rather than only 'test'.
Example fix
# before
model.fit(dataset) # ValueError: Empty data from dataset
# after
for seg in ["train", "valid"]:
df = dataset.prepare(seg, col_set=["feature", "label"], data_key=DataHandlerLP.DK_L)
print(seg, df.shape) # find which segment is empty, fix segments/instruments
model.fit(dataset) Defensive patterns
Strategy: validation
Validate before calling
from qlib.data.dataset.handler import DataHandlerLP
for seg in ("train", "valid"):
df = dataset.prepare(seg, col_set=["feature", "label"], data_key=DataHandlerLP.DK_L)
if df.empty:
raise RuntimeError(f"segment '{seg}' is empty; check segments/instruments/calendar")
model.fit(dataset) Try / catch
try:
model.fit(dataset)
except ValueError as e:
if "Empty data from dataset" in str(e):
# log segments and calendar range, then fix dataset config
log.error("empty segment; train=%s valid=%s", train_shape, valid_shape)
raise Prevention
- Print .shape for every prepared segment before fitting.
- Assert segment date ranges overlap D.calendar() for your provider.
- Validate the instrument universe resolves to non-empty data before building handlers.
When it happens
Trigger: Calling fit() on a DatasetH whose 'train' or 'valid' segment has no data: date segments that don't overlap the underlying qlib calendar/data range, wrong segment keys in handlers (e.g. missing 'train' or 'valid'), an instrument universe with no valid stock data in the segment window, or a data handler config that filters out all rows under DK_L.
Common situations: Copying an example config (e.g. alpha158 workflow) but changing the date range to years not present in the local qlib data dump; specifying test/start dates outside the calendar; using a custom instrument file whose instruments have no cached features; passing a dataset built with data_key that yields empty learn data.
Related errors
- Empty data from dataset, please check your dataset config.
- Empty data from dataset, please check your dataset config.
- Empty data from dataset, please check your dataset config.
- unknown loss `%s`
- unknown metric `%s`
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/1a9b3bce259eac14.
Report an issue: GitHub.