Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/api/analyses.md
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,8 @@ The analysis loads a count of **people in need** in every local authority, from
[Nomis](ons.md), from [MHCLG's homelessness tables](homelessness.md), from
[DESNZ's fuel poverty statistics](fuel-poverty.md), from
[DWP's children in low income families](low-income.md) or from
[UKHSA's childhood vaccinations](vaccinations.md), divides
[UKHSA's childhood vaccinations](vaccinations.md) or from
[ONS's police recorded crime](crime.md), divides
the people you helped in each one by it, and lists
the high-need local authorities where you reach the fewest people per 100 in
need. A local authority is high need when its count is in the top quarter of
Expand All @@ -698,6 +699,8 @@ Choose the need with the `need` option:
| `"children_in_low_income_families_after_housing_costs"` | The same children, with income counted after housing costs, so more children count where housing costs more, from DWP |
| `"children_without_mmr1_by_2"` | Children who turned 2 in the latest year, April to March, without a first dose of the measles, mumps and rubella (MMR) vaccine, England only, from UKHSA. Published for upper-tier councils and shared out to councils |
| `"children_without_mmr2_by_5"` | Children who turned 5 in the latest year without both doses of MMR, England only, from UKHSA. Published for upper-tier councils and shared out to councils |
| `"recorded_crime"` | Offences the police recorded in the latest year, excluding fraud, England and Wales, from ONS. Published for community safety partnerships and shared out to councils |
| `"violence_against_the_person"` | Offences of violence against the person the police recorded in the latest year, England and Wales, from ONS. Published for community safety partnerships and shared out to councils |
| `{"dataset": ..., "label": ..., "filters": {...}}` | Any table Nomis publishes by local authority. The filters are the category codes to add up. Counts are asked for (`measures` 20100) unless the filters name another measure |

The three unemployment needs count the census group "Economically active
Expand Down Expand Up @@ -758,6 +761,18 @@ out as one area, by residents. UKHSA says the children recorded are uncertain
where people move often, as in London, so coverage there may be
underestimated, and the children not vaccinated overcounted.

The two crime needs count **offences**, not people or victims: one person can
be the victim of many offences, and many offences have no one victim. So the
result says "offences recorded" and sets the people you helped per 100
offences. Read that as more or less reach between places, not a share of
anyone. An offence is counted where it was recorded, and forces record crime
in different ways. They are the latest year ONS has published, and the result
cites it, such as "year ending June 2026". ONS publishes them for community
safety partnerships, most of them one council, so they are shared out to
councils, as below. Offences the police could not place in a partnership are
left out, and so is a partnership whose figures ONS marks as incomplete, such
as five in Kent today: the warning names them and their councils.

**Need figures for bigger areas.** Some figures are published for areas bigger
than a council: upper-tier councils (counties), community safety partnerships
or police force areas. A need from one of these is shared out to the councils
Expand All @@ -766,7 +781,8 @@ in it, so the analyses still compare councils. This was decided in
what it costs and when to think again.

- Each council takes its share of the area's count by its share of the area's
residents at the 2021 Census, in whole people, from
residents at the 2021 Census, rounded to whole people, households,
children or offences, as the need counts them, from
[`local_authority_lookup`](areas.md#a-councils-county-partnership-and-police-force-area).
- A council made of whole areas takes their figures as published. A unitary
council is a whole upper-tier council, and Buckinghamshire is four whole
Expand Down
83 changes: 83 additions & 0 deletions docs/api/crime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Police recorded crime

The `uk_open_data.crime` module loads police recorded crime for every
community safety partnership in England and Wales, from the Office for
National Statistics'
[Crime in England and Wales: Police Force Area data tables](https://www.ons.gov.uk/peoplepopulationandcommunity/crimeandjustice/datasets/policeforceareadatatables).
It reads the latest edition, for the year to the end of the latest quarter.

## `load_crime()`

```python
from uk_open_data.crime import load_crime

partnerships = load_crime()
partnerships[["geography_name", "total_recorded_crime", "robbery"]]
```

It returns one row per community safety partnership, 314 in all, as a pandas
or polars table, whichever you have:

| Column | What it holds |
|---|---|
| `geography_code` | The partnership's code: `E22` in England, `W14` in Wales |
| `geography_name` | The partnership's name |
| `police_force_area` | The police force it is in |
| `year` | The year the figures are for, such as "year ending June 2026" |
| `total_recorded_crime` | Every offence the police recorded, excluding fraud |
| `violence_against_the_person`, `sexual_offences`, `robbery`, `theft_offences`, `shoplifting`, ... | The offences of each kind ONS gives, one column each, named from its heading |

A **community safety partnership** is a council, or a few councils, working
with the police on crime. Most are one council. `uk_open_data.areas` gives
the councils in each, with their share of its residents:
[`local_authority_lookup("community_safety_partnership")`](areas.md#a-councils-county-partnership-and-police-force-area).

The figures count **offences** the police recorded, not people or victims.
One person can be the victim of many offences, and many offences have no one
victim. An offence is counted where it was recorded. Forces record crime in
different ways, so a difference between forces may be one of recording.

**Offences left out:**

- Offences the police could not place in a partnership, which ONS lists as
"Unassigned" under each force. They are about 3% of offences across England
and Wales, more in some forces in some quarters.
- **A partnership ONS marks with a note that says its offences are recorded
as "Unassigned"**, such as "Kent [note 5]", has missing figures (null),
never the published number. In the year ending June 2026, ONS says so of
five Kent partnerships (Dartford and Gravesham, Maidstone, Medway, Tonbridge
and Malling, and Tunbridge Wells). Their published figures are partial:
Medway shows 96 offences for the year, and Dartford and Gravesham 11,165,
where it showed over 19,000 a year before. The loader reads the notes from the "Notes - CSP" sheet, so
a note about anything else, such as an undercount of one kind of offence,
leaves a partnership's figures as they are.

The [analyses](analyses.md#need-against-reach) take two of these counts as
needs, `recorded_crime` and `violence_against_the_person`, for England and
Wales, and share each partnership's figure out to its councils.

The figures are fetched once and held for the rest of the session.
`clear_cache()` forgets them, to fetch a new edition.

## How the latest edition is found

ONS publishes a new edition each quarter. The ONS website gives each page as
JSON at the page's address followed by `/data`. The loader reads the
dataset's page that way, takes the first edition it lists, which is the
latest, reads that edition's page for its name and its spreadsheet, and reads
the table of offences by partnership in it. If the dataset lists no edition,
or the table or a column the loader reads has changed, it fails with a
`ValueError` naming ONS's tables. A weekly check fetches the latest edition
to make sure it still reads.

## The source and how to credit it

| | |
|---|---|
| Publisher | Office for National Statistics, from Home Office police recorded crime |
| Coverage | England and Wales, quarterly, for the latest year |
| Status | Official statistics, not accredited official statistics |
| Licence | [Open Government Licence v3.0](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) |
| Credit | Source: Office for National Statistics, Crime in England and Wales: Police Force Area data tables, year ending June 2026. Contains public sector information licensed under the Open Government Licence v3.0. |

`list_datasets()` lists it as `ons-police-recorded-crime`.
21 changes: 21 additions & 0 deletions docs/api/data-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,27 @@ names its groups' documents, and the release page lists its files.

---

## ONS police recorded crime

ONS publishes police recorded crime by community safety partnership in its
[Crime in England and Wales: Police Force Area data tables](https://www.ons.gov.uk/peoplepopulationandcommunity/crimeandjustice/datasets/policeforceareadatatables),
a new edition each quarter. Its dataset of figures by partnership alone was
discontinued in October 2024, when they moved here. The ONS website gives
each page as JSON at the page's address followed by `/data`.

| Item | Value |
|---|---|
| Edition read | The first in the dataset page's `datasets` list, which is the latest, such as `…/policeforceareadatatables/yearendingjune2026`. Its own `/data` gives its name, in `description.edition`, and its files, in `downloads` |
| File read | The edition's `.xlsx` file, at `https://www.ons.gov.uk/file?uri=<edition>/<file>` |
| Table read | `Table C2`, the number of offences by partnership. `Table C4` gives rates and `Table C5` the year before |
| Header | The row whose first cell is "Police Force Area code", below the title and notes. Headings hold line breaks and marks such as "(excluding fraud)", which the loader takes out |
| Columns | The force's code and name, the partnership's code and name, the council's code and name (or "Combined Local Authorities"), then one column per kind of offence |
| Rows | Each force's total (no partnership code), its partnerships (`E22` and `W14`), and its "Unassigned" offences (no code). The loader keeps the partnerships |
| Marked rows | A partnership ONS marks with a note, on its force's name, such as "Kent [note 5]". The notes are in the sheet `Notes - CSP`, by number. When a row's note says offences are recorded as "Unassigned", as note 5 does of five Kent partnerships in the year ending June 2026, the loader gives the partnership no figures. The note's number changes between editions. Notes about other things, such as Northumbria's undercount of robbery in the year ending March 2025, have been on a force's total, not its partnerships |
| Codes | The partnerships' codes match `local_authority_lookup("community_safety_partnership")`: all 314 in the year ending June 2026 |

---

## Dataset Aliases

Instead of remembering NOMIS dataset IDs like `NM_2002_1`, you can use
Expand Down
1 change: 1 addition & 0 deletions docs/api/datasets.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Open Geography Portal.
| [Sub-regional fuel poverty statistics](https://www.gov.uk/government/statistics/sub-regional-fuel-poverty-data-2026-2024-data) | `desnz-fuel-poverty` | Department for Energy Security and Net Zero | [people](https://www.data.gov.uk/collections/people) | Local authority districts (2025) | England | Open Government Licence v3.0 | `need-reach`, `provision-gap`, `access-distance` | Yes |
| [Children in low income families: local area statistics](https://www.gov.uk/government/collections/children-in-low-income-families-local-area-statistics) | `dwp-children-low-income` | Department for Work and Pensions | [people](https://www.data.gov.uk/collections/people) | Local authority districts (2023) | England, Wales, Scotland and Northern Ireland | Open Government Licence v3.0 | `need-reach`, `provision-gap`, `access-distance` | Yes |
| [Childhood vaccination coverage statistics (COVER programme)](https://www.gov.uk/government/collections/cover-of-vaccination-evaluated-rapidly-cover-programme-statistics) | `ukhsa-childhood-vaccinations` | UK Health Security Agency | [people](https://www.data.gov.uk/collections/people) | Counties and unitary authorities (2023) | England | Open Government Licence v3.0 | `need-reach`, `provision-gap`, `access-distance` | Yes |
| [Crime in England and Wales: Police Force Area data tables](https://www.ons.gov.uk/peoplepopulationandcommunity/crimeandjustice/datasets/policeforceareadatatables) | `ons-police-recorded-crime` | Office for National Statistics, from Home Office police recorded crime | [people](https://www.data.gov.uk/collections/people) | Community safety partnerships (2025) | England and Wales | Open Government Licence v3.0 | `need-reach`, `provision-gap`, `access-distance` | Yes |
| [TS021 - Ethnic group (NM_2041_1)](https://www.nomisweb.co.uk/api/v01/dataset/NM_2041_1.def.sdmx.json) | `nomis-nm_2041_1` | Office for National Statistics, through Nomis | [people](https://www.data.gov.uk/collections/people) | Local authority districts (2023) | England and Wales | Open Government Licence v3.0 | `who-we-miss` | Yes |
| [postcodes.io](https://postcodes.io) | `postcodes-io` | postcodes.io, built on the ONS Postcode Directory | [land-and-property](https://www.data.gov.uk/collections/land-and-property) | Postcodes | England, Wales, Scotland and Northern Ireland | Open Government Licence v3.0 | `reach-deprivation`, `reach-connectivity`, `who-we-miss`, `need-reach`, `provision-gap`, `access-distance` | No |
| [Postcode to OA (2021) to LSOA to MSOA to LAD Best Fit Lookup in the UK](https://geoportal.statistics.gov.uk) | `ons-postcode-lookup` | Office for National Statistics | [land-and-property](https://www.data.gov.uk/collections/land-and-property) | Postcodes | England and Wales | Open Government Licence v3.0 | `reach-deprivation`, `reach-connectivity`, `who-we-miss`, `need-reach`, `provision-gap`, `access-distance` | Yes |
Expand Down
11 changes: 11 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ from uk_open_data.vaccinations import load_vaccinations
table = load_vaccinations() # a row per council, age and vaccine
```

### [Police recorded crime](crime.md) — `uk_open_data.crime`

Load the latest year of police recorded crime for every community safety
partnership in England and Wales, from ONS: the offences of each kind.

```python
from uk_open_data.crime import load_crime

partnerships = load_crime() # join on `geography_code`
```

### [Council codes and areas](areas.md) — `uk_open_data.areas`

Bring a council code from any year to today's, so figures from sources made in
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ nav:
- Education statistics: api/education.md
- Children in low income families: api/low-income.md
- Childhood vaccinations: api/vaccinations.md
- Police recorded crime: api/crime.md
- Council codes and areas: api/areas.md
- Analyses: api/analyses.md
- Command Line: api/cli.md
Expand Down
12 changes: 9 additions & 3 deletions src/uk_open_data/_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def read_spreadsheet_rows(
Uses ``python-calamine`` (one fast reader for both formats). ``header_row``
is the 0-based index of the header line, or, for a sheet whose title runs
to a varying number of lines, the text in the first cell of the header
line or a test that is true of the header line's cells. Rows above it are
line, marks such as "[note 2]" aside, or a test that is true of the header
line's cells. Rows above it are
ignored. Header cells are stringified and stripped; fully empty rows are
dropped.

Expand Down Expand Up @@ -164,8 +165,13 @@ def read_spreadsheet_rows(


def _first_cell_is(first_cell: str) -> Callable[[list[Any]], bool]:
"""A test that is true of a row whose first cell is ``first_cell``."""
return lambda row: bool(row) and str(row[0]).strip() == first_cell
"""A test that is true of a row whose first cell is ``first_cell``.

Marks such as "[note 2]" are set aside on both sides, so a header is found
when a publisher adds or renumbers a note on it.
"""
wanted = plain_heading(first_cell)
return lambda row: bool(row) and plain_heading(str(row[0])) == wanted


def _first_row(
Expand Down
17 changes: 16 additions & 1 deletion src/uk_open_data/analyses/_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class NeedWords:
# of them: "people" and "person".
unit: str = "people"
one: str = "person"
# How a sentence says what is counted is in a place: "who live in", or
# "that are in" for offences, and whose place it is: "their" or "its".
who: str = "who"
live_in: str = "live in"
their: str = "their"
# Whether the measure counts everyone, so it is not a need at all.
everyone: bool = False
# Headings for the columns about need, where the shared ones do not fit.
Expand All @@ -51,13 +56,15 @@ def headings(self) -> dict[str, str]:
return dict(self.column_headings)


def _counted_in(noun: str, unit: str, one: str) -> NeedWords:
def _counted_in(noun: str, unit: str, one: str, *, people: bool = True) -> NeedWords:
"""The words for a need counted in something other than people.

Args:
noun: What is counted, such as "households in need".
unit: What one count is in, such as "households".
one: One of them, such as "household".
people: Whether what is counted lives somewhere, as households and
children do. Offences do not: they are in a place.
"""
counted = sentence_case(noun)
return NeedWords(
Expand All @@ -68,6 +75,9 @@ def _counted_in(noun: str, unit: str, one: str) -> NeedWords:
counted=counted,
unit=unit,
one=one,
who="who" if people else "that",
live_in="live in" if people else "are in",
their="their" if people else "its",
column_headings=(
("people_in_need", counted),
("people_per_100_in_need", f"Your people per 100 {noun}"),
Expand Down Expand Up @@ -118,6 +128,11 @@ def sentence_case(text: str) -> str:
CHILDREN_WITHOUT_MMR2 = _counted_in(
"children without both MMR doses", "children", "child"
)
# A need counted in offences, from police recorded crime. Offences are not
# people: one person can be the victim of many.
OFFENCES_RECORDED = _counted_in(
"offences recorded", "offences", "offence", people=False
)


def shown_first(figures: Sequence[Figure], wanted: Sequence[str]) -> tuple[str, ...]:
Expand Down
Loading
Loading