Search code examples
pythonpython-3.xthomson-reuters-eikon

passing two lists in third party API function


I have been stuck in this issue for many days.

 a = pd.date_range(start= '02/02/2017', end='06/02/2018', freq = 'D')
 c = a.format(formatter=lambda x: x.strftime('%Y-%m-%d'))

    for date_to in c:
        date_to= date_to
        print("date_to has been picked up")
        b = pd.date_range(start= '02/01/2017', end='06/2/2018', freq = 'D')
        d = b.format(formatter=lambda x: x.strftime('%Y-%m-%d'))
        for date_from in d:
            date_from= date_from
            print('date_from has been picked up')
            df = ek.get_news_headlines('R:AAPL.O AND Language:LEN', date_from = date_from , date_to = date_to, count=100)

This is above code that I have written to extract news form third-party API (in last line of code) The problem I am facing is , in last line I have to give date_from and date_to to supply the range of date for extracting data. Now I want to get date range changed automatically every time like we do in loops. last loop "date_from" is working but fist loop is not providing "date_to". Thank in advance for your cooperation


Solution

  • Your question is a bit unclear, but I think this is what you want to achieve - taking pairs of dates simultaneously and moving onto the next pair (n iterations), as opposed to two nested for loops (n2 iterations):

    #Create date lists first
    a = pd.date_range(start= '02/02/2017', end='06/02/2018', freq = 'D')
    b = pd.date_range(start= '02/01/2017', end='06/2/2018', freq = 'D')
    c = a.format(formatter=lambda x: x.strftime('%Y-%m-%d'))
    d = b.format(formatter=lambda x: x.strftime('%Y-%m-%d'))
    
    # Single for loop iterating over pairs of elements of c,d
    for date_to,date_from in zip(c,d): 
        print("date_to has been picked up")
        print('date_from has been picked up')
        df = ek.get_news_headlines('R:AAPL.O AND Language:LEN', date_from = date_from , date_to = date_to, count=100)
    

    You may want to read up on how zip() works:

    https://docs.python.org/3.4/library/functions.html#zip