python Pandas日期时间序列报告特定间隔的值

wtlkbnrh  于 2023-01-08  发布在  Python
关注(0)|答案(1)|浏览(121)

给定打印数据(cot_report_splice)

date        symbol     Net Positioning
2020-10-20  PA         3413
            PL         7825
2020-10-27  PA         3468
            PL        10051
2020-11-03  PA         2416
                      ...  
2022-12-06  PL        25636
2022-12-13  PA         -883
            PL        28445
2022-12-20  PA        -2627
            PL        24052

我正在尝试打印(结果)的控制面板
| 符号|1周|2周|1个月|3MO| 1年|
| - ------|- ------|- ------|- ------|- ------|- ------|
| 主动脉|二六二七|八百八十三|二万五千|-1万||
| 损益|小行星24|小行星28445|三万五千|-5000||
然而,下面的for迭代被未知的日期时间字符串格式所束缚,无法解析:符号,我看不出有什么立即的补救措施。

def get_cot_report_fmp(start_date, end_date):

    session = requests.Session()

    request = f"https://financialmodelingprep.com/api/v4/commitment_of_traders_report?from={start_date}&to={end_date}\
                &apikey={FMP_API_KEY}".replace(" ", "")

    r = session.get(request)

    if r.status_code == requests.codes.ok:

        df = pd.DataFrame(json.loads(r.text))

        df['date'] = pd.to_datetime(df['date'])

        df = df.set_index('date').sort_index()

    return df

START_DATE = (date.today()-pd.DateOffset(800)).strftime('%Y-%m-%d')

END_DATE = date.today().strftime('%Y-%m-%d')

cot_report = get_cot_report_fmp(start_date=START_DATE, end_date=END_DATE)

contracts_list = ['PL', 'PA']

cot_report = cot_report[cot_report['symbol'].isin(contracts_list)]

cot_report = cot_report.reset_index().set_index(['date', 'symbol']).unstack().stack()

cot_report = cot_report[['noncomm_positions_long_all', 'noncomm_positions_short_all']]

cot_report['net_positioning'] = cot_report['noncomm_positions_long_all']-cot_report['noncomm_positions_short_all']

cot_report_splice = cot_report.loc[:, 'net_positioning']

results = pd.DataFrame(columns=['symbol', '1W', '2W', '3MO', '1YR'])

for symbol in cot_report_splice['symbol'].unique():
    symbol_df = cot_report_splice[cot_report_splice['symbol'] == symbol]
    
    most_recent_date = symbol_df['date'].max()
    
    week_mask = (symbol_df['date'] >= most_recent_date - pd.Timedelta(weeks=1)) & (symbol_df['date'] <= most_recent_date)
    two_week_mask = (symbol_df['date'] >= most_recent_date - pd.Timedelta(weeks=2)) & (symbol_df['date'] <= most_recent_date)
    three_mo_mask = (symbol_df['date'] >= most_recent_date - pd.Timedelta(weeks=15)) & (symbol_df['date'] <= most_recent_date)
    year_mask = (symbol_df['date'] >= most_recent_date - pd.Timedelta(weeks=52)) & (symbol_df['date'] <= most_recent_date)
    week_row = symbol_df.loc[week_mask, 'net_positioning'].iloc[0]
    two_week_row = symbol_df.loc[two_week_mask, 'net_positioning'].iloc[-1]
    three_mo_row = symbol_df.loc[three_mo_mask, 'net_positioning'].iloc[-1]
    year_row = symbol_df.loc[year_mask, 'net_positioning'].iloc[-1]
    
    results = results.append({'symbol': contract, '1W': week_row, '2W': two_week_row, '3M': three_mo_row, '1YR': year_row}, ignore_index=True)

results.set_index('symbol', inplace=True)

display(HTML(results._repr_html_()))
zkure5ic

zkure5ic1#

你可以用我在这里给出的答案https://stackoverflow.com/a/75036050/10251643
如果您应用了这个方法,那么创建cot_report_splice之后的代码将是:

cot_report_splice = cot_report.loc[:, 'net_positioning']

columns = ['1W', '2W', '3MO', '1YR']
weeks = [1, 2, 12, 52]
filter = [most_recent_date - pd.Timedelta(weeks=w) for w in weeks]
idx_level = cot_report_splice.index.get_level_values('date')
results = cot_report_splice.loc[idx_level.isin(filter)]
results.sort_index(level=['date', 'symbol'], ascending=[False, True], inplace=True)
d = dict(zip(filter, columns))
idx = results.index.levels[0].to_series().replace(d)
results.index = results.index.set_levels(idx, level='date')
results = results.unstack(level='date')
results.columns.rename(None, inplace=True)

有了它,您可以获得 Jmeter 板的信息。

相关问题