我有一些CSV格式的多种产品的金融交易数据,我想用熊猫分析.交易以非规则的间隔发生,并且加时间戳为1秒准确度,这导致一些交易"同时"发生,即具有相同的时间戳.
目前的目标是生成每种产品的累计交易量的图表.
交易数据已使用read_csv()读取到DataFrame中,该解析日期时间为索引.
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 447 entries, 2012-12-07 17:16:46 to 2012-12-10 16:28:29
Data columns:
Account Name 447 non-null values
Exchange 447 non-null values
Instrument 447 non-null values
Fill ID 447 non-null values
Side 447 non-null values
Quantity 447 non-null values
Price 447 non-null values
dtypes: float64(1), int64(1), object(5)
Run Code Online (Sandbox Code Playgroud)
做了一些工作来添加"QuantitySigned"列.
我做了一个"groupby",以便我可以通过仪器访问数据.
grouped = trades.groupby('Instrument', sort=True)
for name, group in grouped:
group.QuantitySigned.cumsum().plot(label=name)
plt.legend()
Run Code Online (Sandbox Code Playgroud)
上面的工作,但我想在一个DataFrame中有TimeSeries(每个工具一个),即每个工具的一列,这样我就可以使用DataFrame.plot().问题是没有两个TimeSeries具有完全相同的索引,即我需要合并所有TimeSeries的索引.
我知道这应该有用,考虑到以下简单的例子:
index=pd.date_range('2012-12-21', periods=5)
s1 = Series(randn(3), index=index[:3])
s2 = Series(randn(3), index=index[2:])
df = DataFrame(index=index) …Run Code Online (Sandbox Code Playgroud)