使用Traffic库下载OpenSky航班数据触发MemoryError该如何解决?
问题解决方法
DtypeWarning是pandas读取数据时自动推断列类型出错,对应列存在混合数据类型;MemoryError是单次拉取15天的全量数据量过大,超出内存分配上限,对应解决方案如下:
方案1:直接透传参数指定dtype
opensky.history方法内部调用pandas做数据读取,所有pandas读取相关的参数都可以直接透传,你可以直接在调用时传入dtype和low_memory参数即可:
from traffic.data import opensky, airports # 自定义dtype映射,把非必要的float64转成float32节省内存,同时指定混合类型列的类型 # 你可以先拉取1小时的小样本确认第18列的字段名,再对应设置类型 dtype_map = { "onground": bool, "latitude": "float32", "longitude": "float32", "altitude": "float32", "velocity": "float32", "heading": "float32", "vertrate": "float32", "callsign": str, "icao24": str } airport = airports['LEMD'] flight = opensky.history( start="2021-06-01 00:00", stop="2021-06-16 00:00", arrival_airport=airport.icao, low_memory=False, dtype=dtype_map ) flight.to_csv(r"C:\Users\peter\Desktop\cosicas.csv")
方案2:分批次拉取(更稳定,推荐)
如果你的内存还是不足以支撑全量数据,可以按天拆分拉取,每次拉取一天的数据追加到csv文件,拉完立即释放内存,避免一次性占用过高内存:
from traffic.data import opensky, airports from datetime import datetime, timedelta airport = airports['LEMD'] start_date = datetime(2021,6,1) end_date = datetime(2021,6,16) csv_path = r"C:\Users\peter\Desktop\cosicas.csv" dtype_map = { "onground": bool, "latitude": "float32", "longitude": "float32", "altitude": "float32", "velocity": "float32", "heading": "float32", "vertrate": "float32", "callsign": str, "icao24": str } current_date = start_date first_write = True while current_date < end_date: next_date = current_date + timedelta(days=1) daily_flight = opensky.history( start=current_date.strftime("%Y-%m-%d %H:%M"), stop=next_date.strftime("%Y-%m-%d %H:%M"), arrival_airport=airport.icao, low_memory=False, dtype=dtype_map ) # 追加写入csv daily_flight.to_csv( csv_path, mode='w' if first_write else 'a', header=first_write, index=False ) first_write = False current_date = next_date # 手动释放内存 del daily_flight
内容的提问来源于stack exchange,提问作者Peter La Anguila
相关产品推荐
相关产品推荐

