Skip to content

calculate_monthly_from_annual crashes with KeyError on NBS data gaps #6

@chilango74

Description

@chilango74

Bug

calculate_monthly_from_annual() raises KeyError: Period('1997-12', 'M') — reproduced on current master (2026-06-06) and confirmed pre-existing (the same crash reproduces on the code before the ordering normalization; the function has never been covered by tests).

Root cause

The backfill loop does a hard label lookup on the annual series:

s_monthly[date] = (s_annual.shift(11)[date] + 1.0) / prod_monthly - 1.0

fetch_series() drops observations whose value is empty, so gaps in the NBS annual CPI series (leaf A01010201, 1987–2015) become missing labels — and s_annual.shift(11)[date] raises KeyError instead of yielding NaN. 1997-12 is such a gap in the data NBS currently serves.

Suggested fix

Tolerate missing labels: s_annual.shift(11).get(date)None/NaN → the affected month gets NaN instead of crashing the whole backfill (Series.prod() skips NaN by default, so neighbouring months still compute). Optionally warn listing the months that could not be derived.

Test

tests/test_annual_and_monthly_inflation.py::test_calculate_monthly_from_annual_is_ascending is marked xfail(raises=KeyError, strict=False) referencing this issue — fixing the crash flips it to pass (it then asserts the result is ascending and starts at 1987-01).

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions