polars_lance
1from collections.abc import Iterator 2from pathlib import Path 3from typing import Literal 4 5import polars as pl 6from polars.io.plugins import register_io_source 7 8from polars_lance import _polars_lance 9 10__all__ = ["scan_lance", "write_lance"] 11 12 13def scan_lance( 14 source: str | Path, 15 *, 16 storage_options: dict[str, str] | None = None, 17) -> pl.LazyFrame: 18 """ 19 Lazily read from a Lance dataset. 20 21 Parameters 22 ---------- 23 source 24 Path or URI to a Lance dataset. 25 storage_options 26 Cloud storage configuration to read remote datasets on AWS S3, 27 Azure Blob Storage, or Google Cloud Storage. Supported keys: 28 - [aws](https://docs.rs/object_store/latest/object_store/aws/enum.AmazonS3ConfigKey.html) 29 - [azure](https://docs.rs/object_store/latest/object_store/azure/enum.AzureConfigKey.html) 30 - [gcp](https://docs.rs/object_store/latest/object_store/gcp/enum.GoogleConfigKey.html) 31 32 Returns 33 ------- 34 LazyFrame 35 36 Examples 37 -------- 38 Scan a local Lance dataset. 39 40 >>> scan_lance("example.lance") 41 42 Scan a remote Lance dataset on AWS S3. 43 44 >>> source = "s3://bucket/example.lance" 45 >>> storage_options = { 46 ... "aws_access_key_id": "AKIAIOSFODNN7EXAMPLE", 47 ... "aws_secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", 48 ... "aws_region": "us-east-1", 49 ... } 50 >>> scan_lance(source, storage_options=storage_options) 51 """ 52 source_str = str(source) 53 54 def io_source( 55 with_columns: list[str] | None, 56 predicate: pl.Expr | None, 57 n_rows: int | None, 58 batch_size: int | None, 59 ) -> Iterator[pl.DataFrame]: 60 lance_scanner = _polars_lance.LanceScanner( 61 uri=source_str, 62 with_columns=with_columns, 63 predicate=predicate, 64 n_rows=n_rows, 65 batch_size=batch_size, 66 storage_options=storage_options, 67 ) 68 69 while (df := lance_scanner.next()) is not None: 70 yield df 71 72 return register_io_source( 73 io_source=io_source, 74 schema=_polars_lance.LanceScanner.schema_for_uri( 75 uri=source_str, 76 storage_options=storage_options, 77 ), 78 ) 79 80 81def write_lance( 82 df: pl.DataFrame, 83 target: str | Path, 84 *, 85 mode: Literal["error", "append", "overwrite"] = "error", 86 storage_options: dict[str, str] | None = None, 87 max_rows_per_file: int | None = None, 88 max_bytes_per_file: int | None = None, 89) -> None: 90 """ 91 Write dataframe to a Lance dataset. 92 93 Parameters 94 ---------- 95 df 96 Dataframe to write. 97 target 98 Path or URI to the Lance dataset. 99 mode : {'error', 'append', 'overwrite'} 100 How to behave if the target dataset already exists. 101 - `error`: raise an error 102 - `append`: append to the existing dataset 103 - `overwrite`: replace the existing dataset 104 storage_options 105 Cloud storage configuration to write remote datasets on AWS S3, 106 Azure Blob Storage, or Google Cloud Storage. Supported keys: 107 - [aws](https://docs.rs/object_store/latest/object_store/aws/enum.AmazonS3ConfigKey.html) 108 - [azure](https://docs.rs/object_store/latest/object_store/azure/enum.AzureConfigKey.html) 109 - [gcp](https://docs.rs/object_store/latest/object_store/gcp/enum.GoogleConfigKey.html) 110 max_rows_per_file 111 Maximum number of rows to write before starting a new data file. 112 max_bytes_per_file 113 Maximum number of bytes to write before starting a new data file. This is a soft 114 limit that is checked after a group is written, meaning that the actual file 115 size may exceed this limit. 116 117 Examples 118 -------- 119 Write a local Lance dataset. 120 121 >>> df = pl.DataFrame({"id": [1, 2], "val": ["a", "b"]}) 122 >>> write_lance(df, "example.lance") 123 124 Write a remote Lance dataset on AWS S3. 125 126 >>> df = pl.DataFrame({"id": [1, 2], "val": ["a", "b"]}) 127 >>> target = "s3://bucket/example.lance" 128 >>> storage_options = { 129 ... "aws_access_key_id": "AKIAIOSFODNN7EXAMPLE", 130 ... "aws_secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", 131 ... "aws_region": "us-east-1", 132 ... } 133 >>> write_lance(df, target, storage_options=storage_options) 134 """ 135 _polars_lance.write_lance( 136 df, 137 target=str(target), 138 mode=mode, 139 storage_options=storage_options, 140 max_rows_per_file=max_rows_per_file, 141 max_bytes_per_file=max_bytes_per_file, 142 )
def
scan_lance( source: str | pathlib.Path, *, storage_options: dict[str, str] | None = None) -> polars.lazyframe.frame.LazyFrame:
14def scan_lance( 15 source: str | Path, 16 *, 17 storage_options: dict[str, str] | None = None, 18) -> pl.LazyFrame: 19 """ 20 Lazily read from a Lance dataset. 21 22 Parameters 23 ---------- 24 source 25 Path or URI to a Lance dataset. 26 storage_options 27 Cloud storage configuration to read remote datasets on AWS S3, 28 Azure Blob Storage, or Google Cloud Storage. Supported keys: 29 - [aws](https://docs.rs/object_store/latest/object_store/aws/enum.AmazonS3ConfigKey.html) 30 - [azure](https://docs.rs/object_store/latest/object_store/azure/enum.AzureConfigKey.html) 31 - [gcp](https://docs.rs/object_store/latest/object_store/gcp/enum.GoogleConfigKey.html) 32 33 Returns 34 ------- 35 LazyFrame 36 37 Examples 38 -------- 39 Scan a local Lance dataset. 40 41 >>> scan_lance("example.lance") 42 43 Scan a remote Lance dataset on AWS S3. 44 45 >>> source = "s3://bucket/example.lance" 46 >>> storage_options = { 47 ... "aws_access_key_id": "AKIAIOSFODNN7EXAMPLE", 48 ... "aws_secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", 49 ... "aws_region": "us-east-1", 50 ... } 51 >>> scan_lance(source, storage_options=storage_options) 52 """ 53 source_str = str(source) 54 55 def io_source( 56 with_columns: list[str] | None, 57 predicate: pl.Expr | None, 58 n_rows: int | None, 59 batch_size: int | None, 60 ) -> Iterator[pl.DataFrame]: 61 lance_scanner = _polars_lance.LanceScanner( 62 uri=source_str, 63 with_columns=with_columns, 64 predicate=predicate, 65 n_rows=n_rows, 66 batch_size=batch_size, 67 storage_options=storage_options, 68 ) 69 70 while (df := lance_scanner.next()) is not None: 71 yield df 72 73 return register_io_source( 74 io_source=io_source, 75 schema=_polars_lance.LanceScanner.schema_for_uri( 76 uri=source_str, 77 storage_options=storage_options, 78 ), 79 )
Lazily read from a Lance dataset.
Parameters
- source: Path or URI to a Lance dataset.
- storage_options: Cloud storage configuration to read remote datasets on AWS S3, Azure Blob Storage, or Google Cloud Storage. Supported keys:
Returns
- LazyFrame
Examples
Scan a local Lance dataset.
>>> scan_lance("example.lance")
Scan a remote Lance dataset on AWS S3.
>>> source = "s3://bucket/example.lance"
>>> storage_options = {
... "aws_access_key_id": "AKIAIOSFODNN7EXAMPLE",
... "aws_secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
... "aws_region": "us-east-1",
... }
>>> scan_lance(source, storage_options=storage_options)
def
write_lance( df: polars.dataframe.frame.DataFrame, target: str | pathlib.Path, *, mode: Literal['error', 'append', 'overwrite'] = 'error', storage_options: dict[str, str] | None = None, max_rows_per_file: int | None = None, max_bytes_per_file: int | None = None) -> None:
82def write_lance( 83 df: pl.DataFrame, 84 target: str | Path, 85 *, 86 mode: Literal["error", "append", "overwrite"] = "error", 87 storage_options: dict[str, str] | None = None, 88 max_rows_per_file: int | None = None, 89 max_bytes_per_file: int | None = None, 90) -> None: 91 """ 92 Write dataframe to a Lance dataset. 93 94 Parameters 95 ---------- 96 df 97 Dataframe to write. 98 target 99 Path or URI to the Lance dataset. 100 mode : {'error', 'append', 'overwrite'} 101 How to behave if the target dataset already exists. 102 - `error`: raise an error 103 - `append`: append to the existing dataset 104 - `overwrite`: replace the existing dataset 105 storage_options 106 Cloud storage configuration to write remote datasets on AWS S3, 107 Azure Blob Storage, or Google Cloud Storage. Supported keys: 108 - [aws](https://docs.rs/object_store/latest/object_store/aws/enum.AmazonS3ConfigKey.html) 109 - [azure](https://docs.rs/object_store/latest/object_store/azure/enum.AzureConfigKey.html) 110 - [gcp](https://docs.rs/object_store/latest/object_store/gcp/enum.GoogleConfigKey.html) 111 max_rows_per_file 112 Maximum number of rows to write before starting a new data file. 113 max_bytes_per_file 114 Maximum number of bytes to write before starting a new data file. This is a soft 115 limit that is checked after a group is written, meaning that the actual file 116 size may exceed this limit. 117 118 Examples 119 -------- 120 Write a local Lance dataset. 121 122 >>> df = pl.DataFrame({"id": [1, 2], "val": ["a", "b"]}) 123 >>> write_lance(df, "example.lance") 124 125 Write a remote Lance dataset on AWS S3. 126 127 >>> df = pl.DataFrame({"id": [1, 2], "val": ["a", "b"]}) 128 >>> target = "s3://bucket/example.lance" 129 >>> storage_options = { 130 ... "aws_access_key_id": "AKIAIOSFODNN7EXAMPLE", 131 ... "aws_secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", 132 ... "aws_region": "us-east-1", 133 ... } 134 >>> write_lance(df, target, storage_options=storage_options) 135 """ 136 _polars_lance.write_lance( 137 df, 138 target=str(target), 139 mode=mode, 140 storage_options=storage_options, 141 max_rows_per_file=max_rows_per_file, 142 max_bytes_per_file=max_bytes_per_file, 143 )
Write dataframe to a Lance dataset.
Parameters
- df: Dataframe to write.
- target: Path or URI to the Lance dataset.
- mode ({'error', 'append', 'overwrite'}):
How to behave if the target dataset already exists.
error: raise an errorappend: append to the existing datasetoverwrite: replace the existing dataset
- storage_options: Cloud storage configuration to write remote datasets on AWS S3, Azure Blob Storage, or Google Cloud Storage. Supported keys:
- max_rows_per_file: Maximum number of rows to write before starting a new data file.
- max_bytes_per_file: Maximum number of bytes to write before starting a new data file. This is a soft limit that is checked after a group is written, meaning that the actual file size may exceed this limit.
Examples
Write a local Lance dataset.
>>> df = pl.DataFrame({"id": [1, 2], "val": ["a", "b"]})
>>> write_lance(df, "example.lance")
Write a remote Lance dataset on AWS S3.
>>> df = pl.DataFrame({"id": [1, 2], "val": ["a", "b"]})
>>> target = "s3://bucket/example.lance"
>>> storage_options = {
... "aws_access_key_id": "AKIAIOSFODNN7EXAMPLE",
... "aws_secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
... "aws_region": "us-east-1",
... }
>>> write_lance(df, target, storage_options=storage_options)