0

thanks in advance to anyone looking to enlighten me here a little bit since I have been struggling for a solid couple of hours :-(

def pullQueue(eventQueue, barLength):
    # Setting start and End times for extractions
    startExtract = dt.time(8, 00, 00, 0)
    endExtract = dt.time(22, 00, 00, 0)
    while dt.datetime.now().time() >  startExtract and dt.datetime.now().time() < endExtract: 
        try:
            event = eventQueue.get(False)
        except Queue.Empty:
            pass
        else:
            if event is not None:
                if event.type == 'TICK':
                    global ts
                    ts.append(pd.Series([event.ask], index=[event.time]))
        time.sleep(heartbeat)

The above function (an indentation error occurs when copying into Stackoverflow - please simply ignore) pulls a custom object (event) out of a Queue object - if the event is a tick event, it is meant to append the ask variable and the corresponding timestamp (as an index value) to a pandas Series object (see last nested if statement in function).

However, it simply doesn't work, when I try to access the Series ts - it always logs the 'old' Series excluding the appended values. Originally when I tried to build this function I used this link(stackoverflow question) as guidance. I also constructed the nested if statement (below) on a standalone basis and it did work - any ideas?

if event.type == 'TICK':
    global ts
    ts.append(pd.Series([event.ask], index=[event.time]))

Artem asked a valid question here - I am using threading - see below: evalThread = threading.Thread(target=pullQueue,args=(eventQueue, barLength)) evalThread.start()

Community
  • 1
  • 1
Philipp
  • 21
  • 5

2 Answers2

1

The method pandas.Series.append doesn't change the original series in-place, it returns a new series, so all you need to do is:

ts = ts.append(pd.Series([event.ask], index=[event.time]))

Daniel Boline
  • 506
  • 4
  • 4
0

Here is almost identical example. You may see that thread worker modifies the variable.

I think the ts variable is not modified by you thread at all. You have several conditional statements (if/while) to reach the globas ts statement and they probably lead to avoiding the block.

import time
import Queue
import threading
import pandas as pd
from collections import namedtuple

Event = namedtuple('Event', ['type', 'ask', 'time'])

eventQueue = Queue.Queue()

for i in xrange(150):
    eventQueue.put(Event(type='TICK', ask=i, time=i))

ts = []

def pullQueue(eventQueue):
    while True:
        try:
            event = eventQueue.get(False)
        except Queue.Empty:
            pass
        else:
            if event is not None:
                if event.type == 'TICK':
                    global ts
                    ts.append(pd.Series([event.ask], index=[event.time]))
        time.sleep(0.01)
evalThread = threading.Thread(target=pullQueue,args=(eventQueue, ))
evalThread.start()

time.sleep(0.01)
print ts

time.sleep(0.01)
print ts
Artem Fedosov
  • 2,163
  • 2
  • 18
  • 28