To avoid the overhead of creating many small Python dictionaries when listing large buckets, use return_arrow=True in the list method. This returns each chunk of results as an Arrow RecordBatch.
This approach allows for zero-copy conversion to other Arrow-backed libraries. This requires the arro3-core dependency.
Supported zero-copy conversions:
- pyarrow:
pyarrow.record_batch(record_batch) - polars:
polars.DataFrame(record_batch) - pandas:
pyarrow.record_batch(record_batch).to_pandas(types_mapper=pd.ArrowDtype) - arro3:
arro3.core.RecordBatch(record_batch)
import pandas as pd
import pyarrow as pa
from obstore.store import S3Store
store = S3Store("sentinel-cogs", region="us-west-2", skip_signature=True)
stream = store.list(chunk_size=20, return_arrow=True)
for record_batch in stream:
# Convert to pyarrow (zero-copy), then to pandas
df = pa.record_batch(record_batch).to_pandas()
print(df.iloc[:5].to_markdown(index=False))
break