To fetch live meteorological data from a public Representational State Transfer Application Programming Interface (REST API) without requiring an API key, use the Python requests library combined with Pandas. Run this snippet instantly inside Google Colab:
import requests
import pandas as pd
# Fetch live hourly weather forecast endpoint (Open-Meteo)
url = "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m,relative_humidity_2m"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
hourly_data = data['hourly']
# Build DataFrame from time-series lists
df = pd.DataFrame({
'Time': pd.to_datetime(hourly_data['time']),
'Temperature_C': hourly_data['temperature_2m'],
'Humidity_Percent': hourly_data['relative_humidity_2m']
})
print(df.head())
else:
print(f"Error fetching weather data: {response.status_code}")
This script connects directly to a public meteorological web service to pull real-time hourly weather forecasts, including air temperature and relative humidity values for specified geographic coordinates. It converts the incoming JSON time series into a clean, structured Pandas table, enabling instant data analysis, temperature trend calculations, and export to local databases or flat files.
Ensure you have the required libraries installed in your Python environment or Google Colab notebook:
!pip install --upgrade requests pandas
Weather APIs return timestamp arrays as string lists. Converting them using pd.to_datetime() and setting them as the DataFrame index allows powerful time-based slicing and filtering:
import requests
import pandas as pd
url = "https://api.open-meteo.com/v1/forecast?latitude=28.61&longitude=77.20&hourly=temperature_2m"
response = requests.get(url)
data = response.json()
df = pd.DataFrame({
'Timestamp': pd.to_datetime(data['hourly']['time']),
'Temp': data['hourly']['temperature_2m']
})
# Set timestamp as index for time-series operations
df.set_index('Timestamp', inplace=True)
print(df.head())
You can aggregate hourly readings into daily summary statistics using Pandas resample():
# Calculate daily maximum and minimum temperatures
daily_summary = df.resample('D').agg({'Temp': ['max', 'min', 'mean']})
print(daily_summary.head())
Persist your processed weather forecasts across various formats:
import sqlite3
# Save to CSV
df.to_csv("weather_forecast.csv")
# Save to SQLite
conn = sqlite3.connect("weather.db")
df.to_sql("hourly_forecast", conn, if_exists="replace", index=True)
conn.close()
print("Weather data successfully persisted!")
Explore live weather API extraction code cells, test datetime resampling, and run self-validating data assertions in Google Colab.
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.