Struct polars_core::series::Series
source · [−]pub struct Series(pub Arc<dyn SeriesTrait>);Expand description
Series
The columnar data type for a DataFrame.
Most of the available functions are definedin the SeriesTrait trait.
The Series struct consists
of typed ChunkedArray’s. To quickly cast
a Series to a ChunkedArray you can call the method with the name of the type:
let s: Series = [1, 2, 3].iter().collect();
// Quickly obtain the ChunkedArray wrapped by the Series.
let chunked_array = s.i32().unwrap();Arithmetic
You can do standard arithmetic on series.
let s: Series = [1, 2, 3].iter().collect();
let out_add = &s + &s;
let out_sub = &s - &s;
let out_div = &s / &s;
let out_mul = &s * &s;Or with series and numbers.
let s: Series = (1..3).collect();
let out_add_one = &s + 1;
let out_multiply = &s * 10;
// Could not overload left hand side operator.
let out_divide = 1.div(&s);
let out_add = 1.add(&s);
let out_subtract = 1.sub(&s);
let out_multiply = 1.mul(&s);Comparison
You can obtain boolean mask by comparing series.
let s = Series::new("dollars", &[1, 2, 3]);
let mask = s.equal(1);
let valid = [true, false, false].iter();
assert!(mask
.into_iter()
.map(|opt_bool| opt_bool.unwrap()) // option, because series can be null
.zip(valid)
.all(|(a, b)| a == *b))See all the comparison operators in the CmpOps trait
Iterators
The Series variants contain differently typed ChunkedArray’s. These structs can be turned into iterators, making it possible to use any function/ closure you want on a Series.
These iterators return an Option<T> because the values of a series may be null.
use polars_core::prelude::*;
let pi = 3.14;
let s = Series::new("angle", [2f32 * pi, pi, 1.5 * pi].as_ref());
let s_cos: Series = s.f32()
.expect("series was not an f32 dtype")
.into_iter()
.map(|opt_angle| opt_angle.map(|angle| angle.cos()))
.collect();Creation
Series can be create from different data structures. Below we’ll show a few ways we can create a Series object.
// Series van be created from Vec's, slices and arrays
Series::new("boolean series", &vec![true, false, true]);
Series::new("int series", &[1, 2, 3]);
// And can be nullable
Series::new("got nulls", &[Some(1), None, Some(2)]);
// Series can also be collected from iterators
let from_iter: Series = (0..10)
.into_iter()
.collect();
Tuple Fields
0: Arc<dyn SeriesTrait>Implementations
random only.This is supported on crate feature random only.
random only.Sample a fraction between 0.0-1.0 of this ChunkedArray.
random only.diff only.This is supported on crate feature moment only.
moment only.Compute the sample skewness of a data set.
For normally distributed data, the skewness should be about zero. For
unimodal continuous distributions, a skewness value greater than zero means
that there is more weight in the right tail of the distribution. The
function skewtest can be used to determine if the skewness value
is close enough to zero, statistically speaking.
see: https://github.com/scipy/scipy/blob/47bb6febaa10658c72962b9615d5d5aa2513fa3a/scipy/stats/stats.py#L1024
This is supported on crate feature moment only.
moment only.Compute the kurtosis (Fisher or Pearson) of a dataset.
Kurtosis is the fourth central moment divided by the square of the
variance. If Fisher’s definition is used, then 3.0 is subtracted from
the result to give 0.0 for a normal distribution.
If bias is false then the kurtosis is calculated using k statistics to
eliminate bias coming from biased moment estimators
see: https://github.com/scipy/scipy/blob/47bb6febaa10658c72962b9615d5d5aa2513fa3a/scipy/stats/stats.py#L1027
This is supported on crate feature round_series only.
round_series only.Round underlying floating point array to given decimal.
This is supported on crate feature round_series only.
round_series only.Floor underlying floating point array to the lowest integers smaller or equal to the float value.
This is supported on crate feature round_series only.
round_series only.Ceil underlying floating point array to the heighest integers smaller or equal to the float value.
Shrink the capacity of this array to fit it’s length.
Append arrow array of same datatype.
Append a Series of the same type in place.
Only implemented for numeric types
Compute the sum of all values in this Series.
Returns None if the array is empty or only contains null values.
If the DataType is one of {Int8, UInt8, Int16, UInt16} the Series is
first cast to Int64 to prevent overflow issues.
let s = Series::new("days", &[1, 2, 3]);
assert_eq!(s.sum(), Some(6));Returns the minimum value in the array, according to the natural order. Returns an option because the array is nullable.
let s = Series::new("days", [1, 2, 3].as_ref());
assert_eq!(s.min(), Some(1));Returns the maximum value in the array, according to the natural order. Returns an option because the array is nullable.
let s = Series::new("days", [1, 2, 3].as_ref());
assert_eq!(s.max(), Some(3));Explode a list or utf8 Series. This expands every item to a new row..
Check if float value is NaN (note this is different than missing/ null)
Check if float value is NaN (note this is different than missing/ null)
Check if float value is finite
Check if float value is finite
This is supported on crate feature zip_with only.
zip_with only.Create a new ChunkedArray with values from self where the mask evaluates true and values
from other where the mask evaluates false
Cast a datelike Series to their physical representation. Primitives remain unchanged
- Date -> Int32
- Datetime-> Int64
- Time -> Int64
- Categorical -> UInt32
pub unsafe fn take_unchecked_threaded(
&self,
idx: &UInt32Chunked,
rechunk: bool
) -> Result<Series>
pub unsafe fn take_unchecked_threaded(
&self,
idx: &UInt32Chunked,
rechunk: bool
) -> Result<Series>
Take by index if ChunkedArray contains a single chunk.
Safety
This doesn’t check any bounds. Null validity is checked.
Take by index. This operation is clone.
Safety
Out of bounds access doesn’t Error but will return a Null value
Filter by boolean mask. This operation clones data.
dot_product only.This is supported on crate feature row_hash only.
row_hash only.Get a hash of this Series
Get the sum of the Series as a new Series of length 1.
If the DataType is one of {Int8, UInt8, Int16, UInt16} the Series is
first cast to Int64 to prevent overflow issues.
This is supported on crate feature cum_agg only.
cum_agg only.Get an array with the cumulative max computed at every element
This is supported on crate feature cum_agg only.
cum_agg only.Get an array with the cumulative min computed at every element
This is supported on crate feature cum_agg only.
cum_agg only.Get an array with the cumulative sum computed at every element
If the DataType is one of {Int8, UInt8, Int16, UInt16} the Series is
first cast to Int64 to prevent overflow issues.
This is supported on crate feature cum_agg only.
cum_agg only.Get an array with the cumulative product computed at every element
If the DataType is one of {Int8, UInt8, Int16, UInt16, Int32, UInt32} the Series is
first cast to Int64 to prevent overflow issues.
This is supported on crate feature product only.
product only.Get the product of an array.
If the DataType is one of {Int8, UInt8, Int16, UInt16} the Series is
first cast to Int64 to prevent overflow issues.
This is supported on crate feature rolling_window only.
rolling_window only.Apply a rolling variance to a Series. See:
This is supported on crate feature rolling_window only.
rolling_window only.Apply a rolling std to a Series. See:
This is supported on crate feature rolling_window only.
rolling_window only.Apply a rolling mean to a Series. See: ChunkedArray::rolling_mean
This is supported on crate feature rolling_window only.
rolling_window only.Apply a rolling sum to a Series. See: ChunkedArray::rolling_sum
This is supported on crate feature rolling_window only.
rolling_window only.Apply a rolling median to a Series. See: ChunkedArray::rolling_median.
pub fn rolling_quantile(
&self,
_quantile: f64,
_interpolation: QuantileInterpolOptions,
_options: RollingOptions
) -> Result<Series>
This is supported on crate feature rolling_window only.
pub fn rolling_quantile(
&self,
_quantile: f64,
_interpolation: QuantileInterpolOptions,
_options: RollingOptions
) -> Result<Series>
rolling_window only.Apply a rolling quantile to a Series. See: ChunkedArray::rolling_quantile.
This is supported on crate feature rolling_window only.
rolling_window only.Apply a rolling min to a Series. See: ChunkedArray::rolling_min
This is supported on crate feature rolling_window only.
rolling_window only.Apply a rolling max to a Series. See: ChunkedArray::rolling_max
rank only.Cast throws an error if conversion had overflows
Check if the underlying data is a logical type.
Check if underlying physical data is numeric.
Date types and Categoricals are also considered numeric.
Check if underlying data is numeric
This is supported on crate feature abs only.
abs only.convert numerical values to their absolute value
Check if series are equal. Note that None == None evaluates to false
Check if all values in series are equal where None == None evaluates to true.
Get a pointer to the underlying data of this Series. Can be useful for fast comparisons.
Methods from Deref<Target = dyn SeriesTrait>
Trait Implementations
We don’t implement Deref so that the caller is aware of converting to Series
Performs the conversion.
Apply a closure F elementwise.
Apply a closure elementwise. The closure gets the index of the element as first argument.
Apply a closure elementwise. The closure gets the index of the element as first argument.
fn apply_cast_numeric<F, S>(&self, f: F) -> ChunkedArray<S> where
F: Fn(Series) -> S::Native + Copy,
S: PolarsNumericType,
fn apply_cast_numeric<F, S>(&self, f: F) -> ChunkedArray<S> where
F: Fn(Series) -> S::Native + Copy,
S: PolarsNumericType,
Apply a closure elementwise and cast to a Numeric ChunkedArray. This is fastest when the null check branching is more expensive than the closure application. Read more
fn branch_apply_cast_numeric_no_null<F, S>(&self, f: F) -> ChunkedArray<S> where
F: Fn(Option<Series>) -> S::Native + Copy,
S: PolarsNumericType,
fn branch_apply_cast_numeric_no_null<F, S>(&self, f: F) -> ChunkedArray<S> where
F: Fn(Option<Series>) -> S::Native + Copy,
S: PolarsNumericType,
Apply a closure on optional values and cast to Numeric ChunkedArray without null values.
Apply a closure elementwise including null values.
Create a boolean mask by checking for equality.
Create a boolean mask by checking for inequality.
Create a boolean mask by checking if self > rhs.
Create a boolean mask by checking if self >= rhs.
Create a boolean mask by checking if self < rhs.
Create a boolean mask by checking if self <= rhs.
Check for equality and regard missing values as equal.
Check for equality and regard missing values as equal.
Check for equality.
Check for inequality.
Greater than comparison.
Greater than or equal comparison.
Less than comparison.
Less than or equal comparison
Check for equality and regard missing values as equal.
Check for equality.
Check for inequality.
Greater than comparison.
Greater than or equal comparison.
Less than comparison.
Less than or equal comparison
Replace None values with a give value T.
type Target = dyn SeriesTrait
type Target = dyn SeriesTrait
The resulting type after dereferencing.
impl<T> From<ChunkedArray<T>> for Series where
T: PolarsDataType,
ChunkedArray<T>: IntoSeries,
impl<T> From<ChunkedArray<T>> for Series where
T: PolarsDataType,
ChunkedArray<T>: IntoSeries,
Performs the conversion.
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Panics
Panics if Series have different lengths.
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Checked integer division. Computes self / rhs, returning None if rhs == 0 or the division results in overflow.
Auto Trait Implementations
impl !RefUnwindSafe for Series
impl !UnwindSafe for Series
Blanket Implementations
Mutably borrows from an owned value. Read more