So, I'm new about the package Pandas. I was doing some back test on a strategy on ETFs, that I need to do a lot of queries on Pandas Dataframe.
So let's say I'm these two DataFrames, df and df1, the only difference is that: df has datetime Index, while df1 has the timestamp as a column and an integer Index
In[104]: df.head()
Out[104]:
high low open close volume openInterest
2007-04-24 09:31:00 148.28 148.12 148.23 148.15 2304400 341400
2007-04-24 09:32:00 148.21 148.14 148.14 148.19 2753500 449100
2007-04-24 09:33:00 148.24 148.13 148.18 148.14 2863400 109900
2007-04-24 09:34:00 148.18 148.12 148.13 148.16 3118287 254887
2007-04-24 09:35:00 148.17 148.14 148.16 148.16 3202112 83825
In[105]: df1.head()
Out[105]:
dates high low open close volume openInterest
0 2007-04-24 09:31:00 148.28 148.12 148.23 148.15 2304400 341400
1 2007-04-24 09:32:00 148.21 148.14 148.14 148.19 2753500 449100
2 2007-04-24 09:33:00 148.24 148.13 148.18 148.14 2863400 109900
3 2007-04-24 09:34:00 148.18 148.12 148.13 148.16 3118287 254887
4 2007-04-24 09:35:00 148.17 148.14 148.16 148.16 3202112 83825
so I test the query speed a little bit:
In[100]: %timeit df1[(df1['dates'] >= '2015-11-17') & (df1['dates'] < '2015-11-18')]
%timeit df.loc[(df.index >= '2015-11-17') & (df.index < '2015-11-18')]
%timeit df.loc['2015-11-17']
100 loops, best of 3: 4.67 ms per loop
100 loops, best of 3: 3.14 ms per loop
1 loop, best of 3: 259 ms per loop
To my surprise is that using the logic built in with Pandas is actually the slowest:
df.loc['2015-11-17']
Does anyone know why is that? And are there any documents or blogs about the most efficient ways to query a Pandas DataFrame?