Skip to main content

duckstatsbomb

Get flat tables from Hudl StatsBomb data, in multiple formats, in seconds once cached.

DuckDB does the parsing in parallel. You can parse a whole Premier League season of 1.3 million events from JSON into pandas, Polars or Arrow tables in a few seconds. Or use the default output, a DuckDB relation, and filter to the shots and sum the xG before building a DataFrame, so you can query the whole open-data set on your laptop.

Docs available at: https://duckstatsbomb.readthedocs.io

Installation

The duckstatsbomb default has one dependency, DuckDB. You can install optional dependencies to export to pandas, Polars, or Arrow:

pip install duckstatsbomb
pip install "duckstatsbomb[pandas]"  # pandas
pip install "duckstatsbomb[polars]"  # Polars & PyArrow
pip install "duckstatsbomb[arrow]"  # PyArrow
pip install "duckstatsbomb[all]"  # pandas, Polars & PyArrow

Output formats

By default the library outputs a DuckDBPyRelation, which you can filter, aggregate, or query.

from duckstatsbomb import Sbopen
parser = Sbopen()
events = parser.competition_data(competition_id=43, season_id=106, kind='events')
shots = events.filter("type_name = 'Shot'")
# top 4 goal scorers at the 2022 World Cup
top = (
    shots.aggregate(
        'player_name, team_name, count(*) as shots, '
        'round(sum(shot_statsbomb_xg), 2) as xg, '
        "count(*) filter (outcome_name = 'Goal') as goals",
        'player_name, team_name',
    )
    .order('goals desc, xg desc')
    .limit(4)
)
top.show()

You can also export to different formats:

df_pandas = shots.df()  # pip install "duckstatsbomb[pandas]"
df_polars = shots.pl()  # pip install "duckstatsbomb[polars]"
arrow_table = shots.to_arrow_table()  # pip install "duckstatsbomb[arrow]"
shots.to_csv('world_cup_2022_shots.csv')
shots.to_parquet('world_cup_2022_shots.parquet')

Or set the output format when creating the parser:

parser = Sbopen(output_format='pandas')  # 'relation', 'pandas', 'polars' or 'arrow'

Three parsers

There are three parsers: Sbopen, Sbapi, and Sbfiles.

All three have common methods: competitions (competition info), matches (match info), and match_data (event, lineup, and three-sixty info). Sbopen and Sbapi also include competition_data for getting whole season data (event, lineups, and three-sixty).

Competitions data

from duckstatsbomb import Sbopen  # or Sbapi/Sbfiles
parser = Sbopen()  # or Sbapi() / Sbfiles()
competitions = parser.competitions()  # filenames for Sbfiles

Matches data

from duckstatsbomb import Sbopen  # or Sbapi/Sbfiles
parser = Sbopen()  # or Sbapi() / Sbfiles()
matches = parser.matches(2, 44)  # filenames for Sbfiles

Event data

from duckstatsbomb import Sbopen  # or Sbapi/Sbfiles
parser = Sbopen()  # or Sbapi() / Sbfiles()
# see parser.kinds for valid kind
events = parser.match_data(3857254, kind='events')  # filenames for Sbfiles

Competition/season data

from duckstatsbomb import Sbopen  # or Sbapi
parser = Sbopen()  # or Sbapi()
# see parser.kinds for valid kind
# no method for Sbfiles
lineup_players = parser.competition_data(competition_id=43, season_id=106,
                                         kind='lineup_players')

Sbapi needs a Hudl StatsBomb API subscription. Pass the username and password as class arguments, or set the SB_USERNAME and SB_PASSWORD environment variables.

Cache

Sbopen and Sbapi cache the downloaded match files as raw JSON in the cache_path directory. You can list the cache, delete the files for particular matches, or delete the whole directory.

from duckstatsbomb import Sbopen
parser = Sbopen()
parser.cached_files()  # path, size and UTC download time of each file
parser.sources  # the match files: ['events', 'lineups', 'threesixty']
parser.stale_matches(43, 106, source='events')  # identify stale match IDs
parser.clear_match_data([3857254, 3857255], source='events')
parser.clear_cache()

The competitions and matches files are never cached, as they may change often when Hudl StatsBomb release or reprocess data.

You can turn off the cache with the class argument cache_enabled, change the cache_path or design your own cache backend and pass it to cache.

Release files for duckstatsbomb 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for duckstatsbomb 0.4.0
File Size Uploaded
duckstatsbomb-0.4.0.tar.gz 108.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for duckstatsbomb 0.4.0
File Interpreter ABI Platform
duckstatsbomb-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 150.6 kB

Release files / duckstatsbomb-0.4.0.tar.gz

Download URL duckstatsbomb-0.4.0.tar.gz
Size 108.9 kB
Tags Source
SHA-256 checksum
How to use checksums
72d55bfd9e16a5dc45af3638d1c22f72057ecb20986f9ae0a0e9a4cf64a7e727
BLAKE2b-256 checksum
How to use checksums
26aa57f17b2ca3dcf76848f0e5a622b201569f9e3962146c1da04bc5f22b06cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / duckstatsbomb-0.4.0-py3-none-any.whl

Download URL duckstatsbomb-0.4.0-py3-none-any.whl
Size 41.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa3fa0c9cf08c930804b670ea312bbaeefd85c9c0923f0c4028fb8685430a8d1
BLAKE2b-256 checksum
How to use checksums
b29f8eb7fd0d65c748ac7a6c43272237291ab459aff45697b1a9dc19a2a08419
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page