pub type Schema = Schema<DataType>;
Aliased Type§
struct Schema { /* private fields */ }
Implementations
§impl<D> Schema<D>
impl<D> Schema<D>
pub fn with_capacity(capacity: usize) -> Schema<D>
pub fn is_empty(&self) -> bool
pub fn rename(&mut self, old: &str, new: PlSmallStr) -> Option<PlSmallStr>
pub fn rename(&mut self, old: &str, new: PlSmallStr) -> Option<PlSmallStr>
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.
pub fn insert(&mut self, key: PlSmallStr, value: D) -> Option<D>
pub fn insert_at_index(
&mut self,
index: usize,
name: PlSmallStr,
dtype: D,
) -> Result<Option<D>, PolarsError>
pub fn insert_at_index( &mut self, index: usize, name: PlSmallStr, dtype: D, ) -> Result<Option<D>, PolarsError>
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
][Self::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)
pub fn get(&self, name: &str) -> Option<&D>
pub fn get(&self, name: &str) -> Option<&D>
Get a reference to the dtype of the field named name
, or None
if the field doesn’t exist.
pub fn get_mut(&mut self, name: &str) -> Option<&mut D>
pub fn get_mut(&mut self, name: &str) -> Option<&mut D>
Get a mutable reference to the dtype of the field named name
, or None
if the field doesn’t exist.
pub fn try_get(&self, name: &str) -> Result<&D, PolarsError>
pub fn try_get(&self, name: &str) -> Result<&D, PolarsError>
Get a reference to the dtype of the field named name
, or Err(PolarsErr)
if the field doesn’t exist.
pub fn try_get_mut(&mut self, name: &str) -> Result<&mut D, PolarsError>
pub fn try_get_mut(&mut self, name: &str) -> Result<&mut D, PolarsError>
Get a mutable reference to the dtype of the field named name
, or Err(PolarsErr)
if the field doesn’t exist.
pub fn get_full(&self, name: &str) -> Option<(usize, &PlSmallStr, &D)>
pub fn get_full(&self, name: &str) -> Option<(usize, &PlSmallStr, &D)>
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.
pub fn try_get_full(
&self,
name: &str,
) -> Result<(usize, &PlSmallStr, &D), PolarsError>
pub fn try_get_full( &self, name: &str, ) -> Result<(usize, &PlSmallStr, &D), PolarsError>
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.
pub fn get_at_index(&self, index: usize) -> Option<(&PlSmallStr, &D)>
pub fn get_at_index(&self, index: usize) -> Option<(&PlSmallStr, &D)>
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
][Self::get_at_index_mut] for a mutable version.
pub fn try_get_at_index( &self, index: usize, ) -> Result<(&PlSmallStr, &D), PolarsError>
pub fn get_at_index_mut(
&mut self,
index: usize,
) -> Option<(&mut PlSmallStr, &mut D)>
pub fn get_at_index_mut( &mut self, index: usize, ) -> Option<(&mut PlSmallStr, &mut D)>
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
][Self::get_at_index] for an immutable version.
pub fn remove(&mut self, name: &str) -> Option<D>
pub fn remove(&mut self, name: &str) -> Option<D>
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
][Self::shift_remove].
pub fn shift_remove(&mut self, name: &str) -> Option<D>
pub fn shift_remove(&mut self, name: &str) -> Option<D>
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
][Self::remove].
pub fn shift_remove_index(&mut self, index: usize) -> Option<(PlSmallStr, D)>
pub fn shift_remove_index(&mut self, index: usize) -> Option<(PlSmallStr, D)>
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
][Self::remove].
pub fn set_dtype(&mut self, name: &str, dtype: D) -> Option<D>
pub fn set_dtype(&mut self, name: &str, dtype: D) -> Option<D>
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
][Self::with_column] or [insert_at_index
][Self::insert_at_index].
pub fn set_dtype_at_index(&mut self, index: usize, dtype: D) -> Option<D>
pub fn set_dtype_at_index(&mut self, index: usize, dtype: D) -> Option<D>
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
][Self::with_column] or [insert_at_index
][Self::insert_at_index].
pub fn with_column(&mut self, name: PlSmallStr, dtype: D) -> Option<D>
pub fn with_column(&mut self, name: PlSmallStr, dtype: D) -> Option<D>
Insert a column into the [Schema
].
If the schema already has this column, this instead updates it with the new value and returns the old one. Otherwise, the column is inserted at the end.
To enforce the index of the resulting field, use [insert_at_index
][Self::insert_at_index].
pub fn try_insert(
&mut self,
name: PlSmallStr,
value: D,
) -> Result<(), PolarsError>
pub fn try_insert( &mut self, name: PlSmallStr, value: D, ) -> Result<(), PolarsError>
Raises DuplicateError if this column already exists in the schema.
pub fn hstack_mut(
&mut self,
columns: impl IntoIterator<Item = impl Into<(PlSmallStr, D)>>,
) -> Result<(), PolarsError>
pub fn hstack_mut( &mut self, columns: impl IntoIterator<Item = impl Into<(PlSmallStr, D)>>, ) -> Result<(), PolarsError>
Performs [Schema::try_insert
] for every column.
Raises DuplicateError if a column already exists in the schema.
pub fn hstack(
self,
columns: impl IntoIterator<Item = impl Into<(PlSmallStr, D)>>,
) -> Result<Schema<D>, PolarsError>
pub fn hstack( self, columns: impl IntoIterator<Item = impl Into<(PlSmallStr, D)>>, ) -> Result<Schema<D>, PolarsError>
Performs [Schema::try_insert
] for every column.
Raises DuplicateError if a column already exists in the schema.
pub fn merge(&mut self, other: Schema<D>)
pub fn merge(&mut self, other: Schema<D>)
Merge other
into self
.
Merging logic:
- Fields that occur in
self
but notother
are unmodified - Fields that occur in
other
but notself
are appended, in order, to the end ofself
- Fields that occur in both
self
andother
are updated with the dtype fromother
, but keep their original index
pub fn iter(&self) -> impl ExactSizeIterator
pub fn iter(&self) -> impl ExactSizeIterator
Iterates over the (&name, &dtype)
pairs in this schema.
For an owned version, use [iter_fields
][Self::iter_fields], which clones the data to iterate owned Field
s
pub fn iter_mut(&mut self) -> impl ExactSizeIterator
pub fn iter_names(&self) -> impl ExactSizeIterator
pub fn iter_names(&self) -> impl ExactSizeIterator
Iterates over references to the names in this schema.
pub fn iter_names_cloned(&self) -> impl ExactSizeIterator
pub fn iter_values(&self) -> impl ExactSizeIterator
pub fn iter_values(&self) -> impl ExactSizeIterator
Iterates over references to the dtypes in this schema.
pub fn into_iter_values(self) -> impl ExactSizeIterator
pub fn iter_values_mut(&mut self) -> impl ExactSizeIterator
pub fn iter_values_mut(&mut self) -> impl ExactSizeIterator
Iterates over mut references to the dtypes in this schema.
pub fn index_of(&self, name: &str) -> Option<usize>
pub fn try_index_of(&self, name: &str) -> Result<usize, PolarsError>
pub fn field_compare<'a, 'b>(
&'a self,
other: &'b Schema<D>,
self_extra: &mut Vec<(usize, (&'a PlSmallStr, &'a D))>,
other_extra: &mut Vec<(usize, (&'b PlSmallStr, &'b D))>,
)
pub fn field_compare<'a, 'b>( &'a self, other: &'b Schema<D>, self_extra: &mut Vec<(usize, (&'a PlSmallStr, &'a D))>, other_extra: &mut Vec<(usize, (&'b PlSmallStr, &'b D))>, )
Compare the fields between two schema returning the additional columns that each schema has.
§impl<D> Schema<D>
impl<D> Schema<D>
pub fn new_inserting_at_index(
&self,
index: usize,
name: PlSmallStr,
field: D,
) -> Result<Schema<D>, PolarsError>
pub fn new_inserting_at_index( &self, index: usize, name: PlSmallStr, field: D, ) -> Result<Schema<D>, PolarsError>
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
][Self::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)
pub fn merge_from_ref(&mut self, other: &Schema<D>)
pub fn merge_from_ref(&mut self, other: &Schema<D>)
Merge borrowed other
into self
.
Merging logic:
- Fields that occur in
self
but notother
are unmodified - Fields that occur in
other
but notself
are appended, in order, to the end ofself
- Fields that occur in both
self
andother
are updated with the dtype fromother
, but keep their original index
pub fn try_project<I>(&self, columns: I) -> Result<Schema<D>, PolarsError>
pub fn try_project<I>(&self, columns: I) -> Result<Schema<D>, PolarsError>
Generates another schema with just the specified columns selected from this one.
pub fn try_project_indices( &self, indices: &[usize], ) -> Result<Schema<D>, PolarsError>
Trait Implementations
§impl<'de, D> Deserialize<'de> for Schema<D>where
D: Deserialize<'de>,
impl<'de, D> Deserialize<'de> for Schema<D>where
D: Deserialize<'de>,
§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Schema<D>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Schema<D>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl<F, D> Extend<F> for Schema<D>where
F: Into<(PlSmallStr, D)>,
impl<F, D> Extend<F> for Schema<D>where
F: Into<(PlSmallStr, D)>,
§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = F>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = F>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl<D> From<IndexMap<PlSmallStr, D, RandomState>> for Schema<D>
impl<D> From<IndexMap<PlSmallStr, D, RandomState>> for Schema<D>
§fn from(fields: IndexMap<PlSmallStr, D, RandomState>) -> Schema<D>
fn from(fields: IndexMap<PlSmallStr, D, RandomState>) -> Schema<D>
§impl<F, D> FromIterator<F> for Schema<D>where
F: Into<(PlSmallStr, D)>,
impl<F, D> FromIterator<F> for Schema<D>where
F: Into<(PlSmallStr, D)>,
§fn from_iter<I>(iter: I) -> Schema<D>where
I: IntoIterator<Item = F>,
fn from_iter<I>(iter: I) -> Schema<D>where
I: IntoIterator<Item = F>,
§impl<D> IntoIterator for Schema<D>
impl<D> IntoIterator for Schema<D>
§type IntoIter = <IndexMap<PlSmallStr, D, RandomState> as IntoIterator>::IntoIter
type IntoIter = <IndexMap<PlSmallStr, D, RandomState> as IntoIterator>::IntoIter
§type Item = (PlSmallStr, D)
type Item = (PlSmallStr, D)
§fn into_iter(self) -> <Schema<D> as IntoIterator>::IntoIter
fn into_iter(self) -> <Schema<D> as IntoIterator>::IntoIter
Source§impl SchemaExt for Schema<DataType>
impl SchemaExt for Schema<DataType>
Source§fn get_field(&self, name: &str) -> Option<Field>
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
][Self::get] or [get_full
][Self::get_full].
Source§fn try_get_field(&self, name: &str) -> Result<Field, PolarsError>
fn try_get_field(&self, name: &str) -> Result<Field, PolarsError>
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
][Self::get] or [get_full
][Self::get_full].
Source§fn to_arrow(&self, compat_level: CompatLevel) -> Schema<Field>
fn to_arrow(&self, compat_level: CompatLevel) -> Schema<Field>
Convert self to ArrowSchema
by cloning the fields.
Source§fn iter_fields(&self) -> impl ExactSizeIterator
fn iter_fields(&self) -> impl ExactSizeIterator
Source§fn to_supertype(
&mut self,
other: &Schema<DataType>,
) -> Result<bool, PolarsError>
fn to_supertype( &mut self, other: &Schema<DataType>, ) -> Result<bool, PolarsError>
Take another Schema
and try to find the supertypes between them.