I am using Python's deque()
to implement a simple circular buffer:
from collections import deque
import numpy as np
test_sequence = np.array(range(100)*2).reshape(100,2)
mybuffer = deque(np.zeros(20).reshape((10, 2)))
for i in test_sequence:
mybuffer.popleft()
mybuffer.append(i)
do_something_on(mybuffer)
I was wondering if there's a simple way of obtaining the same thing in Pandas using a Series
(or DataFrame
). In other words, how can I efficiently add a single row at the end and remove a single row at the beginning of a Series
or DataFrame
?
Edit: I tried this:
myPandasBuffer = pd.DataFrame(columns=('A','B'), data=np.zeros(20).reshape((10, 2)))
newpoint = pd.DataFrame(columns=('A','B'), data=np.array([[1,1]]))
for i in test_sequence:
newpoint[['A','B']] = i
myPandasBuffer = pd.concat([myPandasBuffer.ix[1:],newpoint], ignore_index = True)
do_something_on(myPandasBuffer)
But it's painfully slower than the deque()
method.