Path: blob/main/crates/polars-core/src/frame/row/dataframe.rs
6940 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_no_checks(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})?;77let v = buffers78.into_iter()79.zip(schema.iter_names())80.map(|(b, name)| {81let mut c = b.into_series().into_column();82// if the schema adds a column not in the rows, we83// fill it with nulls84if c.is_empty() {85Column::full_null(name.clone(), expected_len, c.dtype())86} else {87c.rename(name.clone());88c89}90})91.collect();92DataFrame::new(v)93}9495/// Create a new [`DataFrame`] from an iterator over rows. This should only be used when you have row wise data,96/// as this is a lot slower than creating the [`Series`] in a columnar fashion97pub fn try_from_rows_iter_and_schema<'a, I>(mut rows: I, schema: &Schema) -> PolarsResult<Self>98where99I: Iterator<Item = PolarsResult<&'a Row<'a>>>,100{101let capacity = rows.size_hint().0;102103let mut buffers: Vec<_> = schema104.iter_values()105.map(|dtype| {106let buf: AnyValueBuffer = (dtype, capacity).into();107buf108})109.collect();110111let mut expected_len = 0;112rows.try_for_each::<_, PolarsResult<()>>(|row| {113expected_len += 1;114for (value, buf) in row?.0.iter().zip(&mut buffers) {115buf.add_fallible(value)?116}117Ok(())118})?;119let v = buffers120.into_iter()121.zip(schema.iter_names())122.map(|(b, name)| {123let mut c = b.into_series().into_column();124// if the schema adds a column not in the rows, we125// fill it with nulls126if c.is_empty() {127Column::full_null(name.clone(), expected_len, c.dtype())128} else {129c.rename(name.clone());130c131}132})133.collect();134DataFrame::new(v)135}136137/// Create a new [`DataFrame`] from rows. This should only be used when you have row wise data,138/// as this is a lot slower than creating the [`Series`] in a columnar fashion139pub fn from_rows(rows: &[Row]) -> PolarsResult<Self> {140let schema = rows_to_schema_first_non_null(rows, Some(50))?;141let has_nulls = schema142.iter_values()143.any(|dtype| matches!(dtype, DataType::Null));144polars_ensure!(145!has_nulls, ComputeError: "unable to infer row types because of null values"146);147Self::from_rows_and_schema(rows, &schema)148}149}150151152