|  | 
| 13 | 13 | 
 | 
| 14 | 14 | from nested_pandas.series.dtype import NestedDtype | 
| 15 | 15 | from nested_pandas.series.packer import pack_sorted_df_into_struct | 
|  | 16 | +from nested_pandas.series.utils import nested_types_mapper | 
| 16 | 17 | 
 | 
| 17 | 18 | __all__ = ["NestSeriesAccessor"] | 
| 18 | 19 | 
 | 
| @@ -70,25 +71,10 @@ def to_lists(self, fields: list[str] | None = None) -> pd.DataFrame: | 
| 70 | 71 |         if len(fields) == 0: | 
| 71 | 72 |             raise ValueError("Cannot convert a struct with no fields to lists") | 
| 72 | 73 | 
 | 
| 73 |  | -        list_chunks = defaultdict(list) | 
| 74 |  | -        for chunk in self._series.array._chunked_array.iterchunks(): | 
| 75 |  | -            struct_array = cast(pa.StructArray, chunk) | 
| 76 |  | -            for field in fields: | 
| 77 |  | -                list_array = cast(pa.ListArray, struct_array.field(field)) | 
| 78 |  | -                list_chunks[field].append(list_array) | 
|  | 74 | +        list_df = self._series.array.pa_table.select(fields).to_pandas(types_mapper=nested_types_mapper) | 
|  | 75 | +        list_df.index = self._series.index | 
| 79 | 76 | 
 | 
| 80 |  | -        list_series = {} | 
| 81 |  | -        for field, chunks in list_chunks.items(): | 
| 82 |  | -            chunked_array = pa.chunked_array(chunks) | 
| 83 |  | -            list_series[field] = pd.Series( | 
| 84 |  | -                chunked_array, | 
| 85 |  | -                dtype=pd.ArrowDtype(chunked_array.type), | 
| 86 |  | -                index=self._series.index, | 
| 87 |  | -                name=field, | 
| 88 |  | -                copy=False, | 
| 89 |  | -            ) | 
| 90 |  | - | 
| 91 |  | -        return pd.DataFrame(list_series) | 
|  | 77 | +        return list_df | 
| 92 | 78 | 
 | 
| 93 | 79 |     def to_flat(self, fields: list[str] | None = None) -> pd.DataFrame: | 
| 94 | 80 |         """Convert nested series into dataframe of flat arrays | 
| @@ -130,7 +116,7 @@ def to_flat(self, fields: list[str] | None = None) -> pd.DataFrame: | 
| 130 | 116 |         index = pd.Series(self.get_flat_index(), name=self._series.index.name) | 
| 131 | 117 | 
 | 
| 132 | 118 |         flat_chunks = defaultdict(list) | 
| 133 |  | -        for chunk in self._series.array._chunked_array.iterchunks(): | 
|  | 119 | +        for chunk in self._series.array.struct_array.iterchunks(): | 
| 134 | 120 |             struct_array = cast(pa.StructArray, chunk) | 
| 135 | 121 |             for field in fields: | 
| 136 | 122 |                 list_array = cast(pa.ListArray, struct_array.field(field)) | 
| @@ -439,7 +425,7 @@ def get_flat_series(self, field: str) -> pd.Series: | 
| 439 | 425 |         """ | 
| 440 | 426 | 
 | 
| 441 | 427 |         flat_chunks = [] | 
| 442 |  | -        for nested_chunk in self._series.array._chunked_array.iterchunks(): | 
|  | 428 | +        for nested_chunk in self._series.array.struct_array.iterchunks(): | 
| 443 | 429 |             struct_array = cast(pa.StructArray, nested_chunk) | 
| 444 | 430 |             list_array = cast(pa.ListArray, struct_array.field(field)) | 
| 445 | 431 |             flat_array = list_array.flatten() | 
| @@ -483,12 +469,7 @@ def get_list_series(self, field: str) -> pd.Series: | 
| 483 | 469 |         Name: flux, dtype: list<item: double>[pyarrow] | 
| 484 | 470 |         """ | 
| 485 | 471 | 
 | 
| 486 |  | -        list_chunks = [] | 
| 487 |  | -        for nested_chunk in self._series.array._chunked_array.iterchunks(): | 
| 488 |  | -            struct_array = cast(pa.StructArray, nested_chunk) | 
| 489 |  | -            list_array = struct_array.field(field) | 
| 490 |  | -            list_chunks.append(list_array) | 
| 491 |  | -        list_chunked_array = pa.chunked_array(list_chunks) | 
|  | 472 | +        list_chunked_array = self._series.array.pa_table[field] | 
| 492 | 473 |         return pd.Series( | 
| 493 | 474 |             list_chunked_array, | 
| 494 | 475 |             dtype=pd.ArrowDtype(list_chunked_array.type), | 
|  | 
0 commit comments