From 4da3f3b104dd36b1a73de77e93fa0d15ecc237dd Mon Sep 17 00:00:00 2001 From: "wangwenxi.handsome" Date: Wed, 1 Sep 2021 13:17:55 +0000 Subject: [PATCH] broadcast_to and get single data --- qlib/backtest/high_performance_ds.py | 2 +- qlib/utils/index_data.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/qlib/backtest/high_performance_ds.py b/qlib/backtest/high_performance_ds.py index b185f0d51..86d631bfa 100644 --- a/qlib/backtest/high_performance_ds.py +++ b/qlib/backtest/high_performance_ds.py @@ -150,7 +150,7 @@ class CN1minNumpyQuote(BaseQuote): # single data # If it don't consider the classification of single data, it will consume a lot of time. - if is_single_value(start_time, end_time, self.freq) and method is not None: + if is_single_value(start_time, end_time, self.freq): # this is a very special case. # skip aggregating function to speed-up the query calculation try: diff --git a/qlib/utils/index_data.py b/qlib/utils/index_data.py index e4fc026d7..5594a27f9 100644 --- a/qlib/utils/index_data.py +++ b/qlib/utils/index_data.py @@ -335,7 +335,8 @@ class IndexData(metaclass=index_data_ops_creator): data_shape = tuple(data_shape) # broadcast the data to expected shape - self.data = np.broadcast_to(self.data, data_shape) + if self.data.shape != data_shape: + self.data = np.broadcast_to(self.data, data_shape) self.data = self.data.astype(np.float64) # Please notice following cases when converting the type @@ -492,6 +493,8 @@ class SingleData(IndexData): elif isinstance(data, pd.Series): assert len(index) == 0 index, data = data.index, data.values + elif isinstance(data, (int, float, np.number)): + data = [data] super().__init__(data, index) assert self.ndim == 1