Path: blob/main/crates/polars-core/src/frame/row/dataframe.rs
8424 views
use super::*;12impl DataFrame {3/// Get a row from a [`DataFrame`]. Use of this is discouraged as it will likely be slow.4pub fn get_row(&self, idx: usize) -> PolarsResult<Row<'_>> {5let values = self6.materialized_column_iter()7.map(|s| s.get(idx))8.collect::<PolarsResult<Vec<_>>>()?;9Ok(Row(values))10}1112/// Amortize allocations by reusing a row.13/// The caller is responsible to make sure that the row has at least the capacity for the number14/// of columns in the [`DataFrame`]15pub fn get_row_amortized<'a>(&'a self, idx: usize, row: &mut Row<'a>) -> PolarsResult<()> {16for (s, any_val) in self.materialized_column_iter().zip(&mut row.0) {17*any_val = s.get(idx)?;18}19Ok(())20}2122/// Amortize allocations by reusing a row.23/// The caller is responsible to make sure that the row has at least the capacity for the number24/// of columns in the [`DataFrame`]25///26/// # Safety27/// Does not do any bounds checking.28#[inline]29pub unsafe fn get_row_amortized_unchecked<'a>(&'a self, idx: usize, row: &mut Row<'a>) {30self.materialized_column_iter()31.zip(&mut row.0)32.for_each(|(s, any_val)| {33*any_val = s.get_unchecked(idx);34});35}3637/// Create a new [`DataFrame`] from rows.38///39/// This should only be used when you have row wise data, as this is a lot slower40/// than creating the [`Series`] in a columnar fashion41pub fn from_rows_and_schema(rows: &[Row], schema: &Schema) -> PolarsResult<Self> {42Self::from_rows_iter_and_schema(rows.iter(), schema)43}4445/// Create a new [`DataFrame`] from an iterator over rows.46///47/// This should only be used when you have row wise data, as this is a lot slower48/// than creating the [`Series`] in a columnar fashion.49pub fn from_rows_iter_and_schema<'a, I>(mut rows: I, schema: &Schema) -> PolarsResult<Self>50where51I: Iterator<Item = &'a Row<'a>>,52{53if schema.is_empty() {54let height = rows.count();55let columns = Vec::new();56return Ok(unsafe { DataFrame::new_unchecked(height, columns) });57}5859let capacity = rows.size_hint().0;6061let mut buffers: Vec<_> = schema62.iter_values()63.map(|dtype| {64let buf: AnyValueBuffer = (dtype, capacity).into();65buf66})67.collect();6869let mut expected_len = 0;70rows.try_for_each::<_, PolarsResult<()>>(|row| {71expected_len += 1;72for (value, buf) in row.0.iter().zip(&mut buffers) {73buf.add_fallible(value)?74}75Ok(())76})?;7778let v = buffers79.into_iter()80.zip(schema.iter_names())81.map(|(b, name)| {82let mut c = b.into_series().into_column();83// if the schema adds a column not in the rows, we84// fill it with nulls85if c.is_empty() {86Column::full_null(name.clone(), expected_len, c.dtype())87} else {88c.rename(name.clone());89c90}91})92.collect();9394DataFrame::new(expected_len, v)95}9697/// Create a new [`DataFrame`] from an iterator over rows. This should only be used when you have row wise data,98/// as this is a lot slower than creating the [`Series`] in a columnar fashion99pub fn try_from_rows_iter_and_schema<'a, I>(mut rows: I, schema: &Schema) -> PolarsResult<Self>100where101I: Iterator<Item = PolarsResult<&'a Row<'a>>>,102{103let capacity = rows.size_hint().0;104105let mut buffers: Vec<_> = schema106.iter_values()107.map(|dtype| {108let buf: AnyValueBuffer = (dtype, capacity).into();109buf110})111.collect();112113let mut expected_len = 0;114rows.try_for_each::<_, PolarsResult<()>>(|row| {115expected_len += 1;116for (value, buf) in row?.0.iter().zip(&mut buffers) {117buf.add_fallible(value)?118}119Ok(())120})?;121let v = buffers122.into_iter()123.zip(schema.iter_names())124.map(|(b, name)| {125let mut c = b.into_series().into_column();126// if the schema adds a column not in the rows, we127// fill it with nulls128if c.is_empty() {129Column::full_null(name.clone(), expected_len, c.dtype())130} else {131c.rename(name.clone());132c133}134})135.collect();136137DataFrame::new(expected_len, v)138}139140/// Create a new [`DataFrame`] from rows. This should only be used when you have row wise data,141/// as this is a lot slower than creating the [`Series`] in a columnar fashion142pub fn from_rows(rows: &[Row]) -> PolarsResult<Self> {143let schema = rows_to_schema_first_non_null(rows, Some(50))?;144let has_nulls = schema145.iter_values()146.any(|dtype| matches!(dtype, DataType::Null));147polars_ensure!(148!has_nulls, ComputeError: "unable to infer row types because of null values"149);150Self::from_rows_and_schema(rows, &schema)151}152}153154155