Enum polars_core::frame::row::AnyValueBuffer
source · pub enum AnyValueBuffer<'a> {
Boolean(BooleanChunkedBuilder),
Int32(PrimitiveChunkedBuilder<Int32Type>),
Int64(PrimitiveChunkedBuilder<Int64Type>),
UInt32(PrimitiveChunkedBuilder<UInt32Type>),
UInt64(PrimitiveChunkedBuilder<UInt64Type>),
Float32(PrimitiveChunkedBuilder<Float32Type>),
Float64(PrimitiveChunkedBuilder<Float64Type>),
Utf8(Utf8ChunkedBuilder),
All(DataType, Vec<AnyValue<'a>>),
}Variants§
Boolean(BooleanChunkedBuilder)
Int32(PrimitiveChunkedBuilder<Int32Type>)
Int64(PrimitiveChunkedBuilder<Int64Type>)
UInt32(PrimitiveChunkedBuilder<UInt32Type>)
UInt64(PrimitiveChunkedBuilder<UInt64Type>)
Float32(PrimitiveChunkedBuilder<Float32Type>)
Float64(PrimitiveChunkedBuilder<Float64Type>)
Utf8(Utf8ChunkedBuilder)
All(DataType, Vec<AnyValue<'a>>)
Implementations§
source§impl<'a> AnyValueBuffer<'a>
impl<'a> AnyValueBuffer<'a>
sourcepub fn add(&mut self, val: AnyValue<'a>) -> Option<()>
pub fn add(&mut self, val: AnyValue<'a>) -> Option<()>
Examples found in repository?
src/frame/row.rs (line 162)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
pub(crate) fn transpose_from_dtype(&self, dtype: &DataType) -> PolarsResult<DataFrame> {
let new_width = self.height();
let new_height = self.width();
match dtype {
#[cfg(feature = "dtype-i8")]
DataType::Int8 => numeric_transpose::<Int8Type>(&self.columns),
#[cfg(feature = "dtype-i16")]
DataType::Int16 => numeric_transpose::<Int16Type>(&self.columns),
DataType::Int32 => numeric_transpose::<Int32Type>(&self.columns),
DataType::Int64 => numeric_transpose::<Int64Type>(&self.columns),
#[cfg(feature = "dtype-u8")]
DataType::UInt8 => numeric_transpose::<UInt8Type>(&self.columns),
#[cfg(feature = "dtype-u16")]
DataType::UInt16 => numeric_transpose::<UInt16Type>(&self.columns),
DataType::UInt32 => numeric_transpose::<UInt32Type>(&self.columns),
DataType::UInt64 => numeric_transpose::<UInt64Type>(&self.columns),
DataType::Float32 => numeric_transpose::<Float32Type>(&self.columns),
DataType::Float64 => numeric_transpose::<Float64Type>(&self.columns),
_ => {
let mut buffers = (0..new_width)
.map(|_| {
let buf: AnyValueBuffer = (dtype, new_height).into();
buf
})
.collect::<Vec<_>>();
let columns = self
.columns
.iter()
.map(|s| s.cast(dtype).unwrap())
.collect::<Vec<_>>();
// this is very expensive. A lot of cache misses here.
// This is the part that is performance critical.
columns.iter().for_each(|s| {
s.iter().zip(buffers.iter_mut()).for_each(|(av, buf)| {
let _out = buf.add(av);
debug_assert!(_out.is_some());
});
});
let cols = buffers
.into_iter()
.enumerate()
.map(|(i, buf)| {
let mut s = buf.into_series();
s.rename(&format!("column_{i}"));
s
})
.collect::<Vec<_>>();
Ok(DataFrame::new_no_checks(cols))
}
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "rows")))]
/// Transpose a DataFrame. This is a very expensive operation.
pub fn transpose(&self) -> PolarsResult<DataFrame> {
let height = self.height();
let width = self.width();
if height == 0 || width == 0 {
return Err(PolarsError::NoData("empty dataframe".into()));
}
let dtype = self.get_supertype().unwrap()?;
self.transpose_from_dtype(&dtype)
}
}
type Tracker = PlIndexMap<String, PlHashSet<DataType>>;
pub fn infer_schema(
iter: impl Iterator<Item = Vec<(String, impl Into<DataType>)>>,
infer_schema_length: usize,
) -> Schema {
let mut values: Tracker = Tracker::default();
let len = iter.size_hint().1.unwrap_or(infer_schema_length);
let max_infer = std::cmp::min(len, infer_schema_length);
for inner in iter.take(max_infer) {
for (key, value) in inner {
add_or_insert(&mut values, &key, value.into());
}
}
Schema::from(resolve_fields(values).into_iter())
}
fn add_or_insert(values: &mut Tracker, key: &str, data_type: DataType) {
if data_type == DataType::Null {
return;
}
if values.contains_key(key) {
let x = values.get_mut(key).unwrap();
x.insert(data_type);
} else {
// create hashset and add value type
let mut hs = PlHashSet::new();
hs.insert(data_type);
values.insert(key.to_string(), hs);
}
}
fn resolve_fields(spec: Tracker) -> Vec<Field> {
spec.iter()
.map(|(k, hs)| {
let v: Vec<&DataType> = hs.iter().collect();
Field::new(k, coerce_data_type(&v))
})
.collect()
}
/// Coerces a slice of datatypes into a single supertype.
pub fn coerce_data_type<A: Borrow<DataType>>(datatypes: &[A]) -> DataType {
use DataType::*;
let are_all_equal = datatypes.windows(2).all(|w| w[0].borrow() == w[1].borrow());
if are_all_equal {
return datatypes[0].borrow().clone();
}
if datatypes.len() > 2 {
return Utf8;
}
let (lhs, rhs) = (datatypes[0].borrow(), datatypes[1].borrow());
try_get_supertype(lhs, rhs).unwrap_or(Utf8)
}
fn is_nested_null(av: &AnyValue) -> bool {
match av {
AnyValue::Null => true,
AnyValue::List(s) => s.null_count() == s.len(),
#[cfg(feature = "dtype-struct")]
AnyValue::Struct(_, _, _) => av._iter_struct_av().all(|av| is_nested_null(&av)),
_ => false,
}
}
// nested dtypes that are all null, will be set as null leave dtype
fn infer_dtype_dynamic(av: &AnyValue) -> DataType {
match av {
AnyValue::List(s) if s.null_count() == s.len() => DataType::List(Box::new(DataType::Null)),
#[cfg(feature = "dtype-struct")]
AnyValue::Struct(_, _, _) => DataType::Struct(
av._iter_struct_av()
.map(|av| {
let dtype = infer_dtype_dynamic(&av);
Field::new("", dtype)
})
.collect(),
),
av => av.into(),
}
}
pub fn any_values_to_dtype(column: &[AnyValue]) -> PolarsResult<DataType> {
// we need an index-map as the order of dtypes influences how the
// struct fields are constructed.
let mut types_set = PlIndexSet::new();
for val in column.iter() {
let dtype = infer_dtype_dynamic(val);
types_set.insert(dtype);
}
types_set_to_dtype(types_set)
}
fn types_set_to_dtype(types_set: PlIndexSet<DataType>) -> PolarsResult<DataType> {
types_set
.into_iter()
.map(Ok)
.fold_first_(|a, b| try_get_supertype(&a?, &b?))
.unwrap()
}
/// Infer schema from rows and set the supertypes of the columns as column data type.
pub fn rows_to_schema_supertypes(
rows: &[Row],
infer_schema_length: Option<usize>,
) -> PolarsResult<Schema> {
// no of rows to use to infer dtype
let max_infer = infer_schema_length.unwrap_or(rows.len());
let mut dtypes: Vec<PlIndexSet<DataType>> = vec![PlIndexSet::new(); rows[0].0.len()];
for row in rows.iter().take(max_infer) {
for (val, types_set) in row.0.iter().zip(dtypes.iter_mut()) {
let dtype = infer_dtype_dynamic(val);
types_set.insert(dtype);
}
}
dtypes
.into_iter()
.enumerate()
.map(|(i, types_set)| {
let dtype = types_set_to_dtype(types_set)?;
Ok(Field::new(format!("column_{i}").as_ref(), dtype))
})
.collect::<PolarsResult<_>>()
}
/// Infer schema from rows and set the first no null type as column data type.
pub fn rows_to_schema_first_non_null(rows: &[Row], infer_schema_length: Option<usize>) -> Schema {
// no of rows to use to infer dtype
let max_infer = infer_schema_length.unwrap_or(rows.len());
let mut schema: Schema = (&rows[0]).into();
// the first row that has no nulls will be used to infer the schema.
// if there is a null, we check the next row and see if we can update the schema
for row in rows.iter().take(max_infer).skip(1) {
// for i in 1..max_infer {
let nulls: Vec<_> = schema
.iter_dtypes()
.enumerate()
.filter_map(|(i, dtype)| {
// double check struct and list types types
// nested null values can be wrongly inferred by front ends
match dtype {
DataType::Null | DataType::List(_) => Some(i),
#[cfg(feature = "dtype-struct")]
DataType::Struct(_) => Some(i),
_ => None,
}
})
.collect();
if nulls.is_empty() {
break;
} else {
for i in nulls {
let val = &row.0[i];
if !is_nested_null(val) {
let dtype = val.into();
schema.coerce_by_index(i, dtype).unwrap();
}
}
}
}
schema
}
impl<'a> From<&AnyValue<'a>> for Field {
fn from(val: &AnyValue<'a>) -> Self {
Field::new("", val.into())
}
}
impl From<&Row<'_>> for Schema {
fn from(row: &Row) -> Self {
let fields = row.0.iter().enumerate().map(|(i, av)| {
let dtype = av.into();
Field::new(format!("column_{i}").as_ref(), dtype)
});
Schema::from(fields)
}
}
pub enum AnyValueBuffer<'a> {
Boolean(BooleanChunkedBuilder),
Int32(PrimitiveChunkedBuilder<Int32Type>),
Int64(PrimitiveChunkedBuilder<Int64Type>),
UInt32(PrimitiveChunkedBuilder<UInt32Type>),
UInt64(PrimitiveChunkedBuilder<UInt64Type>),
#[cfg(feature = "dtype-date")]
Date(PrimitiveChunkedBuilder<Int32Type>),
#[cfg(feature = "dtype-datetime")]
Datetime(
PrimitiveChunkedBuilder<Int64Type>,
TimeUnit,
Option<TimeZone>,
),
#[cfg(feature = "dtype-duration")]
Duration(PrimitiveChunkedBuilder<Int64Type>, TimeUnit),
#[cfg(feature = "dtype-time")]
Time(PrimitiveChunkedBuilder<Int64Type>),
Float32(PrimitiveChunkedBuilder<Float32Type>),
Float64(PrimitiveChunkedBuilder<Float64Type>),
Utf8(Utf8ChunkedBuilder),
All(DataType, Vec<AnyValue<'a>>),
}
impl<'a> AnyValueBuffer<'a> {
#[inline]
pub fn add(&mut self, val: AnyValue<'a>) -> Option<()> {
use AnyValueBuffer::*;
match (self, val) {
(Boolean(builder), AnyValue::Null) => builder.append_null(),
(Boolean(builder), AnyValue::Boolean(v)) => builder.append_value(v),
(Boolean(builder), val) => {
let v = val.extract::<u8>()?;
builder.append_value(v == 1)
}
(Int32(builder), AnyValue::Null) => builder.append_null(),
(Int32(builder), val) => builder.append_value(val.extract()?),
(Int64(builder), AnyValue::Null) => builder.append_null(),
(Int64(builder), val) => builder.append_value(val.extract()?),
(UInt32(builder), AnyValue::Null) => builder.append_null(),
(UInt32(builder), val) => builder.append_value(val.extract()?),
(UInt64(builder), AnyValue::Null) => builder.append_null(),
(UInt64(builder), val) => builder.append_value(val.extract()?),
#[cfg(feature = "dtype-date")]
(Date(builder), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-date")]
(Date(builder), AnyValue::Date(v)) => builder.append_value(v),
#[cfg(feature = "dtype-datetime")]
(Datetime(builder, _, _), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-datetime")]
(Datetime(builder, tu_l, _), AnyValue::Datetime(v, tu_r, _)) => {
// we convert right tu to left tu
// so we swap.
let v = convert_time_units(v, tu_r, *tu_l);
builder.append_value(v)
}
#[cfg(feature = "dtype-duration")]
(Duration(builder, _), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-duration")]
(Duration(builder, tu_l), AnyValue::Duration(v, tu_r)) => {
let v = convert_time_units(v, tu_r, *tu_l);
builder.append_value(v)
}
#[cfg(feature = "dtype-time")]
(Time(builder), AnyValue::Time(v)) => builder.append_value(v),
#[cfg(feature = "dtype-time")]
(Time(builder), AnyValue::Null) => builder.append_null(),
(Float32(builder), AnyValue::Null) => builder.append_null(),
(Float64(builder), AnyValue::Null) => builder.append_null(),
(Float32(builder), val) => builder.append_value(val.extract()?),
(Float64(builder), val) => builder.append_value(val.extract()?),
(Utf8(builder), AnyValue::Utf8(v)) => builder.append_value(v),
(Utf8(builder), AnyValue::Utf8Owned(v)) => builder.append_value(v),
(Utf8(builder), AnyValue::Null) => builder.append_null(),
// Struct and List can be recursive so use anyvalues for that
(All(_, vals), v) => vals.push(v),
// dynamic types
(Utf8(builder), av) => match av {
AnyValue::Int64(v) => builder.append_value(&format!("{v}")),
AnyValue::Float64(v) => builder.append_value(&format!("{v}")),
AnyValue::Boolean(true) => builder.append_value("true"),
AnyValue::Boolean(false) => builder.append_value("false"),
_ => return None,
},
_ => return None,
};
Some(())
}
pub(crate) fn add_fallible(&mut self, val: &AnyValue<'a>) -> PolarsResult<()> {
self.add(val.clone()).ok_or_else(|| {
PolarsError::ComputeError(format!("Could not append {val:?} to builder; make sure that all rows have the same schema.\n\
Or consider increasing the the 'schema_inference_length' argument.").into())
})
}sourcepub fn into_series(self) -> Series
pub fn into_series(self) -> Series
Examples found in repository?
src/frame/row.rs (line 95)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
pub fn from_rows_iter_and_schema<'a, I>(mut rows: I, schema: &Schema) -> PolarsResult<Self>
where
I: Iterator<Item = &'a Row<'a>>,
{
let capacity = rows.size_hint().0;
let mut buffers: Vec<_> = schema
.iter_dtypes()
.map(|dtype| {
let buf: AnyValueBuffer = (dtype, capacity).into();
buf
})
.collect();
let mut expected_len = 0;
rows.try_for_each::<_, PolarsResult<()>>(|row| {
expected_len += 1;
for (value, buf) in row.0.iter().zip(&mut buffers) {
buf.add_fallible(value)?
}
Ok(())
})?;
let v = buffers
.into_iter()
.zip(schema.iter_names())
.map(|(b, name)| {
let mut s = b.into_series();
// if the schema adds a column not in the rows, we
// fill it with nulls
if s.is_empty() {
Series::full_null(name, expected_len, s.dtype())
} else {
s.rename(name);
s
}
})
.collect();
DataFrame::new(v)
}
/// Create a new DataFrame from rows. This should only be used when you have row wise data,
/// as this is a lot slower than creating the `Series` in a columnar fashion
#[cfg_attr(docsrs, doc(cfg(feature = "rows")))]
pub fn from_rows(rows: &[Row]) -> PolarsResult<Self> {
let schema = rows_to_schema_first_non_null(rows, Some(50));
let has_nulls = schema
.iter_dtypes()
.any(|dtype| matches!(dtype, DataType::Null));
if has_nulls {
return Err(PolarsError::ComputeError(
"Could not infer row types, because of the null values".into(),
));
}
Self::from_rows_and_schema(rows, &schema)
}
pub(crate) fn transpose_from_dtype(&self, dtype: &DataType) -> PolarsResult<DataFrame> {
let new_width = self.height();
let new_height = self.width();
match dtype {
#[cfg(feature = "dtype-i8")]
DataType::Int8 => numeric_transpose::<Int8Type>(&self.columns),
#[cfg(feature = "dtype-i16")]
DataType::Int16 => numeric_transpose::<Int16Type>(&self.columns),
DataType::Int32 => numeric_transpose::<Int32Type>(&self.columns),
DataType::Int64 => numeric_transpose::<Int64Type>(&self.columns),
#[cfg(feature = "dtype-u8")]
DataType::UInt8 => numeric_transpose::<UInt8Type>(&self.columns),
#[cfg(feature = "dtype-u16")]
DataType::UInt16 => numeric_transpose::<UInt16Type>(&self.columns),
DataType::UInt32 => numeric_transpose::<UInt32Type>(&self.columns),
DataType::UInt64 => numeric_transpose::<UInt64Type>(&self.columns),
DataType::Float32 => numeric_transpose::<Float32Type>(&self.columns),
DataType::Float64 => numeric_transpose::<Float64Type>(&self.columns),
_ => {
let mut buffers = (0..new_width)
.map(|_| {
let buf: AnyValueBuffer = (dtype, new_height).into();
buf
})
.collect::<Vec<_>>();
let columns = self
.columns
.iter()
.map(|s| s.cast(dtype).unwrap())
.collect::<Vec<_>>();
// this is very expensive. A lot of cache misses here.
// This is the part that is performance critical.
columns.iter().for_each(|s| {
s.iter().zip(buffers.iter_mut()).for_each(|(av, buf)| {
let _out = buf.add(av);
debug_assert!(_out.is_some());
});
});
let cols = buffers
.into_iter()
.enumerate()
.map(|(i, buf)| {
let mut s = buf.into_series();
s.rename(&format!("column_{i}"));
s
})
.collect::<Vec<_>>();
Ok(DataFrame::new_no_checks(cols))
}
}
}