pub fn shift(&self, shift: Duration, pad: T) -> Result<Self, TimeSeriesError> {
        let shift_len = match self.timeline.periodicity {
            Period::Year => {
                if shift.days() > 0 || shift.months() > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.years() {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
            Period::Quarter => {
                if shift.days() > 0 || shift.months() % 3 > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.years() * 4 + shift.months() / 3 {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
            Period::Month => {
                if shift.days() > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.years() * 12 + shift.months() {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
            Period::Week => {
                if shift.years() > 0 || shift.months() > 0 || shift.days() % 7 > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.days() / 7 {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
            Period::Day => {
                if shift.years() > 0 || shift.months() > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.days() {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
        };
        let mut data = Vec::with_capacity(self.values.len());
        let data_range = self.values.len() - shift_len;
        let mut pad = vec![pad; shift_len];
        if shift.forwards() {
            data.extend_from_slice(&self.values[shift_len..]);
            data.append(&mut pad);
        } else {
            data.append(&mut pad);
            data.extend_from_slice(&self.values[..data_range]);
        }
        let ts = Self::new(self.timeline, data)?;
        Ok(ts)
    }
/// Shift the TimeSeries by a fixed duration. Intened to use values in
/// different periods e.g. operate on value from 6 months ago.
///
/// This operation will be lossy in the sense that some values will be
/// shifted off the end of the timeline, and will get discarded. On the
/// other side, new values will be brought into scope, which is what the
/// pad value is required for
Shift the TimeSeries by a fixed duration. Intened to use values in
different periods e.g. operate on value from 6 months ago.

This operation will be lossy in the sense that some values will be
shifted off the end of the timeline, and will get discarded. On the
other side, new values will be brought into scope, which is what the
pad value is required for
Shift the TimeSeries by a fixed duration . Intened to use values in different periods e . g . operate on value from 6 months ago . This operation will be lossy in the sense that some values will be shifted off the end of the timeline and will get discarded . On the other side new values will be brought into scope which is what the pad value is required for
pub fn shift(&self, shift: Duration, pad: T) -> Result<Self, TimeSeriesError> {
        let shift_len = match self.timeline.periodicity {
            Period::Year => {
                if shift.days() > 0 || shift.months() > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.years() {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
            Period::Quarter => {
                if shift.days() > 0 || shift.months() % 3 > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.years() * 4 + shift.months() / 3 {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
            Period::Month => {
                if shift.days() > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.years() * 12 + shift.months() {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
            Period::Week => {
                if shift.years() > 0 || shift.months() > 0 || shift.days() % 7 > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.days() / 7 {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
            Period::Day => {
                if shift.years() > 0 || shift.months() > 0 {
                    return Err(TimeSeriesError::BadShift(Period::Year));
                }
                match shift.days() {
                    l if l < 0 => cmp::min(-l as usize, self.values.len()),
                    l if l > 0 => cmp::min(l as usize, self.values.len()),
                    _ => 0,
                }
            }
        };
        let mut data = Vec::with_capacity(self.values.len());
        let data_range = self.values.len() - shift_len;
        let mut pad = vec![pad; shift_len];
        if shift.forwards() {
            data.extend_from_slice(&self.values[shift_len..]);
            data.append(&mut pad);
        } else {
            data.append(&mut pad);
            data.extend_from_slice(&self.values[..data_range]);
        }
        let ts = Self::new(self.timeline, data)?;
        Ok(ts)
    }
pub fn shift ( & self , shift : Duration , pad : T ) -> Result < Self , TimeSeriesError > { let shift_len = match self . timeline . periodicity { Period :: Year => { if shift . days ( ) > 0 || shift . months ( ) > 0 { return Err ( TimeSeriesError :: BadShift ( Period :: Year ) ) ; } match shift . years ( ) { l if l < 0 => cmp :: min ( - l as usize , self . values . len ( ) ) , l if l > 0 => cmp :: min ( l as usize , self . values . len ( ) ) , _ => 0 , } } Period :: Quarter => { if shift . days ( ) > 0 || shift . months ( ) % 3 > 0 { return Err ( TimeSeriesError :: BadShift ( Period :: Year ) ) ; } match shift . years ( ) * 4 + shift . months ( ) / 3 { l if l < 0 => cmp :: min ( - l as usize , self . values . len ( ) ) , l if l > 0 => cmp :: min ( l as usize , self . values . len ( ) ) , _ => 0 , } } Period :: Month => { if shift . days ( ) > 0 { return Err ( TimeSeriesError :: BadShift ( Period :: Year ) ) ; } match shift . years ( ) * 12 + shift . months ( ) { l if l < 0 => cmp :: min ( - l as usize , self . values . len ( ) ) , l if l > 0 => cmp :: min ( l as usize , self . values . len ( ) ) , _ => 0 , } } Period :: Week => { if shift . years ( ) > 0 || shift . months ( ) > 0 || shift . days ( ) % 7 > 0 { return Err ( TimeSeriesError :: BadShift ( Period :: Year ) ) ; } match shift . days ( ) / 7 { l if l < 0 => cmp :: min ( - l as usize , self . values . len ( ) ) , l if l > 0 => cmp :: min ( l as usize , self . values . len ( ) ) , _ => 0 , } } Period :: Day => { if shift . years ( ) > 0 || shift . months ( ) > 0 { return Err ( TimeSeriesError :: BadShift ( Period :: Year ) ) ; } match shift . days ( ) { l if l < 0 => cmp :: min ( - l as usize , self . values . len ( ) ) , l if l > 0 => cmp :: min ( l as usize , self . values . len ( ) ) , _ => 0 , } } } ; let mut data = Vec :: with_capacity ( self . values . len ( ) ) ; let data_range = self . values . len ( ) - shift_len ; let mut pad = vec ! [ pad shift_len ] ; if shift . forwards ( ) { data . extend_from_slice ( & self . values [ shift_len .. ] ) ; data . append ( & mut pad ) ; } else { data . append ( & mut pad ) ; data . extend_from_slice ( & self . values [ .. data_range ] ) ; } let ts = Self :: new ( self . timeline , data ) ? ; Ok ( ts ) }
Shift the TimeSeries by a fixed duration.
Shift the TimeSeries by a fixed duration .
&self
shift
Duration
pad
T
&self
shift
Duration
pad
T