|
68 | 68 | from pandas.core.arrays import ExtensionArray
|
69 | 69 | from pandas.core.base import IndexOpsMixin, PandasObject
|
70 | 70 | import pandas.core.common as com
|
71 |
| -from pandas.core.construction import extract_array |
72 | 71 | from pandas.core.indexers import maybe_convert_indices
|
73 | 72 | from pandas.core.indexes.frozen import FrozenList
|
74 | 73 | import pandas.core.missing as missing
|
@@ -4627,48 +4626,40 @@ def get_value(self, series, key):
|
4627 | 4626 | # would convert to numpy arrays and raise later any way) - GH29926
|
4628 | 4627 | raise InvalidIndexError(key)
|
4629 | 4628 |
|
4630 |
| - # if we have something that is Index-like, then |
4631 |
| - # use this, e.g. DatetimeIndex |
4632 |
| - # Things like `Series._get_value` (via .at) pass the EA directly here. |
4633 |
| - s = extract_array(series, extract_numpy=True) |
4634 |
| - if isinstance(s, ExtensionArray): |
| 4629 | + try: |
4635 | 4630 | # GH 20882, 21257
|
4636 | 4631 | # First try to convert the key to a location
|
4637 | 4632 | # If that fails, raise a KeyError if an integer
|
4638 | 4633 | # index, otherwise, see if key is an integer, and
|
4639 | 4634 | # try that
|
4640 |
| - try: |
4641 |
| - iloc = self.get_loc(key) |
4642 |
| - return s[iloc] |
4643 |
| - except KeyError: |
4644 |
| - if len(self) > 0 and (self.holds_integer() or self.is_boolean()): |
4645 |
| - raise |
4646 |
| - elif is_integer(key): |
4647 |
| - return s[key] |
4648 |
| - |
4649 |
| - k = self._convert_scalar_indexer(key, kind="getitem") |
4650 |
| - try: |
4651 |
| - loc = self._engine.get_loc(k) |
4652 |
| - |
4653 |
| - except KeyError as e1: |
| 4635 | + loc = self._engine.get_loc(key) |
| 4636 | + except KeyError: |
4654 | 4637 | if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
|
4655 | 4638 | raise
|
4656 |
| - |
4657 |
| - try: |
4658 |
| - return libindex.get_value_at(s, key) |
4659 |
| - except IndexError: |
| 4639 | + elif is_integer(key): |
| 4640 | + # If the Index cannot hold integer, then this is unambiguously |
| 4641 | + # a locational lookup. |
| 4642 | + loc = key |
| 4643 | + else: |
4660 | 4644 | raise
|
4661 |
| - except Exception: |
4662 |
| - raise e1 |
4663 |
| - except TypeError: |
4664 |
| - # e.g. "[False] is an invalid key" |
4665 |
| - raise IndexError(key) |
4666 | 4645 |
|
4667 |
| - else: |
4668 |
| - if is_scalar(loc): |
4669 |
| - tz = getattr(series.dtype, "tz", None) |
4670 |
| - return libindex.get_value_at(s, loc, tz=tz) |
4671 |
| - return series.iloc[loc] |
| 4646 | + return self._get_values_for_loc(series, loc) |
| 4647 | + |
| 4648 | + def _get_values_for_loc(self, series, loc): |
| 4649 | + """ |
| 4650 | + Do a positional lookup on the given Series, returning either a scalar |
| 4651 | + or a Series. |
| 4652 | +
|
| 4653 | + Assumes that `series.index is self` |
| 4654 | + """ |
| 4655 | + if is_integer(loc): |
| 4656 | + if isinstance(series._values, np.ndarray): |
| 4657 | + # Since we have an ndarray and not DatetimeArray, we dont |
| 4658 | + # have to worry about a tz. |
| 4659 | + return libindex.get_value_at(series._values, loc, tz=None) |
| 4660 | + return series._values[loc] |
| 4661 | + |
| 4662 | + return series.iloc[loc] |
4672 | 4663 |
|
4673 | 4664 | def set_value(self, arr, key, value):
|
4674 | 4665 | """
|
|
0 commit comments