Skip to main content

Python client for the canwxdb Canadian weather observations API

Project description

canwxdb-py

Python client for the canwxdb API — historical Canadian weather observations from Environment and Climate Change Canada, queryable by geography and time range.

Community project — not affiliated with or endorsed by Environment and Climate Change Canada.

Installation

pip install canwxdb

Optional extras for richer output formats and progress bars:

pip install canwxdb[pandas]      # DataFrame support
pip install canwxdb[geo]         # GeoDataFrame support (includes pandas)
pip install canwxdb[progress]    # tqdm progress bars
pip install canwxdb[all]         # everything above

Quick start

from canwxdb import Client

client = Client()

Look up a station

station = client.station(52)
# {'type': 'Feature', 'geometry': ..., 'properties': {'name': 'ESQUIMALT HARBOUR', ...}}

Find stations near a location

# By lat/lon + radius
stations = client.stations(lat=48.43, lon=-123.37, radius_km=50)

# By Canadian postal code
stations = client.stations(postal_code="V8W1A1", radius_km=25)

# By bounding box  (min_lon, min_lat, max_lon, max_lat)
stations = client.stations(bbox=(-124.5, 48.3, -123.0, 49.0))

Fetch observations

# Daily observations for a single station
obs = client.daily(station_id=52, start="2024-01-01", end="2024-12-31")

# Hourly observations
obs = client.hourly(station_id=52, start="2024-07-01", end="2024-07-31")

# Monthly observations
obs = client.monthly(station_id=52, start="1990-01-01", end="2024-12-31")

All three methods accept the same geographic filters as stations().

Date arguments accept ISO 8601 strings, datetime.date, or datetime.datetime objects.

Get a pandas DataFrame

df = client.daily(
    station_id=52,
    start="2024-01-01",
    end="2024-12-31",
    format="dataframe",
)

df.head()
#         date  station_id  mean_temp_c  ...  latitude  longitude
# 0 2024-01-01          52         -1.2  ...     48.43    -123.43
# 1 2024-01-02          52          0.5  ...     48.43    -123.43

Get a GeoDataFrame (GeoPandas)

gdf = client.daily(
    postal_code="V8W1A1",
    radius_km=100,
    start="2023-01-01",
    end="2023-12-31",
    format="geodataframe",
)

# Plot mean temperature across stations
gdf.plot(column="mean_temp_c", legend=True, figsize=(10, 6))

The GeoDataFrame uses WGS-84 (EPSG:4326) and has a Point geometry column built from each station's coordinates.

Progress bars (recommended for large queries)

For multi-page queries — especially in Jupyter notebooks — pass show_progress=True to see live row counts. The library automatically uses tqdm.notebook inside Jupyter and plain tqdm elsewhere.

# In a Jupyter notebook this renders a rich notebook-style progress bar.
df = client.daily(
    bbox=(-124.5, 48.3, -123.0, 49.0),
    start="2015-01-01",
    end="2024-12-31",
    format="dataframe",
    show_progress=True,
)
# Fetching: 45000 rows [00:18, 2490 rows/s]

Limit pages for exploratory queries

When exploring a new dataset, use max_pages to avoid accidentally fetching millions of rows:

# Just peek at the first page
df = client.hourly(
    bbox=(-80, 43, -76, 44),
    start="2020-01-01",
    end="2023-12-31",
    format="dataframe",
    max_pages=1,
)

Using as a context manager

with Client() as client:
    df = client.daily(station_id=52, start="2024-01-01", end="2024-12-31",
                      format="dataframe")

Connecting to a custom endpoint

client = Client(base_url="https://my-private-instance.example.com")

Output formats

format= Return type Requires
"geojson" (default) dict (GeoJSON FeatureCollection)
"dataframe" pandas.DataFrame pandas
"geodataframe" geopandas.GeoDataFrame geopandas, shapely

Error handling

from canwxdb import Client, WxDBNotFoundError, WxDBRateLimitError

client = Client()

try:
    station = client.station(99999)
except WxDBNotFoundError:
    print("Station not found")

# The client automatically retries 429 responses with exponential back-off.
# WxDBRateLimitError is raised only if all retries are exhausted.
Exception Raised when
WxDBNotFoundError HTTP 404
WxDBRateLimitError HTTP 429 after all retries
WxDBHTTPError Any other non-2xx response
WxDBError Base class for all of the above

Data source

Weather data is sourced from Environment and Climate Change Canada's open data under the Government of Canada Open Government Licence.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

canwxdb-0.1.0.tar.gz (148.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

canwxdb-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file canwxdb-0.1.0.tar.gz.

File metadata

  • Download URL: canwxdb-0.1.0.tar.gz
  • Upload date:
  • Size: 148.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for canwxdb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d5405e26e03de9c1f828efb693c51cc983ff47ad3722258ca84141f333c316e1
MD5 5149d464df0f8eb1bb8ddc9825cc09d6
BLAKE2b-256 90b5ca73338d3d14c76ae439cad05d7ad177f96d5a6c3d20d18d7b0572f6426b

See more details on using hashes here.

File details

Details for the file canwxdb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: canwxdb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for canwxdb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d738ac44e8d85560c74083c450ea53ab268b0215431255f1078e613de830bd2f
MD5 6dd09e1ead3a154508291a89db691eff
BLAKE2b-256 f4047344ffdb7bf64831463d7b7886a0fc19a26ceb6d97ab680f73c9d74c6984

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page