簡體   English   中英

Python中使用條件的時間序列分析

[英]Time series analysis in Python using conditions

我有以下數據(樣本)

Symbol Sections      iBid     Bid                Date
0    O.U20       O1  99.73167  99.730 2020-06-29 16:32:25
1    O.Z20       O1  99.70250  99.700 2020-06-29 16:32:25
2    O.H21       O1       NaN  99.795 2020-06-29 16:32:25
3    O.M21       O1  99.81167  99.810 2020-06-29 16:32:25
4    O.U21       O2  99.81667  99.815 2020-06-29 16:32:25
5    O.Z21       O2       NaN  99.795 2020-06-29 16:32:25
6    O.H22       O2  99.81000  99.810 2020-06-29 16:32:25
7    O.M22       O2  99.79500  99.795 2020-06-29 16:32:25
16  F3.U26       F3       NaN   1.000 2020-06-29 16:32:25
17  F3.Z26       F3       NaN  -3.000 2020-06-29 16:32:25
18  F3.H27       F3       NaN  -1.000 2020-06-29 16:32:25
19  F6.H26       F6  -1.75000     NaN 2020-06-29 16:32:25
20  F6.M26       F6  -4.50000     NaN 2020-06-29 16:32:25
21  F6.U26       F6  -5.50000     NaN 2020-06-29 16:32:25
22  F9.U20       F9  -8.50000  -9.000 2020-06-29 16:32:25
23   O.U20       O3  99.73167  99.730 2020-06-29 16:32:26
24   O.Z20       O3  99.70250  99.700 2020-06-29 16:32:26
25   O.H21       O3       NaN  99.795 2020-06-29 16:32:26
26   O.M21       O3  99.81167  99.810 2020-06-29 16:32:26
27   O.U21       O4  99.81667  99.815 2020-06-29 16:32:26
28   O.Z21       O4       NaN  99.795 2020-06-29 16:32:26
29   O.H22       O4  99.81000  99.810 2020-06-29 16:32:26
30   O.M22       O4  99.79500  99.795 2020-06-29 16:32:26

我想要做的是繪制散點圖或折線圖或任何適合這種分析的圖表,如果滿足條件,可以分析隨時間變化的趨勢。 例如,我想查看每個符號(O、S、F)以及部分(O1、F3 等)的 iBid 比 Bid 加班高多少倍

我知道我需要展示一些工作,但我不確定這樣的圖表是否可能? 到目前為止,我只能根據 Symbol 對數據進行拆分

df_O = df[df['Contract'].str.contains('O')]

並過濾掉類似的結果

IbidgreaterBid = big_frame[(big_frame.iBid > big_frame.Bid)]

是否可以獲得可以分析 Ibid > Bid 何時以 Date 列為 x 軸的圖表? (日期列有千行,只有秒的差異)

當同上>出價時可以分析的圖表不清楚您的意思。 但是,我可以建議一種基於 Ibid >/< Bid 來區分數據的方法。 在以下示例中,紅色散點表示 Ibid > Bid 的數據點,藍色表示其他情況。 此外,由於差異僅在秒的范圍內,我使用mdates日期格式化程序將 xticks 設置為僅顯示 HMS。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.offsetbox import AnchoredText
import matplotlib.dates as mdates
from datetime import timedelta
plt.style.use('seaborn-whitegrid')

n_sections=df['Sections'].nunique()
cols=2
rows=int(round(n_sections/2.0))
#setup the plot
fig, ax = plt.subplots(rows, cols, figsize=(16,8),sharex=False,sharey=False) # if you want to turn off sharing axis.
row=0 #to iterate over rows/cols
col=0 #to iterate over rows/cols


for index, Section in df.groupby('Sections'):
    ax[row][col].scatter(np.array(Section['Datetime']),Section['iBid'] , color='blue')
    ax[row][col].scatter(np.array(Section['Datetime'][Section['iBid']>Section['Bid']]),Section['iBid'][Section['iBid']>Section['Bid']] , color='red')
    ax[row][col].set_xlim([min(Section['Datetime'])-timedelta(seconds=5), max(Section['Datetime'])+timedelta(seconds=5)])
    ax[row][col].set_xlabel('Date Time',fontsize=20)
    ax[row][col].set_ylabel('iBid',fontsize=20)
    anchored_text = AnchoredText("{}".format(Section['Sections'].unique()[0]), loc=4,prop=dict(size=20))
    ax[row][col].add_artist(anchored_text)

    ax[row][col].xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
    ax[row][col].tick_params(axis='both', direction='in', which='major', length=5, width=2,labelsize=16)
    
    row=row+1
    if row==rows:
        row=0
        col=col+1

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM