У меня есть API, которому нужно передать координаты вместе с датами от и до в эпоху мс и извлечь хранилище результатов в датафрейме. Как я могу динамически зациклить координаты, от и до значений в API для каждой строки, извлечь значение и сохранить его в фрейме данных
Я пытался создать список с разделенными запятыми значениями, но не сделалработай. Я поместил свой код для справки.
headers = {
'Authorization': 'Api-Key XXXXXXXXXXX'
}
params = (
('coords', '47.62:122.349, 25.277:55.296'),
('from', '1572782400000'),
('to', '1572825600000')
)
response = requests.get('https://example.com/weather/v1/forecasts', headers=headers, params=params)
Входные данные
Date Latitude Longitude unix_time newdate unix_time_shifted
0 2019-02-11 47.620422 122.349358 1549843200000000 2019-02-12 1549929600000
1 2019-02-11 25.276987 55.296249 1549843200000000 2019-02-12 1549929600000
2 2019-02-11 19.432608 99.133200 1549843200000000 2019-02-12 1549929600000
3 2019-02-11 35.652832 139.839478 1549843200000000 2019-02-12 1549929600000
4 2019-03-11 47.620422 122.349358 1552262400000000 2019-03-12 1552348800000
5 2019-03-11 25.276987 55.296249 1552262400000000 2019-03-12 1552348800000
6 2019-03-11 19.432608 99.133200 1552262400000000 2019-03-12 1552348800000
7 2019-04-11 35.652832 139.839478 1554940800000000 2019-04-12 1555027200000
8 2019-04-12 35.652832 139.839478 1555027200000000 2019-04-13 1555113600000
9 2019-08-11 47.620422 122.349358 1565481600000000 2019-08-12 1565568000000
10 2019-08-11 25.276987 55.296249 1565481600000000 2019-08-12 1565568000000
Мой код считывает значение из API
ds['Date']=pd.to_datetime(ds['Date'])
ds['unix_time']=(ds['Date']-dt.datetime(1970,1,1)).dt.total_seconds()
ds['unix_time'] = round(ds['unix_time'],0).astype(int)*1000
ds['newdate'] = pd.to_datetime(ds['Date']).apply(pd.DateOffset(1))
ds['unix_time_shifted']=(ds['newdate']-dt.datetime(1970,1,1)).dt.total_seconds()
ds['unix_time_shifted'] = round(ds['unix_time_shifted'],0).astype(int)*1000
ds['latp'] = round(ds['Latitude'], 3)
ds['lonp'] = round(ds['Longitude'], 3)
ds['param'] = ds['latp'].astype(str) + ':' + ds['lonp'].astype(str)
l =ds.param.str.cat(sep=', ')
f = ', '.join(ds.unix_time.astype(str))
t = ', '.join(ds.unix_time_shifted.astype(str))
import requests
headers = {
'Authorization': 'Api-Key IntegratedLogistics'
}
params = (
('coords', l),
('from', f),
('to', t)
)
response = requests.get('https://example.com/weather/v1/forecasts', headers=headers, params=params)
d = json.loads(response.text)
out = []
for x in d['results']:
t = x['place']['type']
v = x['place']['value']
for y in x['measures']:
y[t] = v
out.append(y)
df = pd.DataFrame(out)
Ожидаемый вывод (Df)
Здесь столбец ts должен быть равен unix_time
coord dir fog precip prsmsl rh2m skcover snowd t2m t_max t_min thunderstorm ts wgust wspd
0 47.62:122.349 WSW H 0.00 1028 36 clear 0 -1.77 5.75 -1.79 N 1572782400000 5 5
1 47.62:122.349 WNW H 0.00 1026 39 clear 0 -2.41 -1.75 -2.44 N 1572804000000 6 5
2 47.62:122.349 W H 0.00 1025 40 clear 0 -0.15 -0.15 -2.83 N 1572825600000 4 3
3 25.277:55.296 WNW L 0.00 1011 49 clear 0 29.01 30.06 27.21 N 1572782400000 16 16
4 25.277:55.296 ENE M 0.00 1013 57 clear 0 25.66 29.02 25.61 N 1572804000000 7 7
Как это можно сделать?