1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-12 07:16:54 +08:00

Fix the aggregation function of IndexData

This commit is contained in:
Young
2021-10-22 15:20:45 +08:00
parent a58bc03a8e
commit 64130d9407
3 changed files with 27 additions and 4 deletions

View File

@@ -160,6 +160,11 @@ class NumpyQuote(BaseQuote):
if is_single_value(start_time, end_time, self.freq, self.region):
# this is a very special case.
# skip aggregating function to speed-up the query calculation
# FIXME:
# it will go to the else logic when it comes to the
# 1) the day before holiday when daily trading
# 2) the last minute of the day when intraday trading
try:
return self.data[stock_id].loc[start_time, field]
except KeyError:

View File

@@ -401,6 +401,10 @@ class IndexData(metaclass=index_data_ops_creator):
def columns(self):
return self.indices[1]
def __getitem__(self, args):
# NOTE: this tries to behave like a numpy array to be compatible with numpy aggregating function like nansum and nanmean
return self.iloc[args]
def _align_indices(self, other: "IndexData") -> "IndexData":
"""
Align all indices of `other` to `self` before performing the arithmetic operations.
@@ -409,7 +413,7 @@ class IndexData(metaclass=index_data_ops_creator):
Parameters
----------
other : "IndexData"
the index in `other` is to be chagned
the index in `other` is to be changed
Returns
-------
@@ -455,7 +459,8 @@ class IndexData(metaclass=index_data_ops_creator):
"""
return len(self.data)
def sum(self, axis=None):
def sum(self, axis=None, dtype=None, out=None):
assert out is None and dtype is None, "`out` is just for compatible with numpy's aggregating function"
# FIXME: weird logic and not general
if axis is None:
return np.nansum(self.data)
@@ -468,7 +473,8 @@ class IndexData(metaclass=index_data_ops_creator):
else:
raise ValueError(f"axis must be None, 0 or 1")
def mean(self, axis=None):
def mean(self, axis=None, dtype=None, out=None):
assert out is None and dtype is None, "`out` is just for compatible with numpy's aggregating function"
# FIXME: weird logic and not general
if axis is None:
return np.nanmean(self.data)