import { type Readable } from "node:stream"; import { type DataFrame } from "./dataframe"; import type { DataType } from "./datatypes"; import { type LazyDataFrame } from "./lazy/dataframe"; import type { ReadParquetOptions, ScanParquetOptions } from "./types"; export interface ReadCsvOptions { inferSchemaLength: number | null; batchSize: number; nRows: number; hasHeader: boolean; ignoreErrors: boolean; endRows: number; startRows: number; projection: number; sep: string; schema: Record; columns: string[]; rechunk: boolean; encoding: "utf8" | "utf8-lossy"; numThreads: number; dtypes: Record; lowMemory: boolean; commentPrefix: string; quoteChar: string; eolChar: string; nullValues: string | Array | Record; chunkSize: number; skipRows: number; skipLines: number; decimalComma: boolean; tryParseDates: boolean; skipRowsAfterHeader: number; rowIndexName: string; rowIndexOffset: number; raiseIfEmpty: boolean; truncateRaggedLines: boolean; missingIsNull: boolean; } export interface ReadJsonOptions { batchSize: number; inferSchemaLength: number | null; format: "lines" | "json"; } export declare function readRecords(records: Record[], options?: { schema: Record; }): DataFrame; export declare function readRecords(records: Record[], options?: { inferSchemaLength?: number; }): DataFrame; /** * __Read a CSV file or string into a Dataframe.__ * ___ * @param pathOrBody - path or buffer or string * - path: Path to a file or a file like string. Any valid filepath can be used. Example: `file.csv`. * Gzip (`.csv.gz`) and zstd (`.csv.zst`) compressed files are decompressed transparently. * - body: String or buffer to be read as a CSV. A buffer holding gzip or zstd compressed * data is also decompressed transparently. * @param options * @param options.inferSchemaLength -Maximum number of lines to read to infer schema. If set to 0, all columns will be read as pl.Utf8. * If set to `null`, a full table scan will be done (slow). * @param options.nRows -After n rows are read from the CSV, it stops reading. * During multi-threaded parsing, an upper bound of `n` rows * cannot be guaranteed. * @param options.batchSize - Number of lines to read into the buffer at once. Modify this to change performance. * @param options.hasHeader - Indicate if first row of dataset is header or not. If set to False first row will be set to `column_x`, * `x` being an enumeration over every column in the dataset. * @param options.ignoreErrors -Try to keep reading lines if some lines yield errors. * @param options.endRows -After n rows are read from the CSV, it stops reading. * During multi-threaded parsing, an upper bound of `n` rows * cannot be guaranteed. * @param options.startRows -Start reading after `startRows` position. * @param options.skipRows -Start reading after `skipRows` rows. The header is parsed at this offset. * CSV escaping and comments are respected while skipping. To skip by newline only, use `skipLines`. * @param options.skipLines -Start reading after `skipLines` lines. The header is parsed at this offset. * CSV escaping is *not* respected while skipping. To skip valid CSV records, use `skipRows`. * @param options.decimalComma -Parse floats using a comma as the decimal separator instead of a period. * @param options.projection -Indices of columns to select. Note that column indices start at zero. * @param options.sep -Character to use as delimiter in the file. * @param options.columns -Columns to select. * @param options.rechunk -Make sure that all columns are contiguous in memory by aggregating the chunks into a single array. * @param options.encoding -Allowed encodings: `utf8`, `utf8-lossy`. Lossy means that invalid utf8 values are replaced with `�` character. * @param options.numThreads -Number of threads to use in csv parsing. Defaults to the number of physical cpu's of your system. * @param options.dtype -Overwrite the dtypes during inference. * @param options.schema -Set the CSV file's schema. This only accepts datatypes that are implemented in the csv parser and expects a complete Schema. * @param options.lowMemory - Reduce memory usage in expense of performance. * @param options.commentPrefix - character that indicates the start of a comment line, for instance '#'. * @param options.quoteChar - Character that is used for csv quoting, Default: '"'. Set to null to turn special handling and escaping of quotes off. * @param options.nullValues - Values to interpret as null values. You can provide a * - `string` -> all values encountered equal to this string will be null * - `Array` -> A null value per column. * - `Record` -> An object or map that maps column name to a null value string.Ex. {"column_1": 0} * @param options.parseDates -Whether to attempt to parse dates or not * @returns DataFrame */ export declare function readCSV(pathOrBody: string | Buffer, options?: Partial): DataFrame; export interface ScanCsvOptions { hasHeader: boolean; sep: string; commentPrefix: string; quoteChar: string; skipRows: number; skipLines: number; nullValues: string | Array | Record; ignoreErrors: boolean; cache: boolean; inferSchemaLength: number | null; rechunk: boolean; nRows: number; nThreads: number; encoding: string; lowMemory: boolean; parseDates: boolean; skipRowsAfterHeader: number; rowIndexName: string; rowIndexOffset: number; eolChar: string; missingUtf8IsEmptyString: boolean; raiseIfEmpty: boolean; truncateRaggedLines: boolean; decimalComma: boolean; glob: boolean; schema: Record; cloudOptions: Record; includeFilePaths: string; missingColumns: "insert" | "raise"; } /** * __Lazily read from a CSV file or multiple files via glob patterns.__ * * This allows the query optimizer to push down predicates and * projections to the scan level, thereby potentially reducing * memory overhead. * ___ * @param path path to a file * @param options.hasHeader - Indicate if first row of dataset is header or not. If set to False first row will be set to `column_x`, * `x` being an enumeration over every column in the dataset. * @param options.sep -Character to use as delimiter in the file. * @param options.commentPrefix - character that indicates the start of a comment line, for instance '#'. * @param options.quoteChar -character that is used for csv quoting. Default: '"'. Set to null to turn special handling and escaping of quotes off. * @param options.skipRows -Start reading after `skipRows` position. * @param options.skipLines -Start reading after `skipLines` lines. The header will be parsed at this offset. * Note that CSV records may span multiple lines, so this skips lines rather than records. * @param options.nullValues - Values to interpret as null values. You can provide a * - `string` -> all values encountered equal to this string will be null * - `Array` -> A null value per column. * - `Record` -> An object or map that maps column name to a null value string.Ex. {"column_1": 0} * @param options.ignoreErrors -Try to keep reading lines if some lines yield errors. * @param options.cache Cache the result after reading. * @param options.inferSchemaLength -Maximum number of lines to read to infer schema. If set to 0, all columns will be read as pl.Utf8. * If set to `null`, a full table scan will be done (slow). * @param options.nRows -After n rows are read from the CSV, it stops reading. * During multi-threaded parsing, an upper bound of `n` rows * cannot be guaranteed. * @param options.nThreads -Number of threads to use in csv parsing. Defaults to the number of physical cpu's of your system. * @param options.rechunk -Make sure that all columns are contiguous in memory by aggregating the chunks into a single array. * @param options.lowMemory - Reduce memory usage in expense of performance. * @param options.rowIndexName - Insert a row index column with this name. * @param options.rowIndexOffset - Start the row index at this offset. Only used when `rowIndexName` is set. * @param options.decimalComma - Parse floats using a comma as the decimal separator instead of a period. * @param options.glob - Expand path given via globbing rules. * @param options.schema - Set the CSV file's schema. This only accepts datatypes that are implemented in the csv parser and expects a complete Schema. * @param options.cloudOptions - Options that indicate how to connect to a cloud provider. * Also accepts the retry keys `max_retries`, `retry_timeout_ms`, `retry_init_backoff_ms`, * `retry_max_backoff_ms`, `retry_base_multiplier`, and `file_cache_ttl`. * @param options.includeFilePaths - Include the path of the source file(s) as a column with this name. * @param options.missingColumns - How to handle columns of the schema that are missing from a file: * `"insert"` fills them with nulls, `"raise"` (default) raises an error. * ___ * */ export declare function scanCSV(path: string, options?: Partial): LazyDataFrame; /** * __Read a JSON file or string into a DataFrame.__ * * @param pathOrBody - path or buffer or string * - path: Path to a file or a file like string. Any valid filepath can be used. Example: `file.csv`. * - body: String or buffer to be read as a CSV * @param options * @param options.inferSchemaLength -Maximum number of lines to read to infer schema. If set to 0, all columns will be read as pl.Utf8. * If set to `null`, a full table scan will be done (slow). * @param options.format - Either "lines" or "json" * @param options.batchSize - Number of lines to read into the buffer at once. Modify this to change performance. * @returns ({@link DataFrame}) * @example * ``` * const jsonString = ` * {"a": 1, "b": "foo", "c": 3} * {"a": 2, "b": "bar", "c": 6} * ` * > const df = pl.readJSON(jsonString, {"format": "lines"}) * > console.log(df) * shape: (2, 3) * ╭─────┬─────┬─────╮ * │ a ┆ b ┆ c │ * │ --- ┆ --- ┆ --- │ * │ i64 ┆ str ┆ i64 │ * ╞═════╪═════╪═════╡ * │ 1 ┆ foo ┆ 3 │ * ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤ * │ 2 ┆ bar ┆ 6 │ * ╰─────┴─────┴─────╯ * ``` */ export declare function readJSON(pathOrBody: string | Buffer, options?: Partial): DataFrame; export interface ScanJsonOptions { /** Maximum number of rows to scan for schema inference. Set to `null` for a full scan (slow). Default -> 100 */ inferSchemaLength: number | null; /** Number of rows to read in each batch. */ batchSize: number; /** Stop reading after `nRows` rows. */ nRows: number; /** Reduce memory pressure at the expense of performance. */ lowMemory: boolean; /** Reallocate to contiguous memory when all chunks/files are parsed. */ rechunk: boolean; /** Return `null` if parsing fails because of a schema mismatch. */ ignoreErrors: boolean; /** Declare the schema of the frame. Types that are not given are inferred. */ schema: unknown; /** Override the dtype of one or more columns; takes precedence over `schema`. */ schemaOverrides: unknown; /** Insert a row index column with this name. */ rowIndexName: string; /** Offset to start the row index column at (only used if `rowIndexName` is set). */ rowIndexOffset: number; /** * Options that indicate how to connect to a cloud provider. * * The cloud providers currently supported are AWS, GCP, and Azure. * See supported keys here: * * * `aws `_ * * `gcp `_ * * `azure `_ * * If not provided, Polars will try to infer the information from environment variables. */ cloudOptions: Record; /** Include the path of the source file(s) as a column with this name. */ includeFilePaths: string; } /** * __Lazily read from a newline delimited JSON file or multiple files via glob patterns.__ * * This allows the query optimizer to push down predicates and projections to the scan * level, thereby potentially reducing memory overhead. * * _Note: Currently only newline delimited JSON is supported_ * @param path - path to json file * - path: Path to a file or a file like string. Any valid filepath can be used. Example: `./file.json`. * @param options * @param options.inferSchemaLength - Maximum number of rows to scan for schema inference. * If set to `null`, the full data may be scanned *(this is slow)*. Default -> 100 * @param options.schema - Declare the schema of the frame. Types that are not given are inferred. * @param options.schemaOverrides - Override the dtype of one or more columns; note that any * dtypes inferred from the `schema` param will be overridden. * @param options.batchSize - Number of rows to read in each batch. * @param options.nRows - Stop reading from the JSON file after reading `nRows`. * @param options.lowMemory - Reduce memory pressure at the expense of performance. * @param options.rechunk - Reallocate to contiguous memory when all chunks/files are parsed. * @param options.ignoreErrors - Return `null` if parsing fails because of a schema mismatch. * @param options.rowIndexName - If given, insert a row index column with this name. * @param options.rowIndexOffset - Offset to start the row index column at (only used if the name is set). * @param options.cloudOptions - Options that indicate how to connect to a cloud provider. * * The cloud providers currently supported are AWS, GCP, and Azure. * See supported keys here: * * * `aws `_ * * `gcp `_ * * `azure `_ * * If `cloudOptions` is not provided, Polars will try to infer the information from * environment variables. * @param options.includeFilePaths - Include the path of the source file(s) as a column with this name. * @returns ({@link LazyDataFrame}) * @example * ``` * > const df = pl.scanJson('path/to/file.json', {nRows: 2}).collectSync() * > console.log(df) * shape: (2, 3) * ╭─────┬─────┬─────╮ * │ a ┆ b ┆ c │ * │ --- ┆ --- ┆ --- │ * │ i64 ┆ str ┆ i64 │ * ╞═════╪═════╪═════╡ * │ 1 ┆ foo ┆ 3 │ * ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤ * │ 2 ┆ bar ┆ 6 │ * ╰─────┴─────┴─────╯ * ``` */ export declare function scanJson(path: string, options?: Partial): LazyDataFrame; /** * Read into a DataFrame from a parquet file. * @param pathOrBody * Path to a file, list of files, or a file like object. If the path is a directory, that directory will be used * as partition aware scan. * @param options.columns Columns to select. Accepts a list of column indices (starting at zero) or a list of column names. * @param options.nRows Stop reading from parquet file after reading ``nRows``. * @param options.parallel * Any of 'auto' | 'columns' | 'row_groups' | 'none' This determines the direction of parallelism. 'auto' will try to determine the optimal direction. Defaults to 'auto' * @param options.rowIndexName Insert a row index column with this name. * @param options.rowIndexOffset Start the row index at this offset. Only used when `rowIndexName` is set. */ export declare function readParquet(pathOrBody: string | Buffer, options?: Partial): DataFrame; export interface ReadAvroOptions { columns: string[] | Array | number[]; projection: number; nRows: number; } /** * Read into a DataFrame from an avro file. * @param pathOrBody * Path to a file, list of files, or a file like object. If the path is a directory, that directory will be used * as partition aware scan. * @param options.columns Columns to select. Accepts a list of column names. * @param options.projection -Indices of columns to select. Note that column indices start at zero. * @param options.nRows Stop reading from avro file after reading ``nRows``. */ export declare function readAvro(pathOrBody: string | Buffer, options?: Partial): DataFrame; /** * Lazily read from a local or cloud-hosted parquet file (or files). This function allows the query optimizer to push down predicates and projections to the scan level, typically increasing performance and reducing memory overhead. * This allows the query optimizer to push down predicates and projections to the scan level, * thereby potentially reducing memory overhead. * @param source - Path(s) to a file. If a single path is given, it can be a globbing pattern. @param options.nRows - Stop reading from parquet file after reading `n_rows`. @param options.rowIndexName - If not None, this will insert a row index column with the given name into the DataFrame @param options.rowIndexOffset - Offset to start the row index column (only used if the name is set) @param options.parallel : {'auto', 'columns', 'row_groups', 'prefiltered', 'none'} This determines the direction and strategy of parallelism. 'auto' will try to determine the optimal direction. The `prefiltered` strategy first evaluates the pushed-down predicates in parallel and determines a mask of which rows to read. Then, it parallelizes over both the columns and the row groups while filtering out rows that do not need to be read. This can provide significant speedups for large files (i.e. many row-groups) with a predicate that filters clustered rows or filters heavily. In other cases, `prefiltered` may slow down the scan compared to other strategies. It falls back to `auto` if no predicate is given. @param options.useStatistics - Use statistics in the parquet to determine if pages can be skipped from reading. Default -> true @param options.hivePartitioning - Infer statistics and schema from hive partitioned URL and use them to prune reads. @param options.glob - Expand path given via globbing rules. @param options.hiddenFilePrefix - Skip reading files whose names begin with the specified prefix(es). @param options.schema - Specify the datatypes of the columns. The datatypes must match the datatypes in the file(s). If there are extra columns that are not in the file(s), consider also passing `missingColumns: 'insert'`. @param options.hiveSchema - The column names and data types of the columns by which the data is partitioned. If set to `None` (default), the schema of the Hive partitions is inferred. @param options.tryParseHiveDates - Whether to try parsing hive values as date/datetime types. @param options.rechunk - In case of reading multiple files via a glob pattern rechunk the final DataFrame into contiguous memory chunks. @param options.lowMemory - Reduce memory pressure at the expense of performance. @param options.cache - Cache the result after reading. @param options.cloudOptions - Options that indicate how to connect to a cloud provider. If the cloud provider is not supported by Polars, the storage options are passed to `fsspec.open()`. The cloud providers currently supported are AWS, GCP, and Azure. See supported keys here: * `aws `_ * `gcp `_ * `azure `_ If `cloudOptions` is not provided, Polars will try to infer the information from environment variables. @param options.includeFilePaths - Include the path of the source file(s) as a column with this name. @param options.missingColumns - Behavior when columns defined in the schema are missing from the data. Default -> 'raise' * `insert`: Inserts the missing columns using NULLs as the row values. * `raise`: Raises an error. @param options.allowMissingColumns - Deprecated. Use `missingColumns` instead and pass one of ('insert', 'raise'). If both are given, `missingColumns` takes precedence. @param options.extraColumns - Behavior when extra columns outside of the defined schema are encountered in the data. Default -> 'raise' * `ignore`: Silently ignores. * `raise`: Raises an error. @param options.castOptions - Configuration for column type-casting during scans. Useful for datasets containing files that have differing schemas. @example ``` const lazyDf = pl.scanParquet("s3://[bucket-name]/[file-key]", { cloudOptions: { aws_access_key_id: credentials.accessKeyId, aws_secret_access_key: credentials.secretAccessKey, aws_session_token: credentials.sessionToken, aws_region: "us-east-1", }, }); ``` */ export declare function scanParquet(source: string, options?: ScanParquetOptions): LazyDataFrame; export interface ReadIPCOptions { columns: string[] | number[]; nRows: number; } /** * __Read into a DataFrame from Arrow IPC file (Feather v2).__ * ___ * @param pathOrBody - path or buffer or string * - path: Path to a file or a file like string. Any valid filepath can be used. Example: `file.ipc`. * - body: String or buffer to be read as Arrow IPC * @param options.columns Columns to select. Accepts a list of column names. * @param options.nRows Stop reading from parquet file after reading ``nRows``. */ export declare function readIPC(pathOrBody: string | Buffer, options?: Partial): DataFrame; /** * __Read into a DataFrame from Arrow IPC stream.__ * ___ * @param pathOrBody - path or buffer or string * - path: Path to a file or a file like string. Any valid filepath can be used. Example: `file.ipc`. * - body: String or buffer to be read as Arrow IPC * @param options.columns Columns to select. Accepts a list of column names. * @param options.nRows Stop reading from parquet file after reading ``nRows``. */ export declare function readIPCStream(pathOrBody: string | Buffer, options?: Partial): DataFrame; export interface ScanIPCOptions { nRows: number; cache: boolean; rechunk: boolean; } /** * __Lazily read from an Arrow IPC file (Feather v2) or multiple files via glob patterns.__ * ___ * @param path Path to a IPC file. * @param options.nRows Stop reading from IPC file after reading ``nRows`` * @param options.cache Cache the result after reading. * @param options.rechunk Reallocate to contiguous memory when all chunks/ files are parsed. */ export declare function scanIPC(path: string, options?: Partial): LazyDataFrame; /** * __Read a stream into a Dataframe.__ * * **Warning:** this is much slower than `scanCSV` or `readCSV` * * This will consume the entire stream into a single buffer and then call `readCSV` * Only use it when you must consume from a stream, or when performance is not a major consideration * * ___ * @param stream - readable stream containing csv data * @param options * @param options.inferSchemaLength -Maximum number of lines to read to infer schema. If set to 0, all columns will be read as pl.Utf8. * If set to `null`, a full table scan will be done (slow). * @param options.batchSize - Number of lines to read into the buffer at once. Modify this to change performance. * @param options.hasHeader - Indicate if first row of dataset is header or not. If set to False first row will be set to `column_x`, * `x` being an enumeration over every column in the dataset. * @param options.ignoreErrors -Try to keep reading lines if some lines yield errors. * @param options.endRows -After n rows are read from the CSV, it stops reading. * During multi-threaded parsing, an upper bound of `n` rows * cannot be guaranteed. * @param options.startRows -Start reading after `startRows` position. * @param options.projection -Indices of columns to select. Note that column indices start at zero. * @param options.sep -Character to use as delimiter in the file. * @param options.columns -Columns to select. * @param options.rechunk -Make sure that all columns are contiguous in memory by aggregating the chunks into a single array. * @param options.encoding -Allowed encodings: `utf8`, `utf8-lossy`. Lossy means that invalid utf8 values are replaced with `�` character. * @param options.numThreads -Number of threads to use in csv parsing. Defaults to the number of physical cpu's of your system. * @param options.dtype -Overwrite the dtypes during inference. * @param options.lowMemory - Reduce memory usage in expense of performance. * @param options.commentPrefix - character that indicates the start of a comment line, for instance '#'. * @param options.quoteChar -character that is used for csv quoting. Default: '"'. Set to null to turn special handling and escaping of quotes off. * @param options.nullValues - Values to interpret as null values. You can provide a * - `string` -> all values encountered equal to this string will be null * - `Array` -> A null value per column. * - `Record` -> An object or map that maps column name to a null value string.Ex. {"column_1": 0} * @param options.parseDates -Whether to attempt to parse dates or not * @returns Promise * * @example * ``` * >>> const readStream = new Stream.Readable({read(){}}); * >>> readStream.push(`a,b\n`); * >>> readStream.push(`1,2\n`); * >>> readStream.push(`2,2\n`); * >>> readStream.push(`3,2\n`); * >>> readStream.push(`4,2\n`); * >>> readStream.push(null); * * >>> pl.readCSVStream(readStream).then(df => console.log(df)); * shape: (4, 2) * ┌─────┬─────┐ * │ a ┆ b │ * │ --- ┆ --- │ * │ i64 ┆ i64 │ * ╞═════╪═════╡ * │ 1 ┆ 2 │ * ├╌╌╌╌╌┼╌╌╌╌╌┤ * │ 2 ┆ 2 │ * ├╌╌╌╌╌┼╌╌╌╌╌┤ * │ 3 ┆ 2 │ * ├╌╌╌╌╌┼╌╌╌╌╌┤ * │ 4 ┆ 2 │ * └─────┴─────┘ * ``` */ export declare function readCSVStream(stream: Readable, options?: Partial): Promise; /** * __Read a newline delimited JSON stream into a DataFrame.__ * * @param stream - readable stream containing json data * @param options * @param options.inferSchemaLength -Maximum number of lines to read to infer schema. If set to 0, all columns will be read as pl.Utf8. * If set to `null`, a full table scan will be done (slow). * Note: this is done per batch * @param options.batchSize - Number of lines to read into the buffer at once. Modify this to change performance. * @example * ``` * >>> const readStream = new Stream.Readable({read(){}}); * >>> readStream.push(`${JSON.stringify({a: 1, b: 2})} \n`); * >>> readStream.push(`${JSON.stringify({a: 2, b: 2})} \n`); * >>> readStream.push(`${JSON.stringify({a: 3, b: 2})} \n`); * >>> readStream.push(`${JSON.stringify({a: 4, b: 2})} \n`); * >>> readStream.push(null); * * >>> pl.readJSONStream(readStream, { format: "lines" }).then(df => console.log(df)); * shape: (4, 2) * ┌─────┬─────┐ * │ a ┆ b │ * │ --- ┆ --- │ * │ i64 ┆ i64 │ * ╞═════╪═════╡ * │ 1 ┆ 2 │ * ├╌╌╌╌╌┼╌╌╌╌╌┤ * │ 2 ┆ 2 │ * ├╌╌╌╌╌┼╌╌╌╌╌┤ * │ 3 ┆ 2 │ * ├╌╌╌╌╌┼╌╌╌╌╌┤ * │ 4 ┆ 2 │ * └─────┴─────┘ * ``` */ export declare function readJSONStream(stream: Readable, options?: Partial): Promise;