Skip to content

Commit b6b2b84

Browse files
committed
Revert "use hasattr instead of version to check whether rolling.rank is implemented"
This reverts commit f2915cc.
1 parent f2915cc commit b6b2b84

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

qlib/data/ops.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,15 +1148,25 @@ class Rank(Rolling):
11481148

11491149
def __init__(self, feature, N):
11501150
super(Rank, self).__init__(feature, N, "rank")
1151+
major_version, minor_version, *_ = pd.__version__.split(".")
1152+
self._load_internal = (
1153+
self._load_internal_pd14
1154+
if int(major_version) > 1 or int(major_version) == 1 and int(minor_version) > 3
1155+
else self._load_internal_pd_below_13
1156+
)
11511157

1152-
def _load_internal(self, instrument, start_index, end_index, *args):
1158+
def _load_internal_pd14(self, instrument, start_index, end_index, *args):
11531159
series = self.feature.load(instrument, start_index, end_index, *args)
1154-
rolling_or_expending = series.expanding(min_periods=1) if self.N == 0 else series.rolling(self.N, min_periods=1)
1160+
if self.N == 0:
1161+
series = series.expanding(min_periods=1).rank(pct=True)
1162+
else:
1163+
series = series.rolling(self.N, min_periods=1).rank(pct=True)
1164+
return series
11551165

1156-
if hasattr(rolling_or_expending, 'rank'):
1157-
return rolling_or_expending.rank(pct=True)
1166+
# for compatiblity of python 3.7, which doesn't support pandas 1.4.0+ which implements Rolling.rank
1167+
def _load_internal_pd_below_13(self, instrument, start_index, end_index, *args):
1168+
series = self.feature.load(instrument, start_index, end_index, *args)
11581169

1159-
# for compatiblity of python 3.7, which doesn't support pandas 1.4.0+ since when Rolling.rank is implemented
11601170
def rank(x):
11611171
if np.isnan(x[-1]):
11621172
return np.nan
@@ -1165,7 +1175,11 @@ def rank(x):
11651175
return np.nan
11661176
return percentileofscore(x1, x1[-1]) / 100
11671177

1168-
return rolling_or_expending.apply(rank, raw=True)
1178+
if self.N == 0:
1179+
series = series.expanding(min_periods=1).apply(rank, raw=True)
1180+
else:
1181+
series = series.rolling(self.N, min_periods=1).apply(rank, raw=True)
1182+
return series
11691183

11701184

11711185
class Count(Rolling):

0 commit comments

Comments
 (0)