python / python/cpython

statistics.kde has inconsistent and counterintuitive caching behaviour

Open
#154,942 2 comments 0 reactions 1 assignee View on GitHub

@rhettinger is already working on this.

Since Aug 6, 2026.

docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The author appears to be trying to support this pattern:

data = [1, 2, 3]

f = kde(data, h=1)
data.append(4) # length changed
f(2)

In that case len(data) != n becomes true and the sorted sample is rebuilt.

However, this pattern is implemented inconsistently, and does not check value changes, only length changes.

import statistics
data = [1, 2, 3]
f = statistics.kde(data, h=1.0, kernel='triangular')
z = f(1.5)

print(z)
data[0] = 5
print(f(1.5))
data.pop()
print(f(1.5))


data = [1, 2, 3]
f = statistics.kde(data, h=1.0, kernel='normal')
z = f(1.5)

print(z)
data[0] = 5
print(f(1.5))
data.pop()
print(f(1.5))

output:

0.3333333333333333
0.3333333333333333
0.25
vs.
0.2778827497314969
0.16081853504174567
0.17646900472967264
CPython versions tested on:

3.13

Operating systems tested on:

Other

Linked PRs
  • gh-155034

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.