polars_core/frame/
upstream_traits.rs

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
use std::ops::{Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

use arrow::record_batch::RecordBatchT;

use crate::prelude::*;

impl FromIterator<Series> for DataFrame {
    /// # Panics
    ///
    /// Panics if Series have different lengths.
    fn from_iter<T: IntoIterator<Item = Series>>(iter: T) -> Self {
        let v = iter.into_iter().map(Column::from).collect();
        DataFrame::new(v).expect("could not create DataFrame from iterator")
    }
}

impl FromIterator<Column> for DataFrame {
    /// # Panics
    ///
    /// Panics if Column have different lengths.
    fn from_iter<T: IntoIterator<Item = Column>>(iter: T) -> Self {
        let v = iter.into_iter().collect();
        DataFrame::new(v).expect("could not create DataFrame from iterator")
    }
}

impl TryExtend<RecordBatchT<Box<dyn Array>>> for DataFrame {
    fn try_extend<I: IntoIterator<Item = RecordBatchT<Box<dyn Array>>>>(
        &mut self,
        iter: I,
    ) -> PolarsResult<()> {
        for record_batch in iter {
            self.append_record_batch(record_batch)?;
        }

        Ok(())
    }
}

impl TryExtend<PolarsResult<RecordBatchT<Box<dyn Array>>>> for DataFrame {
    fn try_extend<I: IntoIterator<Item = PolarsResult<RecordBatchT<Box<dyn Array>>>>>(
        &mut self,
        iter: I,
    ) -> PolarsResult<()> {
        for record_batch in iter {
            self.append_record_batch(record_batch?)?;
        }

        Ok(())
    }
}

impl Index<usize> for DataFrame {
    type Output = Column;

    fn index(&self, index: usize) -> &Self::Output {
        &self.columns[index]
    }
}

macro_rules! impl_ranges {
    ($range_type:ty) => {
        impl Index<$range_type> for DataFrame {
            type Output = [Column];

            fn index(&self, index: $range_type) -> &Self::Output {
                &self.columns[index]
            }
        }
    };
}

impl_ranges!(Range<usize>);
impl_ranges!(RangeInclusive<usize>);
impl_ranges!(RangeFrom<usize>);
impl_ranges!(RangeTo<usize>);
impl_ranges!(RangeToInclusive<usize>);
impl_ranges!(RangeFull);

// we don't implement Borrow<str> or AsRef<str> as upstream crates may add impl of trait for usize.
impl Index<&str> for DataFrame {
    type Output = Column;

    fn index(&self, index: &str) -> &Self::Output {
        let idx = self.check_name_to_idx(index).unwrap();
        &self.columns[idx]
    }
}