天气数据

python
数据获取
作者

不止BI

发布于

2024年2月20日

meteostat提供了一个快捷获取历史天气数据的接口

根据坐标获取附近的气象站

代码
from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Stations, Daily
chongqing = (29.56301, 106.55156)

stations = Stations()
nearby_stations = stations.nearby(*chongqing).fetch(20)
nearby_stations
name country region wmo icao latitude longitude elevation timezone hourly_start hourly_end daily_start daily_end monthly_start monthly_end distance
id
57516 Chongqing CN CQ 57516 ZUCK 29.5167 106.4833 351.0 Asia/Chongqing 1944-11-11 2024-06-21 1944-11-11 2024-06-16 1944-01-01 2022-01-01 8373.975722
57411 Nanchong CN SC 57411 <NA> 30.8000 106.0833 310.0 Asia/Chongqing NaT NaT 1951-01-09 2024-06-16 1951-01-01 2022-01-01 144723.481282
56492 Yibin CN SC 56492 <NA> 28.8000 104.6000 342.0 Asia/Chongqing NaT NaT 1944-04-01 2024-06-16 1944-01-01 2022-01-01 207586.288529
57713 Zunyi CN GZ 57713 <NA> 27.7000 106.8833 845.0 Asia/Chongqing NaT NaT 1951-01-01 2012-12-31 1951-01-01 2012-01-01 209671.730894
57633 Youyang CN CQ 57633 <NA> 28.8333 108.7667 665.0 Asia/Chongqing NaT NaT 1951-01-01 2024-06-16 1951-01-01 2022-01-01 229810.189390
56294 Chengdu CN SC 56294 ZUUU 30.6667 104.0167 508.0 Asia/Chongqing 1956-08-20 2024-06-21 1951-01-01 2022-04-25 1951-01-01 2022-01-01 272951.363503
56196 Mianyang CN SC 56196 <NA> 31.4667 104.6833 472.0 Asia/Chongqing NaT NaT NaT NaT NaT NaT 277185.210787
57707 Bijie CN GZ 57707 <NA> 27.3000 105.2333 1511.0 Asia/Chongqing NaT NaT 1951-01-01 2024-06-16 1951-01-01 2022-01-01 282722.969890
56187 Liucheng / Wenjiang CN SC 56187 <NA> 30.7500 103.8667 541.0 Asia/Chongqing NaT NaT 2004-01-01 2024-06-16 2004-01-01 2022-01-01 289909.384876
57447 Enshi CN HU 57447 <NA> 30.2833 109.4667 458.0 Asia/Shanghai NaT NaT 1945-03-31 2024-06-16 1945-01-01 2022-01-01 292121.181393
57237 Wanyuan CN SC 57237 <NA> 32.0667 108.0333 674.0 Asia/Chongqing NaT NaT 1952-01-01 2024-06-16 1952-01-01 2022-01-01 312283.824717
57816 Guiyang CN GZ 57816 ZUGY 26.5833 106.7167 1074.0 Asia/Chongqing 1945-02-04 2024-06-21 1945-02-04 2024-06-16 1945-01-01 2022-01-01 331724.387656
ZGDY0 Sifangyan / Zhangjiajie / Dayong CN HN <NA> ZGDY 29.1028 110.4430 211.0 Asia/Shanghai 2007-10-10 2024-06-16 NaT NaT NaT NaT 380665.912370
57745 Zhijiang CN HN 57745 <NA> 27.4500 109.6833 273.0 Asia/Shanghai NaT NaT 1943-10-01 2024-06-16 1943-01-01 2022-01-01 385777.194866
57127 Hanzhong CN SA 57127 <NA> 33.0667 107.0333 509.0 Asia/Chongqing NaT NaT 1951-01-01 2024-06-16 1951-01-01 2022-01-01 392269.403309
57245 Ankang CN SA 57245 <NA> 32.7167 109.0333 291.0 Asia/Chongqing NaT NaT NaT NaT NaT NaT 422762.407542
56182 Songpan CN SC 56182 <NA> 32.6500 103.5667 2852.0 Asia/Chongqing NaT NaT 1951-01-01 2024-06-16 1951-01-01 2022-01-01 445574.268898
56096 Wudu CN GS 56096 <NA> 33.4000 104.9167 1079.0 Asia/Chongqing NaT NaT 1951-01-01 2024-06-16 1951-01-01 2022-01-01 453925.554837
56571 Xichang CN <NA> 56571 <NA> 27.9000 102.2667 1599.0 Asia/Chongqing NaT NaT 1944-03-30 2024-06-16 1944-01-01 2022-01-01 456842.343806
57461 Yichang CN HU 57461 <NA> 30.7000 111.3000 134.0 Asia/Shanghai NaT NaT 1951-01-01 2024-06-16 1951-01-01 2022-01-01 473788.054130

通过气象站ID获取天气数据

代码
# 获取最近的气象站的ID
station_id = nearby_stations.index[0]

# 设置时间范围
start = datetime(2021, 1, 1)
end = datetime(2021, 12, 31)

# 获取每日数据
data = Daily(station_id, start, end)
data = data.fetch()
data.head()
tavg tmin tmax prcp snow wdir wspd wpgt pres tsun
time
2021-01-01 6.3 4.0 10.7 0.0 NaN 341.0 6.9 NaN 1029.0 NaN
2021-01-02 6.6 5.0 10.7 0.0 NaN 5.0 9.8 NaN 1024.6 NaN
2021-01-03 7.1 5.0 10.3 0.0 NaN 238.0 6.6 NaN 1027.8 NaN
2021-01-04 7.1 5.0 10.3 0.0 NaN 21.0 9.2 NaN 1025.6 NaN
2021-01-05 7.8 5.0 10.8 0.0 NaN 325.0 5.1 NaN 1027.5 NaN
回到顶部