進階
結果與匯出
讀取發布資訊,匯出為 Polars、pandas、Arrow 或地理物件。
清單查詢回傳 Records,用法與 tuple 相同,並以 info 提供更新時間與版本:
stops = client.bus.stops("Taipei", limit=100)
print(len(stops), stops[0].name, stops.info.updated_at, stops.info.version) 每筆資料是不可變的 pydantic 模型,可用 model_dump() 轉換為字典。
表格
frame = stops.to_polars()
table = stops.to_arrow()
pandas_frame = stops.to_pandas() 巢狀欄位展開為以點號命名的欄位,例如 name.zh_tw、position.longitude。各方法需要同名的
座標
from tdx import Position
here = Position.at(longitude=121.517, latitude=25.047)
for stop in stops:
if stop.position:
print(here.distance_to(stop.position), stop.position.to_geohash()) distance_to() 計算測地線距離,單位為公尺。to_shapely() 轉換為 Shapely 點,to_geohash() 轉換為 geohash。