循環遍歷數據范圍以從 API 下載數據

迷失在蟒蛇

我正在從 API 下載數據,最長回溯期為 833 天,但從我的測試中我知道他們的數據可以追溯到 2002 年。我在下面有一個函數,它定義了從今天到兩個日期時間“結束”的 833 天和“開始”,這些被輸入到一個 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 
jezza_99

如果您首先定義日期範圍,您將能夠在每 833 天的時間段內迭代以使用 API 提取數據。然後,您需要為每次迭代將數據附加到數據框(或 csv)。

import datetime as dt

# Date range to pull data over
start_date = dt.date(2002,5,1)
end_date = dt.date.today()
delta = dt.timedelta(days=832) # 832 so you have a range of 833 days inclusive

# Iterating from start date, recording date ranges of 833 days
date_ranges = []
temp_start_date = start_date
while temp_start_date < end_date:
    temp_end_date = temp_start_date + delta 
    if temp_end_date > end_date:
        temp_end_date = end_date
    date_ranges.append([temp_start_date, temp_end_date])
    temp_start_date = temp_end_date + dt.timedelta(days=1)

# For each date range, pass dates into API
# Initialise dataframe here
for start_date, end_date in date_ranges:
    start_date_str = start_date.strftime("%Y-%m-%d")
    end_date_str = end_date.strftime("%Y-%m-%d")

    # Input to API with start and end dates in correct string format

    # Process data into dataframe

不需要計算 833 天,正如您所說,API 將開始日期和結束日期作為參數,因此您只需要為每個日期範圍找到它們。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

循環遍歷數組和數據幀

for 循環不會遍歷所有數據?

如何循環遍歷具有不同數據類型的集合/映射數組

如何在python BeautifulSou中遍歷URL列表並從中下載特定數據

循環遍歷python中的數據框以選擇特定行

sql查詢從mysql數據庫下載BLOB數據

如何從當前日期開始循環遍歷天數數組

如何遍歷 API 調用數據並在視圖中顯示

從 api 數據集中獲取數據鍵

lambda 表達式 v. for 循環遍歷 Pandas 數據幀中的特定列

Python Pandas - 在刪除異常值的同時更快地遍歷數據中的類別(無 For 循環)

每秒從api獲取數據

在 SwiftUI 中循環遍歷實體數組以基於數組數據中的值構建樹結構視圖

SwiftUI 加載數據

在這種情況下,如何從 API 調用中 console.log 數據?

jquery:循環遍歷 json 數組

循環遍歷嵌套的對像數組

如何從 Nestjs 中的 URL 將數據下載到 JSON 文件中?

如何將 .ods 數據從網絡下載到 R?

如何編寫一個函數來循環遍歷年份和國家數據並按年份計算國家?

如何從嵌套數組中刪除數組,循環遍歷數組的其餘部分。然後循環遍歷嵌套數組的 1 個值,全部在 vanilla js 中

從 API 數據訪問數組元素

使用 useContext 重新加載頁面會丟失 API 數據

如何在角度應用程序中加載頁面期間填充 API 數據?

更改 Powerpoint 中圖表的源數據范圍

循環遍歷嵌套數組並從第一個循環數組中返回包含字符串的項

從 Api 獲取數據並將 Html 替換為 Api 數據 Nodejs

遍歷數組並添加數據

無法遍歷包含數據點的數組