|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from pandas.compat import pa_version_under7p0 |
| 6 | + |
| 7 | +if not pa_version_under7p0: |
| 8 | + import pyarrow as pa |
| 9 | + import pyarrow.compute as pc |
| 10 | + |
| 11 | + from pandas.core.dtypes.dtypes import ArrowDtype |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from pandas import ( |
| 15 | + DataFrame, |
| 16 | + Series, |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +class StructAccessor: |
| 21 | + _validation_msg = "Can only use the '.struct' accessor with 'struct[pyarrow]' data." |
| 22 | + |
| 23 | + def __init__(self, data=None) -> None: |
| 24 | + self._parent = data |
| 25 | + self._validate(data) |
| 26 | + |
| 27 | + def _validate(self, data): |
| 28 | + dtype = data.dtype |
| 29 | + if not isinstance(dtype, ArrowDtype): |
| 30 | + raise AttributeError(self._validation_message) |
| 31 | + |
| 32 | + if not pa.types.is_struct(dtype.pyarrow_dtype): |
| 33 | + raise AttributeError(self._validation_message) |
| 34 | + |
| 35 | + @property |
| 36 | + def dtypes(self) -> Series: |
| 37 | + from pandas import ( |
| 38 | + Index, |
| 39 | + Series, |
| 40 | + ) |
| 41 | + |
| 42 | + pa_type = self._parent.dtype.pyarrow_dtype |
| 43 | + types = [ArrowDtype(pa_type[i].type) for i in range(pa_type.num_fields)] |
| 44 | + names = [pa_type[i].name for i in range(pa_type.num_fields)] |
| 45 | + return Series(types, index=Index(names)) |
| 46 | + |
| 47 | + def field(self, name_or_index: str | int) -> Series: |
| 48 | + from pandas import Series |
| 49 | + |
| 50 | + pa_arr = self._parent.array._pa_array |
| 51 | + if isinstance(name_or_index, int): |
| 52 | + index = name_or_index |
| 53 | + else: |
| 54 | + index = pa_arr.type.get_field_index(name_or_index) |
| 55 | + |
| 56 | + pa_field = pa_arr.type[index] |
| 57 | + field_arr = pc.struct_field(pa_arr, [index]) |
| 58 | + return Series(field_arr, dtype=ArrowDtype(field_arr.type), name=pa_field.name) |
| 59 | + |
| 60 | + def to_frame(self) -> DataFrame: |
| 61 | + from pandas import concat |
| 62 | + |
| 63 | + pa_type = self._parent.dtype.pyarrow_dtype |
| 64 | + return concat( |
| 65 | + [self.field(i) for i in range(pa_type.num_fields)], axis="columns" |
| 66 | + ) |
0 commit comments