pandrs 0.3.0

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# PandRS API Guide

This guide provides comprehensive documentation for using PandRS effectively, including API patterns and best practices.

## Table of Contents

1. [Core Concepts]#core-concepts
2. [DataFrame Types]#dataframe-types
3. [Series Operations]#series-operations
4. [Column Management]#column-management
5. [Data Access Patterns]#data-access-patterns
6. [I/O Operations]#io-operations
7. [Error Handling]#error-handling
8. [Performance Considerations]#performance-considerations
9. [Best Practices]#best-practices

## Core Concepts

### DataFrame vs OptimizedDataFrame

PandRS provides two main DataFrame implementations:

**DataFrame** (Traditional):
- Compatible with pandas-like API
- Uses Series internally 
- Good for small datasets and compatibility
- String-based operations

**OptimizedDataFrame** (Recommended):
- Column-oriented storage with type specialization
- Better performance for large datasets
- Built-in string pool optimization
- Type-safe column access via ColumnView

```rust
use pandrs::{DataFrame, Series};
use pandrs::optimized::OptimizedDataFrame;

// Traditional DataFrame
let mut df = DataFrame::new();
df.add_column("data".to_string(), Series::new(vec![1, 2, 3], None)?)?;

// Optimized DataFrame (recommended)
let mut opt_df = OptimizedDataFrame::new();
opt_df.add_int_column("data", vec![1, 2, 3])?;
```

### Series Types

Series are one-dimensional arrays that support:
- Generic type parameter `Series<T>`
- Name management with `set_name()` and `with_name()`
- Type conversion utilities
- Statistical operations

```rust
// Create and manage Series
let mut series = Series::new(vec![10, 20, 30], None)?;
series.set_name("values".to_string());

// Fluent API
let named_series = Series::new(vec![1, 2, 3], None)?
    .with_name("measurements".to_string());

// Type conversion
let string_series = series.to_string_series()?;
```

## DataFrame Types

### OptimizedDataFrame (Recommended)

The `OptimizedDataFrame` provides the best performance and is the recommended choice for most use cases.

#### Column Addition

```rust
let mut df = OptimizedDataFrame::new();

// Add different column types
df.add_int_column("age", vec![25, 30, 35])?;
df.add_float_column("salary", vec![50000.0, 60000.0, 70000.0])?;
df.add_string_column("name", vec![
    "Alice".to_string(), 
    "Bob".to_string(), 
    "Charlie".to_string()
])?;
df.add_boolean_column("active", vec![true, false, true])?;
```

#### Basic Operations

```rust
// DataFrame info
println!("Rows: {}", df.row_count());
println!("Columns: {}", df.column_count());
println!("Column names: {:?}", df.column_names());

// Statistical operations
let sum = df.sum("salary")?;
let mean = df.mean("salary")?;
let min = df.min("age")?;
let max = df.max("age")?;
```

### Traditional DataFrame

The traditional `DataFrame` is provided for compatibility and simple use cases.

```rust
let mut df = DataFrame::new();
let ages = Series::new(vec![25, 30, 35], Some("age".to_string()))?;
df.add_column("age".to_string(), ages)?;
```

## Series Operations

### Name Management

```rust
// Create series without name
let mut data = Series::new(vec![1, 2, 3, 4, 5], None)?;

// Set name after creation
data.set_name("measurements".to_string());
assert_eq!(data.name(), Some(&"measurements".to_string()));

// Create with name using fluent API
let named_data = Series::new(vec![10, 20, 30], None)?
    .with_name("scores".to_string());

// Type conversion with name preservation
let string_data = named_data.to_string_series()?;
assert_eq!(string_data.name(), named_data.name());
```

### Statistical Operations

```rust
let numbers = Series::new(vec![10, 20, 30, 40, 50], Some("values".to_string()))?;

// Basic statistics
let sum = numbers.sum();         // 150
let mean = numbers.mean()?;      // 30.0
let min = numbers.min()?;        // 10
let max = numbers.max()?;        // 50
let len = numbers.len();         // 5

// Iteration and access
for (i, value) in numbers.values().iter().enumerate() {
    println!("Index {}: {}", i, value);
}
```

## Column Management

### Column Renaming

```rust
use std::collections::HashMap;

let mut df = OptimizedDataFrame::new();
df.add_int_column("old_name1", vec![1, 2, 3])?;
df.add_int_column("old_name2", vec![4, 5, 6])?;

// Rename specific columns
let mut rename_map = HashMap::new();
rename_map.insert("old_name1".to_string(), "new_name1".to_string());
rename_map.insert("old_name2".to_string(), "new_name2".to_string());
df.rename_columns(&rename_map)?;

// Set all column names at once
df.set_column_names(vec![
    "first_column".to_string(),
    "second_column".to_string(),
])?;
```

### Column Validation

```rust
// Check if column exists
if df.contains_column("column_name") {
    println!("Column exists");
}

// Get column type
let col_type = df.column_type("column_name")?;
println!("Column type: {:?}", col_type);
```

## Data Access Patterns

### Type-Safe Column Access

The `OptimizedDataFrame` provides type-safe column access through `ColumnView`:

```rust
// Get column view
let col_view = df.column("age")?;

// Type-safe access
if let Some(int_col) = col_view.as_int64() {
    // Direct access to Int64Column
    let value = int_col.get(0)?;  // Returns Result<Option<i64>>
    let sum = int_col.sum();      // Returns i64
    let mean = int_col.mean();    // Returns Option<f64>
}

if let Some(str_col) = col_view.as_string() {
    // Direct access to StringColumn
    let value = str_col.get(0)?;  // Returns Result<Option<&str>>
    let length = str_col.len();
}

// Type mismatch returns None
assert!(col_view.as_float64().is_none()); // If column is actually Int64
```

### Safe Data Retrieval

```rust
// Always handle potential errors and None values
match df.column("column_name") {
    Ok(col_view) => {
        if let Some(int_col) = col_view.as_int64() {
            match int_col.get(index) {
                Ok(Some(value)) => println!("Value: {}", value),
                Ok(None) => println!("Null value"),
                Err(e) => println!("Error: {}", e),
            }
        }
    }
    Err(e) => println!("Column not found: {}", e),
}
```

## I/O Operations

### CSV Operations

```rust
// Write CSV
df.to_csv("output.csv", true)?;  // true = include header

// Read CSV
let loaded_df = pandrs::io::read_csv("input.csv", true)?;  // true = has header
```

### Error Handling for I/O

```rust
// Robust I/O with error handling
match df.to_csv("output.csv", true) {
    Ok(_) => {
        println!("Successfully saved to CSV");
        
        // Verify by reading back
        match pandrs::io::read_csv("output.csv", true) {
            Ok(loaded_df) => {
                println!("Successfully loaded {} rows", loaded_df.row_count());
            }
            Err(e) => println!("Error loading CSV: {}", e),
        }
    }
    Err(e) => println!("Error saving CSV: {}", e),
}
```

### Parquet Operations (Feature-gated)

```rust
#[cfg(feature = "parquet")]
{
    use pandrs::io::{write_parquet, read_parquet, ParquetCompression};
    
    // Write Parquet with compression
    write_parquet(&df, "output.parquet", Some(ParquetCompression::Snappy))?;
    
    // Read Parquet
    let loaded_df = read_parquet("input.parquet")?;
}
```

## Error Handling

### Error Types

PandRS uses a comprehensive error system:

```rust
use pandrs::error::{Error, Result};

// Common error patterns
match operation_result {
    Ok(value) => { /* handle success */ }
    Err(Error::ColumnNotFound(name)) => {
        println!("Column '{}' not found", name);
    }
    Err(Error::IndexOutOfBounds { index, size }) => {
        println!("Index {} out of bounds (size: {})", index, size);
    }
    Err(Error::InconsistentRowCount { expected, found }) => {
        println!("Row count mismatch: expected {}, found {}", expected, found);
    }
    Err(Error::IoError(io_err)) => {
        println!("I/O error: {}", io_err);
    }
    Err(e) => {
        println!("Other error: {}", e);
    }
}
```

### Best Error Handling Practices

```rust
// Use Result<()> for functions that can fail
fn process_dataframe() -> Result<()> {
    let mut df = OptimizedDataFrame::new();
    df.add_int_column("data", vec![1, 2, 3])?;
    
    // Use ? operator for error propagation
    let sum = df.sum("data")?;
    println!("Sum: {}", sum);
    
    Ok(())
}

// Handle specific error conditions
fn safe_column_access(df: &OptimizedDataFrame, col_name: &str, index: usize) -> Option<i64> {
    match df.column(col_name) {
        Ok(col_view) => {
            if let Some(int_col) = col_view.as_int64() {
                match int_col.get(index) {
                    Ok(Some(value)) => Some(*value),
                    _ => None,
                }
            } else {
                None
            }
        }
        Err(_) => None,
    }
}
```

## Performance Considerations

### Choosing the Right DataFrame Type

```rust
// For small datasets or compatibility
let mut small_df = DataFrame::new();

// For performance-critical applications (recommended)
let mut large_df = OptimizedDataFrame::new();
```

### String Pool Benefits

The `OptimizedDataFrame` automatically uses string pooling for memory efficiency:

```rust
// This benefits from automatic string deduplication
let mut df = OptimizedDataFrame::new();
let repeated_data = vec!["Category A".to_string(); 10000];  // High duplication
df.add_string_column("category", repeated_data)?;  // Memory efficient
```

### Batch Operations

```rust
// Add multiple columns efficiently
let mut df = OptimizedDataFrame::new();

// Prepare all data first
let ids: Vec<i64> = (1..=1000).collect();
let names: Vec<String> = (1..=1000).map(|i| format!("User_{}", i)).collect();
let scores: Vec<f64> = (1..=1000).map(|i| i as f64 * 0.1).collect();

// Add columns in sequence
df.add_int_column("id", ids)?;
df.add_string_column("name", names)?;
df.add_float_column("score", scores)?;
```

## Best Practices

### 1. Use OptimizedDataFrame for Performance

```rust
// Recommended
let mut df = OptimizedDataFrame::new();
df.add_int_column("data", vec![1, 2, 3])?;
```

### 2. Handle Errors Explicitly

```rust
// Good: explicit error handling
match df.column("column_name") {
    Ok(col) => { /* process column */ }
    Err(e) => { /* handle error */ }
}

// Avoid: ignoring potential errors
let col = df.column("column_name").unwrap(); // Don't do this
```

### 3. Use Type-Safe Access

```rust
// Good: type-safe access
let col_view = df.column("numbers")?;
if let Some(int_col) = col_view.as_int64() {
    let value = int_col.get(0)?;
}

// Avoid: assuming types
let numbers = df.get_int_column("numbers")?; // May not exist in OptimizedDataFrame
```

### 4. Validate Data Early

```rust
// Check data consistency before processing
if df.row_count() == 0 {
    return Err("Empty DataFrame".into());
}

if !df.contains_column("required_column") {
    return Err("Required column missing".into());
}
```

### 5. Use Meaningful Names

```rust
// Good: descriptive names
let sales_data = Series::new(vec![100, 200, 150], None)?
    .with_name("monthly_sales_usd".to_string());

// Avoid: generic names
let data = Series::new(vec![100, 200, 150], None)?
    .with_name("data".to_string());
```

### 6. Prefer Batch Operations

```rust
// Good: prepare data then add columns
let mut df = OptimizedDataFrame::new();
let (ids, names, scores) = prepare_data(); // Batch preparation

df.add_int_column("id", ids)?;
df.add_string_column("name", names)?;
df.add_float_column("score", scores)?;

// Avoid: row-by-row operations in loops
```

### 7. Test Edge Cases

```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_dataframe() {
        let df = OptimizedDataFrame::new();
        assert_eq!(df.row_count(), 0);
        assert_eq!(df.column_count(), 0);
    }

    #[test]
    fn test_column_not_found() {
        let df = OptimizedDataFrame::new();
        assert!(df.column("nonexistent").is_err());
    }

    #[test]
    fn test_type_safety() {
        let mut df = OptimizedDataFrame::new();
        df.add_int_column("numbers", vec![1, 2, 3]).unwrap();
        
        let col_view = df.column("numbers").unwrap();
        assert!(col_view.as_int64().is_some());
        assert!(col_view.as_string().is_none());
    }
}
```

## Advanced Usage

### Custom Error Handling

```rust
use pandrs::error::{Error, Result};

fn process_sales_data(file_path: &str) -> Result<f64> {
    let df = pandrs::io::read_csv(file_path, true)
        .map_err(|e| Error::IoError(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!("Failed to load sales data: {}", e)
        )))?;

    if !df.contains_column("sales") {
        return Err(Error::ColumnNotFound("sales".to_string()));
    }

    let total_sales = df.sum("sales")?;
    Ok(total_sales)
}
```

### Performance Monitoring

```rust
use std::time::Instant;

fn benchmark_operation() -> Result<()> {
    let start = Instant::now();
    
    let mut df = OptimizedDataFrame::new();
    let large_data: Vec<i64> = (0..1_000_000).collect();
    df.add_int_column("data", large_data)?;
    
    let creation_time = start.elapsed();
    println!("DataFrame creation: {:?}", creation_time);
    
    let start = Instant::now();
    let sum = df.sum("data")?;
    let computation_time = start.elapsed();
    println!("Sum computation: {:?}", computation_time);
    println!("Result: {}", sum);
    
    Ok(())
}
```

This guide covers the essential patterns for working with PandRS. For more examples, see the `examples/` directory in the repository.