-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
183 additions
and
180 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
polars/polars-io/src/csv_core/buffer.rs → polars/polars-io/src/csv/buffer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! # (De)serializing CSV files | ||
//! | ||
//! ## Maximal performance | ||
//! Currently [CsvReader::new](CsvReader::new) has an extra copy. If you want optimal performance in CSV parsing/ | ||
//! reading, it is advised to use [CsvReader::from_path](CsvReader::from_path). | ||
//! | ||
//! ## Write a DataFrame to a csv file. | ||
//! | ||
//! ## Example | ||
//! | ||
//! ``` | ||
//! use polars_core::prelude::*; | ||
//! use polars_io::prelude::*; | ||
//! use std::fs::File; | ||
//! | ||
//! fn example(df: &mut DataFrame) -> Result<()> { | ||
//! let mut file = File::create("example.csv").expect("could not create file"); | ||
//! | ||
//! CsvWriter::new(&mut file) | ||
//! .has_header(true) | ||
//! .with_delimiter(b',') | ||
//! .finish(df) | ||
//! } | ||
//! ``` | ||
//! | ||
//! ## Read a csv file to a DataFrame | ||
//! | ||
//! ## Example | ||
//! | ||
//! ``` | ||
//! use polars_core::prelude::*; | ||
//! use polars_io::prelude::*; | ||
//! use std::fs::File; | ||
//! | ||
//! fn example() -> Result<DataFrame> { | ||
//! // always prefer `from_path` as that is fastest. | ||
//! CsvReader::from_path("iris_csv")? | ||
//! .has_header(true) | ||
//! .finish() | ||
//! } | ||
//! ``` | ||
//! | ||
pub(crate) mod buffer; | ||
pub(crate) mod parser; | ||
pub mod read_impl; | ||
|
||
mod read; | ||
#[cfg(not(feature = "private"))] | ||
pub(crate) mod utils; | ||
#[cfg(feature = "private")] | ||
pub mod utils; | ||
mod write; | ||
pub(super) mod write_impl; | ||
|
||
use crate::aggregations::ScanAggregation; | ||
use crate::csv::read_impl::{cast_columns, CoreReader}; | ||
use crate::csv::utils::get_reader_bytes; | ||
use crate::mmap::MmapBytesReader; | ||
use crate::predicates::PhysicalIoExpr; | ||
use crate::utils::resolve_homedir; | ||
use crate::{RowCount, SerReader, SerWriter}; | ||
|
||
use polars_core::prelude::*; | ||
#[cfg(feature = "temporal")] | ||
use polars_time::prelude::*; | ||
#[cfg(feature = "temporal")] | ||
use rayon::prelude::*; | ||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
use std::borrow::Cow; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
|
||
pub use read::{CsvEncoding, CsvReader, NullValues}; | ||
pub use write::CsvWriter; |
File renamed without changes.
152 changes: 1 addition & 151 deletions
152
polars/polars-io/src/csv.rs → polars/polars-io/src/csv/read.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
polars/polars-io/src/csv_core/csv.rs → polars/polars-io/src/csv/read_impl.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
polars/polars-io/src/csv_core/utils.rs → polars/polars-io/src/csv/utils.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use super::*; | ||
|
||
/// Write a DataFrame to csv. | ||
/// | ||
/// Don't use a `Buffered` writer, the `CsvWriter` internally already buffers writes. | ||
#[must_use] | ||
pub struct CsvWriter<W: Write> { | ||
/// File or Stream handler | ||
buffer: W, | ||
options: write_impl::SerializeOptions, | ||
header: bool, | ||
batch_size: usize, | ||
} | ||
|
||
impl<W> SerWriter<W> for CsvWriter<W> | ||
where | ||
W: Write, | ||
{ | ||
fn new(buffer: W) -> Self { | ||
// 9f: all nanoseconds | ||
let options = write_impl::SerializeOptions { | ||
time_format: Some("%T%.9f".to_string()), | ||
datetime_format: Some("%FT%H:%M:%S.%9f".to_string()), | ||
..Default::default() | ||
}; | ||
|
||
CsvWriter { | ||
buffer, | ||
options, | ||
header: true, | ||
batch_size: 1024, | ||
} | ||
} | ||
|
||
fn finish(&mut self, df: &mut DataFrame) -> Result<()> { | ||
df.as_single_chunk_par(); | ||
let names = df.get_column_names(); | ||
if self.header { | ||
write_impl::write_header(&mut self.buffer, &names, &self.options)?; | ||
} | ||
write_impl::write(&mut self.buffer, df, self.batch_size, &self.options) | ||
} | ||
} | ||
|
||
impl<W> CsvWriter<W> | ||
where | ||
W: Write, | ||
{ | ||
/// Set whether to write headers | ||
pub fn has_header(mut self, has_header: bool) -> Self { | ||
self.header = has_header; | ||
self | ||
} | ||
|
||
/// Set the CSV file's column delimiter as a byte character | ||
pub fn with_delimiter(mut self, delimiter: u8) -> Self { | ||
self.options.delimiter = delimiter; | ||
self | ||
} | ||
|
||
pub fn with_batch_size(mut self, batch_size: usize) -> Self { | ||
self.batch_size = batch_size; | ||
self | ||
} | ||
|
||
/// Set the CSV file's date format | ||
pub fn with_date_format(mut self, format: Option<String>) -> Self { | ||
self.options.date_format = format; | ||
self | ||
} | ||
|
||
/// Set the CSV file's time format | ||
pub fn with_time_format(mut self, format: Option<String>) -> Self { | ||
self.options.time_format = format; | ||
self | ||
} | ||
|
||
/// Set the CSV file's timestamp format array in | ||
pub fn with_datetime(mut self, format: Option<String>) -> Self { | ||
self.options.datetime_format = format; | ||
self | ||
} | ||
|
||
/// Set the single byte character used for quoting | ||
pub fn with_quoting_char(mut self, char: u8) -> Self { | ||
self.options.quote = char; | ||
self | ||
} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.