pandrs 0.1.0-beta.2

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
use pandrs::dataframe::base::DataFrame;
use pandrs::dataframe::indexing::{
    selectors, AdvancedIndexingExt, AlignmentStrategy, IndexAligner, MultiLevelIndex,
};
use pandrs::error::Result;
use pandrs::series::base::Series;

#[allow(clippy::result_large_err)]
#[allow(clippy::result_large_err)]
fn main() -> Result<()> {
    println!("=== Alpha 4 Advanced Indexing System Example ===\n");

    // Create sample employee data
    println!("1. Creating Sample Employee Data:");
    let mut df = DataFrame::new();

    let employee_ids = vec![
        "E001", "E002", "E003", "E004", "E005", "E006", "E007", "E008",
    ];
    let names = vec![
        "Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace", "Henry",
    ];
    let departments = vec![
        "IT",
        "HR",
        "IT",
        "Marketing",
        "IT",
        "Finance",
        "HR",
        "Finance",
    ];
    let salaries = vec![
        "75000", "65000", "80000", "58000", "70000", "85000", "60000", "72000",
    ];
    let ages = vec!["28", "35", "42", "26", "31", "45", "29", "38"];
    let experiences = vec!["3", "8", "15", "2", "5", "18", "4", "12"];

    let id_series = Series::new(
        employee_ids.into_iter().map(|s| s.to_string()).collect(),
        Some("ID".to_string()),
    )?;
    let name_series = Series::new(
        names.into_iter().map(|s| s.to_string()).collect(),
        Some("Name".to_string()),
    )?;
    let dept_series = Series::new(
        departments.into_iter().map(|s| s.to_string()).collect(),
        Some("Department".to_string()),
    )?;
    let salary_series = Series::new(
        salaries.into_iter().map(|s| s.to_string()).collect(),
        Some("Salary".to_string()),
    )?;
    let age_series = Series::new(
        ages.into_iter().map(|s| s.to_string()).collect(),
        Some("Age".to_string()),
    )?;
    let exp_series = Series::new(
        experiences.into_iter().map(|s| s.to_string()).collect(),
        Some("Experience".to_string()),
    )?;

    df.add_column("ID".to_string(), id_series)?;
    df.add_column("Name".to_string(), name_series)?;
    df.add_column("Department".to_string(), dept_series)?;
    df.add_column("Salary".to_string(), salary_series)?;
    df.add_column("Age".to_string(), age_series)?;
    df.add_column("Experience".to_string(), exp_series)?;

    println!("Original Employee Data:");
    println!("{df:?}");

    println!("\n=== Position-Based Indexing (.iloc) ===\n");

    // 2. Position-based indexing (iloc)
    println!("2. Position-Based Indexing (.iloc):");

    // Single row by position
    println!("Single row (position 2):");
    let row_2 = df.iloc().get(2)?;
    println!("{row_2:?}");

    // Single value by row and column position
    println!("\nSingle value at (row=1, col=2):");
    let value = df.iloc().get_at(1, 2)?;
    println!("Value: {value}");

    // Range of rows
    println!("\nRows 1-3 (using iloc.get_range):");
    let rows_range = df.iloc().get_range(1..4)?;
    println!("{rows_range:?}");

    // Slice with row and column ranges
    println!("\nSlice (rows 0-2, cols 1-3):");
    let slice = df.iloc().get_slice(0..3, 1..4)?;
    println!("{slice:?}");

    // Multiple specific positions
    println!("\nSpecific positions [0, 2, 4]:");
    let specific_rows = df.iloc().get_positions(&[0, 2, 4])?;
    println!("{specific_rows:?}");

    // Boolean indexing
    println!("\nBoolean mask (every other row):");
    let mask = vec![true, false, true, false, true, false, true, false];
    let boolean_result = df.iloc().get_boolean(&mask)?;
    println!("{boolean_result:?}");

    println!("\n=== Label-Based Indexing (.loc) ===\n");

    // 3. Label-based indexing (loc)
    println!("3. Label-Based Indexing (.loc):");

    // Single row by label (using row index as string)
    println!("Single row by label '0':");
    let label_row = df.loc().get("0")?;
    println!("{label_row:?}");

    // Single value by label and column
    println!("\nSingle value at (label='1', column='Name'):");
    let label_value = df.loc().get_at("1", "Name")?;
    println!("Value: {label_value}");

    // Multiple labels
    println!("\nMultiple labels ['0', '2', '4']:");
    let labels = vec!["0".to_string(), "2".to_string(), "4".to_string()];
    let label_rows = df.loc().get_labels(&labels)?;
    println!("{label_rows:?}");

    println!("\n=== Scalar Indexing (.at and .iat) ===\n");

    // 4. Scalar indexing
    println!("4. Scalar Indexing:");

    // .at for label-based scalar access
    println!("Using .at to get value at (label='3', column='Salary'):");
    let at_value = df.at().get("3", "Salary")?;
    println!("Value: {at_value}");

    // .iat for position-based scalar access
    println!("\nUsing .iat to get value at (row=2, col=1):");
    let iat_value = df.iat().get(2, 1)?;
    println!("Value: {iat_value}");

    println!("\n=== Advanced Selection Builder ===\n");

    // 5. Advanced selection using builder pattern
    println!("5. Advanced Selection Builder:");

    // Select specific rows and columns
    println!("Select rows [1,3,5] and columns ['Name', 'Department', 'Salary']:");
    let selection = df
        .select()
        .rows(selectors::positions(vec![1, 3, 5]))
        .columns(selectors::cols(vec![
            "Name".to_string(),
            "Department".to_string(),
            "Salary".to_string(),
        ]))
        .select()?;
    println!("{selection:?}");

    // Select with range and specific columns
    println!("\nSelect rows 2-5 and specific columns:");
    let range_selection = df
        .select()
        .rows(selectors::range(2, 6))
        .columns(selectors::cols(vec![
            "Name".to_string(),
            "Age".to_string(),
            "Experience".to_string(),
        ]))
        .select()?;
    println!("{range_selection:?}");

    println!("\n=== Column Operations ===\n");

    // 6. Column selection and manipulation
    println!("6. Column Operations:");

    // Select specific columns
    println!("Select columns ['Name', 'Department', 'Salary']:");
    let selected_cols = df.select_columns(&["Name", "Department", "Salary"])?;
    println!("{selected_cols:?}");

    // Drop columns
    println!("\nDrop columns ['ID', 'Experience']:");
    let dropped_cols = df.drop_columns(&["ID".to_string(), "Experience".to_string()])?;
    println!("{dropped_cols:?}");

    println!("\n=== Multi-Level Index ===\n");

    // 7. Multi-level indexing
    println!("7. Multi-Level Index:");

    // Create multi-index from Department and Age columns
    println!("Creating multi-index from Department and Age:");
    let (indexed_df, multi_index) =
        df.set_multi_index(&["Department".to_string(), "Age".to_string()])?;
    println!("Multi-index info:");
    println!("Names: {:?}", multi_index.names);
    println!("Tuples: {:?}", multi_index.tuples);
    println!("DataFrame after indexing:");
    println!("{indexed_df:?}");

    // Query multi-index
    println!("\nQuerying multi-index for tuple ['IT', '28']:");
    let multi_loc = pandrs::dataframe::indexing::LocIndexer::with_index(&indexed_df, &multi_index);
    let tuple_result = multi_loc.get_tuple(&["IT".to_string(), "28".to_string()])?;
    println!("{tuple_result:?}");

    // Get unique values for a level
    println!("\nUnique values for level 0 (Department):");
    let level_values = multi_index.level_values(0)?;
    println!("{level_values:?}");

    println!("\n=== DataFrame Utilities ===\n");

    // 8. DataFrame utilities
    println!("8. DataFrame Utilities:");

    // Head and tail
    println!("Head (first 3 rows):");
    let head = df.head(3)?;
    println!("{head:?}");

    println!("\nTail (last 3 rows):");
    let tail = df.tail(3)?;
    println!("{tail:?}");

    // Sampling (note: this might not work without proper rand setup)
    // println!("\nRandom sample (3 rows):");
    // let sample = df.sample(3)?;
    // println!("{:?}", sample);

    println!("\n=== Index Alignment ===\n");

    // 9. Index alignment
    println!("9. Index Alignment:");

    // Create two DataFrames with different sizes
    let mut df1 = DataFrame::new();
    let mut df2 = DataFrame::new();

    let series1 = Series::new(
        vec!["A".to_string(), "B".to_string(), "C".to_string()],
        Some("Col1".to_string()),
    )?;
    let series2 = Series::new(
        vec![
            "1".to_string(),
            "2".to_string(),
            "3".to_string(),
            "4".to_string(),
            "5".to_string(),
        ],
        Some("Col2".to_string()),
    )?;

    df1.add_column("Col1".to_string(), series1)?;
    df2.add_column("Col2".to_string(), series2)?;

    println!("DataFrame 1:");
    println!("{df1:?}");
    println!("DataFrame 2:");
    println!("{df2:?}");

    // Align with different strategies
    println!("\nOuter alignment (extend to max length):");
    let (aligned1_outer, aligned2_outer) =
        IndexAligner::align(&df1, &df2, AlignmentStrategy::Outer)?;
    println!("Aligned DataFrame 1: {aligned1_outer:?}");
    println!("Aligned DataFrame 2: {aligned2_outer:?}");

    println!("\nInner alignment (truncate to min length):");
    let (aligned1_inner, aligned2_inner) =
        IndexAligner::align(&df1, &df2, AlignmentStrategy::Inner)?;
    println!("Aligned DataFrame 1: {aligned1_inner:?}");
    println!("Aligned DataFrame 2: {aligned2_inner:?}");

    println!("\n=== Macro Usage Examples ===\n");

    // 10. Using macros for convenient indexing
    println!("10. Macro Usage:");

    // Note: These macros would need to be properly defined and exported
    // println!("Using iloc! macro:");
    // let macro_value = iloc!(df, 1, 2)?;
    // println!("Value: {}", macro_value);

    // println!("\nUsing loc! macro:");
    // let macro_label_value = loc!(df, "1", "Name")?;
    // println!("Value: {}", macro_label_value);

    println!("\n=== Performance Demonstration ===\n");

    // 11. Performance with larger dataset
    println!("11. Performance Demonstration:");
    demonstrate_performance()?;

    println!("\n=== Error Handling ===\n");

    // 12. Error handling examples
    println!("12. Error Handling:");
    demonstrate_error_handling(&df)?;

    println!("\n=== Alpha 4 Advanced Indexing System Complete ===");
    println!("\nNew advanced indexing capabilities implemented:");
    println!("✓ Position-based indexing (.iloc)");
    println!("✓ Label-based indexing (.loc)");
    println!("✓ Scalar indexing (.at and .iat)");
    println!("✓ Advanced selection builder with fluent API");
    println!("✓ Multi-level index support");
    println!("✓ Column selection and manipulation");
    println!("✓ Boolean and fancy indexing");
    println!("✓ Range-based selections");
    println!("✓ Index alignment operations");
    println!("✓ DataFrame utilities (head, tail, sample)");
    println!("✓ Comprehensive error handling");
    println!("✓ Helper functions and selectors");

    Ok(())
}

/// Demonstrate performance with larger dataset
#[allow(clippy::result_large_err)]
#[allow(clippy::result_large_err)]
fn demonstrate_performance() -> Result<()> {
    println!("--- Performance with Large Dataset ---");

    // Create a larger dataset
    let mut large_df = DataFrame::new();
    let size = 1000;

    let ids: Vec<String> = (1..=size).map(|i| format!("ID{i:04}")).collect();
    let values: Vec<String> = (1..=size).map(|i| (i as f64 * 1.5).to_string()).collect();
    let categories: Vec<String> = (1..=size)
        .map(|i| match i % 4 {
            0 => "A".to_string(),
            1 => "B".to_string(),
            2 => "C".to_string(),
            _ => "D".to_string(),
        })
        .collect();

    let id_series = Series::new(ids, Some("ID".to_string()))?;
    let value_series = Series::new(values, Some("Value".to_string()))?;
    let cat_series = Series::new(categories, Some("Category".to_string()))?;

    large_df.add_column("ID".to_string(), id_series)?;
    large_df.add_column("Value".to_string(), value_series)?;
    large_df.add_column("Category".to_string(), cat_series)?;

    println!("Created dataset with {size} rows");

    // Test iloc performance
    let start = std::time::Instant::now();
    let _slice = large_df.iloc().get_range(100..200)?;
    let duration = start.elapsed();
    println!("iloc range selection (100 rows) took: {duration:?}");

    // Test column selection performance
    let start = std::time::Instant::now();
    let _cols = large_df.select_columns(&["ID", "Value"])?;
    let duration = start.elapsed();
    println!("Column selection took: {duration:?}");

    // Test head/tail performance
    let start = std::time::Instant::now();
    let _head = large_df.head(50)?;
    let duration = start.elapsed();
    println!("Head operation took: {duration:?}");

    // Test boolean indexing performance
    let start = std::time::Instant::now();
    let mask: Vec<bool> = (0..size).map(|i| i % 5 == 0).collect();
    let _filtered = large_df.iloc().get_boolean(&mask)?;
    let duration = start.elapsed();
    println!("Boolean indexing (every 5th row) took: {duration:?}");

    Ok(())
}

/// Demonstrate error handling
#[allow(clippy::result_large_err)]
#[allow(clippy::result_large_err)]
fn demonstrate_error_handling(df: &DataFrame) -> Result<()> {
    println!("--- Error Handling Examples ---");

    // Out of bounds row access
    match df.iloc().get(100) {
        Ok(_) => println!("Unexpected success with out-of-bounds row"),
        Err(e) => println!("Expected error for out-of-bounds row: {e:?}"),
    }

    // Out of bounds column access
    match df.iloc().get_at(0, 100) {
        Ok(_) => println!("Unexpected success with out-of-bounds column"),
        Err(e) => println!("Expected error for out-of-bounds column: {e:?}"),
    }

    // Invalid column selection
    match df.select_columns(&["NonExistent"]) {
        Ok(_) => println!("Unexpected success with non-existent column"),
        Err(e) => println!("Expected error for non-existent column: {e:?}"),
    }

    // Invalid label access
    match df.loc().get("invalid_label") {
        Ok(_) => println!("Unexpected success with invalid label"),
        Err(e) => println!("Expected error for invalid label: {e:?}"),
    }

    // Empty multi-index creation
    match MultiLevelIndex::new(vec![], vec![]) {
        Ok(_) => println!("Unexpected success with empty multi-index"),
        Err(e) => println!("Expected error for empty multi-index: {e:?}"),
    }

    Ok(())
}