Skip to content

pandas deprecates Index.get_loc with method #5721

Closed
@mathause

Description

@mathause

pandas deprecates the method keyword in Index.get_loc, see pandas-dev/pandas#42269. Therefore we end up with about 5000 warnings in our upstream tests:

FutureWarning: Passing method to Index.get_loc is deprecated and will raise in a future version. Use index.get_indexer([item], method=...) instead

We should fix this before pandas releases because the warning will not be silent (FutureWarning) or ask pandas to give us more time and use a DeprecationWarning at the moment.

We use this here:

indexer = self.index.get_loc(
label_value, method=method, tolerance=tolerance
)

Is this only ever called with one item? Then we might be able to use

 indexer = self.index.get_indexer( 
     [label_value], method=method, tolerance=tolerance 
 ).item()
if indexer == -1:
    raise KeyError(label_value)

imin = index.get_loc(minval, method="nearest")
imax = index.get_loc(maxval, method="nearest")

This one could be easy to fix (replace with imin = index.get_indexer([minval], method="nearest").item())


It is also defined in CFTimeIndex, which complicates things:

def get_loc(self, key, method=None, tolerance=None):
"""Adapted from pandas.tseries.index.DatetimeIndex.get_loc"""
if isinstance(key, str):
return self._get_string_slice(key)
else:
return pd.Index.get_loc(self, key, method=method, tolerance=tolerance)

because get_indexer expects an iterable and thus the if isinstance(key, str) test no longer works.

@benbovy @spencerkclark

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