|
1 | | -import datetime |
2 | 1 | from sys import getsizeof |
3 | 2 | from typing import Any, Hashable, Iterable, List, Optional, Sequence, Tuple, Union |
4 | 3 | import warnings |
|
7 | 6 |
|
8 | 7 | from pandas._config import get_option |
9 | 8 |
|
10 | | -from pandas._libs import Timestamp, algos as libalgos, index as libindex, lib, tslibs |
| 9 | +from pandas._libs import algos as libalgos, index as libindex, lib |
11 | 10 | from pandas._libs.hashtable import duplicated_int64 |
12 | 11 | from pandas._typing import AnyArrayLike, ArrayLike, Scalar |
13 | 12 | from pandas.compat.numpy import function as nv |
@@ -2321,65 +2320,33 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None): |
2321 | 2320 |
|
2322 | 2321 | def get_value(self, series, key): |
2323 | 2322 | # Label-based |
2324 | | - s = com.values_from_object(series) |
2325 | | - k = com.values_from_object(key) |
| 2323 | + if not is_hashable(key) or is_iterator(key): |
| 2324 | + # We allow tuples if they are hashable, whereas other Index |
| 2325 | + # subclasses require scalar. |
| 2326 | + # We have to explicitly exclude generators, as these are hashable. |
| 2327 | + raise InvalidIndexError(key) |
2326 | 2328 |
|
2327 | 2329 | def _try_mi(k): |
2328 | 2330 | # TODO: what if a level contains tuples?? |
2329 | 2331 | loc = self.get_loc(k) |
| 2332 | + |
2330 | 2333 | new_values = series._values[loc] |
| 2334 | + if is_scalar(loc): |
| 2335 | + return new_values |
| 2336 | + |
2331 | 2337 | new_index = self[loc] |
2332 | 2338 | new_index = maybe_droplevels(new_index, k) |
2333 | 2339 | return series._constructor( |
2334 | 2340 | new_values, index=new_index, name=series.name |
2335 | 2341 | ).__finalize__(self) |
2336 | 2342 |
|
2337 | 2343 | try: |
2338 | | - return self._engine.get_value(s, k) |
2339 | | - except KeyError as e1: |
2340 | | - try: |
2341 | | - return _try_mi(key) |
2342 | | - except KeyError: |
2343 | | - pass |
2344 | | - |
2345 | | - try: |
2346 | | - return libindex.get_value_at(s, k) |
2347 | | - except IndexError: |
| 2344 | + return _try_mi(key) |
| 2345 | + except KeyError: |
| 2346 | + if is_integer(key): |
| 2347 | + return series._values[key] |
| 2348 | + else: |
2348 | 2349 | raise |
2349 | | - except TypeError: |
2350 | | - # generator/iterator-like |
2351 | | - if is_iterator(key): |
2352 | | - raise InvalidIndexError(key) |
2353 | | - else: |
2354 | | - raise e1 |
2355 | | - except Exception: # pragma: no cover |
2356 | | - raise e1 |
2357 | | - except TypeError: |
2358 | | - |
2359 | | - # a Timestamp will raise a TypeError in a multi-index |
2360 | | - # rather than a KeyError, try it here |
2361 | | - # note that a string that 'looks' like a Timestamp will raise |
2362 | | - # a KeyError! (GH5725) |
2363 | | - if isinstance(key, (datetime.datetime, np.datetime64, str)): |
2364 | | - try: |
2365 | | - return _try_mi(key) |
2366 | | - except KeyError: |
2367 | | - raise |
2368 | | - except (IndexError, ValueError, TypeError): |
2369 | | - pass |
2370 | | - |
2371 | | - try: |
2372 | | - return _try_mi(Timestamp(key)) |
2373 | | - except ( |
2374 | | - KeyError, |
2375 | | - TypeError, |
2376 | | - IndexError, |
2377 | | - ValueError, |
2378 | | - tslibs.OutOfBoundsDatetime, |
2379 | | - ): |
2380 | | - pass |
2381 | | - |
2382 | | - raise InvalidIndexError(key) |
2383 | 2350 |
|
2384 | 2351 | def _convert_listlike_indexer(self, keyarr, kind=None): |
2385 | 2352 | """ |
|
0 commit comments