0

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

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Faisal
  • 151
  • 3
  • 10
  • 1
    What do you mean by "not providing"? You realise that the way you've structured your loops currently means that you will run through every date_from once with date_to as "02/02/2017", then again with date_to as "03/02/2017", and so on and so forth - is this intentional? – ACascarino Jun 04 '18 at 12:58
  • yes, it is intentional because date in API date always going backward. Note providing means that That date is not changing. Only once date coming then only second loop date is changing but I want them simultenously to change and compile data – Faisal Jun 04 '18 at 13:03
  • So could you please describe what you're looking for as an output and what you are currently getting? – ACascarino Jun 04 '18 at 13:05
  • in output date_from is chaning like "03 feb 17, 02 feb 17, 1 feb 17" that is satisfyting one Kwag but "date_to" is static as 02 feb 17 but I wnat it to change as well parallel to second date so that date can be compiled in order without repition – Faisal Jun 04 '18 at 13:10
  • `d` does not have the same number of dates as `c` (that is to say, `len(d) != len(c)` - if you want them to move together then what do you want to happen when one of the lists has run out of unprocessed dates but the other still has unprocessed dates? – ACascarino Jun 04 '18 at 13:25
  • ek.get_news(arguments) is a function to get the news .this function must have the 2 dates range to be filled. well, if I give dates range manually one time , it is working perfectly fine. but I take me too much time to collect one year data .so I just want it done automatically with one code. regading your question,first date that is 02/02/2017 reamins same in above code and seocnd date that is 03/02/2017 go backward (03/02/17) and collect one day news then the second iteration works and 04/02/2017 go backward again (this time its calculate 04 feb news correctly but with repetition of 03 – Faisal Jun 04 '18 at 13:40

1 Answers1

1

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

iacob
  • 20,084
  • 6
  • 92
  • 119