|
| 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 structured data properties of the Series values. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + data : Series |
| 29 | + Series containing Arrow struct data. |
| 30 | + """ |
| 31 | + |
| 32 | + _validation_msg = "Can only use the '.struct' accessor with 'struct[pyarrow]' data." |
| 33 | + |
| 34 | + def __init__(self, data=None) -> None: |
| 35 | + self._parent = data |
| 36 | + self._validate(data) |
| 37 | + |
| 38 | + def _validate(self, data): |
| 39 | + dtype = data.dtype |
| 40 | + if not isinstance(dtype, ArrowDtype): |
| 41 | + raise AttributeError(self._validation_msg) |
| 42 | + |
| 43 | + if not pa.types.is_struct(dtype.pyarrow_dtype): |
| 44 | + raise AttributeError(self._validation_msg) |
| 45 | + |
| 46 | + @property |
| 47 | + def dtypes(self) -> Series: |
| 48 | + """ |
| 49 | + Return the dtype object of each child field of the struct. |
| 50 | +
|
| 51 | + Returns |
| 52 | + ------- |
| 53 | + pandas.Series |
| 54 | + The data type of each child field. |
| 55 | +
|
| 56 | + Examples |
| 57 | + -------- |
| 58 | + >>> import pyarrow as pa |
| 59 | + >>> s = pd.Series( |
| 60 | + ... [ |
| 61 | + ... {"version": 1, "project": "pandas"}, |
| 62 | + ... {"version": 2, "project": "pandas"}, |
| 63 | + ... {"version": 1, "project": "numpy"}, |
| 64 | + ... ], |
| 65 | + ... dtype=pd.ArrowDtype(pa.struct( |
| 66 | + ... [("version", pa.int64()), ("project", pa.string())] |
| 67 | + ... )) |
| 68 | + ... ) |
| 69 | + >>> s.struct.dtypes |
| 70 | + version int64[pyarrow] |
| 71 | + project string[pyarrow] |
| 72 | + dtype: object |
| 73 | + """ |
| 74 | + from pandas import ( |
| 75 | + Index, |
| 76 | + Series, |
| 77 | + ) |
| 78 | + |
| 79 | + pa_type = self._parent.dtype.pyarrow_dtype |
| 80 | + types = [ArrowDtype(pa_type[i].type) for i in range(pa_type.num_fields)] |
| 81 | + names = [pa_type[i].name for i in range(pa_type.num_fields)] |
| 82 | + return Series(types, index=Index(names)) |
| 83 | + |
| 84 | + def field(self, name_or_index: str | int) -> Series: |
| 85 | + """ |
| 86 | + Extract a child field of a struct as a Series. |
| 87 | +
|
| 88 | + Parameters |
| 89 | + ---------- |
| 90 | + name_or_index : str | int |
| 91 | + Name or index of the child field to extract. |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + pandas.Series |
| 96 | + The data corresponding to the selected child field. |
| 97 | +
|
| 98 | + See Also |
| 99 | + -------- |
| 100 | + Series.struct.to_frame : Return all child fields as a DataFrame. |
| 101 | +
|
| 102 | + Examples |
| 103 | + -------- |
| 104 | + >>> import pyarrow as pa |
| 105 | + >>> s = pd.Series( |
| 106 | + ... [ |
| 107 | + ... {"version": 1, "project": "pandas"}, |
| 108 | + ... {"version": 2, "project": "pandas"}, |
| 109 | + ... {"version": 1, "project": "numpy"}, |
| 110 | + ... ], |
| 111 | + ... dtype=pd.ArrowDtype(pa.struct( |
| 112 | + ... [("version", pa.int64()), ("project", pa.string())] |
| 113 | + ... )) |
| 114 | + ... ) |
| 115 | +
|
| 116 | + Extract by field name. |
| 117 | +
|
| 118 | + >>> s.struct.field("project") |
| 119 | + 0 pandas |
| 120 | + 1 pandas |
| 121 | + 2 numpy |
| 122 | + Name: project, dtype: string[pyarrow] |
| 123 | +
|
| 124 | + Extract by field index. |
| 125 | +
|
| 126 | + >>> s.struct.field(0) |
| 127 | + 0 1 |
| 128 | + 1 2 |
| 129 | + 2 1 |
| 130 | + Name: version, dtype: int64[pyarrow] |
| 131 | + """ |
| 132 | + from pandas import Series |
| 133 | + |
| 134 | + pa_arr = self._parent.array._pa_array |
| 135 | + if isinstance(name_or_index, int): |
| 136 | + index = name_or_index |
| 137 | + else: |
| 138 | + index = pa_arr.type.get_field_index(name_or_index) |
| 139 | + |
| 140 | + pa_field = pa_arr.type[index] |
| 141 | + field_arr = pc.struct_field(pa_arr, [index]) |
| 142 | + return Series(field_arr, dtype=ArrowDtype(field_arr.type), name=pa_field.name) |
| 143 | + |
| 144 | + def to_frame(self) -> DataFrame: |
| 145 | + """ |
| 146 | + Extract all child fields of a struct as a DataFrame. |
| 147 | +
|
| 148 | + Returns |
| 149 | + ------- |
| 150 | + pandas.DataFrame |
| 151 | + The data corresponding to all child fields. |
| 152 | +
|
| 153 | + See Also |
| 154 | + -------- |
| 155 | + Series.struct.field : Return a single child field as a Series. |
| 156 | +
|
| 157 | + Examples |
| 158 | + -------- |
| 159 | + >>> import pyarrow as pa |
| 160 | + >>> s = pd.Series( |
| 161 | + ... [ |
| 162 | + ... {"version": 1, "project": "pandas"}, |
| 163 | + ... {"version": 2, "project": "pandas"}, |
| 164 | + ... {"version": 1, "project": "numpy"}, |
| 165 | + ... ], |
| 166 | + ... dtype=pd.ArrowDtype(pa.struct( |
| 167 | + ... [("version", pa.int64()), ("project", pa.string())] |
| 168 | + ... )) |
| 169 | + ... ) |
| 170 | +
|
| 171 | + >>> s.struct.to_frame() |
| 172 | + version project |
| 173 | + 0 1 pandas |
| 174 | + 1 2 pandas |
| 175 | + 2 1 numpy |
| 176 | + """ |
| 177 | + from pandas import concat |
| 178 | + |
| 179 | + pa_type = self._parent.dtype.pyarrow_dtype |
| 180 | + return concat( |
| 181 | + [self.field(i) for i in range(pa_type.num_fields)], axis="columns" |
| 182 | + ) |
0 commit comments