pub struct Schema<D> { /* private fields */ }
Implementations§
Source§impl<D> Schema<D>
impl<D> Schema<D>
pub fn with_capacity(capacity: usize) -> Self
pub fn is_empty(&self) -> bool
Sourcepub 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>
Sourcepub fn insert_at_index(
&mut self,
index: usize,
name: PlSmallStr,
dtype: D,
) -> PolarsResult<Option<D>>
pub fn insert_at_index( &mut self, index: usize, name: PlSmallStr, dtype: D, ) -> PolarsResult<Option<D>>
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)
Sourcepub 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.
Sourcepub fn try_get(&self, name: &str) -> PolarsResult<&D>
pub fn try_get(&self, name: &str) -> PolarsResult<&D>
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 D>
pub fn try_get_mut(&mut self, name: &str) -> PolarsResult<&mut D>
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, &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.
Sourcepub fn try_get_full(&self, name: &str) -> PolarsResult<(usize, &PlSmallStr, &D)>
pub fn try_get_full(&self, name: &str) -> PolarsResult<(usize, &PlSmallStr, &D)>
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 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
for a mutable version.
pub fn try_get_at_index(&self, index: usize) -> PolarsResult<(&PlSmallStr, &D)>
Sourcepub 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
for an immutable version.
Sourcepub 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
.
Sourcepub 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
.
Sourcepub 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
.
Sourcepub 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
or insert_at_index
.
Sourcepub 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
or insert_at_index
.
Sourcepub 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 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 [D
] 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
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
Sourcepub fn iter(&self) -> impl ExactSizeIterator<Item = (&PlSmallStr, &D)> + '_
pub fn iter(&self) -> impl ExactSizeIterator<Item = (&PlSmallStr, &D)> + '_
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<Item = (&PlSmallStr, &mut D)> + '_
Sourcepub fn iter_names(&self) -> impl '_ + ExactSizeIterator<Item = &PlSmallStr>
pub fn iter_names(&self) -> impl '_ + ExactSizeIterator<Item = &PlSmallStr>
Iterates over references to the names in this schema.
pub fn iter_names_cloned( &self, ) -> impl '_ + ExactSizeIterator<Item = PlSmallStr>
Sourcepub fn iter_values(&self) -> impl '_ + ExactSizeIterator<Item = &D>
pub fn iter_values(&self) -> impl '_ + ExactSizeIterator<Item = &D>
Iterates over references to the dtypes in this schema.
pub fn into_iter_values(self) -> impl ExactSizeIterator<Item = D>
Sourcepub fn iter_values_mut(&mut self) -> impl '_ + ExactSizeIterator<Item = &mut D>
pub fn iter_values_mut(&mut self) -> impl '_ + ExactSizeIterator<Item = &mut D>
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) -> PolarsResult<usize>
Source§impl<D> Schema<D>
impl<D> Schema<D>
Sourcepub fn new_inserting_at_index(
&self,
index: usize,
name: PlSmallStr,
field: D,
) -> PolarsResult<Self>
pub fn new_inserting_at_index( &self, index: usize, name: PlSmallStr, field: D, ) -> 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 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
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
Sourcepub fn try_project<I>(&self, columns: I) -> PolarsResult<Self>
pub fn try_project<I>(&self, columns: I) -> PolarsResult<Self>
Generates another schema with just the specified columns selected from this one.
pub fn try_project_indices(&self, indices: &[usize]) -> PolarsResult<Self>
Trait Implementations§
Source§impl<D> From<IndexMap<PlSmallStr, D, RandomState>> for Schema<D>
impl<D> From<IndexMap<PlSmallStr, D, RandomState>> for Schema<D>
Source§fn from(fields: PlIndexMap<PlSmallStr, D>) -> Self
fn from(fields: PlIndexMap<PlSmallStr, D>) -> Self
Source§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)>,
Source§fn from_iter<I: IntoIterator<Item = F>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = F>>(iter: I) -> Self
Source§impl<D> IntoIterator for Schema<D>
impl<D> IntoIterator for Schema<D>
Source§type IntoIter = <IndexMap<PlSmallStr, D, RandomState> as IntoIterator>::IntoIter
type IntoIter = <IndexMap<PlSmallStr, D, RandomState> as IntoIterator>::IntoIter
Source§type Item = (PlSmallStr, D)
type Item = (PlSmallStr, D)
impl<D: Eq> Eq for Schema<D>
Auto Trait Implementations§
impl<D> Freeze for Schema<D>
impl<D> RefUnwindSafe for Schema<D>where
D: RefUnwindSafe,
impl<D> Send for Schema<D>where
D: Send,
impl<D> Sync for Schema<D>where
D: Sync,
impl<D> Unpin for Schema<D>where
D: Unpin,
impl<D> UnwindSafe for Schema<D>where
D: UnwindSafe,
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
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§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.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