Skip to content

Off-by-one indexing error in paired_manhattan_distances docstring #34315

Closed

Description

@shipitdev

Describe the issue linked to the documentation

found a small index error in the docstring for sklearn.metrics.pairwise.paired_manhattan_distances.

The docstring in sklearn/metrics/pairwise.py says it calculates distances up to:
"(X[0], Y[0]), (X[1], Y[1]), ..., (X[n_samples], Y[n_samples])."

Because Python starts counting at 0, the last item in an array of size n_samples is actually at n_samples - 1. If we try to use X[n_samples] in real code, Python throws an IndexError.

Steps/Code to Reproduce

Here is a quick code example using the docstring's own matrix shape to show the error:

import sklearn
import numpy as np 

print(f"Scikit-Learn Version: {sklearn.__version__}")

n_samples = 3
X = np.array([[1, 1, 0], [0, 1, 0], [0, 0, 1]]) 

try:
    X[n_samples] 
except IndexError as e:
    print(f"Error: {e}")
    
# Output: Error: index 3 is out of bounds for axis 0 with size 3

Scikit-Learn Version: 1.8.0

Suggest a potential alternative/fix

To fix this, we just need to update the docstring in sklearn/metrics/pairwise.py to end with (X[n_samples - 1], Y[n_samples - 1]).

Let me know if I should open a quick PR to patch this!

Activity

  1. Aravind-1294 commented on Jun 17, 2026

    @Aravind-1294

    Hi, I'm interested in contributing this fix. If no one is already working on it, I'd be happy to open a PR. Thanks!

  2. AnneBeyer commented on Jun 17, 2026

    @AnneBeyer
    Member

    Hi @Aravind-1294, thank you for your interest, but please note the warning message at the top of the issue and the fact that @shipitdev already expressed interest in opening a PR for this.

  3. Aravind-1294 commented on Jun 17, 2026

    @Aravind-1294

    My bad, I overlooked the warning and @shipitdev's comment. Thanks for the clarification!

  4. shipitdev commented on Jun 17, 2026

    @shipitdev
    ContributorAuthor

    thanks @AnneBeyer for keeping an eye on the thread, And no worries at all @Aravind-1294 :) I appreciate the enthusiasm. i am just hanging tight until a core maintainer drops the Needs Triage label so i can raise a PR. i hope im right about the protocol here, please let me know if that's not how the it works. Thank you :)

  5. AnneBeyer commented on Jul 1, 2026

    @AnneBeyer
    Member

    Hi @shipitdev, thank you for bringing this up and thank you for your patience. Looking at the metric's docstrings, there are different versions used across the paired distances (all that return ndarray of shape (n_samples,)). While only paired_manhattan_distances has an indexing error, would you mind making them consistent while you are at it? I think the return value description in paired_cosine_distances is actually the clearest. For the docstring, I would simply go with Distances are calculated between (X[0], Y[0]), (X[1], Y[1]), etc. to keep it short.

  6. removed
    Needs Triage
    on Jul 1, 2026
  7. shipitdev commented on Jul 1, 2026

    @shipitdev
    ContributorAuthor

    Hi @shipitdev, thank you for bringing this up and thank you for your patience. Looking at the metric's docstrings, there are different versions used across the paired distances (all that return ndarray of shape (n_samples,)). While only paired_manhattan_distances has an indexing error, would you mind making them consistent while you are at it? I think the return value description in paired_cosine_distances is actually the clearest. For the docstring, I would simply go with Distances are calculated between (X[0], Y[0]), (X[1], Y[1]), etc. to keep it short.

    Hey @AnneBeyer, thanks for looking into this and removing the triage label!

    I'd be happy to make those consistent. I'll standardize the docstring to your request and modify the return blocks to match paired_cosine_distances across all the 4 paired_distance functions i found in this file and push the changes to my open PR shortly! Thank you for the suggestion :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions