mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-10 06:20:57 +08:00
fix some comments and add docstring
This commit is contained in:
@@ -70,3 +70,27 @@ def fetch_df_by_index(
|
||||
return df.loc[
|
||||
pd.IndexSlice[idx_slc],
|
||||
]
|
||||
|
||||
|
||||
def convert_index_format(df: Union[pd.DataFrame, pd.Series], level: str = "datetime") -> Union[pd.DataFrame, pd.Series]:
|
||||
"""
|
||||
Convert the format of df.MultiIndex according to the following rules:
|
||||
- If `level` is the first level of df.MultiIndex, do nothing
|
||||
- If `level` is the second level of df.MultiIndex, swap the level of index.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
df : Union[pd.DataFrame, pd.Series]
|
||||
raw DataFrame/Series
|
||||
level : str, optional
|
||||
the level that will be converted to the first one, by default "datetime"
|
||||
|
||||
Returns
|
||||
-------
|
||||
Union[pd.DataFrame, pd.Series]
|
||||
converted DataFrame/Series
|
||||
"""
|
||||
|
||||
if get_level_index(df, level=level) == 1:
|
||||
df = df.swaplevel().sort_index()
|
||||
return df
|
||||
|
||||
Reference in New Issue
Block a user