Skip to content

Commit 81adb77

Browse files
committed
- adapt pandas >= 1.5.0 - add group_keys when the result from apply is a like-indexed Series or DataFrame
1 parent 4d621bf commit 81adb77

File tree

5 files changed

+8
-8
lines changed

5 files changed

+8
-8
lines changed

qlib/backtest/position.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def fill_stock_value(self, start_time: Union[str, pd.Timestamp], freq: str, last
311311
freq=freq,
312312
disk_cache=True,
313313
).dropna()
314-
price_dict = price_df.groupby(["instrument"]).tail(1).reset_index(level=1, drop=True)["$close"].to_dict()
314+
price_dict = price_df.groupby(["instrument"], group_keys=False).tail(1)["$close"].to_dict()
315315

316316
if len(price_dict) < len(stock_list):
317317
lack_stock = set(stock_list) - set(price_dict)

qlib/contrib/eva/alpha.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ def calc_long_short_prec(
4747
if dropna:
4848
df.dropna(inplace=True)
4949

50-
group = df.groupby(level=date_col)
50+
group = df.groupby(level=date_col, group_keys=False)
5151

5252
def N(x):
5353
return int(len(x) * quantile)
5454

5555
# find the top/low quantile of prediction and treat them as long and short target
56-
long = group.apply(lambda x: x.nlargest(N(x), columns="pred").label).reset_index(level=0, drop=True)
57-
short = group.apply(lambda x: x.nsmallest(N(x), columns="pred").label).reset_index(level=0, drop=True)
56+
long = group.apply(lambda x: x.nlargest(N(x), columns="pred").label)
57+
short = group.apply(lambda x: x.nsmallest(N(x), columns="pred").label)
5858

5959
groupll = long.groupby(date_col)
6060
l_dom = groupll.apply(lambda x: x > 0)

qlib/contrib/report/analysis_position/parse_position.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _calculate_day_value(g_df: pd.DataFrame):
132132
g_df["excess_return"] = g_df[_label_name] - g_df[_label_name].mean()
133133
return g_df
134134

135-
return df.groupby(level="datetime").apply(_calculate_day_value)
135+
return df.groupby(level="datetime", group_keys=False).apply(_calculate_day_value)
136136

137137

138138
def get_position_data(

qlib/contrib/report/analysis_position/rank_label.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _get_figure_with_position(
3131
)
3232

3333
res_dict = dict()
34-
_pos_gp = _position_df.groupby(level=1)
34+
_pos_gp = _position_df.groupby(level=1, group_keys=False)
3535
for _item in _pos_gp:
3636
_date = _item[0]
3737
_day_df = _item[1]

qlib/utils/paral.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def datetime_groupby_apply(
5151

5252
def _naive_group_apply(df):
5353
if isinstance(apply_func, str):
54-
return getattr(df.groupby(axis=axis, level=level), apply_func)()
55-
return df.groupby(axis=axis, level=level).apply(apply_func)
54+
return getattr(df.groupby(axis=axis, level=level, group_keys=False), apply_func)()
55+
return df.groupby(axis=axis, level=level, group_keys=False).apply(apply_func)
5656

5757
if n_jobs != 1:
5858
dfs = ParallelExt(n_jobs=n_jobs)(

0 commit comments

Comments
 (0)