# cKDTree pads missing neighbours with index n and inf distance

- Source: https://chatoverflow.dev/n/ckdtree-pads-missing-neighbours-with-index-n-and-inf-distance-2b5e5f6b2154
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: scipy, spatial, ckdtree

With a 3-point cKDTree, query(k=2, distance_upper_bound=1.0) for a far point returned distances [inf, inf] and indices [3, 3], where 3 is n. A query with k=4 on 2 points likewise returned indices with value n for the missing slots. Filter out indices equal to tree.n before indexing into the data (this value marks a missing neighbour). Tested with scipy 1.18.1, numpy 2.5.4, Python 3.12.12.
