Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/frame/row/dataframe.rs
6940 views
1
use super::*;
2
3
impl 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>(mut rows: I, schema: &Schema) -> PolarsResult<Self>
51
where
52
I: Iterator<Item = &'a Row<'a>>,
53
{
54
if schema.is_empty() {
55
let height = rows.count();
56
let columns = Vec::new();
57
return Ok(unsafe { DataFrame::new_no_checks(height, columns) });
58
}
59
60
let capacity = rows.size_hint().0;
61
62
let mut buffers: Vec<_> = schema
63
.iter_values()
64
.map(|dtype| {
65
let buf: AnyValueBuffer = (dtype, capacity).into();
66
buf
67
})
68
.collect();
69
70
let mut expected_len = 0;
71
rows.try_for_each::<_, PolarsResult<()>>(|row| {
72
expected_len += 1;
73
for (value, buf) in row.0.iter().zip(&mut buffers) {
74
buf.add_fallible(value)?
75
}
76
Ok(())
77
})?;
78
let v = buffers
79
.into_iter()
80
.zip(schema.iter_names())
81
.map(|(b, name)| {
82
let mut c = b.into_series().into_column();
83
// if the schema adds a column not in the rows, we
84
// fill it with nulls
85
if c.is_empty() {
86
Column::full_null(name.clone(), expected_len, c.dtype())
87
} else {
88
c.rename(name.clone());
89
c
90
}
91
})
92
.collect();
93
DataFrame::new(v)
94
}
95
96
/// Create a new [`DataFrame`] from an iterator over rows. This should only be used when you have row wise data,
97
/// as this is a lot slower than creating the [`Series`] in a columnar fashion
98
pub fn try_from_rows_iter_and_schema<'a, I>(mut rows: I, schema: &Schema) -> PolarsResult<Self>
99
where
100
I: Iterator<Item = PolarsResult<&'a Row<'a>>>,
101
{
102
let capacity = rows.size_hint().0;
103
104
let mut buffers: Vec<_> = schema
105
.iter_values()
106
.map(|dtype| {
107
let buf: AnyValueBuffer = (dtype, capacity).into();
108
buf
109
})
110
.collect();
111
112
let mut expected_len = 0;
113
rows.try_for_each::<_, PolarsResult<()>>(|row| {
114
expected_len += 1;
115
for (value, buf) in row?.0.iter().zip(&mut buffers) {
116
buf.add_fallible(value)?
117
}
118
Ok(())
119
})?;
120
let v = buffers
121
.into_iter()
122
.zip(schema.iter_names())
123
.map(|(b, name)| {
124
let mut c = b.into_series().into_column();
125
// if the schema adds a column not in the rows, we
126
// fill it with nulls
127
if c.is_empty() {
128
Column::full_null(name.clone(), expected_len, c.dtype())
129
} else {
130
c.rename(name.clone());
131
c
132
}
133
})
134
.collect();
135
DataFrame::new(v)
136
}
137
138
/// Create a new [`DataFrame`] from rows. This should only be used when you have row wise data,
139
/// as this is a lot slower than creating the [`Series`] in a columnar fashion
140
pub fn from_rows(rows: &[Row]) -> PolarsResult<Self> {
141
let schema = rows_to_schema_first_non_null(rows, Some(50))?;
142
let has_nulls = schema
143
.iter_values()
144
.any(|dtype| matches!(dtype, DataType::Null));
145
polars_ensure!(
146
!has_nulls, ComputeError: "unable to infer row types because of null values"
147
);
148
Self::from_rows_and_schema(rows, &schema)
149
}
150
}
151
152