Skip to content

Add a data source

A data source teaches the package to read one more public dataset. Once it is in, anyone can join that data to their own, and an analysis can build on it. It suits analysts and data volunteers, and it is a good first contribution.

What a data source is

One loader: a function that fetches a published dataset and returns a tidy table, with one row for each area or record. These are the loaders in the package today. Each is a good model to copy.

Module Loader What it reads
uk_open_data.geo load_geodata Area boundaries from the ONS Open Geography Portal
uk_open_data.ons load_ons Statistics from the Nomis API
uk_open_data.postcodes postcodes_to_geography Postcodes placed in areas
uk_open_data.imd load_imd The deprivation index of each of the four nations

A good loader:

  • returns the table library the person already uses, pandas or polars. The helpers in uk_open_data/_frames.py do this for you.
  • has a geography_code column when its rows are areas, so it joins to everything else in the package.
  • says where its data comes from: who publishes it, which edition, under which licence, and the link.
  • gives up in good time. Every web request passes a timeout. A check in CI looks for this.

Data sources explains the services behind the loaders that exist, and their quirks.

Before you start: the template

Open an issue and paste this in, filled in. Wait for a reply before you write code: some datasets cannot be reused, and some are already planned.

**Dataset:** the name the publisher gives it
**Publisher and link:** who publishes it, and the page it is on
**Licence:** for example, Open Government Licence v3.0
**What one row is:** for example, one local authority, or one charity
**Coverage:** which nations, and which years
**How it is published:** a file to download, or a service you query
**How often it changes:** for example, once a year
**Who would use it, and for what:** one or two sentences

Steps

  1. Set up the project with the steps in DEVELOPERS.md.
  2. Pick the closest loader and copy its shape. For a file to download, start from imd/api.py. For a service you query, start from ons/api.py.
  3. Put the loader in a module of its own, such as src/uk_open_data/<name>/api.py, with an __init__.py that exports it. Give it type hints and a docstring that names the source and its licence.
  4. Write tests in tests/test_<name>_api.py. Save a few rows of the real format in the test and stand in for the web request, as tests/test_imd_api.py does. No test may use the network.
  5. Add a page to docs/api/ that shows how to load the data, with the publisher, the licence and the words to use when you credit it. List the page under User Guide in mkdocs.yml.
  6. Add a Dataset for it to DATASETS in src/uk_open_data/analyses/datasets.py, which list_datasets() returns. Data sources explains its fields. If a copy can be kept on the machine, give the entry its downloads. Then download_datasets(), and every server built with it, keep the dataset with no other change. If the loader holds figures in memory, mark the function that forgets them with @_caches.register, from uk_open_data._caches, so clear_caches() forgets them too. Add a live check for it in tests/test_live_sources.py, and update the table on the Datasets page. Tests fail until all three are done, and the page's test prints the whole table to paste.
  7. Run the checks, then open a pull request.

Checklist

Copy this into your pull request and tick each line.

- [ ] A maintainer replied to the issue for this dataset
- [ ] The licence lets anyone reuse the data, and the docs say how to credit it
- [ ] The loader returns pandas or polars, whichever the person has
- [ ] Rows that are areas have a `geography_code` column
- [ ] Every web request has a timeout
- [ ] Tests use saved rows and no network
- [ ] No data about a real person in code, tests or docs
- [ ] A docs page is listed in `mkdocs.yml`
- [ ] In `DATASETS`, with a live check and a row on the Datasets page
- [ ] If it can be kept, its entry has `downloads`
- [ ] Anything its loader holds in memory is forgotten by `clear_caches()`
- [ ] Lint, format, types, tests and the docs build all pass
- [ ] Commits are signed off (`git commit -s`)
- [ ] The pull request says which tools helped, or "None"

Ideas to start from

These are datasets we want. Each would make a good first pull request.

  • Friendly names for the Nomis labour market tables.
  • A loader for the Charity Commission register of charities.
  • A loader for 360Giving grants data.
  • Loaders for the Scottish and Northern Irish charity registers.

Issues for this way in carry the lane: data source label.