Select a subset of self. Parameters ---------- item : int, slice, or ndarray * int: The position in 'self' to get. * slice: A slice object, where 'start', 'stop', and 'step' are integers or None * ndarray: A 1-d boolean NumPy
(self, item: PositionalIndexer)
| 719 | return pa_array |
| 720 | |
| 721 | def __getitem__(self, item: PositionalIndexer): |
| 722 | """Select a subset of self. |
| 723 | |
| 724 | Parameters |
| 725 | ---------- |
| 726 | item : int, slice, or ndarray |
| 727 | * int: The position in 'self' to get. |
| 728 | * slice: A slice object, where 'start', 'stop', and 'step' are |
| 729 | integers or None |
| 730 | * ndarray: A 1-d boolean NumPy ndarray the same length as 'self' |
| 731 | |
| 732 | Returns |
| 733 | ------- |
| 734 | item : scalar or ExtensionArray |
| 735 | |
| 736 | Notes |
| 737 | ----- |
| 738 | For scalar ``item``, return a scalar value suitable for the array's |
| 739 | type. This should be an instance of ``self.dtype.type``. |
| 740 | For slice ``key``, return an instance of ``ExtensionArray``, even |
| 741 | if the slice is length 0 or 1. |
| 742 | For a boolean mask, return an instance of ``ExtensionArray``, filtered |
| 743 | to the values where ``item`` is True. |
| 744 | """ |
| 745 | item = check_array_indexer(self, item) |
| 746 | |
| 747 | if isinstance(item, np.ndarray): |
| 748 | if not len(item): |
| 749 | # Removable once we migrate StringDtype[pyarrow] to ArrowDtype[string] |
| 750 | if ( |
| 751 | isinstance(self._dtype, StringDtype) |
| 752 | and self._dtype.storage == "pyarrow" |
| 753 | ): |
| 754 | # TODO(infer_string) should this be large_string? |
| 755 | pa_dtype = pa.string() |
| 756 | else: |
| 757 | pa_dtype = self._dtype.pyarrow_dtype |
| 758 | result = pa.chunked_array([], type=pa_dtype) |
| 759 | return self._from_pyarrow_array(result) |
| 760 | |
| 761 | elif item.dtype.kind in "iu": |
| 762 | return self.take(item) |
| 763 | elif item.dtype.kind == "b": |
| 764 | return self._from_pyarrow_array(self._pa_array.filter(item)) |
| 765 | else: |
| 766 | raise IndexError( |
| 767 | "Only integers, slices and integer or " |
| 768 | "boolean arrays are valid indices." |
| 769 | ) |
| 770 | elif isinstance(item, tuple): |
| 771 | item = unpack_tuple_and_ellipses(item) |
| 772 | |
| 773 | if item is Ellipsis: |
| 774 | # TODO: should be handled by pyarrow? |
| 775 | item = slice(None) |
| 776 | |
| 777 | if is_scalar(item) and not is_integer(item): |
| 778 | # e.g. "foo" or 2.5 |
nothing calls this directly
no test coverage detected