API reference¶
All public functions are available from the top-level package:
import pyerviss as pv
Querying¶
- pyerviss.get_ili(countries=None, start=None, end=None, season=None, age_groups=None, as_of=None)¶
Weekly influenza-like illness (ILI) consultation rates (primary care).
- Parameters:
countries (str | Iterable[str] | None) – Country name(s) or ISO2 code(s), case-insensitive, e.g. “Italy” or [“FR”, “Spain”]. Default: all countries.
start (str | date | None) – First week to include, as “2024-W40”, “2024-10-01” or a date.
end (str | date | None) – Last week to include (inclusive), in the same formats.
season (str | Iterable[str] | None) – Season(s) such as “2024/25”, which runs from 2024-W40 to 2025-W39. Cannot be combined with start/end.
age_groups (str | Iterable[str] | None) – Age group(s) among “0-4”, “5-14”, “15-64”, “65+” and “total”. Default: all. Before 2022-W25, some countries only have “total”.
as_of (str | date | None) – Return the data as ECDC published it on this date (a date or “YYYY-MM-DD”), for backtesting without later revisions. Default: current data.
- Returns:
DataFrame with columns indicator (“ili”, “ari” or “sari”), country, country_code, year_week, date (Sunday of the week), age, value (the rate) and unit (e.g. “per 100,000 population”). Empty if nothing matches. With as_of, also vintage: the date of the ECDC snapshot a row comes from, or NaT for weeks older than the snapshot covers.
- Raises:
InvalidParameterError – Unknown indicator, country or age group, a malformed week or season, or season combined with start/end.
DataNotFoundError – A requested country has no data for this indicator.
- Return type:
- pyerviss.get_ari(countries=None, start=None, end=None, season=None, age_groups=None, as_of=None)¶
Weekly acute respiratory infection (ARI) consultation rates (primary care).
- Parameters:
countries (str | Iterable[str] | None) – Country name(s) or ISO2 code(s), case-insensitive, e.g. “Italy” or [“FR”, “Spain”]. Default: all countries.
start (str | date | None) – First week to include, as “2024-W40”, “2024-10-01” or a date.
end (str | date | None) – Last week to include (inclusive), in the same formats.
season (str | Iterable[str] | None) – Season(s) such as “2024/25”, which runs from 2024-W40 to 2025-W39. Cannot be combined with start/end.
age_groups (str | Iterable[str] | None) – Age group(s) among “0-4”, “5-14”, “15-64”, “65+” and “total”. Default: all. Before 2022-W25, some countries only have “total”.
as_of (str | date | None) – Return the data as ECDC published it on this date (a date or “YYYY-MM-DD”), for backtesting without later revisions. Default: current data.
- Returns:
DataFrame with columns indicator (“ili”, “ari” or “sari”), country, country_code, year_week, date (Sunday of the week), age, value (the rate) and unit (e.g. “per 100,000 population”). Empty if nothing matches. With as_of, also vintage: the date of the ECDC snapshot a row comes from, or NaT for weeks older than the snapshot covers.
- Raises:
InvalidParameterError – Unknown indicator, country or age group, a malformed week or season, or season combined with start/end.
DataNotFoundError – A requested country has no data for this indicator.
- Return type:
- pyerviss.get_sari(countries=None, start=None, end=None, season=None, age_groups=None, as_of=None)¶
Weekly severe acute respiratory infection (SARI) rates (hospitals; Slovakia: ICU).
- Parameters:
countries (str | Iterable[str] | None) – Country name(s) or ISO2 code(s), case-insensitive, e.g. “Italy” or [“FR”, “Spain”]. Default: all countries.
start (str | date | None) – First week to include, as “2024-W40”, “2024-10-01” or a date.
end (str | date | None) – Last week to include (inclusive), in the same formats.
season (str | Iterable[str] | None) – Season(s) such as “2024/25”, which runs from 2024-W40 to 2025-W39. Cannot be combined with start/end.
age_groups (str | Iterable[str] | None) – Age group(s) among “0-4”, “5-14”, “15-64”, “65+” and “total”. Default: all. Before 2022-W25, some countries only have “total”.
as_of (str | date | None) – Return the data as ECDC published it on this date (a date or “YYYY-MM-DD”), for backtesting without later revisions. Default: current data.
- Returns:
DataFrame with columns indicator (“ili”, “ari” or “sari”), country, country_code, year_week, date (Sunday of the week), age, value (the rate) and unit (e.g. “per 100,000 population”). Empty if nothing matches. With as_of, also vintage: the date of the ECDC snapshot a row comes from, or NaT for weeks older than the snapshot covers.
- Raises:
InvalidParameterError – Unknown indicator, country or age group, a malformed week or season, or season combined with start/end.
DataNotFoundError – A requested country has no data for this indicator.
- Return type:
- pyerviss.get_data(indicator, countries=None, start=None, end=None, season=None, age_groups=None, as_of=None)¶
Get rates for an indicator, optionally filtered.
- Parameters:
indicator (str) – “ili”, “ari” or “sari”.
countries (str | Iterable[str] | None) – Country name(s) or ISO2 code(s), case-insensitive, e.g. “Italy” or [“FR”, “Spain”]. Default: all countries.
start (str | date | None) – First week to include, as “2024-W40”, “2024-10-01” or a date.
end (str | date | None) – Last week to include (inclusive), in the same formats.
season (str | Iterable[str] | None) – Season(s) such as “2024/25”, which runs from 2024-W40 to 2025-W39. Cannot be combined with start/end.
age_groups (str | Iterable[str] | None) – Age group(s) among “0-4”, “5-14”, “15-64”, “65+” and “total”. Default: all. Before 2022-W25, some countries only have “total”.
as_of (str | date | None) – Return the data as ECDC published it on this date (a date or “YYYY-MM-DD”), for backtesting without later revisions. Default: current data.
- Returns:
DataFrame with columns indicator (“ili”, “ari” or “sari”), country, country_code, year_week, date (Sunday of the week), age, value (the rate) and unit (e.g. “per 100,000 population”). Empty if nothing matches. With as_of, also vintage: the date of the ECDC snapshot a row comes from, or NaT for weeks older than the snapshot covers.
- Raises:
InvalidParameterError – Unknown indicator, country or age group, a malformed week or season, or season combined with start/end.
DataNotFoundError – A requested country has no data for this indicator.
- Return type:
Positivity¶
- pyerviss.get_positivity(pathogen=None, setting=None, countries=None, start=None, end=None, season=None, as_of=None)¶
Weekly test positivity for influenza, RSV and SARS-CoV-2.
Positivity is the percentage of tested samples that were positive. Primary care samples come from patients with ILI and/or ARI at sentinel GPs; hospital samples from SARI patients. Data is available for all ages combined only.
- Parameters:
pathogen (str | Iterable[str] | None) – “influenza”, “rsv” or “sars-cov-2” (case-insensitive; “flu” and “covid” are accepted), or a list. Default: all three.
setting (str | Iterable[str] | None) – “primary care” or “hospital”, or a list. Default: both.
countries (str | Iterable[str] | None) – Country name(s) or ISO2 code(s), case-insensitive. “EU/EEA” (code “EU”) is ECDC’s aggregate for the region. Default: all countries.
start (str | date | None) – First week to include, as “2024-W40”, “2024-10-01” or a date.
end (str | date | None) – Last week to include (inclusive), in the same formats.
season (str | Iterable[str] | None) – Season(s) such as “2024/25”, which runs from 2024-W40 to 2025-W39. Cannot be combined with start/end.
as_of (str | date | None) – Return the data as ECDC published it on this date (a date or “YYYY-MM-DD”), for backtesting without later revisions. Default: current data.
- Returns:
DataFrame with columns indicator (“positivity”), setting, pathogen (“Influenza”, “RSV” or “SARS-CoV-2”), country, country_code, year_week, date (Sunday of the week), age (“total”), value (positivity in percent), unit (“%”), tests and detections (the counts positivity is computed from; small numbers of tests make positivity noisy). Empty if nothing matches. With as_of, also vintage: the date of the ECDC snapshot a row comes from, or NaT for weeks older than the snapshot covers.
- Raises:
InvalidParameterError – Unknown pathogen, setting or country, a malformed week or season, or season combined with start/end.
DataNotFoundError – A requested country has no positivity data for the requested pathogens and settings.
- Return type:
- pyerviss.get_detections(pathogen=None, setting=None, countries=None, start=None, end=None, season=None, level='type', as_of=None)¶
Weekly influenza and RSV detections by type or subtype, with their shares.
For each country and week: how many positive samples were of each type (influenza A, B; RSV-A, RSV-B) or subtype (A(H1)pdm09, A(H3), B/Victoria…), and that category’s share of all detections of the pathogen. Samples that were not typed or subtyped are kept as categories (“untyped”, “A (unknown)”, “B (unknown)”), so shares add up to 1; for shares among typed samples only, drop those rows and rescale.
ECDC only publishes categories with at least one detection: a category missing from a week means 0 detections. Weeks without any typed detection have no rows, and neither do weeks before a country started reporting types (for RSV, mostly before 2021).
- Parameters:
pathogen (str | Iterable[str] | None) – “influenza” or “rsv” (case-insensitive; “flu” is accepted), or both. Default: both. SARS-CoV-2 detections are not split by type.
setting (str | Iterable[str] | None) – “primary care” or “hospital”, or a list. Default: both.
countries (str | Iterable[str] | None) – Country name(s) or ISO2 code(s), case-insensitive. Default: all.
start (str | date | None) – First week to include, as “2024-W40”, “2024-10-01” or a date.
end (str | date | None) – Last week to include (inclusive), in the same formats.
season (str | Iterable[str] | None) – Season(s) such as “2024/25”, which runs from 2024-W40 to 2025-W39. Cannot be combined with start/end.
level (str) – “type” (influenza A, B, untyped; RSV-A, RSV-B, untyped) or “subtype” (A(H1)pdm09, A(H3), A (unknown), B/Victoria, B/Yamagata, B (unknown), untyped; RSV is the same as at type level).
as_of (str | date | None) – Return the data as ECDC published it on this date (a date or “YYYY-MM-DD”), for backtesting without later revisions. Default: current data.
- Returns:
DataFrame with columns indicator (“detections”), setting, pathogen (“Influenza” or “RSV”), type, subtype (with level=”subtype” only), country, country_code, year_week, date (Sunday of the week), age (“total”), value (number of detections), unit (“count”), share (value / total, NaN when total is 0) and total (detections of the pathogen that week, summed over the categories). With as_of, also vintage. Empty if nothing matches.
- Raises:
InvalidParameterError – Unknown or untyped pathogen (SARS-CoV-2), unknown setting, country or level, a malformed week or season, or season combined with start/end.
DataNotFoundError – A requested country has no detections for the requested pathogens and settings.
- Return type:
Past vintages¶
Pass as_of to any query function above to get data as ECDC published it on a past
date; see Past vintages (backtesting).
- pyerviss.list_vintages(indicator, setting=None)¶
Dates of ECDC’s snapshots of an indicator, usable as as_of, oldest first.
- Parameters:
- Return type:
ILI and ARI snapshots start on 2023-11-24, primary care positivity on 2023-12-01, SARI and hospital positivity on 2024-11-08. Most weeks have one snapshot, but ECDC skipped some weeks.
Plotting¶
Requires matplotlib: pip install "pyerviss[plot]".
- pyerviss.plot_seasons(df: DataFrame, highlight: str | Iterable[str] | None = None, facet: None = None, sharey: bool = False, ax: matplotlib.axes.Axes | None = None, start_week: int = 25, past_seasons: int | None = 3) matplotlib.axes.Axes¶
- pyerviss.plot_seasons(df: DataFrame, highlight: str | Iterable[str] | None = None, *, facet: str, sharey: bool = False, ax: matplotlib.axes.Axes | None = None, start_week: int = 25, past_seasons: int | None = 3) matplotlib.figure.Figure
- pyerviss.plot_seasons(df: DataFrame, highlight: str | Iterable[str] | None, facet: str, sharey: bool = False, ax: matplotlib.axes.Axes | None = None, start_week: int = 25, past_seasons: int | None = 3) matplotlib.figure.Figure
Plot one line per season, aligned on the week of the season (W25 to W24).
As on ERVISS’s dashboard, plotted seasons run from week 25 to week 24 of the next year, so the summer trough comes first and the autumn rise and winter peak are fully visible. A plotted season “2024/25” therefore runs from 2024-W25 to 2025-W24, unlike the
seasonfilter of queries (W40 to W39); passstart_week=40to plot those.Highlighted seasons are drawn in colour. For context, the 3 most recent other seasons are drawn in shades of grey (darker = more recent) and named in the legend. If
dfhasci_lowandci_highcolumns (seeadd_positivity_ci), the interval is drawn as a band around highlighted seasons. Weeks without data are left as gaps.- Parameters:
df (pandas.DataFrame) – A result of
get_ili,get_ari,get_sariorget_positivity. Withoutfacetit must contain a single series: one indicator, country and age group (and, for positivity, one pathogen and setting).highlight (str | Iterable[str] | None) – Season(s) to draw in colour, e.g. “2024/25” or [“2023/24”, “2024/25”] (at most three). Default: the most recent season in
df.facet (str | None) – “country” or “age” to draw one panel per country or age group. Every other column must then have a single value.
sharey (bool) – Use the same y-axis range in all panels (facets only). Only allowed when all panels have the same unit.
ax (matplotlib.axes.Axes | None) – Matplotlib axes to draw on (without facet only). Default: a new figure.
start_week (int) – ISO week that starts a plotted season. Default 25.
past_seasons (int | None) – How many other seasons to draw for context, the most recent ones first. Default 3. None draws every season in the data, in a single grey labelled “other seasons”; 0 draws only the highlighted seasons.
- Returns:
The matplotlib
Axeswithout facet, or theFigurewith facets.- Raises:
InvalidParameterError –
dfis empty or has several series, an invalid highlight, facet or sharey, oraxcombined withfacet.ImportError – matplotlib is not installed.
- Return type:
Derived columns¶
- pyerviss.add_season_week(df, start_week=40)¶
Add the season and the week of the season to each row.
A season “2024/25” runs from 2024-W40 to 2025-W39. Its weeks are numbered from 1 (W40) to 52, or 53 in seasons containing an ISO week 53.
- Parameters:
- Returns:
A copy of
dfwithseason(e.g. “2024/25”) andseason_week(int) columns added.- Return type:
- pyerviss.add_positivity_ci(df, level=0.95)¶
Add a confidence interval for positivity, computed from tests and detections.
Uses the Wilson score interval, which stays sensible for small numbers of tests (1 positive out of 1 test gives roughly 21% to 100%, not a confident 100%).
- Parameters:
- Returns:
A copy of
dfwithci_lowandci_highcolumns, in percent. They are missing (NaN) where tests are missing or zero.- Return type:
Discovering data¶
- pyerviss.coverage(indicator, as_of=None)¶
Per country: first and last week with data, number of weeks, and unit.
- Parameters:
- Returns:
A DataFrame. For rates, one row per country with columns country, country_code, first_week, last_week, n_weeks (weeks with data in any age group), age_groups and unit. For positivity, one row per setting, pathogen and country with columns setting, pathogen, country, country_code, first_week, last_week, n_weeks and unit.
- Return type:
- pyerviss.list_countries(indicator)¶
Countries with data for an indicator (“ili”, “ari”, “sari” or “positivity”).
- pyerviss.list_seasons(indicator=None)¶
Seasons with data, e.g. [“2014/15”, …, “2026/27”]. Default: any indicator.
Cache¶
- pyerviss.update_data()¶
Check all data files for updates now and download any that changed.
- Return type:
None
- pyerviss.clear_cache()¶
Delete all cached data files.
- Return type:
None
Exceptions¶
All exceptions derive from PyERVISSError.
Custom exceptions for PyERVISS.
- exception pyerviss.exceptions.DataNotFoundError¶
Bases:
PyERVISSErrorRaised when requested data is not available.
- exception pyerviss.exceptions.DataFetchError¶
Bases:
PyERVISSErrorRaised when data fetching from remote source fails.
- exception pyerviss.exceptions.InvalidParameterError¶
Bases:
PyERVISSErrorRaised when invalid parameters are provided.
- exception pyerviss.exceptions.CacheError¶
Bases:
PyERVISSErrorRaised when cache operations fail.
Lower level¶
These are not needed for normal use.
- pyerviss.data_loader.get_metadata(force_refresh=False)¶
Return data/metadata.json: last update, record counts, date ranges and units.
- pyerviss.data_loader.load_csv(file_name, force_refresh=False)¶
Load a data file as a DataFrame with ECDC’s columns (value as float).
- pyerviss.data_loader.fetch_file(file_name, force_refresh=False)¶
Return the path of an up-to-date cached copy of a data file.
- Parameters:
- Raises:
DataFetchError – The file could not be downloaded and no cached copy exists.
- Return type:
- pyerviss.cache.get_cache_dir()¶
Return the cache directory, creating it if needed.
- Return type:
Weeks and seasons¶
ISO week and season helpers.
Weeks are ISO 8601 weeks written “YYYY-Www” (e.g. “2024-W40”). The date of a week is
its last day, Sunday, matching RespiCast’s truth_date. A season “2024/25” runs from
2024-W40 to 2025-W39.
- pyerviss.utils.parse_yearweek(year_week)¶
Split “2024-W40” into (2024, 40), checking that the week exists.
- pyerviss.utils.yearweek_to_date(year_week)¶
Last day (Sunday) of an ISO week: “2024-W40” -> date(2024, 10, 6).
- pyerviss.utils.date_to_yearweek(d)¶
ISO week containing a date: date(2024, 10, 1) -> “2024-W40”.
- pyerviss.utils.to_yearweek(value)¶
Normalize a week given as “2024-W40”, “2024-10-01”, a date or a datetime.
- pyerviss.utils.parse_season(season)¶
First and last week of a season: “2024/25” -> (“2024-W40”, “2025-W39”).
- pyerviss.utils.check_start_week(start_week)¶
Raise InvalidParameterError unless start_week is an ISO week from 1 to 52.
- Parameters:
start_week (int)
- Return type:
None
- pyerviss.utils.season_of(year_week, start_week=40)¶
Season a week belongs to: “2024-W40” -> “2024/25”, “2025-W39” -> “2024/25”.
With another
start_week, seasons run from that week to the week before it, e.g. start_week=25: “2024-W25” -> “2024/25”, “2025-W24” -> “2024/25”.