Maybe convert a given key to its equivalent i8 value(s). Used as a preprocessing step prior to IntervalTree queries (self._engine), which expects numeric data. Parameters ---------- key : scalar or list-like The key that should maybe be c
(self, key)
| 649 | return isinstance(key, i8_types) |
| 650 | |
| 651 | def _maybe_convert_i8(self, key): |
| 652 | """ |
| 653 | Maybe convert a given key to its equivalent i8 value(s). Used as a |
| 654 | preprocessing step prior to IntervalTree queries (self._engine), which |
| 655 | expects numeric data. |
| 656 | |
| 657 | Parameters |
| 658 | ---------- |
| 659 | key : scalar or list-like |
| 660 | The key that should maybe be converted to i8. |
| 661 | |
| 662 | Returns |
| 663 | ------- |
| 664 | scalar or list-like |
| 665 | The original key if no conversion occurred, int if converted scalar, |
| 666 | Index with an int64 dtype if converted list-like. |
| 667 | """ |
| 668 | if is_list_like(key): |
| 669 | key = ensure_index(key) |
| 670 | key = maybe_upcast_numeric_to_64bit(key) |
| 671 | |
| 672 | if not self._needs_i8_conversion(key): |
| 673 | return key |
| 674 | |
| 675 | scalar = is_scalar(key) |
| 676 | key_dtype = getattr(key, "dtype", None) |
| 677 | if isinstance(key_dtype, IntervalDtype) or isinstance(key, Interval): |
| 678 | # convert left/right and reconstruct |
| 679 | left = self._maybe_convert_i8(key.left) |
| 680 | right = self._maybe_convert_i8(key.right) |
| 681 | constructor = Interval if scalar else IntervalIndex.from_arrays |
| 682 | return constructor(left, right, closed=self.closed) |
| 683 | |
| 684 | if scalar: |
| 685 | # Timestamp/Timedelta |
| 686 | key_dtype, key_i8 = infer_dtype_from_scalar(key) |
| 687 | if isinstance(key, Period): |
| 688 | key_i8 = key.ordinal |
| 689 | elif isinstance(key_i8, Timestamp): |
| 690 | key_i8 = key_i8._value |
| 691 | elif isinstance(key_i8, (np.datetime64, np.timedelta64)): |
| 692 | key_i8 = key_i8.view("i8") |
| 693 | else: |
| 694 | # DatetimeIndex/TimedeltaIndex |
| 695 | key_dtype, key_i8 = key.dtype, Index(key.asi8, copy=False) |
| 696 | if key.hasnans: |
| 697 | # convert NaT from its i8 value to np.nan so it's not viewed |
| 698 | # as a valid value, maybe causing errors (e.g. is_overlapping) |
| 699 | key_i8 = key_i8.where(~key._isnan) |
| 700 | |
| 701 | # ensure consistency with IntervalIndex subtype |
| 702 | # error: Item "ExtensionDtype"/"dtype[Any]" of "Union[dtype[Any], |
| 703 | # ExtensionDtype]" has no attribute "subtype" |
| 704 | subtype = self.dtype.subtype # type: ignore[union-attr] |
| 705 | |
| 706 | if subtype != key_dtype: |
| 707 | raise ValueError( |
| 708 | f"Cannot index an IntervalIndex of subtype {subtype} with " |