MCPcopy
hub / github.com/microsoft/qlib / sum_by_index

Function sum_by_index

qlib/utils/index_data.py:57–84  ·  view source on GitHub ↗

concat all SingleData by new index. Parameters ---------- data_list : List[SingleData] the list of all SingleData to sum. new_index : list the new_index of new SingleData. fill_value : float fill the missing values or replace np.nan. Returns ----

(data_list: Union[SingleData], new_index: list, fill_value=0)

Source from the content-addressed store, hash-verified

55
56
57def sum_by_index(data_list: Union[SingleData], new_index: list, fill_value=0) -> SingleData:
58 """concat all SingleData by new index.
59
60 Parameters
61 ----------
62 data_list : List[SingleData]
63 the list of all SingleData to sum.
64 new_index : list
65 the new_index of new SingleData.
66 fill_value : float
67 fill the missing values or replace np.nan.
68
69 Returns
70 -------
71 SingleData
72 the SingleData with new_index and values after sum.
73 """
74 data_list = [data.to_dict() for data in data_list]
75 data_sum = {}
76 for id in new_index:
77 item_sum = 0
78 for data in data_list:
79 if id in data and not np.isnan(data[id]):
80 item_sum += data[id]
81 else:
82 item_sum += fill_value
83 data_sum[id] = item_sum
84 return SingleData(data_sum)
85
86
87class Index:

Callers

nothing calls this directly

Calls 2

SingleDataClass · 0.85
to_dictMethod · 0.80

Tested by

no test coverage detected