| 787 | return self.storage[i] |
| 788 | |
| 789 | def to_numpy(self, zero_copy_only=True): |
| 790 | storage: pa.ListArray = self.storage |
| 791 | null_mask = storage.is_null().to_numpy(zero_copy_only=False) |
| 792 | |
| 793 | if self.type.shape[0] is not None: |
| 794 | size = 1 |
| 795 | null_indices = np.arange(len(storage))[null_mask] - np.arange(np.sum(null_mask)) |
| 796 | |
| 797 | for i in range(self.type.ndims): |
| 798 | size *= self.type.shape[i] |
| 799 | storage = storage.flatten() |
| 800 | numpy_arr = storage.to_numpy(zero_copy_only=zero_copy_only) |
| 801 | numpy_arr = numpy_arr.reshape(len(self) - len(null_indices), *self.type.shape) |
| 802 | |
| 803 | if len(null_indices): |
| 804 | numpy_arr = np.insert(numpy_arr.astype(np.float64), null_indices, np.nan, axis=0) |
| 805 | |
| 806 | else: |
| 807 | shape = self.type.shape |
| 808 | ndims = self.type.ndims |
| 809 | arrays = [] |
| 810 | first_dim_offsets = np.array([off.as_py() for off in storage.offsets]) |
| 811 | for i, is_null in enumerate(null_mask): |
| 812 | if is_null: |
| 813 | arrays.append(np.nan) |
| 814 | else: |
| 815 | storage_el = storage[i : i + 1] |
| 816 | first_dim = first_dim_offsets[i + 1] - first_dim_offsets[i] |
| 817 | # flatten storage |
| 818 | for _ in range(ndims): |
| 819 | storage_el = storage_el.flatten() |
| 820 | |
| 821 | numpy_arr = storage_el.to_numpy(zero_copy_only=zero_copy_only) |
| 822 | arrays.append(numpy_arr.reshape(first_dim, *shape[1:])) |
| 823 | |
| 824 | if len(np.unique(np.diff(first_dim_offsets))) > 1: |
| 825 | # ragged |
| 826 | numpy_arr = np.empty(len(arrays), dtype=object) |
| 827 | numpy_arr[:] = arrays |
| 828 | else: |
| 829 | numpy_arr = np.array(arrays) |
| 830 | |
| 831 | return numpy_arr |
| 832 | |
| 833 | def to_pylist(self, maps_as_pydicts: Optional[Literal["lossy", "strict"]] = None): |
| 834 | zero_copy_only = _is_zero_copy_only(self.storage.type, unnest=True) |