Skip to content

ENH: Support method and tolerance kwargs in Index.slice_indexer #62287

@TomNicholas

Description

@TomNicholas

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I wish I could pass method and tolerance kwargs to pandas.Index.slice_indexer, as I can to pandas.Index.get_indexer.

Feature Description

Add new parameters to pandas.Index.slice_indexer, so that it looks like

class Index:
    def slice_indexer(start=None, end=None, step=None, method=None, tolerance=None):
        """
        Compute the slice indexer for input labels and step.

        Index needs to be ordered and unique.

        Parameters
        ----------
        start : label, default None
            If None, defaults to the beginning.
        end : label, default None
            If None, defaults to the end.
        step : int, default None
        method : {None, ‘pad/ffill’, ‘backfill/bfill’, ‘nearest’}, optional
            - default: exact matches only.
            - pad / ffill: find the PREVIOUS index value if no exact match.
            - backfill / bfill: use NEXT index value if no exact match
            - nearest: use the NEAREST index value if no exact match. Tied distances are broken by preferring the larger index value.
        tolerance : optional 
            Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation abs(index[indexer] - target) <= tolerance.

            Tolerance may be a scalar value, which applies the same tolerance to all values, or list-like, which applies variable tolerance per element. List-like includes list, tuple, array, Series, and must be the same size as the index and its dtype must exactly match the indexs type.

        Returns
        -------
        slice

Alternative Solutions

Currently I've effectively written a custom version of .slice_indexer that calls .get_indexer on the start and end bounds, but I'm worried that doing this downstream will miss edge cases compared to solving it upstream in pandas.

Additional Context

This would be nice for use within the internals of xarray's .sel() methods. See pydata/xarray#10710.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementIndexingRelated to indexing on series/frames, not to indexes themselvesNeeds InfoClarification about behavior needed to assess issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions