Skip to content

REF: Index.get_value call self.get_loc instead of self._engine.get_loc #31713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
d3b361c
REF: call _maybe_cast_integer first
jbrockmendel Feb 4, 2020
85a000e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 4, 2020
3ba2833
revert non-central
jbrockmendel Feb 4, 2020
465e3d7
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 4, 2020
cbfcec6
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 4, 2020
d3d09fd
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 4, 2020
2598531
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 4, 2020
834e73d
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 5, 2020
9dfe264
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 5, 2020
1c00f34
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 5, 2020
ba78e29
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 5, 2020
dae5ca9
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 5, 2020
ad1c0f7
REF: Index.get_value call self.get_loc instead of self._engine.get_loc
jbrockmendel Feb 5, 2020
16b064c
fixup remove unused imports
jbrockmendel Feb 5, 2020
d01f2db
Merge branch 'master' of https://github.com/pandas-dev/pandas into ge…
jbrockmendel Feb 6, 2020
6b02184
inherit docstring
jbrockmendel Feb 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4590,9 +4590,9 @@ def get_value(self, series: "Series", key):
# If that fails, raise a KeyError if an integer
# index, otherwise, see if key is an integer, and
# try that
loc = self._engine.get_loc(key)
loc = self.get_loc(key)
except KeyError:
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
if not self._should_fallback_to_positional():
raise
elif is_integer(key):
# If the Index cannot hold integer, then this is unambiguously
Expand All @@ -4603,6 +4603,14 @@ def get_value(self, series: "Series", key):

return self._get_values_for_loc(series, loc)

def _should_fallback_to_positional(self) -> bool:
"""
If an integer key is not found, should we fall back to positional indexing?
"""
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
return False
return True

def _get_values_for_loc(self, series: "Series", loc):
"""
Do a positional lookup on the given Series, returning either a scalar
Expand Down
29 changes: 1 addition & 28 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Shared methods for Index subclasses backed by ExtensionArray.
"""
from typing import TYPE_CHECKING, List
from typing import List

import numpy as np

Expand All @@ -11,7 +11,6 @@
from pandas.core.dtypes.common import (
ensure_platform_int,
is_dtype_equal,
is_integer,
is_object_dtype,
)
from pandas.core.dtypes.generic import ABCSeries
Expand All @@ -21,9 +20,6 @@
from pandas.core.indexes.base import Index
from pandas.core.ops import get_op_result_name

if TYPE_CHECKING:
from pandas import Series


def inherit_from_data(name: str, delegate, cache: bool = False, wrap: bool = False):
"""
Expand Down Expand Up @@ -297,26 +293,3 @@ def astype(self, dtype, copy=True):
# pass copy=False because any copying will be done in the
# _data.astype call above
return Index(new_values, dtype=new_values.dtype, name=self.name, copy=False)

# --------------------------------------------------------------------
# Indexing Methods

@Appender(Index.get_value.__doc__)
def get_value(self, series: "Series", key):
"""
Fast lookup of value from 1-dimensional ndarray. Only use this if you
know what you're doing
"""
try:
loc = self.get_loc(key)
except KeyError:
# e.g. DatetimeIndex doesn't hold integers
if is_integer(key) and not self.holds_integer():
# Fall back to positional
loc = key
else:
raise

return self._get_values_for_loc(series, loc)

# --------------------------------------------------------------------
7 changes: 4 additions & 3 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,10 @@ def is_overlapping(self) -> bool:
# GH 23309
return self._engine.is_overlapping

def holds_integer(self):
return self.dtype.subtype.kind not in ["m", "M"]
# TODO: There must already exist something for this?
def _should_fallback_to_positional(self):
# integer lookups in Series.__getitem__ are unambiguously
# positional in this case
return self.dtype.subtype.kind in ["m", "M"]

@Appender(Index._convert_scalar_indexer.__doc__)
def _convert_scalar_indexer(self, key, kind=None):
Expand Down
27 changes: 11 additions & 16 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any
from typing import Any

import numpy as np

Expand Down Expand Up @@ -32,12 +32,9 @@

from pandas.core import algorithms
import pandas.core.common as com
from pandas.core.indexes.base import Index, InvalidIndexError, maybe_extract_name
from pandas.core.indexes.base import Index, maybe_extract_name
from pandas.core.ops import get_op_result_name

if TYPE_CHECKING:
from pandas import Series

_num_index_shared_docs = dict()


Expand Down Expand Up @@ -383,6 +380,13 @@ def astype(self, dtype, copy=True):
return Int64Index(arr)
return super().astype(dtype, copy=copy)

# ----------------------------------------------------------------
# Indexing Methods

@Appender(Index._should_fallback_to_positional.__doc__)
def _should_fallback_to_positional(self):
return False

@Appender(Index._convert_scalar_indexer.__doc__)
def _convert_scalar_indexer(self, key, kind=None):
assert kind in ["loc", "getitem", "iloc", None]
Expand All @@ -401,6 +405,8 @@ def _convert_slice_indexer(self, key: slice, kind=None):
# translate to locations
return self.slice_indexer(key.start, key.stop, key.step, kind=kind)

# ----------------------------------------------------------------

def _format_native_types(
self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
):
Expand All @@ -416,17 +422,6 @@ def _format_native_types(
)
return formatter.get_result_as_array()

@Appender(Index.get_value.__doc__)
def get_value(self, series: "Series", key):
"""
We always want to get an index value, never a value.
"""
if not is_scalar(key):
raise InvalidIndexError

loc = self.get_loc(key)
return self._get_values_for_loc(series, loc)

def equals(self, other) -> bool:
"""
Determines if two Index objects contain the same elements.
Expand Down