pub struct DataFrame {
pub columns: Vec<Box<dyn SeriesTrait>>,
/* private fields */
}
Expand description
高性能数据处理框架的核心数据结构 DataFrame。
DataFrame 是一个二维表格数据结构,类似于电子表格或数据库表, 由多个具有相同长度的列(Series)组成。每列可以包含不同类型的数据。
§特性
- 类型安全: 使用 Rust 的类型系统确保数据类型安全
- 高性能: 利用 Rayon 实现并行处理
- 内存高效: 零拷贝操作和智能内存管理
- 丰富的操作: 支持过滤、连接、分组、排序等操作
§示例
use axion::dataframe::DataFrame;
use axion::series::Series;
// 创建一个简单的 DataFrame
let name_series = Series::new("姓名".to_string(), vec!["张三", "李四", "王五"]);
let age_series = Series::new("年龄".to_string(), vec![25, 30, 35]);
let df = DataFrame::new(vec![
Box::new(name_series),
Box::new(age_series),
])?;
println!("{}", df);
Fields§
§columns: Vec<Box<dyn SeriesTrait>>
存储所有列的向量,每列都是一个实现了 SeriesTrait 的对象
Implementations§
Source§impl DataFrame
impl DataFrame
Sourcepub fn new(columns: Vec<Box<dyn SeriesTrait>>) -> AxionResult<Self>
pub fn new(columns: Vec<Box<dyn SeriesTrait>>) -> AxionResult<Self>
从列向量创建新的 DataFrame。
§参数
columns
- 实现了SeriesTrait
的列向量
§返回值
成功时返回新创建的 DataFrame,失败时返回错误
§错误
AxionError::MismatchedLengths
- 当列长度不一致时AxionError::DuplicateColumnName
- 当存在重复列名时
§示例
let columns = vec![
Box::new(Series::new("A".to_string(), vec![1, 2, 3])),
Box::new(Series::new("B".to_string(), vec![4, 5, 6])),
];
let df = DataFrame::new(columns)?;
Sourcepub fn columns_names(&self) -> Vec<&str>
pub fn columns_names(&self) -> Vec<&str>
Sourcepub fn column(&self, name: &str) -> AxionResult<&dyn SeriesTrait>
pub fn column(&self, name: &str) -> AxionResult<&dyn SeriesTrait>
Sourcepub fn column_mut<'a>(
&'a mut self,
name: &str,
) -> AxionResult<&'a mut dyn SeriesTrait>
pub fn column_mut<'a>( &'a mut self, name: &str, ) -> AxionResult<&'a mut dyn SeriesTrait>
根据列名获取列的可变引用。
Sourcepub fn column_at(&self, index: usize) -> AxionResult<&dyn SeriesTrait>
pub fn column_at(&self, index: usize) -> AxionResult<&dyn SeriesTrait>
Sourcepub fn column_at_mut(
&mut self,
index: usize,
) -> AxionResult<&mut dyn SeriesTrait>
pub fn column_at_mut( &mut self, index: usize, ) -> AxionResult<&mut dyn SeriesTrait>
根据索引获取列的可变引用。
Sourcepub fn add_column(&mut self, series: Box<dyn SeriesTrait>) -> AxionResult<()>
pub fn add_column(&mut self, series: Box<dyn SeriesTrait>) -> AxionResult<()>
Sourcepub fn drop_column(&mut self, name: &str) -> AxionResult<Box<dyn SeriesTrait>>
pub fn drop_column(&mut self, name: &str) -> AxionResult<Box<dyn SeriesTrait>>
Sourcepub fn rename_column(
&mut self,
old_name: &str,
new_name: &str,
) -> AxionResult<()>
pub fn rename_column( &mut self, old_name: &str, new_name: &str, ) -> AxionResult<()>
Sourcepub fn downcast_column<T>(&self, name: &str) -> AxionResult<&Series<T>>where
T: DataTypeTrait + 'static,
Series<T>: 'static,
pub fn downcast_column<T>(&self, name: &str) -> AxionResult<&Series<T>>where
T: DataTypeTrait + 'static,
Series<T>: 'static,
Sourcepub fn select(&self, names: &[&str]) -> AxionResult<DataFrame>
pub fn select(&self, names: &[&str]) -> AxionResult<DataFrame>
Sourcepub fn drop(&self, name_to_drop: &str) -> AxionResult<DataFrame>
pub fn drop(&self, name_to_drop: &str) -> AxionResult<DataFrame>
Sourcepub fn par_filter(&self, mask: &Series<bool>) -> AxionResult<DataFrame>
pub fn par_filter(&self, mask: &Series<bool>) -> AxionResult<DataFrame>
Sourcepub fn inner_join(
&self,
right: &DataFrame,
left_on: &str,
right_on: &str,
) -> AxionResult<DataFrame>
pub fn inner_join( &self, right: &DataFrame, left_on: &str, right_on: &str, ) -> AxionResult<DataFrame>
Sourcepub fn left_join(
&self,
right: &DataFrame,
left_on: &str,
right_on: &str,
) -> AxionResult<DataFrame>
pub fn left_join( &self, right: &DataFrame, left_on: &str, right_on: &str, ) -> AxionResult<DataFrame>
左连接操作。
保留左侧 DataFrame 的所有行,如果右侧没有匹配则填充空值。
Sourcepub fn right_join(
&self,
right: &DataFrame,
left_on: &str,
right_on: &str,
) -> AxionResult<DataFrame>
pub fn right_join( &self, right: &DataFrame, left_on: &str, right_on: &str, ) -> AxionResult<DataFrame>
右连接操作。
保留右侧 DataFrame 的所有行,如果左侧没有匹配则填充空值。
Sourcepub fn outer_join(
&self,
right: &DataFrame,
left_on: &str,
right_on: &str,
) -> AxionResult<DataFrame>
pub fn outer_join( &self, right: &DataFrame, left_on: &str, right_on: &str, ) -> AxionResult<DataFrame>
外连接操作。
保留两个 DataFrame 的所有行,没有匹配的地方填充空值。
Sourcepub fn groupby<'a>(&'a self, keys: &[&str]) -> AxionResult<GroupBy<'a>>
pub fn groupby<'a>(&'a self, keys: &[&str]) -> AxionResult<GroupBy<'a>>
Sourcepub fn to_csv(
&self,
filepath: impl AsRef<Path>,
options: Option<WriteCsvOptions>,
) -> AxionResult<()>
pub fn to_csv( &self, filepath: impl AsRef<Path>, options: Option<WriteCsvOptions>, ) -> AxionResult<()>
将 DataFrame 导出为 CSV 文件。
§参数
filepath
- 输出文件路径options
- 可选的 CSV 写入配置
§错误
AxionError::IoError
- 文件创建或写入失败
§示例
use axion::io::csv::WriteCsvOptions;
// 使用默认配置导出
df.to_csv("output.csv", None)?;
// 使用自定义配置导出
let options = WriteCsvOptions {
has_header: true,
delimiter: b';',
..Default::default()
};
df.to_csv("output.csv", Some(options))?;
Sourcepub fn to_csv_writer<W: Write>(
&self,
writer: &mut W,
options: Option<WriteCsvOptions>,
) -> AxionResult<()>
pub fn to_csv_writer<W: Write>( &self, writer: &mut W, options: Option<WriteCsvOptions>, ) -> AxionResult<()>
将 DataFrame 写入到实现了 Write trait 的写入器中。
这是核心的 CSV 写入逻辑。
§参数
writer
- 实现了std::io::Write
的写入器options
- 可选的 CSV 写入配置
Trait Implementations§
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Converts
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>
Converts
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