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
7 changes: 6 additions & 1 deletion qlib/data/dataset/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ def __init__(
self.inst_processors
), f"freq(={self.freq}), inst_processors(={self.inst_processors}) cannot be None/empty"

def load(self, instruments=None, start_time=None, end_time=None) -> pd.DataFrame:
if self.is_group and isinstance(instruments, str):
instruments = D.instruments(instruments, filter_pipe=self.filter_pipe)
return super().load(instruments, start_time, end_time)

def load_group_df(
self,
instruments,
Expand All @@ -213,7 +218,7 @@ def load_group_df(
instruments = "all"
if isinstance(instruments, str):
instruments = D.instruments(instruments, filter_pipe=self.filter_pipe)
elif self.filter_pipe is not None:
elif self.filter_pipe is not None and not isinstance(instruments, dict):
warnings.warn("`filter_pipe` is not None, but it will not be used with `instruments` as list")

freq = self.freq[gp_name] if isinstance(self.freq, dict) else self.freq
Expand Down
34 changes: 34 additions & 0 deletions tests/data_mid_layer_tests/test_dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import sys
import unittest
import qlib
import pandas as pd
from pathlib import Path
from unittest.mock import patch

sys.path.append(str(Path(__file__).resolve().parent))
from qlib.data.dataset.loader import NestedDataLoader, QlibDataLoader
Expand Down Expand Up @@ -84,6 +86,38 @@ def test_nested_data_loader(self):
print(data)
"""

def test_grouped_loader_applies_filter_pipe_once(self):
loader = QlibDataLoader(
config={
"feature": (["$close"], ["close"]),
"label": (["Ref($close, -1)"], ["label"]),
},
filter_pipe=[{"filter_type": "SeriesDFilter"}],
)
instruments_config = {"market": "csi300", "filter_pipe": loader.filter_pipe}
index = pd.MultiIndex.from_tuples(
[("SH600000", pd.Timestamp("2020-01-01"))],
names=["instrument", "datetime"],
)

def fake_features(instruments, exprs, start_time, end_time, freq="day", inst_processors=None):
self.assertIs(instruments, instruments_config)
return pd.DataFrame([[1.0] * len(exprs)], index=index, columns=exprs)

with (
patch(
"qlib.data.dataset.loader.D.instruments",
return_value=instruments_config,
create=True,
) as instruments_mock,
patch("qlib.data.dataset.loader.D.features", side_effect=fake_features, create=True) as features_mock,
):
df = loader.load("csi300", start_time="2020-01-01", end_time="2020-01-02")

instruments_mock.assert_called_once_with("csi300", filter_pipe=loader.filter_pipe)
self.assertEqual(features_mock.call_count, 2)
self.assertEqual({"feature", "label"}, set(df.columns.get_level_values(0)))


if __name__ == "__main__":
unittest.main()