【发布时间】:2021-10-26 10:27:58
【问题描述】:
我正在从 API 下载数据,最长回溯期为 833 天,但根据我的测试,我知道他们的数据可以追溯到 2002 年。我在下面有一个函数,它定义了从今天到两个日期时间的 833 天“end”和“start”,它们被输入到 API 命令中。请注意,它们需要采用字符串格式并以这种方式格式化,以便 api 接受它们。
d=datetime.today()
end = str(d.year) + "-" + str(d.month) + "-" + str(d.day)
lookbook_period = 833
# Take current date and minus the the max time delta of 833 days to get 'start' var
time_delta = timedelta(days=lookbook_period)
now = datetime.now()
#split the answer and format it to the required timestamp type.
start = str(now - time_delta).split(" ")[0]
我想要做的是下载 833 天部分中的数据帧,然后将它们全部拼凑成 CSV 或数据帧。到目前为止,我的数据低于我的水平,但我不确定如何创建一个可以随时更改日期的函数。
def time_machine():
df_total = pd.DataFrame
start_str = str(2002) + "-0" + str(5) + "-0" + str(1)
start = datetime(2002,5,1)
print(start)
# amount of days from 2002-05-01 to now
rolling_td = timedelta(days=int(str((datetime.today() - start)).split(" ")[0]))
print(rolling_td, "\n")
# API maximum amount of lookbook days
max_td = timedelta(days=833)
# The function would do something similar to this, and on each pass, calling the API and saving the data to a dataframe or CSV.
s1 = start + max_td
print(s1)
s2 = s1 + max_td
print(s2)
s3 = s2 + max_td
print(s3)
d=datetime.today()
end = str(d.year) + "-" + str(d.month) + "-" + str(d.day)
print(d)
非常感谢您提供任何建议或工具/库。我一直在用 while 循环测试一些东西,但我仍然在这方面盲目地跑到黑暗中。
这是我认为我需要的粗略 sudo 代码,但我仍然不确定如何进入下一部分
while count > 0 and > 833:
start =
end =
call the API first to download first set of data.
Check date range:
get most recent date + 833 days to it
Download next section
repeat
if count < 833:
calulate requied dates for start and end
【问题讨论】:
标签: python pandas dataframe while-loop oanda