What I have:
1) a list of GPS coordinates: latitude, longitude, and ID.
2) a defined function to scrape the last 24 hours of hourly temperature and humidity data. it returns a dataframe of 3 columns: temperature, humidity, ID, and hourly data as a DatetimeIndex. the function accepts 3 arguments: lat, lon, ID.
What I want:
- To edit the function to join the ID column for each time iterrows is passed
Here is the function that works for one lat/lon/ID set:
# grab only weather of interest
attributes = [u'temperature', u'humidity']
# 24 hours ago #round to closest hour
date = dt.datetime.now().replace(microsecond=0,second=0,minute=0) -
dt.timedelta(hours=24)
#initalize
times = []
data = {}
for attr in attributes:
data[attr] = []
def scrape_weather(LAT, LON, Id):
for offset in range(1,2): #i.e 1 day
forecast = forecastio.load_forecast(api_key, LAT, LON,
time=date+dt.timedelta(offset), units = 'ca' )
h = forecast.hourly()
d = h.data
for p in d:
times.append(p.time)
try:
for i in attributes:
data[i].append(p.d[i])
except:
print(KeyError)
df2 = pd.DataFrame(data)
df1 = pd.DataFrame(times)
df1.reset_index(drop=True, inplace=True)
df2.reset_index(drop=True, inplace=True)
dfweather = pd.concat([df1, df2], axis=1)
dfweather['ID'] = Id
dfweather = dfweather.set_index(pd.DatetimeIndex(dfweather[0]))
dfweather = dfweather.drop([0], axis=1)
return dfweather
This works fine when passing a singe column of the dataframe with the lat/lon/Ids
scrape_weather(df.at[0,'latitude'],df.at[0,'longitude'], df.at[0,'Id'])
But when I pass
for index, row in dummy_gps.iterrows():
test = scrape_weather(row['longitude'],row['latitude'], row['Id'])
The expected results of the look something like this:
temperature humidity ID
2019-05-14 07:00:00 22.58 0.34 1
2019-05-14 08:00:00 20.50 0.42 1
....
2019-05-14 07:00:00 22.58 0.34 2
2019-05-14 08:00:00 20.50 0.42 2
....
But instead the Ids are wrong and only one ID is copy pasted for over everyone like this:
temperature humidity ID
2019-05-14 07:00:00 22.58 0.34 2
2019-05-14 08:00:00 20.50 0.42 2
....
2019-05-14 07:00:00 22.58 0.34 2
2019-05-14 08:00:00 20.50 0.42 2
....
So i am unsure where in the weather scraper function to add the ID logic to ensure each ID is being associated with each forecast