Return an iterator yielding the chunks. By default (None), yields the chunks that the data is stored as by the producer. If given, ``n_chunks`` must be a multiple of ``self.num_chunks()``, meaning the producer must subdivide each chunk before yielding it.
(
self, n_chunks: Optional[int] = None
)
| 174 | ) |
| 175 | |
| 176 | def get_chunks( |
| 177 | self, n_chunks: Optional[int] = None |
| 178 | ) -> Iterable[_PyArrowDataFrame]: |
| 179 | """ |
| 180 | Return an iterator yielding the chunks. |
| 181 | |
| 182 | By default (None), yields the chunks that the data is stored as by the |
| 183 | producer. If given, ``n_chunks`` must be a multiple of |
| 184 | ``self.num_chunks()``, meaning the producer must subdivide each chunk |
| 185 | before yielding it. |
| 186 | |
| 187 | Note that the producer must ensure that all columns are chunked the |
| 188 | same way. |
| 189 | """ |
| 190 | # Subdivide chunks |
| 191 | if n_chunks and n_chunks > 1: |
| 192 | chunk_size = self.num_rows() // n_chunks |
| 193 | if self.num_rows() % n_chunks != 0: |
| 194 | chunk_size += 1 |
| 195 | if isinstance(self._df, pa.Table): |
| 196 | batches = self._df.to_batches(max_chunksize=chunk_size) |
| 197 | else: |
| 198 | batches = [] |
| 199 | for start in range(0, chunk_size * n_chunks, chunk_size): |
| 200 | batches.append(self._df.slice(start, chunk_size)) |
| 201 | # In case when the size of the chunk is such that the resulting |
| 202 | # list is one less chunk then n_chunks -> append an empty chunk |
| 203 | if len(batches) == n_chunks - 1: |
| 204 | batches.append(pa.record_batch([[]], schema=self._df.schema)) |
| 205 | # yields the chunks that the data is stored as |
| 206 | else: |
| 207 | if isinstance(self._df, pa.Table): |
| 208 | batches = self._df.to_batches() |
| 209 | else: |
| 210 | batches = [self._df] |
| 211 | |
| 212 | # Create an iterator of RecordBatches |
| 213 | iterator = [_PyArrowDataFrame(batch, |
| 214 | self._nan_as_null, |
| 215 | self._allow_copy) |
| 216 | for batch in batches] |
| 217 | return iterator |
nothing calls this directly
no test coverage detected