Skip to main content

resample

Function resample 

Source
pub fn resample(
    data: &[(DateTime<Utc>, Option<f64>)],
    interval: TimeDelta,
    resampling_function: ResamplingFunction<f64, SimpleSample>,
    closed: Closed,
    label: Label,
) -> Vec<(DateTime<Utc>, Option<f64>)>
Expand description

Resamples a list of timestamp/value pairs in a single call.

This is a convenience function for one-shot resampling without needing to manage a Resampler instance.

§Arguments

  • data - A slice of (timestamp, value) tuples to resample. Must be sorted by timestamp.
  • interval - The resampling interval.
  • resampling_function - The function to use for aggregating values within each interval.
  • closed - Controls which edge of the interval is closed for sample membership. Use Closed::Left for [start, end) intervals or Closed::Right for (start, end] intervals.
  • label - Controls which edge of the interval is used for output timestamps. Use Label::Left for the start of each interval or Label::Right for the end of each interval.

§Returns

A vector of (timestamp, value) tuples representing the resampled data.

The helper mirrors pandas-style bucket coverage for the input range. In particular, with Closed::Right, it includes the leading bucket that ends exactly at the first sample timestamp when that timestamp lies on an interval boundary.

§Example

use chrono::{DateTime, TimeDelta, Utc};
use frequenz_resampling::{resample, Closed, Label, ResamplingFunction};

let start = DateTime::from_timestamp(0, 0).unwrap();
let step = TimeDelta::seconds(1);
let data: Vec<(DateTime<Utc>, Option<f64>)> = (0..10)
    .map(|i| (start + step * i, Some((i + 1) as f64)))
    .collect();

let result = resample(
    &data,
    TimeDelta::seconds(5),
    ResamplingFunction::Average,
    Closed::Left,
    Label::Left,
);
// Result: [(t=0, 3.0), (t=5, 8.0)]
assert_eq!(result.len(), 2);
assert_eq!(result[0].1, Some(3.0));
assert_eq!(result[1].1, Some(8.0));