Struct polars_core::schema::Schema

source ·
pub struct Schema { /* private fields */ }
Expand description

A map from field/column name (String) to the type of that field/column (DataType)

Implementations§

source§

impl Schema

source

pub fn new() -> Self

Create a new, empty schema

source

pub fn with_capacity(capacity: usize) -> Self

Create a new, empty schema with capacity

If you know the number of fields you have ahead of time, using this is more efficient than using new. Also consider using Schema::from_iter if you have the collection of fields available ahead of time.

source

pub fn reserve(&mut self, additional: usize)

Reserve additional memory spaces in the schema.

source

pub fn len(&self) -> usize

The number of fields in the schema

source

pub fn is_empty(&self) -> bool

source

pub fn rename(&mut self, old: &str, new: SmartString) -> Option<SmartString>

Rename field old to new, and return the (owned) old name

If old is not present in the schema, the schema is not modified and None is returned. Otherwise the schema is updated and Some(old_name) is returned.

source

pub fn new_inserting_at_index( &self, index: usize, name: SmartString, dtype: DataType ) -> PolarsResult<Self>

Create a new schema from this one, inserting a field with name and dtype at the given index

If a field named name already exists, it is updated with the new dtype. Regardless, the field named name is always moved to the given index. Valid indices range from 0 (front of the schema) to self.len() (after the end of the schema).

For a mutating version that doesn’t clone, see insert_at_index.

Runtime: O(m * n) where m is the (average) length of the field names and n is the number of fields in the schema. This method clones every field in the schema.

Returns: Ok(new_schema) if index <= self.len(), else Err(PolarsError)

source

pub fn insert_at_index( &mut self, index: usize, name: SmartString, dtype: DataType ) -> PolarsResult<Option<DataType>>

Insert a field with name and dtype at the given index into this schema

If a field named name already exists, it is updated with the new dtype. Regardless, the field named name is always moved to the given index. Valid indices range from 0 (front of the schema) to self.len() (after the end of the schema).

For a non-mutating version that clones the schema, see new_inserting_at_index.

Runtime: O(n) where n is the number of fields in the schema.

Returns:

  • If index is out of bounds, Err(PolarsError)
  • Else if name was already in the schema, Ok(Some(old_dtype))
  • Else Ok(None)
source

pub fn get(&self, name: &str) -> Option<&DataType>

Get a reference to the dtype of the field named name, or None if the field doesn’t exist

source

pub fn try_get(&self, name: &str) -> PolarsResult<&DataType>

Get a reference to the dtype of the field named name, or Err(PolarsErr) if the field doesn’t exist

source

pub fn try_get_mut(&mut self, name: &str) -> PolarsResult<&mut DataType>

Get a mutable reference to the dtype of the field named name, or Err(PolarsErr) if the field doesn’t exist

source

pub fn get_full(&self, name: &str) -> Option<(usize, &SmartString, &DataType)>

Return all data about the field named name: its index in the schema, its name, and its dtype

Returns Some((index, &name, &dtype)) if the field exists, None if it doesn’t.

source

pub fn try_get_full( &self, name: &str ) -> PolarsResult<(usize, &SmartString, &DataType)>

Return all data about the field named name: its index in the schema, its name, and its dtype

Returns Ok((index, &name, &dtype)) if the field exists, Err(PolarsErr) if it doesn’t.

source

pub fn get_field(&self, name: &str) -> Option<Field>

Look up the name in the schema and return an owned Field by cloning the data

Returns None if the field does not exist.

This method constructs the Field by cloning the name and dtype. For a version that returns references, see get or get_full.

source

pub fn try_get_field(&self, name: &str) -> PolarsResult<Field>

Look up the name in the schema and return an owned Field by cloning the data

Returns Err(PolarsErr) if the field does not exist.

This method constructs the Field by cloning the name and dtype. For a version that returns references, see get or get_full.

source

pub fn get_at_index(&self, index: usize) -> Option<(&SmartString, &DataType)>

Get references to the name and dtype of the field at index

If index is inbounds, returns Some((&name, &dtype)), else None. See get_at_index_mut for a mutable version.

source

pub fn try_get_at_index( &self, index: usize ) -> PolarsResult<(&SmartString, &DataType)>

source

pub fn get_at_index_mut( &mut self, index: usize ) -> Option<(&mut SmartString, &mut DataType)>

Get mutable references to the name and dtype of the field at index

If index is inbounds, returns Some((&mut name, &mut dtype)), else None. See get_at_index for an immutable version.

source

pub fn remove(&mut self, name: &str) -> Option<DataType>

Swap-remove a field by name and, if the field existed, return its dtype

If the field does not exist, the schema is not modified and None is returned.

This method does a swap_remove, which is O(1) but changes the order of the schema: the field named name is replaced by the last field, which takes its position. For a slower, but order-preserving, method, use shift_remove.

source

pub fn shift_remove(&mut self, name: &str) -> Option<DataType>

Remove a field by name, preserving order, and, if the field existed, return its dtype

If the field does not exist, the schema is not modified and None is returned.

This method does a shift_remove, which preserves the order of the fields in the schema but is O(n). For a faster, but not order-preserving, method, use remove.

source

pub fn shift_remove_index( &mut self, index: usize ) -> Option<(SmartString, DataType)>

Remove a field by name, preserving order, and, if the field existed, return its dtype

If the field does not exist, the schema is not modified and None is returned.

This method does a shift_remove, which preserves the order of the fields in the schema but is O(n). For a faster, but not order-preserving, method, use remove.

source

pub fn contains(&self, name: &str) -> bool

Whether the schema contains a field named name

source

pub fn set_dtype(&mut self, name: &str, dtype: DataType) -> Option<DataType>

Change the field named name to the given dtype and return the previous dtype

If name doesn’t already exist in the schema, the schema is not modified and None is returned. Otherwise returns Some(old_dtype).

This method only ever modifies an existing field and never adds a new field to the schema. To add a new field, use with_column or insert_at_index.

source

pub fn set_dtype_at_index( &mut self, index: usize, dtype: DataType ) -> Option<DataType>

Change the field at the given index to the given dtype and return the previous dtype

If the index is out of bounds, the schema is not modified and None is returned. Otherwise returns Some(old_dtype).

This method only ever modifies an existing index and never adds a new field to the schema. To add a new field, use with_column or insert_at_index.

source

pub fn with_column( &mut self, name: SmartString, dtype: DataType ) -> Option<DataType>

Insert a new column in the Schema

If an equivalent name already exists in the schema: the name remains and retains in its place in the order, its corresponding value is updated with DataType and the older dtype is returned inside Some(_).

If no equivalent key existed in the map: the new name-dtype pair is inserted, last in order, and None is returned.

To enforce the index of the resulting field, use insert_at_index.

Computes in O(1) time (amortized average).

source

pub fn merge(&mut self, other: Self)

Merge other into self

Merging logic:

  • Fields that occur in self but not other are unmodified
  • Fields that occur in other but not self are appended, in order, to the end of self
  • Fields that occur in both self and other are updated with the dtype from other, but keep their original index
source

pub fn merge_from_ref(&mut self, other: &Self)

Merge borrowed other into self

Merging logic:

  • Fields that occur in self but not other are unmodified
  • Fields that occur in other but not self are appended, in order, to the end of self
  • Fields that occur in both self and other are updated with the dtype from other, but keep their original index
source

pub fn to_arrow(&self, pl_flavor: bool) -> ArrowSchema

Convert self to ArrowSchema by cloning the fields

source

pub fn iter_fields(&self) -> impl ExactSizeIterator<Item = Field> + '_

Iterates the Fields in this schema, constructing them anew by cloning each (&name, &dtype) pair

Note that this clones each name and dtype in order to form an owned Field. For a clone-free version, use iter, which returns (&name, &dtype).

source

pub fn iter_dtypes(&self) -> impl '_ + ExactSizeIterator<Item = &DataType>

Iterates over references to the dtypes in this schema

source

pub fn iter_dtypes_mut( &mut self ) -> impl '_ + ExactSizeIterator<Item = &mut DataType>

Iterates over mut references to the dtypes in this schema

source

pub fn iter_names(&self) -> impl '_ + ExactSizeIterator<Item = &SmartString>

Iterates over references to the names in this schema

source

pub fn iter(&self) -> impl Iterator<Item = (&SmartString, &DataType)> + '_

Iterates over the (&name, &dtype) pairs in this schema

For an owned version, use iter_fields, which clones the data to iterate owned Fields

source

pub fn to_supertype(&mut self, other: &Schema) -> PolarsResult<bool>

Take another Schema and try to find the supertypes between them.

Trait Implementations§

source§

impl Clone for Schema

source§

fn clone(&self) -> Schema

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Schema

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Schema

source§

fn default() -> Schema

Returns the “default value” for a type. Read more
source§

impl From<&[Series]> for Schema

source§

fn from(value: &[Series]) -> Self

Converts to this type from the input type.
source§

impl From<&Arc<ArrowSchema>> for Schema

source§

fn from(value: &ArrowSchemaRef) -> Self

Converts to this type from the input type.
source§

impl From<&ArrowSchema> for Schema

source§

fn from(value: &ArrowSchema) -> Self

Converts to this type from the input type.
source§

impl From<&Row<'_>> for Schema

Available on crate features rows or object only.
source§

fn from(row: &Row<'_>) -> Self

Converts to this type from the input type.
source§

impl From<&Schema> for DataFrame

source§

fn from(schema: &Schema) -> Self

Converts to this type from the input type.
source§

impl From<Arc<ArrowSchema>> for Schema

source§

fn from(value: ArrowSchemaRef) -> Self

Converts to this type from the input type.
source§

impl From<ArrowSchema> for Schema

source§

fn from(value: ArrowSchema) -> Self

Converts to this type from the input type.
source§

impl<F> FromIterator<F> for Schema
where F: Into<Field>,

source§

fn from_iter<T: IntoIterator<Item = F>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl Hash for Schema

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl IndexOfSchema for Schema

source§

fn index_of(&self, name: &str) -> Option<usize>

Get the index of a column by name.
source§

fn get_names(&self) -> Vec<&str>

Get a vector of all column names.
source§

fn try_index_of(&self, name: &str) -> PolarsResult<usize>

source§

impl IntoIterator for Schema

§

type Item = (SmartString<LazyCompact>, DataType)

The type of the elements being iterated over.
§

type IntoIter = <IndexMap<SmartString<LazyCompact>, DataType, RandomState> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for Schema

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Schema

Auto Trait Implementations§

§

impl Freeze for Schema

§

impl !RefUnwindSafe for Schema

§

impl Send for Schema

§

impl Sync for Schema

§

impl Unpin for Schema

§

impl !UnwindSafe for Schema

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<A, T, E> FromFallibleIterator<A, E> for T
where T: FromIterator<A>, E: Error,

source§

fn from_fallible_iter<F>(iter: F) -> Result<T, E>
where F: FallibleIterator<E, Item = A>,

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<I> IntoStreamingIterator for I
where I: IntoIterator,

source§

fn into_streaming_iter(self) -> Convert<Self::IntoIter>

source§

fn into_streaming_iter_ref<'a, T>(self) -> ConvertRef<'a, Self::IntoIter, T>
where Self: IntoIterator<Item = &'a T>, T: ?Sized,

Turns an IntoIterator of references into a StreamingIterator. Read more
source§

fn into_streaming_iter_mut<'a, T>(self) -> ConvertMut<'a, Self::IntoIter, T>
where Self: IntoIterator<Item = &'a mut T>, T: ?Sized,

Turns an IntoIterator of mutable references into a StreamingIteratorMut. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V