Struct polars_io::csv::CsvReader

source ·
pub struct CsvReader<'a, R>
where R: MmapBytesReader,
{ /* private fields */ }
Available on crate features csv or json only.
Expand description

Create a new DataFrame by reading a csv file.

§Example

use polars_core::prelude::*;
use polars_io::prelude::*;
use std::fs::File;

fn example() -> PolarsResult<DataFrame> {
    CsvReader::from_path("iris.csv")?
            .has_header(true)
            .finish()
}

Implementations§

source§

impl<'a, R> CsvReader<'a, R>
where R: 'a + MmapBytesReader,

source

pub fn with_skip_rows_after_header(self, offset: usize) -> Self

Skip these rows after the header

source

pub fn with_row_index(self, row_index: Option<RowIndex>) -> Self

Add a row index column.

source

pub fn with_chunk_size(self, chunk_size: usize) -> Self

Sets the chunk size used by the parser. This influences performance

source

pub fn with_encoding(self, enc: CsvEncoding) -> Self

source

pub fn with_n_rows(self, num_rows: Option<usize>) -> Self

Try to stop parsing when n rows are parsed. During multithreaded parsing the upper bound n cannot be guaranteed.

source

pub fn with_ignore_errors(self, ignore: bool) -> Self

Continue with next batch when a ParserError is encountered.

source

pub fn with_schema(self, schema: Option<SchemaRef>) -> Self

Set the CSV file’s schema. This only accepts datatypes that are implemented in the csv parser and expects a complete Schema.

It is recommended to use with_dtypes instead.

source

pub fn with_skip_rows(self, skip_rows: usize) -> Self

Skip the first n rows during parsing. The header will be parsed at n lines.

source

pub fn with_rechunk(self, rechunk: bool) -> Self

Rechunk the DataFrame to contiguous memory after the CSV is parsed.

source

pub fn has_header(self, has_header: bool) -> Self

Set whether the CSV file has headers

source

pub fn with_separator(self, separator: u8) -> Self

Set the CSV file’s column separator as a byte character

source

pub fn with_comment_prefix(self, comment_prefix: Option<&str>) -> Self

Set the comment prefix for this instance. Lines starting with this prefix will be ignored.

source

pub fn _with_comment_prefix(self, comment_prefix: Option<CommentPrefix>) -> Self

Sets the comment prefix from CsvParserOptions for internal initialization.

source

pub fn with_end_of_line_char(self, eol_char: u8) -> Self

source

pub fn with_null_values(self, null_values: Option<NullValues>) -> Self

Set values that will be interpreted as missing/ null. Note that any value you set as null value will not be escaped, so if quotation marks are part of the null value you should include them.

source

pub fn with_missing_is_null(self, missing_is_null: bool) -> Self

Treat missing fields as null.

source

pub fn with_dtypes(self, schema: Option<SchemaRef>) -> Self

Overwrite the schema with the dtypes in this given Schema. The given schema may be a subset of the total schema.

source

pub fn with_dtypes_slice(self, dtypes: Option<&'a [DataType]>) -> Self

Overwrite the dtypes in the schema in the order of the slice that’s given. This is useful if you don’t know the column names beforehand

source

pub fn infer_schema(self, max_records: Option<usize>) -> Self

Set the CSV reader to infer the schema of the file

§Arguments
  • max_records - Maximum number of rows read for schema inference. Setting this to None will do a full table scan (slow).
source

pub fn with_projection(self, projection: Option<Vec<usize>>) -> Self

Set the reader’s column projection. This counts from 0, meaning that vec![0, 4] would select the 1st and 5th column.

source

pub fn with_columns(self, columns: Option<Vec<String>>) -> Self

Columns to select/ project

source

pub fn with_n_threads(self, n: Option<usize>) -> Self

Set the number of threads used in CSV reading. The default uses the number of cores of your cpu.

Note that this only works if this is initialized with CsvReader::from_path. Note that the number of cores is the maximum allowed number of threads.

source

pub fn with_path<P: Into<PathBuf>>(self, path: Option<P>) -> Self

The preferred way to initialize this builder. This allows the CSV file to be memory mapped and thereby greatly increases parsing performance.

source

pub fn sample_size(self, size: usize) -> Self

Sets the size of the sample taken from the CSV file. The sample is used to get statistic about the file. These statistics are used to try to optimally allocate up front. Increasing this may improve performance.

source

pub fn raise_if_empty(self, toggle: bool) -> Self

Raise an error if CSV is empty (otherwise return an empty frame)

source

pub fn low_memory(self, toggle: bool) -> Self

Reduce memory consumption at the expense of performance

source

pub fn with_quote_char(self, quote_char: Option<u8>) -> Self

Set the char used as quote char. The default is b'"'. If set to [None] quoting is disabled.

source

pub fn with_try_parse_dates(self, toggle: bool) -> Self

Automatically try to parse dates/ datetimes and time. If parsing fails, columns remain of dtype [DataType::String].

source

pub fn with_predicate(self, predicate: Option<Arc<dyn PhysicalIoExpr>>) -> Self

source

pub fn truncate_ragged_lines(self, toggle: bool) -> Self

Truncate lines that are longer than the schema.

source§

impl<'a> CsvReader<'a, File>

source

pub fn from_path<P: Into<PathBuf>>(path: P) -> PolarsResult<Self>

This is the recommended way to create a csv reader as this allows for fastest parsing.

source§

impl<'a, R: MmapBytesReader + 'a> CsvReader<'a, R>

source

pub fn batched_borrowed_mmap( &'a mut self ) -> PolarsResult<BatchedCsvReaderMmap<'a>>

source

pub fn batched_borrowed_read( &'a mut self ) -> PolarsResult<BatchedCsvReaderRead<'a>>

source§

impl<'a> CsvReader<'a, Box<dyn MmapBytesReader>>

source

pub fn batched_mmap( self, schema: Option<SchemaRef> ) -> PolarsResult<OwnedBatchedCsvReaderMmap>

source

pub fn batched_read( self, schema: Option<SchemaRef> ) -> PolarsResult<OwnedBatchedCsvReader>

Trait Implementations§

source§

impl<'a, R> SerReader<R> for CsvReader<'a, R>
where R: MmapBytesReader + 'a,

source§

fn new(reader: R) -> Self

Create a new CsvReader from a file/ stream

source§

fn finish(self) -> PolarsResult<DataFrame>

Read the file and create the DataFrame.

source§

fn set_rechunk(self, _rechunk: bool) -> Self
where Self: Sized,

Make sure that all columns are contiguous in memory by aggregating the chunks into a single array.

Auto Trait Implementations§

§

impl<'a, R> !RefUnwindSafe for CsvReader<'a, R>

§

impl<'a, R> Send for CsvReader<'a, R>

§

impl<'a, R> Sync for CsvReader<'a, R>

§

impl<'a, R> Unpin for CsvReader<'a, R>
where R: Unpin,

§

impl<'a, R> !UnwindSafe for CsvReader<'a, R>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more