Skip to main content

polars_core/frame/row/
dataframe.rs

1use super::*;
2
3impl DataFrame {
4    /// Get a row from a [`DataFrame`]. Use of this is discouraged as it will likely be slow.
5    pub fn get_row(&self, idx: usize) -> PolarsResult<Row<'_>> {
6        let values = self
7            .materialized_column_iter()
8            .map(|s| s.get(idx))
9            .collect::<PolarsResult<Vec<_>>>()?;
10        Ok(Row(values))
11    }
12
13    /// Amortize allocations by reusing a row.
14    /// The caller is responsible to make sure that the row has at least the capacity for the number
15    /// of columns in the [`DataFrame`]
16    pub fn get_row_amortized<'a>(&'a self, idx: usize, row: &mut Row<'a>) -> PolarsResult<()> {
17        for (s, any_val) in self.materialized_column_iter().zip(&mut row.0) {
18            *any_val = s.get(idx)?;
19        }
20        Ok(())
21    }
22
23    /// Amortize allocations by reusing a row.
24    /// The caller is responsible to make sure that the row has at least the capacity for the number
25    /// of columns in the [`DataFrame`]
26    ///
27    /// # Safety
28    /// Does not do any bounds checking.
29    #[inline]
30    pub unsafe fn get_row_amortized_unchecked<'a>(&'a self, idx: usize, row: &mut Row<'a>) {
31        self.materialized_column_iter()
32            .zip(&mut row.0)
33            .for_each(|(s, any_val)| {
34                *any_val = s.get_unchecked(idx);
35            });
36    }
37
38    /// Create a new [`DataFrame`] from rows.
39    ///
40    /// This should only be used when you have row wise data, as this is a lot slower
41    /// than creating the [`Series`] in a columnar fashion
42    pub fn from_rows_and_schema(rows: &[Row], schema: &Schema) -> PolarsResult<Self> {
43        Self::from_rows_iter_and_schema(rows.iter(), schema)
44    }
45
46    /// Create a new [`DataFrame`] from an iterator over rows.
47    ///
48    /// This should only be used when you have row wise data, as this is a lot slower
49    /// than creating the [`Series`] in a columnar fashion.
50    pub fn from_rows_iter_and_schema<'a, I>(rows: I, schema: &Schema) -> PolarsResult<Self>
51    where
52        I: Iterator<Item = &'a Row<'a>>,
53    {
54        Self::try_from_rows_iter_and_schema(rows.map(Ok), schema)
55    }
56
57    /// Create a new [`DataFrame`] from an iterator over rows. This should only be used when you have
58    /// row-wise data, as this is a lot slower than creating the [`Series`] in a columnar fashion
59    pub fn try_from_rows_iter_and_schema<'a, I>(mut rows: I, schema: &Schema) -> PolarsResult<Self>
60    where
61        I: Iterator<Item = PolarsResult<&'a Row<'a>>>,
62    {
63        let capacity = rows.size_hint().0;
64
65        let mut buffers: Vec<_> = schema
66            .iter_values()
67            .map(|dtype| AnyValueBufferBatched::new(dtype, capacity))
68            .collect();
69
70        let mut expected_len = 0;
71        let mut width = None;
72        rows.try_for_each::<_, PolarsResult<()>>(|row| {
73            let row = row?;
74            check_row_width(row, &mut width, buffers.len(), expected_len)?;
75            expected_len += 1;
76            for (value, buf) in row.0.iter().zip(&mut buffers) {
77                buf.add_fallible(value)?;
78                if buf.needs_flush() {
79                    buf.flush()?;
80                }
81            }
82            Ok(())
83        })?;
84        let v = buffers
85            .into_iter()
86            .zip(schema.iter_names())
87            .map(|(b, name)| b.into_column(name.clone(), expected_len))
88            .collect::<PolarsResult<Vec<_>>>()?;
89
90        DataFrame::new(expected_len, v)
91    }
92
93    /// Create a new [`DataFrame`] from rows. This should only be used when you have row-wise
94    /// data, as this is a lot slower than creating the [`Series`] in a columnar fashion
95    pub fn from_rows(rows: &[Row]) -> PolarsResult<Self> {
96        let schema = rows_to_schema_first_non_null(rows, Some(50))?;
97        let has_nulls = schema
98            .iter_values()
99            .any(|dtype| matches!(dtype, DataType::Null));
100        polars_ensure!(
101            !has_nulls, ComputeError: "unable to infer row types because of null values"
102        );
103        Self::from_rows_and_schema(rows, &schema)
104    }
105}
106
107/// Every row must have the same width as the first, which can be narrower than
108/// the schema (the missing columns are filled with nulls) but not wider, as
109/// the extra values would otherwise be silently dropped.
110#[inline]
111pub fn check_row_width(
112    row: &Row,
113    width: &mut Option<usize>,
114    n_columns: usize,
115    idx: usize,
116) -> PolarsResult<()> {
117    let expected = *width.get_or_insert(row.0.len().min(n_columns));
118    polars_ensure!(
119        row.0.len() == expected,
120        ShapeMismatch: "row at index {} has length {} (expected {})", idx, row.0.len(), expected
121    );
122    Ok(())
123}