|
| 1 | +"""Accessors for arrow-backed data.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from pandas.compat import pa_version_under7p0 |
| 8 | + |
| 9 | +if not pa_version_under7p0: |
| 10 | + import pyarrow as pa |
| 11 | + import pyarrow.compute as pc |
| 12 | + |
| 13 | + from pandas.core.dtypes.dtypes import ArrowDtype |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from pandas import ( |
| 17 | + DataFrame, |
| 18 | + Series, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +class StructAccessor: |
| 23 | + """ |
| 24 | + Accessor object for categorical properties of the Series values. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + data : Series or CategoricalIndex |
| 29 | + """ |
| 30 | + |
| 31 | + _validation_msg = "Can only use the '.struct' accessor with 'struct[pyarrow]' data." |
| 32 | + |
| 33 | + def __init__(self, data=None) -> None: |
| 34 | + self._parent = data |
| 35 | + self._validate(data) |
| 36 | + |
| 37 | + def _validate(self, data): |
| 38 | + dtype = data.dtype |
| 39 | + if not isinstance(dtype, ArrowDtype): |
| 40 | + raise AttributeError(self._validation_msg) |
| 41 | + |
| 42 | + if not pa.types.is_struct(dtype.pyarrow_dtype): |
| 43 | + raise AttributeError(self._validation_msg) |
| 44 | + |
| 45 | + @property |
| 46 | + def dtypes(self) -> Series: |
| 47 | + """ |
| 48 | + Return the dtype object of each child field of the struct. |
| 49 | +
|
| 50 | + Examples |
| 51 | + -------- |
| 52 | + >>> import pyarrow as pa |
| 53 | + >>> s = pd.Series( |
| 54 | + ... [{"i": 1, "j": "a"}, {"i": 2, "j": "b"}, {"i": 3, "j": "c"}], |
| 55 | + ... dtype=pd.ArrowDtype(pa.struct([("i", pa.int64()), ("j", pa.string())])) |
| 56 | + ... ) |
| 57 | + >>> s.struct.dtypes |
| 58 | + i int64[pyarrow] |
| 59 | + j string[pyarrow] |
| 60 | + dtype: object |
| 61 | + """ |
| 62 | + from pandas import ( |
| 63 | + Index, |
| 64 | + Series, |
| 65 | + ) |
| 66 | + |
| 67 | + pa_type = self._parent.dtype.pyarrow_dtype |
| 68 | + types = [ArrowDtype(pa_type[i].type) for i in range(pa_type.num_fields)] |
| 69 | + names = [pa_type[i].name for i in range(pa_type.num_fields)] |
| 70 | + return Series(types, index=Index(names)) |
| 71 | + |
| 72 | + def field(self, name_or_index: str | int) -> Series: |
| 73 | + """ |
| 74 | + Extract a child field of a struct as a Series. |
| 75 | +
|
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + name_or_index : str | int |
| 79 | + Name or index of the child field to extract. |
| 80 | +
|
| 81 | + Examples |
| 82 | + -------- |
| 83 | + >>> import pyarrow as pa |
| 84 | + >>> s = pd.Series( |
| 85 | + ... [{"i": 1, "j": "a"}, {"i": 2, "j": "b"}, {"i": 3, "j": "c"}], |
| 86 | + ... dtype=pd.ArrowDtype(pa.struct([("i", pa.int64()), ("j", pa.string())])) |
| 87 | + ... ) |
| 88 | +
|
| 89 | + Extract by field name. |
| 90 | +
|
| 91 | + >>> s.struct.field("j") |
| 92 | + 0 a |
| 93 | + 1 b |
| 94 | + 2 c |
| 95 | + Name: j, dtype: string[pyarrow] |
| 96 | +
|
| 97 | + Extract by field index. |
| 98 | +
|
| 99 | + >>> s.struct.field(0) |
| 100 | + 0 1 |
| 101 | + 1 2 |
| 102 | + 2 3 |
| 103 | + Name: i, dtype: int64[pyarrow] |
| 104 | + """ |
| 105 | + from pandas import Series |
| 106 | + |
| 107 | + pa_arr = self._parent.array._pa_array |
| 108 | + if isinstance(name_or_index, int): |
| 109 | + index = name_or_index |
| 110 | + else: |
| 111 | + index = pa_arr.type.get_field_index(name_or_index) |
| 112 | + |
| 113 | + pa_field = pa_arr.type[index] |
| 114 | + field_arr = pc.struct_field(pa_arr, [index]) |
| 115 | + return Series(field_arr, dtype=ArrowDtype(field_arr.type), name=pa_field.name) |
| 116 | + |
| 117 | + def to_frame(self) -> DataFrame: |
| 118 | + """ |
| 119 | + Extract all child fields of a struct as a DataFrame. |
| 120 | +
|
| 121 | + Examples |
| 122 | + -------- |
| 123 | + >>> import pyarrow as pa |
| 124 | + >>> s = pd.Series( |
| 125 | + ... [{"i": 1, "j": "a"}, {"i": 2, "j": "b"}, {"i": 3, "j": "c"}], |
| 126 | + ... dtype=pd.ArrowDtype(pa.struct([("i", pa.int64()), ("j", pa.string())])) |
| 127 | + ... ) |
| 128 | +
|
| 129 | + >>> s.struct.to_frame() |
| 130 | + i j |
| 131 | + 0 1 a |
| 132 | + 1 2 b |
| 133 | + 2 3 c |
| 134 | + """ |
| 135 | + from pandas import concat |
| 136 | + |
| 137 | + pa_type = self._parent.dtype.pyarrow_dtype |
| 138 | + return concat( |
| 139 | + [self.field(i) for i in range(pa_type.num_fields)], axis="columns" |
| 140 | + ) |
0 commit comments