Struct polars_core::frame::DataFrame
source · pub struct DataFrame { /* private fields */ }
Expand description
A contiguous growable collection of Series
that have the same length.
§Use declarations
All the common tools can be found in crate::prelude
(or in polars::prelude
).
use polars_core::prelude::*; // if the crate polars-core is used directly
// use polars::prelude::*; if the crate polars is used
§Initialization
§Default
A DataFrame
can be initialized empty:
let df = DataFrame::default();
assert!(df.is_empty());
§Wrapping a Vec<Series>
A DataFrame
is built upon a Vec<Series>
where the Series
have the same length.
let s1 = Series::new("Fruit", &["Apple", "Apple", "Pear"]);
let s2 = Series::new("Color", &["Red", "Yellow", "Green"]);
let df: PolarsResult<DataFrame> = DataFrame::new(vec![s1, s2]);
§Using a macro
The df!
macro is a convenient method:
let df: PolarsResult<DataFrame> = df!("Fruit" => &["Apple", "Apple", "Pear"],
"Color" => &["Red", "Yellow", "Green"]);
§Using a CSV file
See the polars_io::csv::CsvReader
.
§Indexing
§By a number
The Index<usize>
is implemented for the DataFrame
.
let df = df!("Fruit" => &["Apple", "Apple", "Pear"],
"Color" => &["Red", "Yellow", "Green"])?;
assert_eq!(df[0], Series::new("Fruit", &["Apple", "Apple", "Pear"]));
assert_eq!(df[1], Series::new("Color", &["Red", "Yellow", "Green"]));
§By a Series
name
let df = df!("Fruit" => &["Apple", "Apple", "Pear"],
"Color" => &["Red", "Yellow", "Green"])?;
assert_eq!(df["Fruit"], Series::new("Fruit", &["Apple", "Apple", "Pear"]));
assert_eq!(df["Color"], Series::new("Color", &["Red", "Yellow", "Green"]));
Implementations§
source§impl DataFrame
impl DataFrame
sourcepub fn to_ndarray<N>(
&self,
ordering: IndexOrder
) -> PolarsResult<Array2<N::Native>>where
N: PolarsNumericType,
Available on crate feature ndarray
only.
pub fn to_ndarray<N>(
&self,
ordering: IndexOrder
) -> PolarsResult<Array2<N::Native>>where
N: PolarsNumericType,
ndarray
only.Create a 2D ndarray::Array
from this DataFrame
. This requires all columns in the
DataFrame
to be non-null and numeric. They will be casted to the same data type
(if they aren’t already).
For floating point data we implicitly convert None
to NaN
without failure.
use polars_core::prelude::*;
let a = UInt32Chunked::new("a", &[1, 2, 3]).into_series();
let b = Float64Chunked::new("b", &[10., 8., 6.]).into_series();
let df = DataFrame::new(vec![a, b]).unwrap();
let ndarray = df.to_ndarray::<Float64Type>(IndexOrder::Fortran).unwrap();
println!("{:?}", ndarray);
Outputs:
[[1.0, 10.0],
[2.0, 8.0],
[3.0, 6.0]], shape=[3, 2], strides=[1, 3], layout=Ff (0xa), const ndim=2
source§impl DataFrame
impl DataFrame
sourcepub fn sample_n(
&self,
n: &Series,
with_replacement: bool,
shuffle: bool,
seed: Option<u64>
) -> PolarsResult<Self>
Available on crate feature random
only.
pub fn sample_n( &self, n: &Series, with_replacement: bool, shuffle: bool, seed: Option<u64> ) -> PolarsResult<Self>
random
only.Sample n datapoints from this DataFrame
.
pub fn sample_n_literal( &self, n: usize, with_replacement: bool, shuffle: bool, seed: Option<u64> ) -> PolarsResult<Self>
random
only.sourcepub fn sample_frac(
&self,
frac: &Series,
with_replacement: bool,
shuffle: bool,
seed: Option<u64>
) -> PolarsResult<Self>
Available on crate feature random
only.
pub fn sample_frac( &self, frac: &Series, with_replacement: bool, shuffle: bool, seed: Option<u64> ) -> PolarsResult<Self>
random
only.Sample a fraction between 0.0-1.0 of this DataFrame
.
source§impl DataFrame
impl DataFrame
pub fn split_chunks(&mut self) -> impl Iterator<Item = DataFrame> + '_
pub fn split_chunks_by_n(self, n: usize, parallel: bool) -> Vec<DataFrame>
source§impl DataFrame
impl DataFrame
pub fn explode_impl(&self, columns: Vec<Series>) -> PolarsResult<DataFrame>
sourcepub fn explode<I, S>(&self, columns: I) -> PolarsResult<DataFrame>
pub fn explode<I, S>(&self, columns: I) -> PolarsResult<DataFrame>
Explode DataFrame
to long format by exploding a column with Lists.
§Example
let s0 = Series::new("a", &[1i64, 2, 3]);
let s1 = Series::new("b", &[1i64, 1, 1]);
let s2 = Series::new("c", &[2i64, 2, 2]);
let list = Series::new("foo", &[s0, s1, s2]);
let s0 = Series::new("B", [1, 2, 3]);
let s1 = Series::new("C", [1, 1, 1]);
let df = DataFrame::new(vec![list, s0, s1])?;
let exploded = df.explode(["foo"])?;
println!("{:?}", df);
println!("{:?}", exploded);
Outputs:
+-------------+-----+-----+
| foo | B | C |
| --- | --- | --- |
| list [i64] | i32 | i32 |
+=============+=====+=====+
| "[1, 2, 3]" | 1 | 1 |
+-------------+-----+-----+
| "[1, 1, 1]" | 2 | 1 |
+-------------+-----+-----+
| "[2, 2, 2]" | 3 | 1 |
+-------------+-----+-----+
+-----+-----+-----+
| foo | B | C |
| --- | --- | --- |
| i64 | i32 | i32 |
+=====+=====+=====+
| 1 | 1 | 1 |
+-----+-----+-----+
| 2 | 1 | 1 |
+-----+-----+-----+
| 3 | 1 | 1 |
+-----+-----+-----+
| 1 | 2 | 1 |
+-----+-----+-----+
| 1 | 2 | 1 |
+-----+-----+-----+
| 1 | 2 | 1 |
+-----+-----+-----+
| 2 | 3 | 1 |
+-----+-----+-----+
| 2 | 3 | 1 |
+-----+-----+-----+
| 2 | 3 | 1 |
+-----+-----+-----+
sourcepub fn melt<I, J>(&self, id_vars: I, value_vars: J) -> PolarsResult<Self>
pub fn melt<I, J>(&self, id_vars: I, value_vars: J) -> PolarsResult<Self>
Unpivot a DataFrame
from wide to long format.
§Example
§Arguments
id_vars
- String slice that represent the columns to use as id variables.value_vars
- String slice that represent the columns to use as value variables.
If value_vars
is empty all columns that are not in id_vars
will be used.
let df = df!("A" => &["a", "b", "a"],
"B" => &[1, 3, 5],
"C" => &[10, 11, 12],
"D" => &[2, 4, 6]
)?;
let melted = df.melt(&["A", "B"], &["C", "D"])?;
println!("{:?}", df);
println!("{:?}", melted);
Outputs:
+-----+-----+-----+-----+
| A | B | C | D |
| --- | --- | --- | --- |
| str | i32 | i32 | i32 |
+=====+=====+=====+=====+
| "a" | 1 | 10 | 2 |
+-----+-----+-----+-----+
| "b" | 3 | 11 | 4 |
+-----+-----+-----+-----+
| "a" | 5 | 12 | 6 |
+-----+-----+-----+-----+
+-----+-----+----------+-------+
| A | B | variable | value |
| --- | --- | --- | --- |
| str | i32 | str | i32 |
+=====+=====+==========+=======+
| "a" | 1 | "C" | 10 |
+-----+-----+----------+-------+
| "b" | 3 | "C" | 11 |
+-----+-----+----------+-------+
| "a" | 5 | "C" | 12 |
+-----+-----+----------+-------+
| "a" | 1 | "D" | 2 |
+-----+-----+----------+-------+
| "b" | 3 | "D" | 4 |
+-----+-----+----------+-------+
| "a" | 5 | "D" | 6 |
+-----+-----+----------+-------+
sourcepub fn melt2(&self, args: MeltArgs) -> PolarsResult<Self>
pub fn melt2(&self, args: MeltArgs) -> PolarsResult<Self>
Similar to melt, but without generics. This may be easier if you want to pass
an empty id_vars
or empty value_vars
.
source§impl DataFrame
impl DataFrame
pub fn group_by_with_series( &self, by: Vec<Series>, multithreaded: bool, sorted: bool ) -> PolarsResult<GroupBy<'_>>
algorithm_group_by
only.sourcepub fn group_by<I, S>(&self, by: I) -> PolarsResult<GroupBy<'_>>
Available on crate feature algorithm_group_by
only.
pub fn group_by<I, S>(&self, by: I) -> PolarsResult<GroupBy<'_>>
algorithm_group_by
only.Group DataFrame using a Series column.
§Example
use polars_core::prelude::*;
fn group_by_sum(df: &DataFrame) -> PolarsResult<DataFrame> {
df.group_by(["column_name"])?
.select(["agg_column_name"])
.sum()
}
sourcepub fn group_by_stable<I, S>(&self, by: I) -> PolarsResult<GroupBy<'_>>
Available on crate feature algorithm_group_by
only.
pub fn group_by_stable<I, S>(&self, by: I) -> PolarsResult<GroupBy<'_>>
algorithm_group_by
only.Group DataFrame using a Series column. The groups are ordered by their smallest row index.
source§impl DataFrame
impl DataFrame
sourcepub fn get_row(&self, idx: usize) -> PolarsResult<Row<'_>>
Available on crate features rows
or object
only.
pub fn get_row(&self, idx: usize) -> PolarsResult<Row<'_>>
rows
or object
only.Get a row from a DataFrame
. Use of this is discouraged as it will likely be slow.
sourcepub fn get_row_amortized<'a>(
&'a self,
idx: usize,
row: &mut Row<'a>
) -> PolarsResult<()>
Available on crate features rows
or object
only.
pub fn get_row_amortized<'a>( &'a self, idx: usize, row: &mut Row<'a> ) -> PolarsResult<()>
rows
or object
only.Amortize allocations by reusing a row.
The caller is responsible to make sure that the row has at least the capacity for the number
of columns in the DataFrame
sourcepub unsafe fn get_row_amortized_unchecked<'a>(
&'a self,
idx: usize,
row: &mut Row<'a>
)
Available on crate features rows
or object
only.
pub unsafe fn get_row_amortized_unchecked<'a>( &'a self, idx: usize, row: &mut Row<'a> )
rows
or object
only.sourcepub fn from_rows_and_schema(
rows: &[Row<'_>],
schema: &Schema
) -> PolarsResult<Self>
Available on crate features rows
or object
only.
pub fn from_rows_and_schema( rows: &[Row<'_>], schema: &Schema ) -> PolarsResult<Self>
rows
or object
only.sourcepub fn from_rows_iter_and_schema<'a, I>(
rows: I,
schema: &Schema
) -> PolarsResult<Self>
Available on crate features rows
or object
only.
pub fn from_rows_iter_and_schema<'a, I>( rows: I, schema: &Schema ) -> PolarsResult<Self>
rows
or object
only.sourcepub fn try_from_rows_iter_and_schema<'a, I>(
rows: I,
schema: &Schema
) -> PolarsResult<Self>
Available on crate features rows
or object
only.
pub fn try_from_rows_iter_and_schema<'a, I>( rows: I, schema: &Schema ) -> PolarsResult<Self>
rows
or object
only.source§impl DataFrame
impl DataFrame
pub fn top_k( &self, k: usize, by_column: impl IntoVec<SmartString>, sort_options: SortMultipleOptions ) -> PolarsResult<DataFrame>
source§impl DataFrame
impl DataFrame
sourcepub fn estimated_size(&self) -> usize
pub fn estimated_size(&self) -> usize
Returns an estimation of the total (heap) allocated size of the DataFrame
in bytes.
§Implementation
This estimation is the sum of the size of its buffers, validity, including nested arrays.
Multiple arrays may share buffers and bitmaps. Therefore, the size of 2 arrays is not the
sum of the sizes computed from this function. In particular, [StructArray
]’s size is an upper bound.
When an array is sliced, its allocated size remains constant because the buffer unchanged. However, this function will yield a smaller number. This is because this function returns the visible size of the buffer, not its total capacity.
FFI buffers are included in this estimation.
pub fn _apply_columns(&self, func: &dyn Fn(&Series) -> Series) -> Vec<Series>
pub fn _apply_columns_par( &self, func: &(dyn Fn(&Series) -> Series + Send + Sync) ) -> Vec<Series>
sourcepub fn new<S: IntoSeries>(columns: Vec<S>) -> PolarsResult<Self>
pub fn new<S: IntoSeries>(columns: Vec<S>) -> PolarsResult<Self>
Create a DataFrame from a Vector of Series.
§Example
let s0 = Series::new("days", [0, 1, 2].as_ref());
let s1 = Series::new("temp", [22.1, 19.9, 7.].as_ref());
let df = DataFrame::new(vec![s0, s1])?;
sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Creates an empty DataFrame
usable in a compile time context (such as static initializers).
§Example
use polars_core::prelude::DataFrame;
static EMPTY: DataFrame = DataFrame::empty();
sourcepub fn pop(&mut self) -> Option<Series>
pub fn pop(&mut self) -> Option<Series>
Removes the last Series
from the DataFrame
and returns it, or None
if it is empty.
§Example
let s1 = Series::new("Ocean", &["Atlantic", "Indian"]);
let s2 = Series::new("Area (km²)", &[106_460_000, 70_560_000]);
let mut df = DataFrame::new(vec![s1.clone(), s2.clone()])?;
assert_eq!(df.pop(), Some(s2));
assert_eq!(df.pop(), Some(s1));
assert_eq!(df.pop(), None);
assert!(df.is_empty());
sourcepub fn with_row_index(
&self,
name: &str,
offset: Option<IdxSize>
) -> PolarsResult<Self>
pub fn with_row_index( &self, name: &str, offset: Option<IdxSize> ) -> PolarsResult<Self>
Add a new column at index 0 that counts the rows.
§Example
let df1: DataFrame = df!("Name" => &["James", "Mary", "John", "Patricia"])?;
assert_eq!(df1.shape(), (4, 1));
let df2: DataFrame = df1.with_row_index("Id", None)?;
assert_eq!(df2.shape(), (4, 2));
println!("{}", df2);
Output:
shape: (4, 2)
+-----+----------+
| Id | Name |
| --- | --- |
| u32 | str |
+=====+==========+
| 0 | James |
+-----+----------+
| 1 | Mary |
+-----+----------+
| 2 | John |
+-----+----------+
| 3 | Patricia |
+-----+----------+
sourcepub fn with_row_index_mut(
&mut self,
name: &str,
offset: Option<IdxSize>
) -> &mut Self
pub fn with_row_index_mut( &mut self, name: &str, offset: Option<IdxSize> ) -> &mut Self
Add a row index column in place.
sourcepub const unsafe fn new_no_checks(columns: Vec<Series>) -> DataFrame
pub const unsafe fn new_no_checks(columns: Vec<Series>) -> DataFrame
Create a new DataFrame
but does not check the length or duplicate occurrence of the Series
.
It is advised to use DataFrame::new in favor of this method.
§Safety
It is the callers responsibility to uphold the contract of all Series
having an equal length and a unique name, if not this may panic down the line.
sourcepub unsafe fn new_no_length_checks(
columns: Vec<Series>
) -> PolarsResult<DataFrame>
pub unsafe fn new_no_length_checks( columns: Vec<Series> ) -> PolarsResult<DataFrame>
Create a new DataFrame
but does not check the length of the Series
,
only check for duplicates.
It is advised to use DataFrame::new in favor of this method.
§Safety
It is the callers responsibility to uphold the contract of all Series
having an equal length, if not this may panic down the line.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink the capacity of this DataFrame to fit its length.
sourcepub fn as_single_chunk(&mut self) -> &mut Self
pub fn as_single_chunk(&mut self) -> &mut Self
Aggregate all the chunks in the DataFrame to a single chunk.
sourcepub fn as_single_chunk_par(&mut self) -> &mut Self
pub fn as_single_chunk_par(&mut self) -> &mut Self
Aggregate all the chunks in the DataFrame to a single chunk in parallel. This may lead to more peak memory consumption.
sourcepub fn should_rechunk(&self) -> bool
pub fn should_rechunk(&self) -> bool
Returns true if the chunks of the columns do not align and re-chunking should be done
sourcepub fn align_chunks(&mut self) -> &mut Self
pub fn align_chunks(&mut self) -> &mut Self
Ensure all the chunks in the DataFrame
are aligned.
sourcepub fn schema(&self) -> Schema
pub fn schema(&self) -> Schema
Get the DataFrame
schema.
§Example
let df: DataFrame = df!("Thing" => &["Observable universe", "Human stupidity"],
"Diameter (m)" => &[8.8e26, f64::INFINITY])?;
let f1: Field = Field::new("Thing", DataType::String);
let f2: Field = Field::new("Diameter (m)", DataType::Float64);
let sc: Schema = Schema::from_iter(vec![f1, f2]);
assert_eq!(df.schema(), sc);
sourcepub fn get_columns(&self) -> &[Series]
pub fn get_columns(&self) -> &[Series]
sourcepub unsafe fn get_columns_mut(&mut self) -> &mut Vec<Series>
pub unsafe fn get_columns_mut(&mut self) -> &mut Vec<Series>
sourcepub fn iter(&self) -> Iter<'_, Series> ⓘ
pub fn iter(&self) -> Iter<'_, Series> ⓘ
Iterator over the columns as Series
.
§Example
let s1: Series = Series::new("Name", &["Pythagoras' theorem", "Shannon entropy"]);
let s2: Series = Series::new("Formula", &["a²+b²=c²", "H=-Σ[P(x)log|P(x)|]"]);
let df: DataFrame = DataFrame::new(vec![s1.clone(), s2.clone()])?;
let mut iterator = df.iter();
assert_eq!(iterator.next(), Some(&s1));
assert_eq!(iterator.next(), Some(&s2));
assert_eq!(iterator.next(), None);
sourcepub fn get_column_names(&self) -> Vec<&str>
pub fn get_column_names(&self) -> Vec<&str>
§Example
let df: DataFrame = df!("Language" => &["Rust", "Python"],
"Designer" => &["Graydon Hoare", "Guido van Rossum"])?;
assert_eq!(df.get_column_names(), &["Language", "Designer"]);
sourcepub fn get_column_names_owned(&self) -> Vec<SmartString>
pub fn get_column_names_owned(&self) -> Vec<SmartString>
Get the Vec<String>
representing the column names.
sourcepub fn set_column_names<S: AsRef<str>>(
&mut self,
names: &[S]
) -> PolarsResult<()>
pub fn set_column_names<S: AsRef<str>>( &mut self, names: &[S] ) -> PolarsResult<()>
Set the column names.
§Example
let mut df: DataFrame = df!("Mathematical set" => &["ℕ", "ℤ", "𝔻", "ℚ", "ℝ", "ℂ"])?;
df.set_column_names(&["Set"])?;
assert_eq!(df.get_column_names(), &["Set"]);
sourcepub fn fields(&self) -> Vec<Field>
pub fn fields(&self) -> Vec<Field>
Get a reference to the schema fields of the DataFrame
.
§Example
let earth: DataFrame = df!("Surface type" => &["Water", "Land"],
"Fraction" => &[0.708, 0.292])?;
let f1: Field = Field::new("Surface type", DataType::String);
let f2: Field = Field::new("Fraction", DataType::Float64);
assert_eq!(earth.fields(), &[f1, f2]);
sourcepub fn shape(&self) -> (usize, usize)
pub fn shape(&self) -> (usize, usize)
Get (height, width) of the DataFrame
.
§Example
let df0: DataFrame = DataFrame::default();
let df1: DataFrame = df!("1" => &[1, 2, 3, 4, 5])?;
let df2: DataFrame = df!("1" => &[1, 2, 3, 4, 5],
"2" => &[1, 2, 3, 4, 5])?;
assert_eq!(df0.shape(), (0 ,0));
assert_eq!(df1.shape(), (5, 1));
assert_eq!(df2.shape(), (5, 2));
sourcepub fn width(&self) -> usize
pub fn width(&self) -> usize
Get the width of the DataFrame
which is the number of columns.
§Example
let df0: DataFrame = DataFrame::default();
let df1: DataFrame = df!("Series 1" => &[0; 0])?;
let df2: DataFrame = df!("Series 1" => &[0; 0],
"Series 2" => &[0; 0])?;
assert_eq!(df0.width(), 0);
assert_eq!(df1.width(), 1);
assert_eq!(df2.width(), 2);
sourcepub fn height(&self) -> usize
pub fn height(&self) -> usize
Get the height of the DataFrame
which is the number of rows.
§Example
let df0: DataFrame = DataFrame::default();
let df1: DataFrame = df!("Currency" => &["€", "$"])?;
let df2: DataFrame = df!("Currency" => &["€", "$", "¥", "£", "₿"])?;
assert_eq!(df0.height(), 0);
assert_eq!(df1.height(), 2);
assert_eq!(df2.height(), 5);
sourcepub unsafe fn hstack_mut_unchecked(&mut self, columns: &[Series]) -> &mut Self
pub unsafe fn hstack_mut_unchecked(&mut self, columns: &[Series]) -> &mut Self
sourcepub fn hstack_mut(&mut self, columns: &[Series]) -> PolarsResult<&mut Self>
pub fn hstack_mut(&mut self, columns: &[Series]) -> PolarsResult<&mut Self>
sourcepub fn hstack(&self, columns: &[Series]) -> PolarsResult<Self>
pub fn hstack(&self, columns: &[Series]) -> PolarsResult<Self>
Add multiple Series
to a DataFrame
.
The added Series
are required to have the same length.
§Example
let df1: DataFrame = df!("Element" => &["Copper", "Silver", "Gold"])?;
let s1: Series = Series::new("Proton", &[29, 47, 79]);
let s2: Series = Series::new("Electron", &[29, 47, 79]);
let df2: DataFrame = df1.hstack(&[s1, s2])?;
assert_eq!(df2.shape(), (3, 3));
println!("{}", df2);
Output:
shape: (3, 3)
+---------+--------+----------+
| Element | Proton | Electron |
| --- | --- | --- |
| str | i32 | i32 |
+=========+========+==========+
| Copper | 29 | 29 |
+---------+--------+----------+
| Silver | 47 | 47 |
+---------+--------+----------+
| Gold | 79 | 79 |
+---------+--------+----------+
sourcepub fn vstack(&self, other: &DataFrame) -> PolarsResult<Self>
pub fn vstack(&self, other: &DataFrame) -> PolarsResult<Self>
Concatenate a DataFrame
to this DataFrame
and return as newly allocated DataFrame
.
If many vstack
operations are done, it is recommended to call DataFrame::align_chunks
.
§Example
let df1: DataFrame = df!("Element" => &["Copper", "Silver", "Gold"],
"Melting Point (K)" => &[1357.77, 1234.93, 1337.33])?;
let df2: DataFrame = df!("Element" => &["Platinum", "Palladium"],
"Melting Point (K)" => &[2041.4, 1828.05])?;
let df3: DataFrame = df1.vstack(&df2)?;
assert_eq!(df3.shape(), (5, 2));
println!("{}", df3);
Output:
shape: (5, 2)
+-----------+-------------------+
| Element | Melting Point (K) |
| --- | --- |
| str | f64 |
+===========+===================+
| Copper | 1357.77 |
+-----------+-------------------+
| Silver | 1234.93 |
+-----------+-------------------+
| Gold | 1337.33 |
+-----------+-------------------+
| Platinum | 2041.4 |
+-----------+-------------------+
| Palladium | 1828.05 |
+-----------+-------------------+
sourcepub fn vstack_mut(&mut self, other: &DataFrame) -> PolarsResult<&mut Self>
pub fn vstack_mut(&mut self, other: &DataFrame) -> PolarsResult<&mut Self>
Concatenate a DataFrame
to this DataFrame
If many vstack
operations are done, it is recommended to call DataFrame::align_chunks
.
§Example
let mut df1: DataFrame = df!("Element" => &["Copper", "Silver", "Gold"],
"Melting Point (K)" => &[1357.77, 1234.93, 1337.33])?;
let df2: DataFrame = df!("Element" => &["Platinum", "Palladium"],
"Melting Point (K)" => &[2041.4, 1828.05])?;
df1.vstack_mut(&df2)?;
assert_eq!(df1.shape(), (5, 2));
println!("{}", df1);
Output:
shape: (5, 2)
+-----------+-------------------+
| Element | Melting Point (K) |
| --- | --- |
| str | f64 |
+===========+===================+
| Copper | 1357.77 |
+-----------+-------------------+
| Silver | 1234.93 |
+-----------+-------------------+
| Gold | 1337.33 |
+-----------+-------------------+
| Platinum | 2041.4 |
+-----------+-------------------+
| Palladium | 1828.05 |
+-----------+-------------------+
sourcepub fn extend(&mut self, other: &DataFrame) -> PolarsResult<()>
pub fn extend(&mut self, other: &DataFrame) -> PolarsResult<()>
Extend the memory backed by this DataFrame
with the values from other
.
Different from vstack
which adds the chunks from other
to the chunks of this DataFrame
extend
appends the data from other
to the underlying memory locations and thus may cause a reallocation.
If this does not cause a reallocation, the resulting data structure will not have any extra chunks and thus will yield faster queries.
Prefer extend
over vstack
when you want to do a query after a single append. For instance during
online operations where you add n
rows and rerun a query.
Prefer vstack
over extend
when you want to append many times before doing a query. For instance
when you read in multiple files and when to store them in a single DataFrame
. In the latter case, finish the sequence
of append
operations with a rechunk
.
sourcepub fn drop_in_place(&mut self, name: &str) -> PolarsResult<Series>
pub fn drop_in_place(&mut self, name: &str) -> PolarsResult<Series>
Remove a column by name and return the column removed.
§Example
let mut df: DataFrame = df!("Animal" => &["Tiger", "Lion", "Great auk"],
"IUCN" => &["Endangered", "Vulnerable", "Extinct"])?;
let s1: PolarsResult<Series> = df.drop_in_place("Average weight");
assert!(s1.is_err());
let s2: Series = df.drop_in_place("Animal")?;
assert_eq!(s2, Series::new("Animal", &["Tiger", "Lion", "Great auk"]));
sourcepub fn drop_nulls<S: AsRef<str>>(
&self,
subset: Option<&[S]>
) -> PolarsResult<Self>
pub fn drop_nulls<S: AsRef<str>>( &self, subset: Option<&[S]> ) -> PolarsResult<Self>
Return a new DataFrame
where all null values are dropped.
§Example
let df1: DataFrame = df!("Country" => ["Malta", "Liechtenstein", "North Korea"],
"Tax revenue (% GDP)" => [Some(32.7), None, None])?;
assert_eq!(df1.shape(), (3, 2));
let df2: DataFrame = df1.drop_nulls::<String>(None)?;
assert_eq!(df2.shape(), (1, 2));
println!("{}", df2);
Output:
shape: (1, 2)
+---------+---------------------+
| Country | Tax revenue (% GDP) |
| --- | --- |
| str | f64 |
+=========+=====================+
| Malta | 32.7 |
+---------+---------------------+
sourcepub fn drop(&self, name: &str) -> PolarsResult<Self>
pub fn drop(&self, name: &str) -> PolarsResult<Self>
sourcepub fn drop_many_amortized(&self, names: &PlHashSet<&str>) -> DataFrame
pub fn drop_many_amortized(&self, names: &PlHashSet<&str>) -> DataFrame
Drop columns that are in names
without allocating a HashSet
.
sourcepub fn insert_column<S: IntoSeries>(
&mut self,
index: usize,
column: S
) -> PolarsResult<&mut Self>
pub fn insert_column<S: IntoSeries>( &mut self, index: usize, column: S ) -> PolarsResult<&mut Self>
Insert a new column at a given index.
sourcepub fn with_column<S: IntoSeries>(
&mut self,
column: S
) -> PolarsResult<&mut Self>
pub fn with_column<S: IntoSeries>( &mut self, column: S ) -> PolarsResult<&mut Self>
Add a new column to this DataFrame
or replace an existing one.
sourcepub unsafe fn with_column_unchecked(&mut self, column: Series) -> &mut Self
pub unsafe fn with_column_unchecked(&mut self, column: Series) -> &mut Self
pub fn _add_columns( &mut self, columns: Vec<Series>, schema: &Schema ) -> PolarsResult<()>
sourcepub fn with_column_and_schema<S: IntoSeries>(
&mut self,
column: S,
schema: &Schema
) -> PolarsResult<&mut Self>
pub fn with_column_and_schema<S: IntoSeries>( &mut self, column: S, schema: &Schema ) -> PolarsResult<&mut Self>
Add a new column to this DataFrame
or replace an existing one.
Uses an existing schema to amortize lookups.
If the schema is incorrect, we will fallback to linear search.
sourcepub fn select_at_idx(&self, idx: usize) -> Option<&Series>
pub fn select_at_idx(&self, idx: usize) -> Option<&Series>
Select a Series
by index.
§Example
let df: DataFrame = df!("Star" => &["Sun", "Betelgeuse", "Sirius A", "Sirius B"],
"Absolute magnitude" => &[4.83, -5.85, 1.42, 11.18])?;
let s1: Option<&Series> = df.select_at_idx(0);
let s2: Series = Series::new("Star", &["Sun", "Betelgeuse", "Sirius A", "Sirius B"]);
assert_eq!(s1, Some(&s2));
sourcepub fn select_by_range<R>(&self, range: R) -> PolarsResult<Self>where
R: RangeBounds<usize>,
pub fn select_by_range<R>(&self, range: R) -> PolarsResult<Self>where
R: RangeBounds<usize>,
sourcepub fn get_column_index(&self, name: &str) -> Option<usize>
pub fn get_column_index(&self, name: &str) -> Option<usize>
Get column index of a Series
by name.
§Example
let df: DataFrame = df!("Name" => &["Player 1", "Player 2", "Player 3"],
"Health" => &[100, 200, 500],
"Mana" => &[250, 100, 0],
"Strength" => &[30, 150, 300])?;
assert_eq!(df.get_column_index("Name"), Some(0));
assert_eq!(df.get_column_index("Health"), Some(1));
assert_eq!(df.get_column_index("Mana"), Some(2));
assert_eq!(df.get_column_index("Strength"), Some(3));
assert_eq!(df.get_column_index("Haste"), None);
sourcepub fn try_get_column_index(&self, name: &str) -> PolarsResult<usize>
pub fn try_get_column_index(&self, name: &str) -> PolarsResult<usize>
Get column index of a Series
by name.
sourcepub fn column(&self, name: &str) -> PolarsResult<&Series>
pub fn column(&self, name: &str) -> PolarsResult<&Series>
Select a single column by name.
§Example
let s1: Series = Series::new("Password", &["123456", "[]B$u$g$s$B#u#n#n#y[]{}"]);
let s2: Series = Series::new("Robustness", &["Weak", "Strong"]);
let df: DataFrame = DataFrame::new(vec![s1.clone(), s2])?;
assert_eq!(df.column("Password")?, &s1);
sourcepub fn columns<I, S>(&self, names: I) -> PolarsResult<Vec<&Series>>
pub fn columns<I, S>(&self, names: I) -> PolarsResult<Vec<&Series>>
Selected multiple columns by name.
§Example
let df: DataFrame = df!("Latin name" => &["Oncorhynchus kisutch", "Salmo salar"],
"Max weight (kg)" => &[16.0, 35.89])?;
let sv: Vec<&Series> = df.columns(&["Latin name", "Max weight (kg)"])?;
assert_eq!(&df[0], sv[0]);
assert_eq!(&df[1], sv[1]);
sourcepub fn select<I, S>(&self, selection: I) -> PolarsResult<Self>
pub fn select<I, S>(&self, selection: I) -> PolarsResult<Self>
pub fn _select_impl(&self, cols: &[SmartString]) -> PolarsResult<Self>
pub fn _select_impl_unchecked(&self, cols: &[SmartString]) -> PolarsResult<Self>
sourcepub fn select_with_schema<I, S>(
&self,
selection: I,
schema: &SchemaRef
) -> PolarsResult<Self>
pub fn select_with_schema<I, S>( &self, selection: I, schema: &SchemaRef ) -> PolarsResult<Self>
Select with a known schema.
sourcepub fn select_with_schema_unchecked<I, S>(
&self,
selection: I,
schema: &Schema
) -> PolarsResult<Self>
pub fn select_with_schema_unchecked<I, S>( &self, selection: I, schema: &Schema ) -> PolarsResult<Self>
Select with a known schema. This doesn’t check for duplicates.
pub fn select_physical<I, S>(&self, selection: I) -> PolarsResult<Self>
sourcepub fn select_series(
&self,
selection: impl IntoVec<SmartString>
) -> PolarsResult<Vec<Series>>
pub fn select_series( &self, selection: impl IntoVec<SmartString> ) -> PolarsResult<Vec<Series>>
Select column(s) from this DataFrame
and return them into a Vec
.
§Example
let df: DataFrame = df!("Name" => &["Methane", "Ethane", "Propane"],
"Carbon" => &[1, 2, 3],
"Hydrogen" => &[4, 6, 8])?;
let sv: Vec<Series> = df.select_series(&["Carbon", "Hydrogen"])?;
assert_eq!(df["Carbon"], sv[0]);
assert_eq!(df["Hydrogen"], sv[1]);
sourcepub fn filter(&self, mask: &BooleanChunked) -> PolarsResult<Self>
pub fn filter(&self, mask: &BooleanChunked) -> PolarsResult<Self>
sourcepub fn _filter_seq(&self, mask: &BooleanChunked) -> PolarsResult<Self>
pub fn _filter_seq(&self, mask: &BooleanChunked) -> PolarsResult<Self>
Same as filter
but does not parallelize.
sourcepub fn take(&self, indices: &IdxCa) -> PolarsResult<Self>
pub fn take(&self, indices: &IdxCa) -> PolarsResult<Self>
sourcepub unsafe fn take_unchecked(&self, idx: &IdxCa) -> Self
pub unsafe fn take_unchecked(&self, idx: &IdxCa) -> Self
§Safety
The indices must be in-bounds.
sourcepub unsafe fn take_unchecked_impl(
&self,
idx: &IdxCa,
allow_threads: bool
) -> Self
pub unsafe fn take_unchecked_impl( &self, idx: &IdxCa, allow_threads: bool ) -> Self
§Safety
The indices must be in-bounds.
sourcepub fn sort_in_place(
&mut self,
by: impl IntoVec<SmartString>,
sort_options: SortMultipleOptions
) -> PolarsResult<&mut Self>
pub fn sort_in_place( &mut self, by: impl IntoVec<SmartString>, sort_options: SortMultipleOptions ) -> PolarsResult<&mut Self>
Sort DataFrame
in place.
See DataFrame::sort
for more instruction.
sourcepub fn sort(
&self,
by: impl IntoVec<SmartString>,
sort_options: SortMultipleOptions
) -> PolarsResult<Self>
pub fn sort( &self, by: impl IntoVec<SmartString>, sort_options: SortMultipleOptions ) -> PolarsResult<Self>
Return a sorted clone of this DataFrame
.
§Example
Sort by a single column with default options:
fn sort_by_sepal_width(df: &DataFrame) -> PolarsResult<DataFrame> {
df.sort(["sepal_width"], Default::default())
}
Sort by a single column with specific order:
fn sort_with_specific_order(df: &DataFrame, descending: bool) -> PolarsResult<DataFrame> {
df.sort(
["sepal_width"],
SortMultipleOptions::new()
.with_order_descending(descending)
)
}
Sort by multiple columns with specifying order for each column:
fn sort_by_multiple_columns_with_specific_order(df: &DataFrame) -> PolarsResult<DataFrame> {
df.sort(
&["sepal_width", "sepal_length"],
SortMultipleOptions::new()
.with_order_descending_multi([false, true])
)
}
See SortMultipleOptions
for more options.
Also see DataFrame::sort_in_place
.
sourcepub fn replace<S: IntoSeries>(
&mut self,
column: &str,
new_col: S
) -> PolarsResult<&mut Self>
pub fn replace<S: IntoSeries>( &mut self, column: &str, new_col: S ) -> PolarsResult<&mut Self>
Replace a column with a Series
.
§Example
let mut df: DataFrame = df!("Country" => &["United States", "China"],
"Area (km²)" => &[9_833_520, 9_596_961])?;
let s: Series = Series::new("Country", &["USA", "PRC"]);
assert!(df.replace("Nation", s.clone()).is_err());
assert!(df.replace("Country", s).is_ok());
sourcepub fn replace_or_add<S: IntoSeries>(
&mut self,
column: &str,
new_col: S
) -> PolarsResult<&mut Self>
pub fn replace_or_add<S: IntoSeries>( &mut self, column: &str, new_col: S ) -> PolarsResult<&mut Self>
Replace or update a column. The difference between this method and DataFrame::with_column
is that now the value of column: &str
determines the name of the column and not the name
of the Series
passed to this method.
sourcepub fn replace_column<S: IntoSeries>(
&mut self,
index: usize,
new_column: S
) -> PolarsResult<&mut Self>
pub fn replace_column<S: IntoSeries>( &mut self, index: usize, new_column: S ) -> PolarsResult<&mut Self>
Replace column at index idx
with a Series
.
§Example
# use polars_core::prelude::*;
let s0 = Series::new("foo", &["ham", "spam", "egg"]);
let s1 = Series::new("ascii", &[70, 79, 79]);
let mut df = DataFrame::new(vec![s0, s1])?;
// Add 32 to get lowercase ascii values
df.replace_column(1, df.select_at_idx(1).unwrap() + 32);
# Ok::<(), PolarsError>(())
sourcepub fn apply<F, S>(&mut self, name: &str, f: F) -> PolarsResult<&mut Self>
pub fn apply<F, S>(&mut self, name: &str, f: F) -> PolarsResult<&mut Self>
Apply a closure to a column. This is the recommended way to do in place modification.
§Example
let s0 = Series::new("foo", &["ham", "spam", "egg"]);
let s1 = Series::new("names", &["Jean", "Claude", "van"]);
let mut df = DataFrame::new(vec![s0, s1])?;
fn str_to_len(str_val: &Series) -> Series {
str_val.str()
.unwrap()
.into_iter()
.map(|opt_name: Option<&str>| {
opt_name.map(|name: &str| name.len() as u32)
})
.collect::<UInt32Chunked>()
.into_series()
}
// Replace the names column by the length of the names.
df.apply("names", str_to_len);
Results in:
+--------+-------+
| foo | |
| --- | names |
| str | u32 |
+========+=======+
| "ham" | 4 |
+--------+-------+
| "spam" | 6 |
+--------+-------+
| "egg" | 3 |
+--------+-------+
sourcepub fn apply_at_idx<F, S>(
&mut self,
idx: usize,
f: F
) -> PolarsResult<&mut Self>
pub fn apply_at_idx<F, S>( &mut self, idx: usize, f: F ) -> PolarsResult<&mut Self>
Apply a closure to a column at index idx
. This is the recommended way to do in place
modification.
§Example
let s0 = Series::new("foo", &["ham", "spam", "egg"]);
let s1 = Series::new("ascii", &[70, 79, 79]);
let mut df = DataFrame::new(vec![s0, s1])?;
// Add 32 to get lowercase ascii values
df.apply_at_idx(1, |s| s + 32);
Results in:
+--------+-------+
| foo | ascii |
| --- | --- |
| str | i32 |
+========+=======+
| "ham" | 102 |
+--------+-------+
| "spam" | 111 |
+--------+-------+
| "egg" | 111 |
+--------+-------+
sourcepub fn try_apply_at_idx<F, S>(
&mut self,
idx: usize,
f: F
) -> PolarsResult<&mut Self>
pub fn try_apply_at_idx<F, S>( &mut self, idx: usize, f: F ) -> PolarsResult<&mut Self>
Apply a closure that may fail to a column at index idx
. This is the recommended way to do in place
modification.
§Example
This is the idiomatic way to replace some values a column of a DataFrame
given range of indexes.
let s0 = Series::new("foo", &["ham", "spam", "egg", "bacon", "quack"]);
let s1 = Series::new("values", &[1, 2, 3, 4, 5]);
let mut df = DataFrame::new(vec![s0, s1])?;
let idx = vec![0, 1, 4];
df.try_apply("foo", |s| {
s.str()?
.scatter_with(idx, |opt_val| opt_val.map(|string| format!("{}-is-modified", string)))
});
Results in:
+---------------------+--------+
| foo | values |
| --- | --- |
| str | i32 |
+=====================+========+
| "ham-is-modified" | 1 |
+---------------------+--------+
| "spam-is-modified" | 2 |
+---------------------+--------+
| "egg" | 3 |
+---------------------+--------+
| "bacon" | 4 |
+---------------------+--------+
| "quack-is-modified" | 5 |
+---------------------+--------+
sourcepub fn try_apply<F, S>(&mut self, column: &str, f: F) -> PolarsResult<&mut Self>
pub fn try_apply<F, S>(&mut self, column: &str, f: F) -> PolarsResult<&mut Self>
Apply a closure that may fail to a column. This is the recommended way to do in place modification.
§Example
This is the idiomatic way to replace some values a column of a DataFrame
given a boolean mask.
let s0 = Series::new("foo", &["ham", "spam", "egg", "bacon", "quack"]);
let s1 = Series::new("values", &[1, 2, 3, 4, 5]);
let mut df = DataFrame::new(vec![s0, s1])?;
// create a mask
let values = df.column("values")?;
let mask = values.lt_eq(1)? | values.gt_eq(5_i32)?;
df.try_apply("foo", |s| {
s.str()?
.set(&mask, Some("not_within_bounds"))
});
Results in:
+---------------------+--------+
| foo | values |
| --- | --- |
| str | i32 |
+=====================+========+
| "not_within_bounds" | 1 |
+---------------------+--------+
| "spam" | 2 |
+---------------------+--------+
| "egg" | 3 |
+---------------------+--------+
| "bacon" | 4 |
+---------------------+--------+
| "not_within_bounds" | 5 |
+---------------------+--------+
sourcepub fn slice(&self, offset: i64, length: usize) -> Self
pub fn slice(&self, offset: i64, length: usize) -> Self
Slice the DataFrame
along the rows.
§Example
let df: DataFrame = df!("Fruit" => &["Apple", "Grape", "Grape", "Fig", "Fig"],
"Color" => &["Green", "Red", "White", "White", "Red"])?;
let sl: DataFrame = df.slice(2, 3);
assert_eq!(sl.shape(), (3, 2));
println!("{}", sl);
Output:
shape: (3, 2)
+-------+-------+
| Fruit | Color |
| --- | --- |
| str | str |
+=======+=======+
| Grape | White |
+-------+-------+
| Fig | White |
+-------+-------+
| Fig | Red |
+-------+-------+
pub fn clear(&self) -> Self
pub fn slice_par(&self, offset: i64, length: usize) -> Self
pub fn _slice_and_realloc(&self, offset: i64, length: usize) -> Self
sourcepub fn head(&self, length: Option<usize>) -> Self
pub fn head(&self, length: Option<usize>) -> Self
Get the head of the DataFrame
.
§Example
let countries: DataFrame =
df!("Rank by GDP (2021)" => &[1, 2, 3, 4, 5],
"Continent" => &["North America", "Asia", "Asia", "Europe", "Europe"],
"Country" => &["United States", "China", "Japan", "Germany", "United Kingdom"],
"Capital" => &["Washington", "Beijing", "Tokyo", "Berlin", "London"])?;
assert_eq!(countries.shape(), (5, 4));
println!("{}", countries.head(Some(3)));
Output:
shape: (3, 4)
+--------------------+---------------+---------------+------------+
| Rank by GDP (2021) | Continent | Country | Capital |
| --- | --- | --- | --- |
| i32 | str | str | str |
+====================+===============+===============+============+
| 1 | North America | United States | Washington |
+--------------------+---------------+---------------+------------+
| 2 | Asia | China | Beijing |
+--------------------+---------------+---------------+------------+
| 3 | Asia | Japan | Tokyo |
+--------------------+---------------+---------------+------------+
sourcepub fn tail(&self, length: Option<usize>) -> Self
pub fn tail(&self, length: Option<usize>) -> Self
Get the tail of the DataFrame
.
§Example
let countries: DataFrame =
df!("Rank (2021)" => &[105, 106, 107, 108, 109],
"Apple Price (€/kg)" => &[0.75, 0.70, 0.70, 0.65, 0.52],
"Country" => &["Kosovo", "Moldova", "North Macedonia", "Syria", "Turkey"])?;
assert_eq!(countries.shape(), (5, 3));
println!("{}", countries.tail(Some(2)));
Output:
shape: (2, 3)
+-------------+--------------------+---------+
| Rank (2021) | Apple Price (€/kg) | Country |
| --- | --- | --- |
| i32 | f64 | str |
+=============+====================+=========+
| 108 | 0.63 | Syria |
+-------------+--------------------+---------+
| 109 | 0.63 | Turkey |
+-------------+--------------------+---------+
sourcepub fn iter_chunks(&self, pl_flavor: bool) -> RecordBatchIter<'_> ⓘ
pub fn iter_chunks(&self, pl_flavor: bool) -> RecordBatchIter<'_> ⓘ
Iterator over the rows in this DataFrame
as Arrow RecordBatches.
§Panics
Panics if the DataFrame
that is passed is not rechunked.
This responsibility is left to the caller as we don’t want to take mutable references here, but we also don’t want to rechunk here, as this operation is costly and would benefit the caller as well.
sourcepub fn iter_chunks_physical(&self) -> PhysRecordBatchIter<'_> ⓘ
pub fn iter_chunks_physical(&self) -> PhysRecordBatchIter<'_> ⓘ
Iterator over the rows in this DataFrame
as Arrow RecordBatches as physical values.
§Panics
Panics if the DataFrame
that is passed is not rechunked.
This responsibility is left to the caller as we don’t want to take mutable references here, but we also don’t want to rechunk here, as this operation is costly and would benefit the caller as well.
sourcepub fn shift(&self, periods: i64) -> Self
pub fn shift(&self, periods: i64) -> Self
Shift the values by a given period and fill the parts that will be empty due to this operation
with Nones
.
See the method on Series for more info on the shift
operation.
sourcepub fn fill_null(&self, strategy: FillNullStrategy) -> PolarsResult<Self>
pub fn fill_null(&self, strategy: FillNullStrategy) -> PolarsResult<Self>
Replace None values with one of the following strategies:
- Forward fill (replace None with the previous value)
- Backward fill (replace None with the next value)
- Mean fill (replace None with the mean of the whole array)
- Min fill (replace None with the minimum of the whole array)
- Max fill (replace None with the maximum of the whole array)
See the method on Series for more info on the fill_null
operation.
sourcepub fn min_horizontal(&self) -> PolarsResult<Option<Series>>
Available on crate feature zip_with
only.
pub fn min_horizontal(&self) -> PolarsResult<Option<Series>>
zip_with
only.Aggregate the column horizontally to their min values.
sourcepub fn max_horizontal(&self) -> PolarsResult<Option<Series>>
Available on crate feature zip_with
only.
pub fn max_horizontal(&self) -> PolarsResult<Option<Series>>
zip_with
only.Aggregate the column horizontally to their max values.
sourcepub fn sum_horizontal(
&self,
null_strategy: NullStrategy
) -> PolarsResult<Option<Series>>
pub fn sum_horizontal( &self, null_strategy: NullStrategy ) -> PolarsResult<Option<Series>>
Sum all values horizontally across columns.
sourcepub fn mean_horizontal(
&self,
null_strategy: NullStrategy
) -> PolarsResult<Option<Series>>
pub fn mean_horizontal( &self, null_strategy: NullStrategy ) -> PolarsResult<Option<Series>>
Compute the mean of all values horizontally across columns.
sourcepub fn pipe<F, B>(self, f: F) -> PolarsResult<B>
pub fn pipe<F, B>(self, f: F) -> PolarsResult<B>
Pipe different functions/ closure operations that work on a DataFrame together.
sourcepub fn pipe_mut<F, B>(&mut self, f: F) -> PolarsResult<B>
pub fn pipe_mut<F, B>(&mut self, f: F) -> PolarsResult<B>
Pipe different functions/ closure operations that work on a DataFrame together.
sourcepub fn pipe_with_args<F, B, Args>(self, f: F, args: Args) -> PolarsResult<B>
pub fn pipe_with_args<F, B, Args>(self, f: F, args: Args) -> PolarsResult<B>
Pipe different functions/ closure operations that work on a DataFrame together.
sourcepub fn unique_stable(
&self,
subset: Option<&[String]>,
keep: UniqueKeepStrategy,
slice: Option<(i64, usize)>
) -> PolarsResult<DataFrame>
Available on crate feature algorithm_group_by
only.
pub fn unique_stable( &self, subset: Option<&[String]>, keep: UniqueKeepStrategy, slice: Option<(i64, usize)> ) -> PolarsResult<DataFrame>
algorithm_group_by
only.Drop duplicate rows from a DataFrame
.
This fails when there is a column of type List in DataFrame
Stable means that the order is maintained. This has a higher cost than an unstable distinct.
§Example
let df = df! {
"flt" => [1., 1., 2., 2., 3., 3.],
"int" => [1, 1, 2, 2, 3, 3, ],
"str" => ["a", "a", "b", "b", "c", "c"]
}?;
println!("{}", df.unique_stable(None, UniqueKeepStrategy::First, None)?);
Returns
+-----+-----+-----+
| flt | int | str |
| --- | --- | --- |
| f64 | i32 | str |
+=====+=====+=====+
| 1 | 1 | "a" |
+-----+-----+-----+
| 2 | 2 | "b" |
+-----+-----+-----+
| 3 | 3 | "c" |
+-----+-----+-----+
sourcepub fn unique(
&self,
subset: Option<&[String]>,
keep: UniqueKeepStrategy,
slice: Option<(i64, usize)>
) -> PolarsResult<DataFrame>
Available on crate feature algorithm_group_by
only.
pub fn unique( &self, subset: Option<&[String]>, keep: UniqueKeepStrategy, slice: Option<(i64, usize)> ) -> PolarsResult<DataFrame>
algorithm_group_by
only.Unstable distinct. See DataFrame::unique_stable
.
pub fn unique_impl( &self, maintain_order: bool, subset: Option<&[String]>, keep: UniqueKeepStrategy, slice: Option<(i64, usize)> ) -> PolarsResult<Self>
algorithm_group_by
only.sourcepub fn is_unique(&self) -> PolarsResult<BooleanChunked>
Available on crate feature algorithm_group_by
only.
pub fn is_unique(&self) -> PolarsResult<BooleanChunked>
algorithm_group_by
only.sourcepub fn is_duplicated(&self) -> PolarsResult<BooleanChunked>
Available on crate feature algorithm_group_by
only.
pub fn is_duplicated(&self) -> PolarsResult<BooleanChunked>
algorithm_group_by
only.sourcepub fn null_count(&self) -> Self
pub fn null_count(&self) -> Self
Create a new DataFrame
that shows the null counts per column.
sourcepub fn hash_rows(
&mut self,
hasher_builder: Option<RandomState>
) -> PolarsResult<UInt64Chunked>
Available on crate feature row_hash
only.
pub fn hash_rows( &mut self, hasher_builder: Option<RandomState> ) -> PolarsResult<UInt64Chunked>
row_hash
only.Hash and combine the row values
sourcepub fn get_supertype(&self) -> Option<PolarsResult<DataType>>
pub fn get_supertype(&self) -> Option<PolarsResult<DataType>>
Get the supertype of the columns in this DataFrame
sourcepub fn partition_by(
&self,
cols: impl IntoVec<String>,
include_key: bool
) -> PolarsResult<Vec<DataFrame>>
Available on crate feature partition_by
only.
pub fn partition_by( &self, cols: impl IntoVec<String>, include_key: bool ) -> PolarsResult<Vec<DataFrame>>
partition_by
only.Split into multiple DataFrames partitioned by groups
sourcepub fn partition_by_stable(
&self,
cols: impl IntoVec<String>,
include_key: bool
) -> PolarsResult<Vec<DataFrame>>
Available on crate feature partition_by
only.
pub fn partition_by_stable( &self, cols: impl IntoVec<String>, include_key: bool ) -> PolarsResult<Vec<DataFrame>>
partition_by
only.Split into multiple DataFrames partitioned by groups Order of the groups are maintained.
source§impl DataFrame
impl DataFrame
sourcepub fn schema_equal(&self, other: &DataFrame) -> PolarsResult<()>
pub fn schema_equal(&self, other: &DataFrame) -> PolarsResult<()>
Check if DataFrame
’ schemas are equal.
sourcepub fn equals(&self, other: &DataFrame) -> bool
pub fn equals(&self, other: &DataFrame) -> bool
Check if DataFrame
s are equal. Note that None == None
evaluates to false
§Example
let df1: DataFrame = df!("Atomic number" => &[1, 51, 300],
"Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
let df2: DataFrame = df!("Atomic number" => &[1, 51, 300],
"Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
assert!(!df1.equals(&df2));
sourcepub fn equals_missing(&self, other: &DataFrame) -> bool
pub fn equals_missing(&self, other: &DataFrame) -> bool
Check if all values in DataFrame
s are equal where None == None
evaluates to true
.
§Example
let df1: DataFrame = df!("Atomic number" => &[1, 51, 300],
"Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
let df2: DataFrame = df!("Atomic number" => &[1, 51, 300],
"Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
assert!(df1.equals_missing(&df2));
Trait Implementations§
source§impl From<&ArrowSchema> for DataFrame
impl From<&ArrowSchema> for DataFrame
source§fn from(schema: &ArrowSchema) -> Self
fn from(schema: &ArrowSchema) -> Self
source§impl FromIterator<Series> for DataFrame
impl FromIterator<Series> for DataFrame
source§impl PartialEq for DataFrame
impl PartialEq for DataFrame
source§impl TryFrom<(RecordBatchT<Box<dyn Array>>, &[Field])> for DataFrame
impl TryFrom<(RecordBatchT<Box<dyn Array>>, &[Field])> for DataFrame
§type Error = PolarsError
type Error = PolarsError
source§fn try_from(arg: (RecordBatch, &[ArrowField])) -> PolarsResult<DataFrame>
fn try_from(arg: (RecordBatch, &[ArrowField])) -> PolarsResult<DataFrame>
Auto Trait Implementations§
impl Freeze for DataFrame
impl !RefUnwindSafe for DataFrame
impl Send for DataFrame
impl Sync for DataFrame
impl Unpin for DataFrame
impl !UnwindSafe for DataFrame
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<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