Pythonic版本的循环

时间:2018-05-16 20:55:34

标签: python loops

我正在编写一个循环来在其他地方进行API调用,以检索两个日期之间的财务时间序列数据。鉴于API的限制,我必须制作一系列小日期间隔并使用循环来进行一系列调用。我能够使用以下循环来完成它:

#create a datelist index of smaller intervals

start_date = dtm.datetime(2014,1,1)
end_date = dtm.datetime.now()
datelist = pd.date_range(start=start_date, end=end_date, freq='M')
start_date = datelist[-1].date() +dtm.timedelta(days=1)
datelist.append(pd.date_range(start=start_date, end=end_date, freq='D'))

#get the intial and next date on the list convert to UNIX format and pass the interval on the API call

for n in range(len(datelist)-1):
    indateunix = tm.mktime(datelist[n].timetuple())
    enddateunix = tm.mktime(datelist[n + 1].timetuple())
    templist = self._api_call.returnChartData(ticker= 'FB', period='D',start=indateunix, end=enddateunix)
    arr = arr + templist

我想这不是最好的方法,是否有相同循环的pythonic版本?

1 个答案:

答案 0 :(得分:-1)

使用WITH cte AS ( SELECT * , ROW_NUMBER() OVER(PARTITION BY date ORDER BY id asc) rn FROM temp ) SELECT id, date FROM rn = 1

zip