Struct polars_core::schema::Schema
source · pub struct Schema { /* private fields */ }Implementations§
source§impl Schema
impl Schema
sourcepub fn with_capacity(capacity: usize) -> Self
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.
pub fn is_empty(&self) -> bool
sourcepub fn rename(&mut self, old: &str, new: SmartString) -> Option<SmartString>
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.
sourcepub fn new_inserting_at_index(
&self,
index: usize,
name: SmartString,
dtype: DataType,
) -> PolarsResult<Self>
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)
sourcepub fn insert_at_index(
&mut self,
index: usize,
name: SmartString,
dtype: DataType,
) -> PolarsResult<Option<DataType>>
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
namewas already in the schema,Ok(Some(old_dtype)) - Else
Ok(None)
sourcepub fn get(&self, name: &str) -> Option<&DataType>
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
sourcepub fn try_get(&self, name: &str) -> PolarsResult<&DataType>
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
sourcepub fn try_get_mut(&mut self, name: &str) -> PolarsResult<&mut DataType>
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
sourcepub fn get_full(&self, name: &str) -> Option<(usize, &SmartString, &DataType)>
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.
sourcepub fn try_get_full(
&self,
name: &str,
) -> PolarsResult<(usize, &SmartString, &DataType)>
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.
sourcepub fn try_get_field(&self, name: &str) -> PolarsResult<Field>
pub fn try_get_field(&self, name: &str) -> PolarsResult<Field>
sourcepub fn get_at_index(&self, index: usize) -> Option<(&SmartString, &DataType)>
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.
pub fn try_get_at_index( &self, index: usize, ) -> PolarsResult<(&SmartString, &DataType)>
sourcepub fn get_at_index_mut(
&mut self,
index: usize,
) -> Option<(&mut SmartString, &mut DataType)>
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.
sourcepub fn remove(&mut self, name: &str) -> Option<DataType>
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.
sourcepub fn shift_remove(&mut self, name: &str) -> Option<DataType>
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.
sourcepub fn shift_remove_index(
&mut self,
index: usize,
) -> Option<(SmartString, DataType)>
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.
sourcepub fn set_dtype(&mut self, name: &str, dtype: DataType) -> Option<DataType>
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.
sourcepub fn set_dtype_at_index(
&mut self,
index: usize,
dtype: DataType,
) -> Option<DataType>
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.
sourcepub fn with_column(
&mut self,
name: SmartString,
dtype: DataType,
) -> Option<DataType>
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).
sourcepub fn merge(&mut self, other: Self)
pub fn merge(&mut self, other: Self)
Merge other into self
Merging logic:
- Fields that occur in
selfbut nototherare unmodified - Fields that occur in
otherbut notselfare appended, in order, to the end ofself - Fields that occur in both
selfandotherare updated with the dtype fromother, but keep their original index
sourcepub fn merge_from_ref(&mut self, other: &Self)
pub fn merge_from_ref(&mut self, other: &Self)
Merge borrowed other into self
Merging logic:
- Fields that occur in
selfbut nototherare unmodified - Fields that occur in
otherbut notselfare appended, in order, to the end ofself - Fields that occur in both
selfandotherare updated with the dtype fromother, but keep their original index
sourcepub fn to_arrow(&self, compat_level: CompatLevel) -> ArrowSchema
pub fn to_arrow(&self, compat_level: CompatLevel) -> ArrowSchema
Convert self to ArrowSchema by cloning the fields
sourcepub fn iter_fields(&self) -> impl ExactSizeIterator<Item = Field> + '_
pub fn iter_fields(&self) -> impl ExactSizeIterator<Item = Field> + '_
sourcepub fn iter_dtypes(&self) -> impl '_ + ExactSizeIterator<Item = &DataType>
pub fn iter_dtypes(&self) -> impl '_ + ExactSizeIterator<Item = &DataType>
Iterates over references to the dtypes in this schema
sourcepub fn iter_dtypes_mut(
&mut self,
) -> impl '_ + ExactSizeIterator<Item = &mut DataType>
pub fn iter_dtypes_mut( &mut self, ) -> impl '_ + ExactSizeIterator<Item = &mut DataType>
Iterates over mut references to the dtypes in this schema
sourcepub fn iter_names(&self) -> impl '_ + ExactSizeIterator<Item = &SmartString>
pub fn iter_names(&self) -> impl '_ + ExactSizeIterator<Item = &SmartString>
Iterates over references to the names in this schema
sourcepub fn iter(&self) -> impl Iterator<Item = (&SmartString, &DataType)> + '_
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
sourcepub fn to_supertype(&mut self, other: &Schema) -> PolarsResult<bool>
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 From<&Arc<ArrowSchema>> for Schema
impl From<&Arc<ArrowSchema>> for Schema
source§fn from(value: &ArrowSchemaRef) -> Self
fn from(value: &ArrowSchemaRef) -> Self
source§impl From<&ArrowSchema> for Schema
impl From<&ArrowSchema> for Schema
source§fn from(value: &ArrowSchema) -> Self
fn from(value: &ArrowSchema) -> Self
source§impl From<Arc<ArrowSchema>> for Schema
impl From<Arc<ArrowSchema>> for Schema
source§fn from(value: ArrowSchemaRef) -> Self
fn from(value: ArrowSchemaRef) -> Self
source§impl From<ArrowSchema> for Schema
impl From<ArrowSchema> for Schema
source§fn from(value: ArrowSchema) -> Self
fn from(value: ArrowSchema) -> Self
source§impl<F> FromIterator<F> for Schema
impl<F> FromIterator<F> for Schema
source§fn from_iter<T: IntoIterator<Item = F>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = F>>(iter: T) -> Self
source§impl IndexOfSchema for Schema
impl IndexOfSchema for Schema
source§impl IntoIterator for Schema
impl IntoIterator for Schema
§type Item = (SmartString<LazyCompact>, DataType)
type Item = (SmartString<LazyCompact>, DataType)
§type IntoIter = <IndexMap<SmartString<LazyCompact>, DataType, RandomState> as IntoIterator>::IntoIter
type IntoIter = <IndexMap<SmartString<LazyCompact>, DataType, RandomState> as IntoIterator>::IntoIter
source§impl SchemaNamesAndDtypes for Schema
impl SchemaNamesAndDtypes for Schema
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.source§impl<A, T, E> FromFallibleIterator<A, E> for Twhere
T: FromIterator<A>,
E: Error,
impl<A, T, E> FromFallibleIterator<A, E> for Twhere
T: FromIterator<A>,
E: Error,
fn from_fallible_iter<F>(iter: F) -> Result<T, E>where
F: FallibleIterator<E, Item = A>,
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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