44import numpy as np
55import pandas as pd
66import pyarrow as pa
7+ import pyarrow .dataset
78import xarray as xr
89from datafusion .context import ArrowStreamExportable
910
@@ -171,19 +172,32 @@ def _parse_schema(ds) -> pa.Schema:
171172 return pa .schema (columns )
172173
173174
174- def read_xarray (ds : xr .Dataset , chunks : Chunks = None ) -> pa .RecordBatchReader :
175- """Pivots an Xarray Dataset into a PyArrow Table, partitioned by chunks.
175+ # def read_xarray(ds: xr.Dataset, chunks: Chunks = None) -> pa.RecordBatchReader:
176+ # """Pivots an Xarray Dataset into a PyArrow Table, partitioned by chunks.
176177
177- Args:
178- ds: An Xarray Dataset. All `data_vars` mush share the same dimensions.
179- chunks: Xarray-like chunks. If not provided, will default to the Dataset's
180- chunks. The product of the chunk sizes becomes the standard length of each
181- dataframe partition.
178+ # Args:
179+ # ds: An Xarray Dataset. All `data_vars` mush share the same dimensions.
180+ # chunks: Xarray-like chunks. If not provided, will default to the Dataset's
181+ # chunks. The product of the chunk sizes becomes the standard length of each
182+ # dataframe partition.
182183
183- Returns:
184- A PyArrow Table, which is a table representation of the input Dataset.
185- """
184+ # Returns:
185+ # A PyArrow Table, which is a table representation of the input Dataset.
186+ # """
187+
188+ # def pivot_block(b: Block):
189+ # return pivot(ds.isel(b))
190+
191+ # fst = next(iter(ds.values())).dims
192+ # assert all(
193+ # da.dims == fst for da in ds.values()
194+ # ), "All dimensions must be equal. Please filter data_vars in the Dataset."
186195
196+ # schema = _parse_schema(ds)
197+ # blocks = block_slices(ds, chunks)
198+ # return from_map_batched(pivot_block, blocks, schema=schema)
199+
200+ def read_xarray (ds : xr .Dataset , chunks : Chunks = None ) -> pa .dataset .Dataset :
187201 def pivot_block (b : Block ):
188202 return pivot (ds .isel (b ))
189203
@@ -194,4 +208,16 @@ def pivot_block(b: Block):
194208
195209 schema = _parse_schema (ds )
196210 blocks = block_slices (ds , chunks )
197- return from_map_batched (pivot_block , blocks , schema = schema )
211+
212+
213+
214+ children = []
215+ for block in blocks :
216+ def map_batches ():
217+ df = pivot_block (block )
218+ yield pa .RecordBatch .from_pandas (df , schema = schema )
219+
220+ children .append (pa .dataset .InMemoryDataset (pa .RecordBatchReader .from_batches (schema , map_batches ()),schema ))
221+
222+ out = pa .dataset .UnionDataset (schema ,children )
223+ return out
0 commit comments